예제 #1
0
 /**
  * Bind all the Chapters and ContentNodes to fetch the complete Article.
  * @param Garp_Model_Db $model
  * @return Void
  */
 public function bindWithChapters(Garp_Model_Db &$model)
 {
     $chapterModel = new Model_Chapter();
     $contentNodeModel = new Model_ContentNode();
     $chapterModel->bindModel('content', array('modelClass' => $contentNodeModel));
     foreach ($this->_config['contentTypes'] as $chapterType) {
         $chapterAlias = $this->_extractContentTypeAlias($chapterType);
         $options = $this->_extractContentTypeBindOptions($chapterType, $contentNodeModel);
         $contentNodeModel->bindModel($chapterAlias, $options);
     }
     $model->bindModel('chapters', array('modelClass' => $chapterModel));
 }
예제 #2
0
 protected function _resolveSluggableBehavior(Garp_Model_Db $model)
 {
     $sluggable = $model->getObserver('Sluggable');
     if (!is_null($sluggable)) {
         return array($sluggable, $model);
     }
     // Try on a derived model
     $translatable = $model->getObserver('Translatable');
     if (!is_null($translatable)) {
         $model = $translatable->getI18nModel($model->getUnilingualModel());
         return $this->_resolveSluggableBehavior($model);
     }
     return array(null, null);
 }
 /**
  * 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;
 }
예제 #4
0
 protected function _fetchAllIds(Garp_Model_Db $model)
 {
     $fields = array('id');
     $select = $model->select()->from($model->getName(), $fields);
     $records = $model->fetchAll($select);
     return $records;
 }
예제 #5
0
 protected function _getElasticModel(Garp_Model_Db $model)
 {
     $modelId = $model->getNameWithoutNamespace();
     $elasticModel = new Garp_Service_Elasticsearch_Model($modelId);
     return $elasticModel;
 }
예제 #6
0
 /**
  * Set WHERE clause that checks for slug existence.
  * @param Zend_Db_Select $select
  * @param String $slugField
  * @param String $slug
  * @param Garp_Model_Db $model
  * @param String $lang
  * @return Void
  */
 protected function _setWhereClause(Zend_Db_Select &$select, $slugField, $slug, Garp_Model_Db $model, $lang = null)
 {
     $slugField = $model->getAdapter()->quoteIdentifier($slugField);
     $select->reset(Zend_Db_Select::WHERE)->where($slugField . ' = ?', $slug);
     if ($lang) {
         $select->where(Garp_Model_Behavior_Translatable::LANG_COLUMN . ' = ?', $lang);
     }
 }
예제 #7
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);
     }
 }
예제 #8
0
 protected function _getRelatedBehavior(Garp_Model_Db $relatedModel)
 {
     $relatedBehavior = $relatedModel->getObserver('Elasticsearchable');
     return $relatedBehavior;
 }
예제 #9
0
 protected function _getForeignKeyColumns(Garp_Model_Db $i18nModel, Garp_Model_Db $model)
 {
     $reference = $i18nModel->getReference(get_class($model));
     $foreignKeyColumns = $reference['columns'];
     return $foreignKeyColumns;
 }
예제 #10
0
 protected function _getColumnType($column, Garp_Model_Db $model)
 {
     $colInfo = $model->getFieldConfiguration($column);
     return $colInfo['type'];
 }
예제 #11
0
 /**
  * Add the WHERE clause that keeps offline items from appearing in the results
  * @param Garp_Model_Db $model
  * @param Zend_Db_Select $select
  * @return Void
  */
 public function addWhereClause(&$model, Zend_Db_Select &$select)
 {
     $statusColumn = $model->getAdapter()->quoteIdentifier(self::STATUS_COLUMN);
     $publishedColumn = $model->getAdapter()->quoteIdentifier(self::PUBLISHED_COLUMN);
     if ($this->_modelAlias) {
         $modelAlias = $this->_modelAlias;
         $modelAlias = $model->getAdapter()->quoteIdentifier($modelAlias);
         $statusColumn = "{$modelAlias}.{$statusColumn}";
         $publishedColumn = "{$modelAlias}.{$publishedColumn}";
     }
     // Add online_status check
     $select->where($statusColumn . ' = ?', self::ONLINE);
     // Add published check
     if ($this->_config['draft_only']) {
         return;
     }
     $ini = Zend_Registry::get('config');
     $timezone = !empty($ini->resources->db->params->timezone) ? $ini->resources->db->params->timezone : null;
     $timecalc = '';
     if ($timezone == 'GMT' || $timezone == 'UTC') {
         $dstStart = strtotime('Last Sunday of March');
         $dstEnd = strtotime('Last Sunday of October');
         $now = time();
         $daylightSavingsTime = $now > $dstStart && $now < $dstEnd;
         $timecalc = '+ INTERVAL';
         if ($daylightSavingsTime) {
             $timecalc .= ' 2 HOUR';
         } else {
             $timecalc .= ' 1 HOUR';
         }
     }
     $select->where($publishedColumn . ' IS NULL OR ' . $publishedColumn . ' <= NOW() ' . $timecalc);
 }
예제 #12
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);
 }
예제 #13
0
파일: Db.php 프로젝트: grrr-amsterdam/garp3
 /**
  * Get all textual columns from a table
  *
  * @param  Garp_Model_Db  $model  The model
  * @return array
  */
 protected function _getTextualColumns(Garp_Model_Db $model)
 {
     $columns = $model->info(Zend_Db_Table::METADATA);
     $textTypes = array('varchar', 'text', 'mediumtext', 'longtext', 'tinytext');
     foreach ($columns as $column => $meta) {
         if (!in_array($meta['DATA_TYPE'], $textTypes)) {
             unset($columns[$column]);
         }
     }
     return array_keys($columns);
 }
예제 #14
0
파일: Db.php 프로젝트: grrr-amsterdam/garp3
 /**
  * Set wether this model caches queries
  * @param $flag Boolean
  * @return $this
  */
 public function setCacheQueries($flag)
 {
     self::$_cacheQueries = $flag;
     return $this;
 }
예제 #15
0
 protected function _insertZip(Garp_Service_PostcodeNl_Zipcode &$zip, Garp_Model_Db &$model)
 {
     $newRow = array('zip' => $zip->zipcode, 'latitude' => $zip->latitude, 'longitude' => $zip->longitude, 'source' => self::SOURCE_LABEL);
     if ($model->insert($newRow)) {
         $this->_storedZips++;
     }
 }
예제 #16
0
 /**
  * Increment the version to invalidate a given model's cache.
  *
  * @param Garp_Model_Db $model
  * @return Void
  */
 protected static function _incrementMemcacheVersion(Garp_Model_Db $model)
 {
     $cache = new Garp_Cache_Store_Versioned($model->getName() . '_version');
     $cache->incrementVersion();
 }
예제 #17
0
 /**
  * Negotiate between joint view and actual table name.
  *
  * @param Garp_Model_Db $model
  * @return string
  */
 protected function _getTableName($model)
 {
     if (!$this->usesJointView()) {
         return $model->getName();
     }
     return $model->getJointView() ?: $model->getName();
 }