示例#1
0
 /**
  * Load translation data
  *
  * @param  string|array  $data
  * @param  string        $locale  Locale/Language to add data for, identical with locale identifier,
  *                                see Zend_Locale for more information
  * @param  array         $options OPTIONAL Options to use
  * @return array
  */
 protected function _loadTranslationData($data, $locale, array $options = array())
 {
     $data = array();
     try {
         $i18nModelFactory = new Garp_I18n_ModelFactory($locale);
         $snippetModel = $i18nModelFactory->getModel('Snippet');
         $out = array();
         $data = $snippetModel->fetchAll($snippetModel->select()->from($snippetModel->getName(), array('identifier', 'text' => new Zend_Db_Expr('IF(text IS NULL, IF(name IS NULL, identifier, name), text)')))->where('has_text = ?', 1)->orWhere('has_name = ?', 1)->order('identifier ASC'));
         $data = $this->_reformatData($data);
     } catch (Zend_Db_Adapter_Exception $e) {
         Garp_ErrorHandler::handlePrematureException($e);
     }
     $out[$locale] = $data;
     return $out;
 }
 /**
  * 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
 /**
  * Retrieve snippet model for system messages.
  *
  * @return Model_Snippet
  */
 protected function _getSnippetModel()
 {
     $snippetModel = new Model_Snippet();
     if ($snippetModel->getObserver('Translatable')) {
         $i18nModelFactory = new Garp_I18n_ModelFactory();
         $snippetModel = $i18nModelFactory->getModel($snippetModel);
     }
     return $snippetModel;
 }
示例#4
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  。^‿^。
             }
         }
     }
 }
示例#5
0
 /**
  * For multilingual models: combine the referenceData with fresh data
  * from its counterpart in the primary language.
  * @param Array $referenceData
  * @param Garp_Model_Db $model
  * @return Void
  */
 protected function _modifyReferenceDataForMultilingualModels(array &$referenceData, Garp_Model_Db $model)
 {
     // Try to fetch the baseFields from the localised model
     $i18nModelFactory = new Garp_I18n_ModelFactory($referenceData[Garp_Model_Behavior_Translatable::LANG_COLUMN]);
     $unilingualModel = $model->getUnilingualModel();
     $localizedModel = $i18nModelFactory->getModel($unilingualModel);
     $localizedModel->unregisterObserver('Translatable');
     $localizedModel->unregisterObserver('Draftable');
     $referenceMap = $model->getReference(get_class($unilingualModel));
     // Construct a query that fetches the base fields from the parent model
     $baseFields = $this->_config['baseField'];
     $baseFields = $this->_baseFieldConfigToColumns($baseFields);
     $select = $localizedModel->select()->from($localizedModel->getName(), $baseFields);
     foreach ($referenceMap['columns'] as $i => $col) {
         if (!isset($referenceMap['refColumns'][$i])) {
             throw new Exception('ReferenceMap is invalid: columns does not match up with refColumns.');
         }
         $refCol = $referenceMap['refColumns'][$i];
         // If the required foreign key is not in $referenceData, we won't be able to solve the problem
         if (!isset($referenceData[$col])) {
             return null;
         }
         $select->where("{$refCol} = ?", $referenceData[$col]);
     }
     $localizedRecord = $localizedModel->fetchRow($select)->toArray();
     $referenceData = array_merge($localizedRecord, $referenceData);
 }
示例#6
0
 /**
  * Bind all HABTM related models so they, too, get exported
  *
  * @param Garp_Model_Db $model
  * @return void
  */
 protected function _bindModels(Garp_Model_Db $model)
 {
     // Add HABTM related records
     $relations = $model->getConfiguration('relations');
     foreach ($relations as $key => $config) {
         if ($config['type'] !== 'hasAndBelongsToMany' && $config['type'] !== 'hasMany') {
             continue;
         }
         $otherModelName = 'Model_' . $config['model'];
         $otherModel = new $otherModelName();
         $multilingual = false;
         $modelFactory = new Garp_I18n_ModelFactory();
         if ($otherModel->getObserver('Translatable')) {
             $otherModel = $modelFactory->getModel($otherModel);
             $multilingual = true;
         }
         $otherModelAlias = $otherModel->getName();
         $bindingModel = null;
         if ($config['type'] === 'hasAndBelongsToMany') {
             $bindingModelName = 'Model_' . $config['bindingModel'];
             $bindingModel = new $bindingModelName();
             if ($multilingual) {
                 $refmapLocaliser = new Garp_Model_ReferenceMapLocalizer($bindingModel);
                 $refmapLocaliser->populate($otherModelName);
             }
             $otherModelAlias = 'm';
         }
         $labelFields = $otherModel->getListFields();
         $prefixedLabelFields = array();
         foreach ($labelFields as $labelField) {
             $prefixedLabelFields[] = "{$otherModelAlias}.{$labelField}";
         }
         $labelFields = 'CONCAT_WS(", ", ' . implode(', ', $prefixedLabelFields) . ')';
         // If the Translatable behavior would be effective,
         // the output would be in a localized array, which is overkill for this
         // purpose.
         $otherModel->unregisterObserver('Translatable');
         $options = array('bindingModel' => $bindingModel, 'modelClass' => $otherModel, 'conditions' => $otherModel->select()->from(array($otherModelAlias => $otherModel->getName()), array($config['label'] => $labelFields))->order("{$otherModelAlias}.id"));
         $model->bindModel($config['label'], $options);
     }
 }