modtemplate.class.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /*
  3. * This file is part of MODX Revolution.
  4. *
  5. * Copyright (c) MODX, LLC. All Rights Reserved.
  6. *
  7. * For complete copyright and license information, see the COPYRIGHT and LICENSE
  8. * files found in the top-level directory of this distribution.
  9. */
  10. /**
  11. * Represents a content element that serves as a resource template.
  12. *
  13. * @property int $id The ID of the Template
  14. * @property string $templatename The name of the Template
  15. * @property string $description A user-provided description of the Template
  16. * @property int $editor_type Deprecated
  17. * @property int $category The Category this Template resides in
  18. * @property string $icon Deprecated
  19. * @property int $template_type Deprecated
  20. * @property string $content The content of the Template
  21. * @property boolean $locked Whether or not this Template can only be edited by Administrators
  22. * @property array $properties An array of default properties for the Template
  23. * @package modx
  24. */
  25. class modTemplate extends modElement {
  26. /**
  27. * Get a sortable, limitable list and total record count of Template Variables.
  28. *
  29. * This list includes an access field indicating their relationship to a modTemplate.
  30. *
  31. * @static
  32. * @param modTemplate &$template A modTemplate instance.
  33. * @param array $sort An array of criteria for sorting the list.
  34. * @param int $limit An optional limit to apply to the list.
  35. * @param array $conditions
  36. * @param int $offset An optional offset to apply to the list.
  37. * @return array An array with the list collection and total records in the collection.
  38. */
  39. public static function listTemplateVars(modTemplate &$template, array $sort = array('name' => 'ASC'), $limit = 0, $offset = 0,array $conditions = array()) {
  40. return array('collection' => array(), 'total' => 0);
  41. }
  42. /**
  43. * @param xPDO $xpdo A reference to the xPDO|modX instance
  44. */
  45. function __construct(xPDO & $xpdo) {
  46. parent :: __construct($xpdo);
  47. $this->setCacheable(false);
  48. }
  49. /**
  50. * Overrides modElement::save to add custom error logging and fire
  51. * modX-specific events.
  52. *
  53. * {@inheritdoc}
  54. */
  55. public function save($cacheFlag = null) {
  56. $isNew = $this->isNew();
  57. if ($this->xpdo instanceof modX) {
  58. $this->xpdo->invokeEvent('OnTemplateBeforeSave',array(
  59. 'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
  60. 'template' => &$this,
  61. 'cacheFlag' => $cacheFlag,
  62. ));
  63. }
  64. $saved = parent::save($cacheFlag);
  65. if ($saved && $this->xpdo instanceof modX) {
  66. $this->xpdo->invokeEvent('OnTemplateSave',array(
  67. 'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
  68. 'template' => &$this,
  69. 'cacheFlag' => $cacheFlag,
  70. ));
  71. } else if (!$saved && !empty($this->xpdo->lexicon)) {
  72. $msg = $isNew ? $this->xpdo->lexicon('template_err_create') : $this->xpdo->lexicon('template_err_save');
  73. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,$msg.$this->toArray());
  74. }
  75. return $saved;
  76. }
  77. /**
  78. * Overrides modElement::remove to add custom error logging.
  79. *
  80. * {@inheritdoc}
  81. */
  82. public function remove(array $ancestors= array ()) {
  83. if ($this->xpdo instanceof modX) {
  84. $this->xpdo->invokeEvent('OnTemplateBeforeRemove',array(
  85. 'template' => &$this,
  86. 'ancestors' => $ancestors,
  87. ));
  88. }
  89. $removed = parent :: remove($ancestors);
  90. if ($removed && $this->xpdo instanceof modX) {
  91. $this->xpdo->invokeEvent('OnTemplateRemove',array(
  92. 'template' => &$this,
  93. 'ancestors' => $ancestors,
  94. ));
  95. } else if (!$removed && !empty($this->xpdo->lexicon)) {
  96. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,$this->xpdo->lexicon('template_err_remove').$this->toArray());
  97. }
  98. return $removed;
  99. }
  100. /**
  101. * Process the template content and return the output.
  102. *
  103. * {@inheritdoc}
  104. */
  105. public function process($properties= null, $content= null) {
  106. parent :: process($properties, $content);
  107. if (!$this->_processed) {
  108. $this->_output= $this->_content;
  109. if (is_string($this->_output) && !empty($this->_output)) {
  110. /* turn the processed properties into placeholders */
  111. $this->xpdo->toPlaceholders($this->_properties, '', '.', true);
  112. /* collect element tags in the content and process them */
  113. $maxIterations= intval($this->xpdo->getOption('parser_max_iterations',null,10));
  114. $this->xpdo->parser->processElementTags($this->_tag, $this->_output, false, false, '[[', ']]', array(), $maxIterations);
  115. }
  116. $this->filterOutput();
  117. $this->_processed= true;
  118. }
  119. $this->xpdo->parser->setProcessingElement(false);
  120. return $this->_output;
  121. }
  122. /**
  123. * Gets a collection of objects related by aggregate or composite relations.
  124. *
  125. * {@inheritdoc}
  126. *
  127. * Includes special handling for related objects with alias {@link
  128. * modTemplateVar}, respecting framework security unless specific criteria
  129. * are provided.
  130. */
  131. public function & getMany($alias, $criteria= null, $cacheFlag= true) {
  132. $collection= array ();
  133. if (($alias === 'TemplateVars' || $alias === 'modTemplateVar') && ($criteria === null || strtolower($criteria) === 'all')) {
  134. $c = $this->xpdo->newQuery('modTemplateVar');
  135. $c->query['distinct'] = 'DISTINCT';
  136. $c->select($this->xpdo->getSelectColumns('modTemplateVar'));
  137. $c->select($this->xpdo->getSelectColumns('modTemplateVarTemplate', 'tvtpl', '', array('rank')));
  138. $c->select(array('value' => $this->xpdo->getSelectColumns('modTemplateVar', 'modTemplateVar', '', array('default_text'))));
  139. $c->innerJoin('modTemplateVarTemplate','tvtpl',array(
  140. 'tvtpl.tmplvarid = modTemplateVar.id',
  141. 'tvtpl.templateid' => $this->get('id'),
  142. ));
  143. $c->sortby('tvtpl.rank,modTemplateVar.rank');
  144. $collection = $this->xpdo->getCollection('modTemplateVar', $c, $cacheFlag);
  145. } else {
  146. $collection= parent :: getMany($alias, $criteria, $cacheFlag);
  147. }
  148. return $collection;
  149. }
  150. /**
  151. * Grabs an array of Template Variables associated with this Template,
  152. * bypassing the many-to-many relationship.
  153. *
  154. * @access public
  155. * @return array An array of TVs.
  156. */
  157. public function getTemplateVars() {
  158. $c = $this->xpdo->newQuery('modTemplateVar');
  159. $c->innerJoin('modTemplateVarTemplate','TemplateVarTemplates');
  160. $c->where(array(
  161. 'TemplateVarTemplates.templateid' => $this->get('id'),
  162. ));
  163. $c->sortby('TemplateVarTemplates.rank','ASC');
  164. return $this->xpdo->getCollection('modTemplateVar',$c);
  165. }
  166. /**
  167. * Get a list of Template Variables and if they are currently associated to this template.
  168. *
  169. * This is a sortable, scrollable list.
  170. *
  171. * @param array $sort An array of criteria for sorting the list.
  172. * @param integer $limit An optional limit to apply to the list.
  173. * @param integer $offset An optional offset to apply to the list.
  174. * @param array $conditions
  175. * @return array An array containing the collection and total.
  176. */
  177. public function getTemplateVarList(array $sort = array('name' => 'ASC'), $limit = 0, $offset = 0,array $conditions = array()) {
  178. return $this->xpdo->call('modTemplate', 'listTemplateVars', array(&$this, $sort, $limit, $offset,$conditions));
  179. }
  180. /**
  181. * Check to see if this Template is assigned the specified Template Var
  182. *
  183. * @param mixed $tvPk Either the ID, name or object of the Template Var
  184. * @return boolean True if the TV is assigned to this Template
  185. */
  186. public function hasTemplateVar($tvPk) {
  187. if (!is_int($tvPk) && !is_object($tvPk)) {
  188. $tv = $this->xpdo->getObject('modTemplateVar',array('name' => $tvPk));
  189. if (empty($tv) || !is_object($tv) || !($tv instanceof modTemplateVar)) {
  190. $this->xpdo->log(modX::LOG_LEVEL_ERROR,'modTemplate::hasTemplateVar - No TV: '.$tvPk);
  191. return false;
  192. }
  193. } else {
  194. $tv =& $tvPk;
  195. }
  196. $templateVarTemplate = $this->xpdo->getObject('modTemplateVarTemplate',array(
  197. 'tmplvarid' => is_object($tv) ? $tv->get('id') : $tv,
  198. 'templateid' => $this->get('id'),
  199. ));
  200. return !empty($templateVarTemplate) && is_object($templateVarTemplate);
  201. }
  202. }