Exemplo n.º 1
0
 /**
  * Show import form and import the uploaded file if posted
  * @return string
  * @throws \Exception
  */
 public function run()
 {
     $model = new ImportForm();
     if (Yii::$app->request->isPost) {
         $model->importFile = UploadedFile::getInstance($model, 'importFile');
         if ($model->validate()) {
             try {
                 $result = $model->import();
                 $message = Yii::t('language', 'Successfully imported {fileName}', ['fileName' => $model->importFile->name]);
                 $message .= "<br/>\n";
                 foreach ($result as $type => $typeResult) {
                     $message .= "<br/>\n" . Yii::t('language', '{type}: {new} new, {updated} updated', ['type' => $type, 'new' => $typeResult['new'], 'updated' => $typeResult['updated']]);
                 }
                 $languageIds = Language::find()->select('language_id')->where(['status' => Language::STATUS_ACTIVE])->column();
                 foreach ($languageIds as $languageId) {
                     $generator = new Generator($this->controller->module, $languageId);
                     $generator->run();
                 }
                 Yii::$app->getSession()->setFlash('success', $message);
             } catch (\Exception $e) {
                 if (YII_DEBUG) {
                     throw $e;
                 } else {
                     Yii::$app->getSession()->setFlash('danger', str_replace("\n", "<br/>\n", $e->getMessage()));
                 }
             }
         }
     }
     return $this->controller->render('import', ['model' => $model]);
 }
 /**
  * @param array $params Search conditions.
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Language::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['status' => $this->status]);
     $query->andFilterWhere(['like', 'language_id', $this->language_id])->andFilterWhere(['like', 'language', $this->language])->andFilterWhere(['like', 'country', $this->country])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'name_ascii', $this->name_ascii]);
     return $dataProvider;
 }
 /**
  * @param array $params Search conditions.
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Language::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['status' => $this->status]);
     $query->andFilterWhere(['like', 'lower(language_id)', mb_strtolower($this->language_id, 'UTF-8')])->andFilterWhere(['like', 'lower(language)', mb_strtolower($this->language, 'UTF-8')])->andFilterWhere(['like', 'lower(country)', mb_strtolower($this->country, 'UTF-8')])->andFilterWhere(['like', 'lower(name)', mb_strtolower($this->name, 'UTF-8')])->andFilterWhere(['like', 'lower(name_ascii)', mb_strtolower($this->name_ascii, 'UTF-8')]);
     return $dataProvider;
 }
Exemplo n.º 4
0
use yii\helpers\Html;
use yii\web\Response;
/* @var $this yii\web\View */
/* @var $model ExportForm */
$this->title = Yii::t('language', 'Export');
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="language-export col-sm-6">

    <?php 
$form = ActiveForm::begin();
?>

    <?php 
echo $form->field($model, 'exportLanguages')->listBox(ArrayHelper::map(Language::find()->all(), 'language_id', 'name_ascii'), ['multiple' => true, 'size' => 20]);
?>

    <?php 
echo $form->field($model, 'format')->radioList([Response::FORMAT_JSON => Response::FORMAT_JSON, Response::FORMAT_XML => Response::FORMAT_XML]);
?>

    <div class="form-group">
        <?php 
echo Html::submitButton(Yii::t('language', 'Export'), ['class' => 'btn btn-primary']);
?>
    </div>

    <?php 
ActiveForm::end();
?>
Exemplo n.º 5
0
 /**
  * Returns language objects.
  * @param boolean $active True/False according to the status of the language.
  * @param bool $asArray Return the languages as language object or as 'flat' array
  * @return Language|array
  */
 public static function getLanguages($active = true, $asArray = false)
 {
     if ($active) {
         return Language::find()->asArray($asArray)->where(['status' => static::STATUS_ACTIVE])->all();
     } else {
         return Language::find()->asArray($asArray)->all();
     }
 }
 /**
  * Find languages matching the minimumStatus
  * @param $minimumStatus int The status of the returned language will be equal or larger than this number.
  * @return Language[]
  */
 public function getDefaultExportLanguages($minimumStatus)
 {
     return Language::find()->select('language_id')->where(['>=', 'status', $minimumStatus])->column();
 }
Exemplo n.º 7
0
 /**
  * Import the uploaded file. Existing languages and translations will be updated, new ones will be created.
  * Source messages won't be updated, only created if they not exist.
  * @return array
  * @throws BadRequestHttpException
  * @throws Exception
  */
 public function import()
 {
     $result = ['languages' => ['new' => 0, 'updated' => 0], 'languageSources' => ['new' => 0, 'updated' => 0], 'languageTranslations' => ['new' => 0, 'updated' => 0]];
     $data = $this->parseImportFile();
     /** @var Language[] $languages */
     $languages = Language::find()->indexBy('language_id')->all();
     foreach ($data['languages'] as $importedLanguage) {
         if (isset($languages[$importedLanguage['language_id']])) {
             $language = $languages[$importedLanguage['language_id']];
         } else {
             $language = new Language();
         }
         //cast integers
         $importedLanguage['status'] = (int) $importedLanguage['status'];
         $language->attributes = $importedLanguage;
         if (count($language->getDirtyAttributes())) {
             $saveType = $language->isNewRecord ? 'new' : 'updated';
             if ($language->save()) {
                 $result['languages'][$saveType]++;
             } else {
                 $this->throwInvalidModelException($language);
             }
         }
     }
     /** @var LanguageSource[] $languageSources */
     $languageSources = LanguageSource::find()->indexBy('id')->all();
     /** @var LanguageTranslate[] $languageTranslations */
     $languageTranslations = LanguageTranslate::find()->all();
     /*
      *  Create 2 dimensional array for current and imported translation, first index by LanguageSource->id
      *  and than indexed by LanguageTranslate->language.
      *  E.g.: [
      *      id => [
      *          language => LanguageTranslate (for $languageTranslations) / Array (for $importedLanguageTranslations)
      *          ...
      *      ]
      *      ...
      * ]
      */
     $languageTranslations = ArrayHelper::map($languageTranslations, 'language', function ($languageTranslation) {
         return $languageTranslation;
     }, 'id');
     $importedLanguageTranslations = ArrayHelper::map($data['languageTranslations'], 'language', function ($languageTranslation) {
         return $languageTranslation;
     }, 'id');
     foreach ($data['languageSources'] as $importedLanguageSource) {
         $languageSource = null;
         //check if id exist and if category and messages are matching
         if (isset($languageSources[$importedLanguageSource['id']]) && $languageSources[$importedLanguageSource['id']]->category == $importedLanguageSource['category'] && $languageSources[$importedLanguageSource['id']]->message == $importedLanguageSource['message']) {
             $languageSource = $languageSources[$importedLanguageSource['id']];
         }
         if (is_null($languageSource)) {
             //no match by id, search by message
             foreach ($languageSources as $languageSourceSearch) {
                 if ($languageSourceSearch->category == $importedLanguageSource['category'] && $languageSourceSearch->message == $importedLanguageSource['message']) {
                     $languageSource = $languageSourceSearch;
                     break;
                 }
             }
         }
         if (is_null($languageSource)) {
             //still no match, create new
             $languageSource = new LanguageSource(['category' => $importedLanguageSource['category'], 'message' => $importedLanguageSource['message']]);
             if ($languageSource->save()) {
                 $result['languageSources']['new']++;
             } else {
                 $this->throwInvalidModelException($languageSource);
             }
         }
         //do we have translations for the current source?
         if (isset($importedLanguageTranslations[$importedLanguageSource['id']])) {
             //loop through the translations for the current source
             foreach ($importedLanguageTranslations[$importedLanguageSource['id']] as $importedLanguageTranslation) {
                 $languageTranslate = null;
                 //is there already a translation for this souce
                 if (isset($languageTranslations[$languageSource->id]) && isset($languageTranslations[$languageSource->id][$importedLanguageTranslation['language']])) {
                     $languageTranslate = $languageTranslations[$languageSource->id][$importedLanguageTranslation['language']];
                 }
                 //no translation found, create a new one
                 if (is_null($languageTranslate)) {
                     $languageTranslate = new LanguageTranslate();
                 }
                 $languageTranslate->attributes = $importedLanguageTranslation;
                 //overwrite the id because the $languageSource->id might be different from the $importedLanguageTranslation['id']
                 $languageTranslate->id = $languageSource->id;
                 if (count($languageTranslate->getDirtyAttributes())) {
                     $saveType = $languageTranslate->isNewRecord ? 'new' : 'updated';
                     if ($languageTranslate->save()) {
                         $result['languageTranslations'][$saveType]++;
                     } else {
                         $this->throwInvalidModelException($languageTranslate);
                     }
                 }
             }
         }
     }
     return $result;
 }
Exemplo n.º 8
0
 /**
  * Returns the completness of a given translation (language).
  * @return integer
  */
 public function getGridStatistic()
 {
     static $statistics;
     if (!$statistics) {
         $count = LanguageSource::find()->count();
         if ($count == 0) {
             return 0;
         }
         $languages = Language::find()->select('language_id, COUNT(`lt`.`id`) AS `status`')->leftJoin(LanguageTranslate::tableName() . ' AS `lt`', '`language`.`language_id` = `lt`.`language`')->groupBy('language_id')->all();
         foreach ($languages as $language) {
             $statistics[$language->language_id] = round($language->status / $count, 2) * 100;
         }
     }
     return isset($statistics[$this->language_id]) ? $statistics[$this->language_id] : 0;
 }