Created
September 28, 2011 20:59
-
-
Save splittingred/1249237 to your computer and use it in GitHub Desktop.
Old vs New MODX Processors (as of 2.2)
This file contains 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 | |
class modActionCreateProcessor extends modProcessor { | |
/** @var modAction $action */ | |
public $action; | |
public function checkPermissions() { | |
return $this->modx->hasPermission('actions'); | |
} | |
public function getLanguageTopics() { | |
return array('action','menu','namespace'); | |
} | |
/** | |
* {@inheritDoc} | |
* | |
* @return mixed | |
*/ | |
public function initialize() { | |
$id = $this->getProperty('id'); | |
if (empty($id)) return $this->modx->lexicon('action_err_ns'); | |
$this->action = $this->modx->getObject('modAction',$id); | |
if (empty($this->action)) return $this->modx->lexicon('action_err_nf'); | |
return true; | |
} | |
/** | |
* {@inheritDoc} | |
* | |
* @return array|string | |
*/ | |
public function process() { | |
$fields = $this->getProperties(); | |
$fields['haslayout'] = !empty($fields['haslayout']); | |
if (!$this->validate($fields)) { | |
return $this->failure(); | |
} | |
/* @var modAction $action */ | |
$this->action = $this->modx->newObject('modAction'); | |
$this->action->fromArray($fields); | |
if (empty($fields['lang_topics'])) { | |
$action->set('lang_topics',''); | |
} | |
if ($this->action->save() == false) { | |
return $this->failure($this->modx->lexicon('action_err_create')); | |
} | |
$this->logManagerAction(); | |
return $this->success('',$this->action); | |
} | |
/** | |
* Validate the incoming data | |
* @param array $fields | |
* @return boolean | |
*/ | |
public function validate(array $fields) { | |
if (empty($fields['controller'])) { | |
$this->addFieldError('controller',$this->modx->lexicon('controller_err_ns')); | |
} | |
/* verify parent */ | |
if (!isset($fields['parent'])) { | |
$this->addFieldError('parent',$this->modx->lexicon('action_parent_err_ns')); | |
} else if (!empty($fields['parent'])) { | |
$parent = $this->modx->getObject('modAction',$fields['parent']); | |
if (empty($parent)) { | |
$this->addFieldError('parent',$this->modx->lexicon('action_parent_err_nf')); | |
} | |
} | |
/* verify namespace */ | |
if (empty($fields['namespace'])) $this->addFieldError('namespace',$this->modx->lexicon('namespace_err_nf')); | |
$namespace = $this->modx->getObject('modNamespace',$fields['namespace']); | |
if (empty($namespace)) { | |
$this->addFieldError('namespace',$this->modx->lexicon('namespace_err_nf')); | |
} | |
return !$this->hasErrors(); | |
} | |
/** | |
* Get the parent action and set fields if found | |
* | |
* @return null|modAction | |
*/ | |
public function getParent() { | |
/* get parent */ | |
$parent = $this->action->getOne('Parent'); | |
if ($parent != null) { | |
$this->action->set('parent',$parent->get('id')); | |
$this->action->set('parent_controller',$parent->get('controller')); | |
} | |
return $parent; | |
} | |
/** | |
* Log the manager action | |
* | |
* @return void | |
*/ | |
public function logManagerAction() { | |
$this->modx->logManagerAction('action_create','modAction',$this->action->get('id')); | |
} | |
} | |
return 'modActionCreateProcessor'; |
This file contains 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 | |
if (!$modx->hasPermission('actions')) return $modx->error->failure($modx->lexicon('permission_denied')); | |
$modx->lexicon->load('action','menu','namespace'); | |
/* verify/format data */ | |
$scriptProperties['haslayout'] = !empty($scriptProperties['haslayout']); | |
if (empty($scriptProperties['controller'])) return $modx->error->failure($modx->lexicon('controller_err_ns')); | |
/* verify parent */ | |
if (!isset($scriptProperties['parent'])) return $modx->error->failure($modx->lexicon('action_parent_err_ns')); | |
if (!empty($scriptProperties['parent'])) { | |
$parent = $modx->getObject('modAction',$scriptProperties['parent']); | |
if ($parent == null) return $modx->error->failure($modx->lexicon('action_parent_err_nf')); | |
} | |
/* verify namespace */ | |
if (empty($scriptProperties['namespace'])) return $modx->error->failure($modx->lexicon('namespace_err_nf')); | |
$namespace = $modx->getObject('modNamespace',$scriptProperties['namespace']); | |
if ($namespace == null) return $modx->error->failure($modx->lexicon('namespace_err_nf')); | |
/* create action */ | |
$action = $modx->newObject('modAction'); | |
$action->fromArray($scriptProperties); | |
/* save action */ | |
if ($action->save() == false) { | |
return $modx->error->failure($modx->lexicon('action_err_create')); | |
} | |
/* log manager action */ | |
$modx->logManagerAction('action_create','modAction',$action->get('id')); | |
return $modx->error->success('',$action); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment