PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/core/model/modx/processors/element/tv/create.class.php

https://github.com/Mark-H/revolution
PHP | 214 lines | 135 code | 14 blank | 65 comment | 22 complexity | 296c7c697609b2811b845ea74335fe8b MD5 | raw file
  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. require_once (dirname(__DIR__).'/create.class.php');
  11. /**
  12. * Create a Template Variable.
  13. *
  14. * @param string $name The name of the TV
  15. * @param string $caption (optional) A short caption for the TV.
  16. * @param string $description (optional) A brief description.
  17. * @param integer $category (optional) The category to assign to. Defaults to no
  18. * category.
  19. * @param boolean $locked (optional) If true, can only be accessed by
  20. * administrators. Defaults to false.
  21. * @param string $els (optional)
  22. * @param integer $rank (optional) The rank of the TV
  23. * @param string $display (optional) The type of output render
  24. * @param string $display_params (optional) Any display rendering parameters
  25. * @param string $default_text (optional) The default value for the TV
  26. * @param json $templates (optional) Templates associated with the TV
  27. * @param json $resource_groups (optional) Resource Groups associated with the
  28. * TV.
  29. * @param json $propdata (optional) A json array of properties
  30. *
  31. * @package modx
  32. * @subpackage processors.element.tv
  33. */
  34. class modTemplateVarCreateProcessor extends modElementCreateProcessor {
  35. public $classKey = 'modTemplateVar';
  36. public $languageTopics = array('tv','category','element');
  37. public $permission = 'new_tv';
  38. public $objectType = 'tv';
  39. public $beforeSaveEvent = 'OnBeforeTVFormSave';
  40. public $afterSaveEvent = 'OnTVFormSave';
  41. /**
  42. * Fire pre-save logic
  43. * {@inheritDoc}
  44. * @return boolean
  45. */
  46. public function beforeSave() {
  47. $template = $this->getProperty('template');
  48. if (empty($template)) { $this->setProperty('template',array()); }
  49. $caption = $this->getProperty('caption','');
  50. if (empty($caption)) $this->setProperty('caption',$this->getProperty('name'));
  51. $this->setInputProperties();
  52. $this->setOutputProperties();
  53. $els = $this->getProperty('els',null);
  54. if ($els !== null) {
  55. $this->object->set('elements',$els);
  56. }
  57. $rank = $this->getProperty('rank',0);
  58. $this->object->set('rank',$rank);
  59. return parent::beforeSave();
  60. }
  61. /**
  62. * Set the Output Options for the TV
  63. * @return array
  64. */
  65. public function setOutputProperties() {
  66. $properties = $this->getProperties();
  67. $outputProperties = array();
  68. foreach ($properties as $key => $value) {
  69. $res = strstr($key,'prop_');
  70. if ($res !== false) {
  71. $outputProperties[str_replace('prop_','',$key)] = $value;
  72. }
  73. }
  74. $this->object->set('output_properties',$outputProperties);
  75. return $outputProperties;
  76. }
  77. /**
  78. * Set the Input Options for the TV
  79. * @return array
  80. */
  81. public function setInputProperties() {
  82. $properties = $this->getProperties();
  83. $inputProperties = array();
  84. foreach ($properties as $key => $value) {
  85. $res = strstr($key,'inopt_');
  86. if ($res !== false) {
  87. $inputProperties[str_replace('inopt_','',$key)] = $value;
  88. }
  89. }
  90. $this->object->set('input_properties',$inputProperties);
  91. return $inputProperties;
  92. }
  93. /**
  94. * Add post-saving options to TVs
  95. *
  96. * {@inheritDoc}
  97. * @return boolean
  98. */
  99. public function afterSave() {
  100. $this->setTemplateAccess();
  101. $this->setResourceGroupAccess();
  102. $this->setMediaSources();
  103. return parent::afterSave();
  104. }
  105. /**
  106. * Set the Template Access to the TV
  107. * @return void
  108. */
  109. public function setTemplateAccess() {
  110. $templates = $this->getProperty('templates',null);
  111. if ($templates !== null) {
  112. $templates = is_array($templates) ? $templates : $this->modx->fromJSON($templates);
  113. foreach ($templates as $id => $template) {
  114. if ($template['access']) {
  115. /** @var modTemplateVarTemplate $templateVarTemplate */
  116. $templateVarTemplate = $this->modx->getObject('modTemplateVarTemplate',array(
  117. 'tmplvarid' => $this->object->get('id'),
  118. 'templateid' => $template['id'],
  119. ));
  120. if (empty($templateVarTemplate)) {
  121. $templateVarTemplate = $this->modx->newObject('modTemplateVarTemplate');
  122. }
  123. $templateVarTemplate->set('tmplvarid',$this->object->get('id'));
  124. $templateVarTemplate->set('templateid',$template['id']);
  125. $templateVarTemplate->set('rank',!empty($template['rank']) ? $template['rank'] : 0);
  126. $templateVarTemplate->save();
  127. } else {
  128. $templateVarTemplate = $this->modx->getObject('modTemplateVarTemplate',array(
  129. 'tmplvarid' => $this->object->get('id'),
  130. 'templateid' => $template['id'],
  131. ));
  132. if (!empty($templateVarTemplate)) {
  133. $templateVarTemplate->remove();
  134. }
  135. }
  136. }
  137. }
  138. }
  139. /**
  140. * Set Resource Groups access to TV
  141. * @return array|string
  142. */
  143. public function setResourceGroupAccess() {
  144. $resourceGroups = $this->getProperty('resource_groups',null);
  145. if ($this->modx->hasPermission('tv_access_permissions') && $resourceGroups !== null) {
  146. $resourceGroups = is_array($resourceGroups) ? $resourceGroups : $this->modx->fromJSON($resourceGroups);
  147. if (is_array($resourceGroups)) {
  148. foreach ($resourceGroups as $id => $group) {
  149. if (!is_array($group)) continue;
  150. /** @var modTemplateVarResourceGroup $templateVarResourceGroup */
  151. $templateVarResourceGroup = $this->modx->getObject('modTemplateVarResourceGroup',array(
  152. 'tmplvarid' => $this->object->get('id'),
  153. 'documentgroup' => $group['id'],
  154. ));
  155. if ($group['access'] == true) {
  156. if (!empty($templateVarResourceGroup)) continue;
  157. $templateVarResourceGroup = $this->modx->newObject('modTemplateVarResourceGroup');
  158. $templateVarResourceGroup->set('tmplvarid',$this->object->get('id'));
  159. $templateVarResourceGroup->set('documentgroup',$group['id']);
  160. $templateVarResourceGroup->save();
  161. } else {
  162. $templateVarResourceGroup->remove();
  163. }
  164. }
  165. }
  166. }
  167. }
  168. /**
  169. * Set source-element maps
  170. * @return void
  171. */
  172. public function setMediaSources() {
  173. $sources = $this->getProperty('sources',null);
  174. if ($sources !== null) {
  175. $sources = is_array($sources) ? $sources : $this->modx->fromJSON($sources);
  176. if (is_array($sources)) {
  177. foreach ($sources as $id => $source) {
  178. if (!is_array($source)) continue;
  179. /** @var modMediaSourceElement $sourceElement */
  180. $sourceElement = $this->modx->getObject('sources.modMediaSourceElement',array(
  181. 'object' => $this->object->get('id'),
  182. 'object_class' => $this->object->_class,
  183. 'context_key' => $source['context_key'],
  184. ));
  185. if (!$sourceElement) {
  186. $sourceElement = $this->modx->newObject('sources.modMediaSourceElement');
  187. $sourceElement->fromArray(array(
  188. 'object' => $this->object->get('id'),
  189. 'object_class' => $this->object->_class,
  190. 'context_key' => $source['context_key'],
  191. ),'',true,true);
  192. }
  193. $sourceElement->set('source',$source['source']);
  194. $sourceElement->save();
  195. }
  196. }
  197. }
  198. }
  199. }
  200. return 'modTemplateVarCreateProcessor';