Esempio n. 1
0
 protected function _getTranslatedViewName(Garp_Spawn_Model_Abstract $model = null)
 {
     if (!$model) {
         $model = $this->getModel();
     }
     $locale = Garp_I18n::getDefaultLocale();
     $i18nView = new Garp_Spawn_MySql_View_I18n($model, $locale);
     $viewName = $i18nView->getName();
     return $viewName;
 }
Esempio n. 2
0
 protected function _fetchRecordsInDefaultLanguage(Garp_Model_Db $model)
 {
     $i18nColumns = array_filter($model->getConfiguration('fields'), function ($col) {
         return $col['multilingual'];
     });
     $i18nColumns = array_map(function ($col) {
         return $col['name'];
     }, $i18nColumns);
     $i18nModel = $model->getObserver('Translatable')->getI18nModel($model);
     $foreignKeyColumns = $this->_getForeignKeyColumns($i18nModel, $model);
     return $i18nModel->fetchAll($i18nModel->select()->from($i18nModel->getName(), array_merge($i18nColumns, $foreignKeyColumns))->where('lang = ?', Garp_I18n::getDefaultLocale()));
 }
Esempio n. 3
0
 protected function _renderSqlForLang()
 {
     $model = $this->getModel();
     $modelId = $this->getTableName();
     $unilingualFields = $model->fields->getFields('multilingual', false);
     $multilingualFields = $this->_getMultilingualFieldsFromModel($model);
     $locale = $this->getLocale();
     $defaultLocale = Garp_I18n::getDefaultLocale();
     $table = $modelId;
     $localeTable = $table . '_' . $locale;
     $defaultLocaleTable = $table . '_' . $defaultLocale;
     $sql = 'SELECT ';
     //  Unilingual fields
     $unilingualFieldRefs = array();
     foreach ($unilingualFields as $field) {
         $unilingualFieldRefs[] = '`' . $table . '`.`' . $field->name . '` AS `' . $field->name . '`';
     }
     $sql .= implode(', ', $unilingualFieldRefs) . ', ';
     //  Multilingual fields
     $multilingualFieldRefs = array();
     foreach ($multilingualFields as $field) {
         $multilingualFieldRefs[] = "`{$modelId}_{$locale}`.{$field->name} AS `{$field->name}`";
         /*
                     $multilingualFieldRefs[] = $locale === $defaultLocale ?
            "`{$modelId}_{$locale}`.{$field->name} AS `{$field->name}`" :
            "IF(`{$modelId}_{$locale}`.`{$field->name}` <> '' AND `{$modelId}_{$locale}`.`{$field->name}` IS NOT NULL, `{$modelId}_{$locale}`.`{$field->name}`, `{$modelId}_{$defaultLocale}`.`{$field->name}`) AS `{$field->name}`"
                     ;
         */
     }
     $sql .= implode(', ', $multilingualFieldRefs) . ' ';
     //  Join translated tables
     $sql .= 'FROM `' . $modelId . '`';
     $sql .= $this->_renderJoinForLocale($locale);
     //if ($locale !== $defaultLocale) {
     //$sql .= $this->_renderJoinForLocale($defaultLocale);
     //}
     return $sql;
 }
Esempio n. 4
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);
 }
Esempio n. 5
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);
 }
Esempio n. 6
0
 /**
  * Get current language from URL
  *
  * @return string
  */
 protected function _getCurrentLanguage()
 {
     if (!isset($_SERVER['REQUEST_URI'])) {
         // Probably CLI context. Return the default locale
         return Garp_I18n::getDefaultLocale();
     }
     $requestUri = $_SERVER['REQUEST_URI'];
     $bits = explode('/', $requestUri);
     // remove empty values
     $bits = array_filter($bits, 'strlen');
     // reindex the array
     $bits = array_values($bits);
     $bits = array_map('strtolower', $bits);
     $locales = $this->_getPossibleLocales();
     if (array_key_exists(0, $bits) && in_array($bits[0], $locales)) {
         return $bits[0];
     }
     return Garp_I18n::getDefaultLocale();
 }