PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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