/** * @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)); } } }