Example #1
0
 /**
  * Parse import data.
  * Import translations and language if needed.
  *
  * @param DIBehaviour $di   Dependency injection.
  * @param array       $data Data to parse.
  *
  * @throws Exception
  * @return Language
  */
 public static function parseImportData($di, $data)
 {
     if (empty($data['language']) || empty($data['locale']) || empty($data['locale'])) {
         throw new Exception($di->getI18n()->_('Language translations package must contains fields (not empty): name, language, locale...'));
     }
     /**
      * Get related language.
      */
     $language = Language::findFirst(["language = '{$data['language']}' AND locale = '{$data['locale']}'"]);
     if (!$language) {
         $language = new Language();
         $language->assign($data);
         if (!$language->save()) {
             throw new Exception(implode(', ', $language->getMessages()));
         }
     }
     /**
      * Import into database.
      */
     $table = LanguageTranslation::getTableName();
     $sql = "INSERT IGNORE INTO `{$table}` (language_id, scope, original, translated) VALUES ";
     $sqlValues = [];
     $counter = 0;
     $totals = [];
     foreach ($data['content'] as $scope => $translations) {
         $totals[$scope] = 0;
         foreach ($translations as $original => $translated) {
             $sqlValues[] = PHP_EOL . sprintf('(%d, "%s", "%s", "%s")', $language->getId(), mysql_real_escape_string($scope), mysql_real_escape_string($original), mysql_real_escape_string($translated));
             $counter++;
             $totals[$scope]++;
             if ($counter == self::LANGUAGE_IMPORT_BATCH_SIZE) {
                 $counter = 0;
                 $sqlValues = '';
                 $di->getModelsManager()->execute($sql . implode(',', $sqlValues));
             }
         }
     }
     if (!empty($sqlValues)) {
         $di->getDb()->execute($sql . implode(',', $sqlValues));
     }
     return [$language, $totals];
 }
 /**
  * Check and set icon for language.
  *
  * @param Language $language Language object.
  * @param FileForm $form     Form object.
  *
  * @return void
  */
 protected function _setLanguageIcon($language, $form)
 {
     // Upload language icon.
     if (!$form->hasFiles()) {
         return;
     }
     if (!is_dir(PUBLIC_PATH . '/' . Language::LANGUAGE_ICON_LOCATION)) {
         mkdir(PUBLIC_PATH . '/' . Language::LANGUAGE_ICON_LOCATION, 766, true);
     }
     $files = $form->getFiles();
     $iconPath = Language::LANGUAGE_ICON_LOCATION . $language->language . ' . ' . pathinfo($files[0]->getName(), PATHINFO_EXTENSION);
     $fullIconPath = PUBLIC_PATH . '/' . $iconPath;
     if (file_exists($fullIconPath)) {
         @unlink($fullIconPath);
     }
     $files[0]->moveTo($fullIconPath);
     $language->icon = $iconPath;
     $language->save();
 }