Browse Source

Allow translate the name of extra field options - refs BT#11015

Angel Fernando Quiroz Campos 8 years ago
parent
commit
baf9f13399
3 changed files with 107 additions and 14 deletions
  1. 22 11
      main/admin/sub_language.php
  2. 1 1
      main/inc/lib/extra_field.lib.php
  3. 84 2
      main/inc/lib/extra_field_option.lib.php

+ 22 - 11
main/admin/sub_language.php

@@ -127,14 +127,13 @@ if (!empty($sublanguage_folder_error)) {
 echo '<div id="div_message_information_id">&nbsp;</div>';
 
 /**
- * Search a term in the language
- * @param string $term the term to search
- * @param bool $search_in_variable the search will include the variable definition of the term
- * @param bool $search_in_english the search will include the english language variables
- * @param bool $search_in_parent the search will include the parent language variables of the sub language
- * @param bool $search_in_sub_language the search will include the sub language variables
+ * @param $term The term to search
+ * @param bool $search_in_variable The search will include the variable definition of the term
+ * @param bool $search_in_english The search will include the english language variables
+ * @param bool $search_in_parent The search will include the parent language variables of the sub language
+ * @param bool $search_in_sub_language The search will include the sub language variables
  * @author Julio Montoya
- *
+ * @return array
  */
 function search_language_term(
 	$term,
@@ -309,8 +308,20 @@ if (isset($_REQUEST['txt_search_word'])) {
 	}
 }
 
-if (isset($_GET['extra_field']) && !empty($_GET['extra_field'])) {
-    $extraFieldInfo = ExtraField::getExtraFieldInfoById($_GET['extra_field'], false);
+if (
+    (isset($_GET['extra_field']) && !empty($_GET['extra_field'])) ||
+    (isset($_GET['extra_field_option']) && !empty($_GET['extra_field_option']))
+) {
+    if (isset($_GET['extra_field'])) {
+        $extraFieldInfo = ExtraField::getInfoById($_GET['extra_field'], false);
+        $variable_language = '$' . api_underscore_to_camel_case($extraFieldInfo['variable']);
+        $original_name = $extraFieldInfo['display_text'];
+    } elseif (isset($_GET['extra_field_option'])) {
+        $extraFieldOptionInfo = ExtraFieldOption::getInfoById($_GET['extra_field_option'], false);
+        $variable_language = '$' . api_underscore_to_camel_case($extraFieldOptionInfo['display_text']);
+        $original_name = $extraFieldOptionInfo['display_text'];
+    }
+
     $platformLanguage = api_get_setting('platformLanguage');
     $languageId = api_get_language_id($platformLanguage);
     $languageInfo = api_get_language_info($languageId);
@@ -328,8 +339,8 @@ if (isset($_GET['extra_field']) && !empty($_GET['extra_field'])) {
     $form->addHidden('redirect', true);
     $form->addButtonSave(get_lang('Save'));
     $form->setDefaults([
-        'variable_language' => '$' . api_underscore_to_camel_case($extraFieldInfo['variable']),
-        'original_name' => $extraFieldInfo['display_text']
+        'variable_language' => $variable_language,
+        'original_name' => $original_name
     ]);
     $form->freeze(['variable_language', 'original_name']);
     $form->display();

+ 1 - 1
main/inc/lib/extra_field.lib.php

@@ -2379,7 +2379,7 @@ JAVASCRIPT;
      * @throws \Doctrine\ORM\OptimisticLockException
      * @throws \Doctrine\ORM\TransactionRequiredException
      */
-    public static function getExtraFieldInfoById($id, $translateDisplayText = true)
+    public static function getInfoById($id, $translateDisplayText = true)
     {
         $extraField = Database::getManager()
             ->find('ChamiloCoreBundle:ExtraField', $id);

+ 84 - 2
main/inc/lib/extra_field_option.lib.php

@@ -615,7 +615,23 @@ class ExtraFieldOption extends Model
         $form->addElement('hidden', 'type', $this->type);
         $form->addElement('hidden', 'field_id', $this->field_id);
 
-        $form->addElement('text', 'display_text', get_lang('Name'));
+        if ($action == 'edit') {
+            $platformLanguage = api_get_setting('platformLanguage');
+            $languageId = api_get_language_id($platformLanguage);
+            $languageInfo = api_get_language_info($languageId);
+            $translateUrl = api_get_path(WEB_CODE_PATH) . 'admin/sub_language.php?' . http_build_query([
+                'id' => $languageInfo['parent_id'],
+                'action' => 'registersublanguage',
+                'sub_language_id' => $languageInfo['id'],
+                'extra_field_option' => $id
+            ]);
+            $translateButton = Display::toolbarButton(get_lang('TranslateThisTerm'), $translateUrl, 'language', 'link');
+
+            $form->addElement('text', 'display_text', [get_lang('Name'), $translateButton]);
+        } else {
+            $form->addElement('text', 'display_text', get_lang('Name'));
+        }
+
         $form->addElement('text', 'option_value', get_lang('Value'));
         $form->addElement('text', 'option_order', get_lang('Order'));
         $form->addElement('select', 'priority', get_lang('Priority'), $this->getPriorityOptions());
@@ -625,7 +641,7 @@ class ExtraFieldOption extends Model
 
         if ($action == 'edit') {
             // Setting the defaults
-            $defaults = $this->get($id);
+            $defaults = $this->get($id, false);
             $form->freeze('option_value');
             $form->addButtonUpdate(get_lang('Modify'));
         } else {
@@ -693,4 +709,70 @@ class ExtraFieldOption extends Model
 
         return $json;
     }
+
+    /**
+     * Gets an element
+     * @param int $id
+     * @param bool $translateDisplayText Optional
+     * @return array
+     */
+    public function get($id, $translateDisplayText = true)
+    {
+        $info = parent::get($id);
+
+        if ($translateDisplayText) {
+            $info['display_text'] = self::translateDisplayName($info['display_text']);
+        }
+
+        return $info;
+    }
+
+    /**
+     * Translate the display text for a extra field option
+     * @param string $defaultDisplayText
+     * @return string
+     */
+    public static function translateDisplayName($defaultDisplayText)
+    {
+        $camelCase = api_underscore_to_camel_case($defaultDisplayText);
+        $camelCase = ucwords($camelCase);
+        $camelCase = str_replace(' ', '', $camelCase);
+
+        return isset($GLOBALS[$camelCase]) ? $GLOBALS[$camelCase] : $defaultDisplayText;
+    }
+
+    /**
+     * Get the info from an extra field option by its id
+     * @param int $id
+     * @param bool $translateDisplayText
+     * @return array
+     * @throws \Doctrine\ORM\ORMException
+     * @throws \Doctrine\ORM\OptimisticLockException
+     * @throws \Doctrine\ORM\TransactionRequiredException
+     */
+    public static function getInfoById($id, $translateDisplayText = true)
+    {
+        $extraField = Database::getManager()
+            ->find('ChamiloCoreBundle:ExtraFieldOptions', $id);
+
+        $objExtraField = null;
+
+        switch ($extraField->getField()->getExtraFieldType()) {
+            case \Chamilo\CoreBundle\Entity\ExtraField::USER_FIELD_TYPE:
+                $objExtraField = new self('user');
+                break;
+            case \Chamilo\CoreBundle\Entity\ExtraField::COURSE_FIELD_TYPE:
+                $objExtraField = new self('course');
+                break;
+            case \Chamilo\CoreBundle\Entity\ExtraField::SESSION_FIELD_TYPE:
+                $objExtraField = new self('session');
+                break;
+        }
+
+        if (!$objExtraField) {
+            return [];
+        }
+
+        return $objExtraField->get($extraField->getId(), $translateDisplayText);
+    }
 }