/**
  * Returns a list of behaviors that this component should behave as.
  *
  * Child classes may override this method to specify the behaviors they want to behave as.
  *
  * The return value of this method should be an array of behavior objects or configurations
  * indexed by behavior names. A behavior configuration can be either a string specifying
  * the behavior class or an array of the following structure:
  *
  * ```php
  * 'behaviorName' => [
  *     'class' => 'BehaviorClass',
  *     'property1' => 'value1',
  *     'property2' => 'value2',
  * ]
  * ```
  *
  * Note that a behavior class must extend from [[Behavior]]. Behavior names can be strings
  * or integers. If the former, they uniquely identify the behaviors. If the latter, the corresponding
  * behaviors are anonymous and their properties and methods will NOT be made available via the component
  * (however, the behaviors can still respond to the component's events).
  *
  * Behaviors declared in this method will be attached to the component automatically (on demand).
  *
  * @return array the behavior configurations.
  */
 public function behaviors()
 {
     $behaviors = ['verbs' => ['class' => VerbFilter::className(), 'actions' => ['delete' => ['post']]]];
     if (Module::hasUserRole()) {
         $behaviors['role'] = ['class' => \navatech\role\filters\RoleFilter::className(), 'name' => Translate::x_management([Translate::language()]), 'actions' => ['index' => Translate::lists(), 'delete' => Translate::delete()]];
     }
     return $behaviors;
 }
예제 #2
0
 /**
  * {@inheritDoc}
  */
 public function init()
 {
     parent::init();
     $languages = ArrayHelper::map(Language::getLanguages(), 'code', 'name');
     if (isset($_GET['language']) && isset($languages[$_GET['language']]) && $_GET['language'] != Yii::$app->request->getCookies()->getValue('_language')) {
         Yii::$app->session['language'] = $_GET['language'];
         $cookie = new Cookie(['name' => '_language', 'value' => $_GET['language'], 'path' => '/']);
         Yii::$app->language = $_GET['language'];
         Yii::$app->response->cookies->add($cookie);
         Yii::$app->response->refresh();
     }
     if (!Yii::$app->request->cookies->has('_language')) {
         if (Module::hasSetting()) {
             $cookie = new Cookie(['name' => '_language', 'value' => Yii::$app->setting->general_language, 'path' => '/']);
             Yii::$app->language = Yii::$app->setting->general_language;
         } else {
             $cookie = new Cookie(['name' => '_language', 'value' => Yii::$app->language, 'path' => '/']);
         }
         Yii::$app->response->cookies->add($cookie);
     } else {
         Yii::$app->language = Yii::$app->request->getCookies()->getValue('_language');
     }
 }
 /**
  * @param PhraseTranslate $model
  *
  * @throws ErrorException|Exception
  * @since 1.0.2
  */
 public static function setData(PhraseTranslate $model)
 {
     $name = $model->phrase->name;
     $language_code = $model->language->code;
     $runtime = Module::isBasic() ? Yii::getAlias('@runtime') : Yii::getAlias('@backend/runtime');
     $path = $runtime . DIRECTORY_SEPARATOR . 'language';
     if (!file_exists($path)) {
         self::setAllData();
     }
     if ($model->isNewRecord) {
         foreach (self::getLanguages() as $language) {
             $file = $path . DIRECTORY_SEPARATOR . 'phrase_' . $language['code'] . '.json';
             if (!file_exists($file)) {
                 self::setAllData($language['code']);
             }
             try {
                 $myFile = fopen($file, "r");
                 $data = fread($myFile, filesize($file));
             } catch (ErrorException $e) {
                 throw new ErrorException('Unable to open "' . $file . '"');
             }
             fclose($myFile);
             $data = Json::decode($data);
             if ($data === null) {
                 self::setAllData($language['code']);
             } else {
                 if (!array_key_exists($name, $data)) {
                     $data[$name] = $model->value;
                     file_put_contents($file, Json::encode($data));
                 }
             }
         }
         $class = $path . DIRECTORY_SEPARATOR . 'Translate.php';
         if (!file_exists($class)) {
             self::setAllData();
         }
         try {
             $myFile = fopen($class, "r");
             $content = fread($myFile, filesize($class));
         } catch (ErrorException $e) {
             throw new ErrorException('Unable to open "' . $class . '"');
         }
         fclose($myFile);
         if (!strpos($content, 'function ' . $name . '(')) {
             $php = '       /**' . PHP_EOL;
             $php .= '       * @param null|array|mixed $parameters' . PHP_EOL;
             $php .= '       * @param null|string $language_code' . PHP_EOL;
             $php .= '       * @return string' . PHP_EOL;
             $php .= '       */' . PHP_EOL;
             $php .= '       public static function ' . $name . '($parameters = null, $language_code = null){}' . PHP_EOL;
             $php .= '//defined_new_method_here' . PHP_EOL;
             $content = str_replace('//defined_new_method_here', $php, $content);
             $fp = fopen($class, 'wb');
             fwrite($fp, $content);
             fclose($fp);
         }
     } else {
         $file = $path . DIRECTORY_SEPARATOR . 'phrase_' . $language_code . '.json';
         if (!file_exists($file)) {
             self::setAllData($language_code);
         }
         try {
             $myFile = fopen($file, "r");
             $data = fread($myFile, filesize($file));
         } catch (ErrorException $e) {
             throw new ErrorException('Unable to open "' . $file . '"');
         }
         $data = Json::decode($data);
         fclose($myFile);
         if ($data === null) {
             self::setAllData($language_code);
         } else {
             $data[$name] = $model->value;
             file_put_contents($file, Json::encode($data));
         }
     }
 }