Backend\Modules\Locale\Engine\Model::getTranslations PHP Method

getTranslations() public static method

Get the translations
public static getTranslations ( string $application, string $module, array $types, array $languages, string $name, string $value ) : array
$application string The application.
$module string The module.
$types array The types of the translations to get.
$languages array The languages of the translations to get.
$name string The name.
$value string The value.
return array
    public static function getTranslations($application, $module, $types, $languages, $name, $value)
    {
        $languages = (array) $languages;
        // create an array for the languages, surrounded by quotes (example: 'en')
        $aLanguages = array();
        foreach ($languages as $key => $val) {
            $aLanguages[$key] = '\'' . $val . '\'';
        }
        // surround the types with quotes
        foreach ($types as $key => $val) {
            $types[$key] = '\'' . $val . '\'';
        }
        // get db
        $db = BackendModel::getContainer()->get('database');
        // build the query
        $query = 'SELECT l.id, l.application, l.module, l.type, l.name, l.value, l.language, UNIX_TIMESTAMP(l.edited_on) as edited_on
             FROM locale AS l
             WHERE
                 l.language IN (' . implode(',', $aLanguages) . ') AND
                 l.name LIKE ? AND
                 l.value LIKE ? AND
                 l.type IN (' . implode(',', $types) . ')';
        // add the parameters
        $parameters = array('%' . $name . '%', '%' . $value . '%');
        // add module to the query if needed
        if ($module) {
            $query .= ' AND l.module = ?';
            $parameters[] = $module;
        }
        // add module to the query if needed
        if ($application) {
            $query .= ' AND l.application = ?';
            $parameters[] = $application;
        }
        // get the translations
        $translations = (array) $db->getRecords($query, $parameters);
        // create an array for the sorted translations
        $sortedTranslations = array();
        // loop translations
        foreach ($translations as $translation) {
            // add to the sorted array
            $sortedTranslations[$translation['type']][$translation['name']][$translation['module']][$translation['language']] = array('id' => $translation['id'], 'value' => $translation['value'], 'edited_on' => $translation['edited_on'], 'application' => $translation['application']);
        }
        // create an array to use in the datagrid
        $dataGridTranslations = array();
        // an id that is used for in the datagrid, this is not the id of the translation!
        $id = 0;
        // save the number of languages so this has not to be executed x number of times
        $numberOfLanguages = count($languages);
        // loop the sorted translations
        foreach ($sortedTranslations as $type => $references) {
            // create array for each type
            $dataGridTranslations[$type] = array();
            foreach ($references as $reference => $translation) {
                // loop modules
                foreach ($translation as $module => $t) {
                    // create translation (and increase id)
                    // we init the application here so it appears in front of the datagrid
                    $trans = array('application' => '', 'module' => $module, 'name' => $reference, 'id' => $id++);
                    // reset this var for every language
                    $edited_on = '';
                    foreach ($languages as $lang) {
                        // if the translation exists the for this language, fill it up
                        // else leave a space for the empty field
                        if (isset($t[$lang])) {
                            $trans[$lang] = $t[$lang]['value'];
                            $trans['application'] = $t[$lang]['application'];
                            // only alter edited_on if the date of a previously added date of another
                            // language is smaller
                            if ($edited_on < $t[$lang]['edited_on']) {
                                $edited_on = $t[$lang]['edited_on'];
                            }
                            if ($numberOfLanguages == 1) {
                                $trans['translation_id'] = $t[$lang]['id'];
                            } else {
                                $trans['translation_id_' . $lang] = $t[$lang]['id'];
                            }
                        } else {
                            $trans[$lang] = '';
                            if ($numberOfLanguages == 1) {
                                $trans['translation_id'] = '';
                            } else {
                                $trans['translation_id_' . $lang] = '';
                            }
                        }
                    }
                    // at the end of the array, add the generated edited_on date
                    $trans['edited_on'] = $edited_on;
                    // add the translation to the array
                    $dataGridTranslations[$type][] = $trans;
                }
            }
        }
        return $dataGridTranslations;
    }

Usage Example

Example #1
0
 /**
  * Load the datagrid
  */
 private function loadDataGrid()
 {
     // init vars
     $langWidth = 60 / count($this->filter['language']);
     // if nothing is submitted
     // we don't need to fetch all the locale
     $this->hasSubmissions = '' !== $this->filter['application'] . $this->filter['module'] . $this->filter['name'] . $this->filter['value'];
     if ($this->hasSubmissions) {
         // get all the translations for the selected languages
         $translations = BackendLocaleModel::getTranslations($this->filter['application'], $this->filter['module'], $this->filter['type'], $this->filter['language'], $this->filter['name'], $this->filter['value']);
     }
     // create datagrids
     $this->dgLabels = new BackendDataGridArray(isset($translations['lbl']) ? $translations['lbl'] : array());
     $this->dgMessages = new BackendDataGridArray(isset($translations['msg']) ? $translations['msg'] : array());
     $this->dgErrors = new BackendDataGridArray(isset($translations['err']) ? $translations['err'] : array());
     $this->dgActions = new BackendDataGridArray(isset($translations['act']) ? $translations['act'] : array());
     // put the datagrids (references) in an array so we can loop them
     $dataGrids = array('lbl' => &$this->dgLabels, 'msg' => &$this->dgMessages, 'err' => &$this->dgErrors, 'act' => &$this->dgActions);
     // loop the datagrids (as references)
     foreach ($dataGrids as $type => &$dataGrid) {
         /** @var $dataGrid BackendDataGridArray */
         $dataGrid->setSortingColumns(array('module', 'name', 'application'), 'name');
         // disable paging
         $dataGrid->setPaging(false);
         // set header label for reference code
         $dataGrid->setHeaderLabels(array('name' => \SpoonFilter::ucfirst(BL::lbl('ReferenceCode'))));
         // hide the application when only one application is shown
         if ($this->filter['application'] != '') {
             $dataGrid->setColumnHidden('application');
         }
         // hide edite_on
         $dataGrid->setColumnHidden('edited_on');
         // set column attributes for each language
         foreach ($this->filter['language'] as $lang) {
             // add a class for the inline edit
             $dataGrid->setColumnAttributes($lang, array('class' => 'translationValue'));
             // add attributes, so the inline editing has all the needed data
             $dataGrid->setColumnAttributes($lang, array('data-id' => '{language: \'' . $lang . '\',application: \'[application]\',module: \'[module]\',name: \'[name]\',type: \'' . $type . '\'}'));
             // escape the double quotes
             $dataGrid->setColumnFunction(array('SpoonFilter', 'htmlentities'), array('[' . $lang . ']', null, ENT_QUOTES), $lang, true);
             if ($type == 'act') {
                 $dataGrid->setColumnFunction('urldecode', array('[' . $lang . ']'), $lang, true);
             }
             // set header labels
             $dataGrid->setHeaderLabels(array($lang => \SpoonFilter::ucfirst(BL::lbl(mb_strtoupper($lang)))));
             // only 1 language selected?
             if (count($this->filter['language']) == 1) {
                 $dataGrid->setColumnAttributes($lang, array('style' => 'width: ' . $langWidth . '%'));
                 // add id of translation for the export
                 $dataGrid->setColumnAttributes($lang, array('data-numeric-id' => '[translation_id]'));
                 // Hide translation_id column (only if only one language is selected
                 // because the key doesn't exist if more than 1 language is selected)
                 $dataGrid->setColumnHidden('translation_id');
                 // check if this action is allowed
                 if (BackendAuthentication::isAllowedAction('Add')) {
                     // add copy button
                     $dataGrid->addColumnAction('copy', null, BL::lbl('Copy'), BackendModel::createURLForAction('Add') . '&amp;id=[translation_id]' . $this->filterQuery);
                 }
                 // check if this action is allowed
                 if (BackendAuthentication::isAllowedAction('Edit')) {
                     // add edit button
                     $dataGrid->addColumn('edit', null, BL::lbl('Edit'), BackendModel::createURLForAction('Edit') . '&amp;id=[translation_id]' . $this->filterQuery);
                 }
             } else {
                 // add id of translation for the export
                 $dataGrid->setColumnAttributes($lang, array('data-numeric-id' => '[translation_id_' . $lang . ']'));
                 $dataGrid->setColumnHidden('translation_id_' . $lang);
                 //ugly fix but the browser does funny things with the percentage when showing lots of languages
                 $dataGrid->setColumnAttributes($lang, array('style' => 'width: ' . $langWidth . '%; max-width: ' . 600 / count($this->filter['language']) . 'px;'));
             }
         }
     }
 }