/core/model/modx/processors/element/tv/update.php

https://github.com/mul14/revolution · PHP · 197 lines · 136 code · 23 blank · 38 comment · 42 complexity · 0f96047bb62357cdc65312c20f327bec MD5 · raw file

  1. <?php
  2. /**
  3. * Updates a TV
  4. *
  5. * @param string $name The name of the TV
  6. * @param string $caption (optional) A short caption for the TV.
  7. * @param string $description (optional) A brief description.
  8. * @param integer $category (optional) The category to assign to. Defaults to no
  9. * category.
  10. * @param boolean $locked (optional) If true, can only be accessed by
  11. * administrators. Defaults to false.
  12. * @param string $els (optional)
  13. * @param integer $rank (optional) The rank of the TV
  14. * @param string $display (optional) The type of output render
  15. * @param string $display_params (optional) Any display rendering parameters
  16. * @param string $default_text (optional) The default value for the TV
  17. * @param json $templates (optional) Templates associated with the TV
  18. * @param json $resource_groups (optional) Resource Groups associated with the
  19. * TV.
  20. * @param json $propdata (optional) A json array of properties
  21. *
  22. * @package modx
  23. * @subpackage processors.element.tv
  24. */
  25. if (!$modx->hasPermission('save_tv')) return $modx->error->failure($modx->lexicon('permission_denied'));
  26. $modx->lexicon->load('tv','category');
  27. /* get tv */
  28. if (empty($scriptProperties['id'])) return $modx->error->failure($modx->lexicon('tv_err_ns'));
  29. $tv = $modx->getObject('modTemplateVar',$scriptProperties['id']);
  30. if ($tv == null) return $modx->error->failure($modx->lexicon('tv_err_nf'));
  31. if (!$tv->checkPolicy('save')) {
  32. return $modx->error->failure($modx->lexicon('access_denied'));
  33. }
  34. /* check locks */
  35. if ($tv->get('locked') && $modx->hasPermission('edit_locked') == false) {
  36. return $modx->error->failure($modx->lexicon('tv_err_locked'));
  37. }
  38. /* category */
  39. if (!empty($scriptProperties['category'])) {
  40. $category = $modx->getObject('modCategory',array('id' => $scriptProperties['category']));
  41. if ($category == null) $modx->error->addField('category',$modx->lexicon('category_err_nf'));
  42. }
  43. if (empty($scriptProperties['name'])) $scriptProperties['name'] = $modx->lexicon('untitled_tv');
  44. /* check to make sure name doesn't already exist */
  45. $nameExists = $modx->getObject('modTemplateVar',array(
  46. 'id:!=' => $tv->get('id'),
  47. 'name' => $scriptProperties['name'],
  48. ));
  49. if ($nameExists) $modx->error->addField('name',$modx->lexicon('tv_err_exists_name',array('name' => $scriptProperties['name'])));
  50. $output_properties = array();
  51. foreach ($scriptProperties as $key => $value) {
  52. $res = strstr($key,'prop_');
  53. if ($res !== false) {
  54. $output_properties[str_replace('prop_','',$key)] = $value;
  55. }
  56. }
  57. $input_properties = array();
  58. foreach ($scriptProperties as $key => $value) {
  59. $res = strstr($key,'inopt_');
  60. if ($res !== false) {
  61. $input_properties[str_replace('inopt_','',$key)] = $value;
  62. }
  63. }
  64. $tv->fromArray($scriptProperties);
  65. $tv->set('elements',$scriptProperties['els']);
  66. $tv->set('input_properties',$input_properties);
  67. $tv->set('output_properties',$output_properties);
  68. $tv->set('rank', !empty($scriptProperties['rank']) ? $scriptProperties['rank'] : 0);
  69. $tv->set('locked', !empty($scriptProperties['locked']));
  70. /* validate TV */
  71. if (!$tv->validate()) {
  72. $validator = $tv->getValidator();
  73. if ($validator->hasMessages()) {
  74. foreach ($validator->getMessages() as $message) {
  75. $modx->error->addField($message['field'], $modx->lexicon($message['message']));
  76. }
  77. }
  78. }
  79. /* if error, return */
  80. if ($modx->error->hasError()) {
  81. return $modx->error->failure();
  82. }
  83. /* invoke OnBeforeTVFormSave event */
  84. $OnBeforeTVFormSave = $modx->invokeEvent('OnBeforeTVFormSave',array(
  85. 'mode' => modSystemEvent::MODE_UPD,
  86. 'id' => $tv->get('id'),
  87. 'tv' => &$tv,
  88. ));
  89. if (is_array($OnBeforeTVFormSave)) {
  90. $canSave = false;
  91. foreach ($OnBeforeTVFormSave as $msg) {
  92. if (!empty($msg)) {
  93. $canSave .= $msg."\n";
  94. }
  95. }
  96. } else {
  97. $canSave = $OnBeforeTVFormSave;
  98. }
  99. if (!empty($canSave)) {
  100. return $modx->error->failure($canSave);
  101. }
  102. /* save TV */
  103. if ($tv->save() === false) {
  104. return $modx->error->failure($modx->lexicon('tv_err_save'));
  105. }
  106. /* change template access to tvs */
  107. if (isset($scriptProperties['templates'])) {
  108. $templateVariables = $modx->fromJSON($scriptProperties['templates']);
  109. if (is_array($templateVariables)) {
  110. foreach ($templateVariables as $id => $template) {
  111. if (!is_array($template)) continue;
  112. if ($template['access']) {
  113. $templateVarTemplate = $modx->getObject('modTemplateVarTemplate',array(
  114. 'tmplvarid' => $tv->get('id'),
  115. 'templateid' => $template['id'],
  116. ));
  117. if (empty($templateVarTemplate)) {
  118. $templateVarTemplate = $modx->newObject('modTemplateVarTemplate');
  119. }
  120. $templateVarTemplate->set('tmplvarid',$tv->get('id'));
  121. $templateVarTemplate->set('templateid',$template['id']);
  122. $templateVarTemplate->set('rank',$template['rank']);
  123. $templateVarTemplate->save();
  124. } else {
  125. $templateVarTemplate = $modx->getObject('modTemplateVarTemplate',array(
  126. 'tmplvarid' => $tv->get('id'),
  127. 'templateid' => $template['id'],
  128. ));
  129. if ($templateVarTemplate && $templateVarTemplate instanceof modTemplateVarTemplate) {
  130. $templateVarTemplate->remove();
  131. }
  132. }
  133. }
  134. }
  135. }
  136. /*
  137. * update access
  138. */
  139. if ($modx->hasPermission('access_permissions')) {
  140. if (isset($scriptProperties['resource_groups'])) {
  141. $docgroups = $modx->fromJSON($scriptProperties['resource_groups']);
  142. if (is_array($docgroups)) {
  143. foreach ($docgroups as $id => $group) {
  144. if (!is_array($group)) continue;
  145. $templateVarResourceGroup = $modx->getObject('modTemplateVarResourceGroup',array(
  146. 'tmplvarid' => $tv->get('id'),
  147. 'documentgroup' => $group['id'],
  148. ));
  149. if ($group['access'] == true) {
  150. if (!empty($templateVarResourceGroup)) continue;
  151. $templateVarResourceGroup = $modx->newObject('modTemplateVarResourceGroup');
  152. $templateVarResourceGroup->set('tmplvarid',$tv->get('id'));
  153. $templateVarResourceGroup->set('documentgroup',$group['id']);
  154. $templateVarResourceGroup->save();
  155. } else if ($templateVarResourceGroup && $templateVarResourceGroup instanceof modTemplateVarResourceGroup) {
  156. $templateVarResourceGroup->remove();
  157. }
  158. }
  159. }
  160. }
  161. }
  162. /* invoke OnTVFormSave event */
  163. $modx->invokeEvent('OnTVFormSave',array(
  164. 'mode' => modSystemEvent::MODE_UPD,
  165. 'id' => $tv->get('id'),
  166. 'tv' => &$tv,
  167. ));
  168. /* log manager action */
  169. $modx->logManagerAction('tv_update','modTemplateVar',$tv->get('id'));
  170. /* empty cache */
  171. if (!empty($scriptProperties['clearCache'])) {
  172. $modx->cacheManager->refresh();
  173. }
  174. return $modx->error->success();