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

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

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

@ -5,7 +5,6 @@
use Chamilo\CoreBundle\Entity\ExtraFieldOptions; use Chamilo\CoreBundle\Entity\ExtraFieldOptions;
/** /**
* Class ExtraFieldOption
* Handles the extra fields for various objects (users, sessions, courses). * Handles the extra fields for various objects (users, sessions, courses).
*/ */
class ExtraFieldOption extends Model class ExtraFieldOption extends Model
@ -549,7 +548,7 @@ class ExtraFieldOption extends Model
'id' => $row->getId(), 'id' => $row->getId(),
'field_id' => $row->getField()->getId(), 'field_id' => $row->getField()->getId(),
'option_value' => $row->getValue(), 'option_value' => $row->getValue(),
'display_text' => get_lang($row->getDisplayText()), 'display_text' => \ExtraField::translateDisplayName($row->getValue(), $row->getDisplayText()),
'priority' => $row->getPriority(), 'priority' => $row->getPriority(),
'priority_message' => $row->getPriorityMessage(), 'priority_message' => $row->getPriorityMessage(),
'option_order' => $row->getOptionOrder(), 'option_order' => $row->getOptionOrder(),
@ -861,40 +860,12 @@ class ExtraFieldOption extends Model
$info = parent::get($id); $info = parent::get($id);
if ($translateDisplayText) { if ($translateDisplayText) {
$info['display_text'] = self::translateDisplayName($info['display_text']); $info['display_text'] = ExtraField::translateDisplayName($info['option_value'], $info['display_text']);
} }
return $info; 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 * @param null $options
* *
@ -905,7 +876,7 @@ class ExtraFieldOption extends Model
$result = parent::get_all($options); $result = parent::get_all($options);
foreach ($result as &$row) { 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; return $result;

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

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

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

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

Loading…
Cancel
Save