예제 #1
0
 /**
  * Add Template Variables to the Resource object
  * @return array
  */
 public function addTemplateVariables()
 {
     $addedTemplateVariables = array();
     $templateKey = $this->getProperty('template', 0);
     $this->template = $this->modx->getObject('modTemplate', $templateKey);
     if (!empty($templateKey) && $this->template) {
         $templateVars = $this->object->getTemplateVars();
         /** @var modTemplateVar $tv */
         foreach ($templateVars as $tv) {
             $tvKey = 'tv' . $tv->get('id');
             $value = $this->getProperty($tvKey, $tv->get('default_text'));
             switch ($tv->get('type')) {
                 /* ensure tag types trim whitespace from tags */
                 case 'tag':
                 case 'autotag':
                     $tags = explode(',', $value);
                     $newTags = array();
                     foreach ($tags as $tag) {
                         $newTags[] = trim($tag);
                     }
                     $value = implode(',', $newTags);
                     break;
                 case 'date':
                     $value = empty($value) ? '' : strftime('%Y-%m-%d %H:%M:%S', strtotime($value));
                     break;
                 case 'url':
                     if ($this->getProperty($tvKey . '_prefix', '--') != '--') {
                         $value = str_replace(array('ftp://', 'http://'), '', $value);
                         $value = $this->getProperty($tvKey . '_prefix', '') . $value;
                     }
                     break;
                 default:
                     /* handles checkboxes & multiple selects elements */
                     if (is_array($value)) {
                         $featureInsert = array();
                         while (list($featureValue, $featureItem) = each($value)) {
                             $featureInsert[count($featureInsert)] = $featureItem;
                         }
                         $value = implode('||', $featureInsert);
                     }
                     break;
             }
             /* save value if it was modified */
             $default = $tv->processBindings($tv->get('default_text'), 0);
             if (strcmp($value, $default) != 0) {
                 /** @var modTemplateVarResource $templateVarResource */
                 $templateVarResource = $this->modx->newObject('modTemplateVarResource');
                 $templateVarResource->set('tmplvarid', $tv->get('id'));
                 $templateVarResource->set('value', $value);
                 $addedTemplateVariables[] = $templateVarResource;
             }
         }
     }
     return $addedTemplateVariables;
 }
 /**
  * Synchronize Resource and its TVs to the translations of the defined languages
  * @param modResource $resource
  * @return boolean
  */
 public function synchronize(modResource $resource)
 {
     $resourceArray = $resource->toArray();
     $this->modx->resource = $resource;
     $languages = $this->getLanguages(true, true, false);
     if ($languages) {
         $langCodes = array();
         foreach ($languages as $language) {
             $langCodes[] = $language['lang_code'];
         }
         // first, delete unused translation
         $c = $this->modx->newQuery('linguaSiteContent');
         $c->innerJoin('linguaLangs', 'Lang');
         $c->where(array('linguaSiteContent.resource_id:=' => $resourceArray['id'], 'Lang.lang_code:NOT IN' => $langCodes));
         $unusedContents = $this->modx->getCollection('linguaSiteContent', $c);
         if ($unusedContents) {
             foreach ($unusedContents as $item) {
                 $item->remove();
             }
         }
         foreach ($languages as $language) {
             $this->setContentTranslation($resourceArray['id'], $language['lang_code'], $resourceArray, false);
         }
         $tvs = $resource->getTemplateVars();
         $translatedTvs = $this->modx->getCollection('linguaSiteTmplvars');
         if ($translatedTvs && $tvs) {
             $translatedTvsArray = array();
             foreach ($translatedTvs as $translatedTv) {
                 $translatedTvsArray[] = $translatedTv->get('tmplvarid');
             }
             foreach ($tvs as $tv) {
                 // first, delete unused translation
                 $c = $this->modx->newQuery('linguaSiteTmplvarContentvalues');
                 $c->innerJoin('linguaLangs', 'Lang');
                 $c->where(array('linguaSiteTmplvarContentvalues.contentid:=' => $resourceArray['id'], 'linguaSiteTmplvarContentvalues.tmplvarid:=' => $tv->get('id'), 'Lang.lang_code:NOT IN' => $langCodes));
                 $unusedContents = $this->modx->getCollection('linguaSiteTmplvarContentvalues', $c);
                 if ($unusedContents) {
                     foreach ($unusedContents as $item) {
                         $item->remove();
                     }
                 }
                 if (!in_array($tv->get('id'), $translatedTvsArray)) {
                     continue;
                 }
                 $this->setTVTranslation($resourceArray['id'], $language['lang_code'], $tv->get('id'), $tv->get('value'), false);
             }
         }
     }
     return true;
 }
예제 #3
0
 /**
  * Set any Template Variables passed to the Resource. You must pass "tvs" as 1 or true to initiate these checks.
  * @return array|mixed
  */
 public function saveTemplateVariables()
 {
     $tvs = $this->getProperty('tvs', null);
     if (!empty($tvs)) {
         $tmplvars = array();
         $tvs = $this->object->getTemplateVars();
         /** @var modTemplateVar $tv */
         foreach ($tvs as $tv) {
             if (!$tv->checkResourceGroupAccess()) {
                 continue;
             }
             $tvKey = 'tv' . $tv->get('id');
             $value = $this->getProperty($tvKey, null);
             /* set value of TV */
             if ($tv->get('type') != 'checkbox') {
                 $value = $value !== null ? $value : $tv->get('default_text');
             } else {
                 $value = $value ? $value : '';
             }
             /* validation for different types */
             switch ($tv->get('type')) {
                 case 'url':
                     $prefix = $this->getProperty($tvKey . '_prefix', '');
                     if ($prefix != '--') {
                         $value = str_replace(array('ftp://', 'http://'), '', $value);
                         $value = $prefix . $value;
                     }
                     break;
                 case 'date':
                     $value = empty($value) ? '' : strftime('%Y-%m-%d %H:%M:%S', strtotime($value));
                     break;
                     /* ensure tag types trim whitespace from tags */
                 /* ensure tag types trim whitespace from tags */
                 case 'tag':
                 case 'autotag':
                     $tags = explode(',', $value);
                     $newTags = array();
                     foreach ($tags as $tag) {
                         $newTags[] = trim($tag);
                     }
                     $value = implode(',', $newTags);
                     break;
                 default:
                     /* handles checkboxes & multiple selects elements */
                     if (is_array($value)) {
                         $featureInsert = array();
                         while (list($featureValue, $featureItem) = each($value)) {
                             if (empty($featureItem)) {
                                 continue;
                             }
                             $featureInsert[count($featureInsert)] = $featureItem;
                         }
                         $value = implode('||', $featureInsert);
                     }
                     break;
             }
             /* if different than default and set, set TVR record */
             $default = $tv->processBindings($tv->get('default_text'), $this->object->get('id'));
             if (strcmp($value, $default) != 0) {
                 /* update the existing record */
                 $tvc = $this->modx->getObject('modTemplateVarResource', array('tmplvarid' => $tv->get('id'), 'contentid' => $this->object->get('id')));
                 if ($tvc == null) {
                     /** @var modTemplateVarResource $tvc add a new record */
                     $tvc = $this->modx->newObject('modTemplateVarResource');
                     $tvc->set('tmplvarid', $tv->get('id'));
                     $tvc->set('contentid', $this->object->get('id'));
                 }
                 $tvc->set('value', $value);
                 $tvc->save();
                 /* if equal to default value, erase TVR record */
             } else {
                 $tvc = $this->modx->getObject('modTemplateVarResource', array('tmplvarid' => $tv->get('id'), 'contentid' => $this->object->get('id')));
                 if (!empty($tvc)) {
                     $tvc->remove();
                 }
             }
         }
     }
     return $tvs;
 }