Created
March 25, 2016 15:54
-
-
Save doctorallen/79af32bc4096531d50af to your computer and use it in GitHub Desktop.
Override Processor for MODX and ImportX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once (dirname(__FILE__).'/update.class.php'); | |
/** | |
* | |
* @param $data A JSON array of data to update from. | |
* | |
* @package modx | |
* @subpackage processors.resource | |
*/ | |
class modResourceUpdateNoOverrideProcessor extends modResourceUpdateProcessor { | |
public function initialize() { | |
return parent::initialize(); | |
} | |
/** | |
* 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 : ''; | |
} | |
// if there is a blank value (it wasn't passed) continue | |
if($value == "") { | |
continue; | |
} | |
/* 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 */ | |
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; | |
} | |
} | |
return 'modResourceUpdateNoOverrideProcessor'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment