ExtraFields: Use variable translation, then display_text

pull/4032/head
Julio 3 years ago
parent c3ec36f1a7
commit 621f87ce4c
  1. 6
      public/main/extrafield/translate.php
  2. 5
      public/main/inc/ajax/model.ajax.php
  3. 42
      public/main/inc/lib/extra_field.lib.php
  4. 35
      public/main/inc/lib/extra_field_option.lib.php
  5. 4
      public/main/inc/lib/extra_field_value.lib.php
  6. 2
      public/main/inc/lib/webservices/Rest.php
  7. 8
      src/CoreBundle/Entity/ExtraField.php
  8. 8
      src/CoreBundle/Entity/ExtraFieldOptions.php

@ -2,6 +2,8 @@
/* For licensing terms, see /license.txt */
exit;
use Chamilo\CoreBundle\Entity\ExtraField;
use Chamilo\CoreBundle\Entity\Language;
@ -25,8 +27,8 @@ if (isset($_GET['extra_field'])) {
} elseif (isset($_GET['extra_field_option'])) {
$extraFieldOption = $em->find('ChamiloCoreBundle:ExtraFieldOptions', intval($_GET['extra_field_option']));
$extraField = $extraFieldOption->getField();
$variableLanguage = '$'.ExtraFieldOption::getLanguageVariable($extraFieldOption->getDisplayText());
$originalName = $extraFieldOption->getDisplayText(false);
//$variableLanguage = '$'.ExtraFieldOption::getLanguageVariable($extraFieldOption->getDisplayText());
$originalName = $extraFieldOption->getDisplayText();
}
if (!$extraField || empty($variableLanguage) || empty($originalName)) {

@ -2365,10 +2365,7 @@ switch ($action) {
get_lang('No')
);
foreach ($result as $item) {
$item['display_text'] = ExtraField::translateDisplayName(
$item['variable'],
$item['displayText']
);
$item['display_text'] = ExtraField::translateDisplayName($item['variable'], $item['displayText']);
$item['field_type'] = $obj->get_field_type_by_id($item['fieldType']);
$item['changeable'] = $item['changeable'] ? $checkIcon : $timesIcon;
$item['visible_to_self'] = $item['visibleToSelf'] ? $checkIcon : $timesIcon;

@ -643,9 +643,9 @@ class ExtraField extends Model
if (Database::num_rows($result)) {
$row = Database::fetch_array($result, 'ASSOC');
$row['display_text'] = $this->translateDisplayName(
$row['variable'],
$row['display_text']
);
$row['variable'],
$row['display_text']
);
// All the tags of the field
$sql = "SELECT * FROM $this->table_field_tag
@ -670,11 +670,21 @@ class ExtraField extends Model
*
* @return string
*/
public static function translateDisplayName($variable, $defaultDisplayText)
public static function translateDisplayName($variable, $defaultDisplayText): string
{
$camelCase = api_underscore_to_camel_case($variable);
// 1st priority variable.
$translatedVariable = get_lang($variable);
if ($variable !== $translatedVariable) {
return $translatedVariable;
}
// 2nd priority display text.
$translatedDisplayText = get_lang($defaultDisplayText);
if ($defaultDisplayText !== $translatedDisplayText) {
return $translatedVariable;
}
return isset($GLOBALS[$camelCase]) ? $GLOBALS[$camelCase] : $defaultDisplayText;
return $defaultDisplayText;
}
/**
@ -940,7 +950,7 @@ class ExtraField extends Model
$option = new ExtraFieldOption($this->type);
if (!empty($extraFields)) {
foreach ($extraFields as &$extraField) {
$extraField['display_text'] = get_lang($extraField['display_text']);
$extraField['display_text'] = self::translateDisplayName($extraField['variable'], $extraField['display_text']);
$extraField['options'] = $option->get_field_options_by_field(
$extraField['id'],
false,
@ -1998,15 +2008,12 @@ class ExtraField extends Model
if (Database::num_rows($result)) {
$row = Database::fetch_array($result, 'ASSOC');
if ($row) {
$row['display_text'] = $this->translateDisplayName(
$row['variable'],
$row['display_text']
);
$row['display_text'] = $this->translateDisplayName($row['variable'], $row['display_text']);
// All the options of the field
$sql = "SELECT * FROM $this->table_field_options
WHERE field_id='".intval($row['id'])."'
ORDER BY option_order ASC";
WHERE field_id='".intval($row['id'])."'
ORDER BY option_order ASC";
$result = Database::query($sql);
while ($option = Database::fetch_array($result)) {
$row['options'][$option['id']] = $option;
@ -2995,11 +3002,7 @@ JAVASCRIPT;
$valueAsArray = [];
$fieldValue = new ExtraFieldValue($this->type);
$valueData = $fieldValue->get_values_by_handler_and_field_id(
$itemId,
$field['id'],
true
);
$valueData = $fieldValue->get_values_by_handler_and_field_id($itemId, $field['id'], true);
$fieldType = (int) $field['field_type'];
if (self::FIELD_TYPE_TAG === $fieldType) {
@ -3022,10 +3025,9 @@ JAVASCRIPT;
$displayedValue = get_lang('None');
switch ($fieldType) {
case self::FIELD_TYPE_CHECKBOX:
$displayedValue = get_lang('No');
if (false !== $valueData && '1' == $valueData['value']) {
$displayedValue = get_lang('Yes');
} else {
$displayedValue = get_lang('No');
}
break;
case self::FIELD_TYPE_DATE:

@ -5,7 +5,6 @@
use Chamilo\CoreBundle\Entity\ExtraFieldOptions;
/**
* Class ExtraFieldOption
* Handles the extra fields for various objects (users, sessions, courses).
*/
class ExtraFieldOption extends Model
@ -549,7 +548,7 @@ class ExtraFieldOption extends Model
'id' => $row->getId(),
'field_id' => $row->getField()->getId(),
'option_value' => $row->getValue(),
'display_text' => get_lang($row->getDisplayText()),
'display_text' => \ExtraField::translateDisplayName($row->getValue(), $row->getDisplayText()),
'priority' => $row->getPriority(),
'priority_message' => $row->getPriorityMessage(),
'option_order' => $row->getOptionOrder(),
@ -861,40 +860,12 @@ class ExtraFieldOption extends Model
$info = parent::get($id);
if ($translateDisplayText) {
$info['display_text'] = self::translateDisplayName($info['display_text']);
$info['display_text'] = ExtraField::translateDisplayName($info['option_value'], $info['display_text']);
}
return $info;
}
/**
* Translate the display text for a extra field option.
*
* @param string $defaultDisplayText
*
* @return string
*/
public static function translateDisplayName($defaultDisplayText)
{
$variableLanguage = self::getLanguageVariable($defaultDisplayText);
return isset($GLOBALS[$variableLanguage]) ? $GLOBALS[$variableLanguage] : $defaultDisplayText;
}
/**
* @param $defaultDisplayText
*
* @return mixed|string
*/
public static function getLanguageVariable($defaultDisplayText)
{
$variableLanguage = api_replace_dangerous_char($defaultDisplayText);
$variableLanguage = str_replace('-', '_', $variableLanguage);
$variableLanguage = api_underscore_to_camel_case($variableLanguage);
return $variableLanguage;
}
/**
* @param null $options
*
@ -905,7 +876,7 @@ class ExtraFieldOption extends Model
$result = parent::get_all($options);
foreach ($result as &$row) {
$row['display_text'] = self::translateDisplayName($row['display_text']);
$row['display_text'] = ExtraField::translateDisplayName($row['option_value'], $row['display_text']);
}
return $result;

@ -561,10 +561,10 @@ class ExtraFieldValue extends Model
if (isset($extra_field_option_result[0])) {
$result['value'] = $extra_field_option_result[0]['display_text'];
}
break;
case ExtraField::FIELD_TYPE_SELECT_WITH_TEXT_FIELD:
$options = explode('::', $result['value']);
$field_option = new ExtraFieldOption($this->type);
$result = $field_option->get($options[0]);
@ -577,11 +577,9 @@ class ExtraFieldValue extends Model
case ExtraField::FIELD_TYPE_TRIPLE_SELECT:
$optionIds = explode(';', $result['value']);
$optionValues = [];
foreach ($optionIds as $optionId) {
$objEfOption = new ExtraFieldOption('user');
$optionInfo = $objEfOption->get($optionId);
$optionValues[] = $optionInfo['display_text'];
}

@ -787,7 +787,7 @@ class Rest extends WebService
/** @var ExtraFieldValues $extraValue */
$extraValue = $extra['value'];
$result['extra'][] = [
'title' => $extraValue->getField()->getDisplayText(true),
'title' => $extraValue->getField()->getDisplayText(),
'value' => $extraValue->getValue(),
];
}

@ -190,16 +190,10 @@ class ExtraField
}
/**
* @param bool $translated Optional. Whether translate the display text
*
* @return string
*/
public function getDisplayText(bool $translated = true)
public function getDisplayText()
{
/*if ($translated) {
return \ExtraField::translateDisplayName($this->variable, $this->displayText);
}*/
return $this->displayText;
}

@ -105,16 +105,10 @@ class ExtraFieldOptions
}
/**
* @param bool $translated Optional. Whether translate the display text
*
* @return string
*/
public function getDisplayText(bool $translated = true)
public function getDisplayText()
{
/*if ($translated) {
return ExtraFieldOption::translateDisplayName($this->displayText);
}*/
return $this->displayText;
}

Loading…
Cancel
Save