Пример #1
0
 public static function deleteAll()
 {
     $locales = Garp_I18n::getLocales();
     foreach ($locales as $locale) {
         parent::deleteAllByPostfix('_' . $locale);
     }
 }
 /**
  * Populate the subject model's referenceMap with
  * localized versions of the given model.
  * @param String|Garp_Model_Db $relatedModel
  * @return Void
  */
 public function populate($relatedModel, $ruleKey = null)
 {
     // Sanity check: does the model have a reference to the
     // given model in the first place?
     // This will throw an exception if not.
     $relatedModel = $relatedModel instanceof Garp_Model_Db ? get_class($relatedModel) : $relatedModel;
     $relatedModel = (substr($relatedModel, 0, 6) !== 'Model_' ? 'Model_' : '') . $relatedModel;
     $ref = $this->_model->getReference($relatedModel, $ruleKey);
     $locales = Garp_I18n::getLocales();
     foreach ($locales as $locale) {
         $factory = new Garp_I18n_ModelFactory($locale);
         $localizedModel = $factory->getModel($relatedModel);
         $localizedModelName = get_class($localizedModel);
         $cleanLocalizedName = $localizedModel->getNameWithoutNamespace();
         $this->_model->addReference($cleanLocalizedName, $ref[Zend_Db_Table_Abstract::COLUMNS], $localizedModelName, $ref[Zend_Db_Table_Abstract::REF_COLUMNS]);
     }
     return $this;
 }
Пример #3
0
 /**
  * @param String $model_name (not prefixed)
  * @param Garp_Db_Table_Row|Int $record The record on which the alternative is based,
  *                                      or the id thereof.
  * @param String $foreign_key Foreign key column to the parent table
  * @return Array
  * @todo Make it work with various other parameters, not just 'slug'
  */
 public function direct($model_name, $foreign_key, $record, $default_to_same_slug = true)
 {
     $record_id = $record;
     if ($record instanceof Garp_Db_Table_Row) {
         $record_id = $record_id->id;
     }
     // Add the slugs of the group in alternate languages for the language picker
     $alt_param_slug = null;
     if ($default_to_same_slug && $record instanceof Garp_Db_Table_Row) {
         $alt_param_slug = $record->slug;
     }
     $alternate_url_params = array_fill_keys(Garp_I18n::getLocales(), array('slug' => $alt_param_slug));
     $model_name = 'Model_' . $model_name . 'I18n';
     $i18n_model = new $model_name();
     $select = $i18n_model->select()->from($i18n_model->getName(), array('slug', 'lang'))->where("{$foreign_key} = ?", $record_id)->where('lang != ?', Garp_I18n::getCurrentLocale());
     $localized_recordset = $i18n_model->fetchAll($select);
     foreach ($localized_recordset as $record) {
         $alternate_url_params[$record->lang] = array('slug' => $record->slug);
     }
     return $alternate_url_params;
 }
Пример #4
0
 public function renderImagePreviewListener(Garp_Spawn_Relation $rel)
 {
     if (!$rel->multilingual) {
         return $this->renderImagePreviewListenerJs($this->getImagePreviewId($rel->column), $rel->column);
     }
     $self = $this;
     return implode("\n", array_map(function ($locale) use($rel, $self) {
         return $self->renderImagePreviewListenerJs($self->getImagePreviewId($rel->column, $locale), $rel->column, $locale);
     }, Garp_I18n::getLocales()));
 }
Пример #5
0
 /**
  * Clear the Memcached cache that stores queries and ini files and whatnot.
  *
  * @param Array|Garp_Model_Db $modelNames
  * @return Void
  */
 public static function purgeMemcachedCache($modelNames = array())
 {
     if (!Zend_Registry::isRegistered('CacheFrontend')) {
         return;
     }
     if (!Zend_Registry::get('CacheFrontend')->getOption('caching')) {
         // caching is disabled
         return;
     }
     if ($modelNames instanceof Garp_Model_Db) {
         $modelNames = self::getTagsFromModel($modelNames);
     }
     if (empty($modelNames)) {
         $cacheFront = Zend_Registry::get('CacheFrontend');
         return $cacheFront->clean(Zend_Cache::CLEANING_MODE_ALL);
     }
     foreach ($modelNames as $modelName) {
         $model = new $modelName();
         self::_incrementMemcacheVersion($model);
         if (!$model->getObserver('Translatable')) {
             continue;
         }
         // Make sure cache is cleared for all languages.
         $locales = Garp_I18n::getLocales();
         foreach ($locales as $locale) {
             try {
                 $modelFactory = new Garp_I18n_ModelFactory($locale);
                 $i18nModel = $modelFactory->getModel($model);
                 self::_incrementMemcacheVersion($i18nModel);
             } catch (Garp_I18n_ModelFactory_Exception_ModelAlreadyLocalized $e) {
                 // all good in the hood  。^‿^。
             }
         }
     }
 }
Пример #6
0
 protected function _createI18nViews(Garp_Spawn_Model_Base $model)
 {
     $locales = Garp_I18n::getLocales();
     foreach ($locales as $locale) {
         $view = new Garp_Spawn_MySql_View_I18n($model, $locale);
         $view->create();
     }
 }
Пример #7
0
 /**
  * Check if value is a multilingual array.
  *
  * @param mixed $value
  * @return bool
  */
 protected function _isMultilingualArray($value)
 {
     if (!is_array($value)) {
         return false;
     }
     $locales = Garp_I18n::getLocales();
     $keys = array_keys($value);
     sort($locales);
     sort($keys);
     return $locales === $keys;
 }
Пример #8
0
 /**
  * OPTIONS
  *
  * @param array $params
  * @return array
  */
 public function options(array $params)
 {
     $schema = new Garp_Content_Api_Rest_Schema('rest');
     if (!array_get($params, 'datatype')) {
         $out = array();
         $out['root'] = (string) new Garp_Util_FullUrl(array(array(), 'rest'));
         $out['i18n'] = array('locales' => Garp_I18n::getLocales(), 'default' => Garp_I18n::getDefaultLocale());
         $out['urls'] = $this->_getUrlsForOptions();
         $out['models'] = $schema->getModelPaths();
         return $this->_formatResponse($out, 200);
     }
     if (array_get($params, 'id') || array_get($params, 'relatedType')) {
         return $this->_formatResponse(null, 200, false);
     }
     return $this->_formatResponse($schema->getModelDetails($params['datatype']), 200);
 }
Пример #9
0
 protected function _saveLocalizedModels()
 {
     $locales = Garp_I18n::getLocales();
     array_walk($locales, array($this, '_saveLocalizedModel'));
 }
Пример #10
0
 protected function _getRecordLabelSqlForMultilingualRelation($relName, $rel)
 {
     $self = $this;
     return array_map(function ($lang) use($self, $relName, $rel) {
         return $self->getRecordLabelSqlForRelation($relName, $rel, $lang);
     }, Garp_I18n::getLocales());
 }
Пример #11
0
 /**
  * A real hacky solution to enable admins to search for translated content in the CMS
  *
  * @param Garp_Model_Db $model
  * @param Zend_Db_Select $select
  * @param string $likeValue
  * @return string A search clause
  */
 protected function _joinCmsSearchQuery(Garp_Model_Db $model, Zend_Db_Select &$select, $likeValue)
 {
     $languages = Garp_I18n::getLocales();
     $default_language = array(Garp_I18n::getDefaultLocale());
     $langColumn = self::LANG_COLUMN;
     // Exclude default language, since that's already joined in the joint view
     $languages = array_diff($languages, $default_language);
     $adapter = $model->getAdapter();
     $where = array();
     foreach ($languages as $language) {
         $i18nModel = $this->getI18nModel($model);
         $i18nAlias = $model->getName() . '_i18n_' . $language;
         $onClause = $i18nModel->refMapToOnClause(get_class($model), $i18nAlias, $model->getJointView());
         // join i18n model
         $select->joinLeft(array($i18nAlias => $i18nModel->getName()), "{$onClause} AND {$i18nAlias}.{$langColumn} = '{$language}'", array());
         // add WHERE clauses that search in the i18n model
         $translatedFields = $this->_translatableFields;
         foreach ($translatedFields as $i18nField) {
             $where[] = "{$i18nAlias}.{$i18nField} LIKE " . $adapter->quote($likeValue);
         }
     }
     return implode(' OR ', $where);
 }
Пример #12
0
 protected function _modelIsLocalized($model)
 {
     $languages = Garp_I18n::getLocales();
     if (!is_array($languages)) {
         return false;
     }
     foreach ($languages as $lang) {
         $langSuffix = ucfirst($lang);
         if (preg_match("/[a-z0-9]+{$langSuffix}\$/", $model)) {
             return true;
         }
     }
     return false;
 }