Skip to content

Instantly share code, notes, and snippets.

@splittingred
Created November 1, 2011 21:58
Show Gist options
  • Save splittingred/1332064 to your computer and use it in GitHub Desktop.
Save splittingred/1332064 to your computer and use it in GitHub Desktop.
<?php
require_once (dirname(dirname(__FILE__)).'/create.class.php');
/**
* Create a snippet.
*
* @param string $name The name of the element
* @param string $snippet The code of the snippet.
* @param string $description (optional) A brief description.
* @param integer $category (optional) The category to assign to. Defaults to no
* category.
* @param boolean $locked (optional) If true, can only be accessed by
* administrators. Defaults to false.
* @param json $propdata (optional) A json array of properties
*
* @package modx
* @subpackage processors.element.snippet
*/
class modSnippetCreateProcessor extends modElementCreateProcessor {
public $classKey = 'modSnippet';
public $languageTopics = array('snippet','category');
public $permission = 'new_snippet';
public $managerAction = 'new_snippet';
public $elementType = 'snippet';
public $eventBeforeSave = 'OnBeforeSnipFormSave';
public $eventAfterSave = 'OnSnipFormSave';
}
return 'modSnippetCreateProcessor';
<?php
require_once (dirname(dirname(__FILE__)).'/create.class.php');
/**
* Create a template
*
* @param string $templatename The name of the template
* @param string $content The code of the template.
* @param string $description (optional) A brief description.
* @param integer $category (optional) The category to assign to. Defaults to no
* category.
* @param boolean $locked (optional) If true, can only be accessed by
* administrators. Defaults to false.
* @param json $propdata (optional) A json array of properties
* @param json $tvs (optional) A json array of TVs associated to the template
*
* @package modx
* @subpackage processors.element.template
*/
class modTemplateCreateProcessor extends modElementCreateProcessor {
public $classKey = 'modTemplate';
public $languageTopics = array('template','category');
public $permission = 'new_template';
public $managerAction = 'new_template';
public $elementType = 'template';
public $eventBeforeSave = 'OnBeforeTempFormSave';
public $eventAfterSave = 'OnTempFormSave';
public function postSaveElement() {
$this->saveTemplateVariables();
return true;
}
/**
* Save template variables associated to the Template
* @return void
*/
public function saveTemplateVariables() {
/* change template access to tvs */
$tvs = $this->getProperty('tvs',null);
if ($tvs != null) {
$templateVariables = is_array($tvs) ? $tvs : $this->modx->fromJSON($tvs);
if (is_array($templateVariables)) {
foreach ($templateVariables as $id => $tv) {
if ($tv['access']) {
/** @var modTemplateVarTemplate $templateVarTemplate */
$templateVarTemplate = $this->modx->getObject('modTemplateVarTemplate',array(
'tmplvarid' => $tv['id'],
'templateid' => $this->element->get('id'),
));
if (empty($templateVarTemplate)) {
$templateVarTemplate = $this->modx->newObject('modTemplateVarTemplate');
}
$templateVarTemplate->set('tmplvarid',$tv['id']);
$templateVarTemplate->set('templateid',$this->element->get('id'));
$templateVarTemplate->set('rank',$tv['rank']);
$templateVarTemplate->save();
} else {
$templateVarTemplate = $this->modx->getObject('modTemplateVarTemplate',array(
'tmplvarid' => $tv['id'],
'templateid' => $this->element->get('id'),
));
if ($templateVarTemplate && $templateVarTemplate instanceof modTemplateVarTemplate) {
$templateVarTemplate->remove();
}
}
}
}
}
}
}
return 'modTemplateCreateProcessor';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment