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

https://github.com/francisreboucas/revolution · PHP · 113 lines · 69 code · 15 blank · 29 comment · 23 complexity · 7f29636ed84485d2c98789304701482c MD5 · raw file

  1. <?php
  2. /**
  3. * Update a snippet
  4. *
  5. * @param integer $id The ID of the snippet
  6. * @param string $name The name of the snippet
  7. * @param string $snippet The code of the snippet.
  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 json $propdata (optional) A json array of properties
  14. *
  15. * @package modx
  16. * @subpackage processors.element.snippet
  17. */
  18. if (!$modx->hasPermission('save_snippet')) return $modx->error->failure($modx->lexicon('permission_denied'));
  19. $modx->lexicon->load('snippet','category');
  20. /* get snippet */
  21. if (empty($scriptProperties['id'])) return $modx->error->failure($modx->lexicon('snippet_err_ns'));
  22. $snippet = $modx->getObject('modSnippet',$scriptProperties['id']);
  23. if ($snippet == null) return $modx->error->failure($modx->lexicon('snippet_err_nf'));
  24. if (!$snippet->checkPolicy('save')) {
  25. return $modx->error->failure($modx->lexicon('access_denied'));
  26. }
  27. /* check if locked, if so, prevent access */
  28. if ($snippet->get('locked') && $modx->hasPermission('edit_locked') == false) {
  29. return $modx->error->failure($modx->lexicon('snippet_err_locked'));
  30. }
  31. /* validation */
  32. if (empty($scriptProperties['name'])) {
  33. $modx->error->addField('name',$modx->lexicon('snippet_err_ns_name'));
  34. }
  35. /* check to see if name already exists */
  36. $nameExists = $modx->getObject('modSnippet',array(
  37. 'id:!=' => $snippet->get('id'),
  38. 'name' => $scriptProperties['name'],
  39. ));
  40. if ($nameExists) $modx->error->addField('name',$modx->lexicon('snippet_err_exists_name',array('name' => $scriptProperties['name'])));
  41. /* category */
  42. if (!empty($scriptProperties['category'])) {
  43. $category = $modx->getObject('modCategory',array('id' => $scriptProperties['category']));
  44. if ($category == null) $modx->error->addField('category',$modx->lexicon('category_err_nf'));
  45. }
  46. /* set fields */
  47. $previousCategory = $snippet->get('category');
  48. $snippet->fromArray($scriptProperties);
  49. $snippet->set('locked',!empty($scriptProperties['locked']));
  50. /* validate */
  51. if (!$snippet->validate()) {
  52. $validator = $snippet->getValidator();
  53. if ($validator->hasMessages()) {
  54. foreach ($validator->getMessages() as $message) {
  55. $modx->error->addField($message['field'], $modx->lexicon($message['message']));
  56. }
  57. }
  58. }
  59. /* if has errors, return */
  60. if ($modx->error->hasError()) {
  61. return $modx->error->failure();
  62. }
  63. /* invoke OnBeforeSnipFormSave event */
  64. $OnBeforeSnipFormSave = $modx->invokeEvent('OnBeforeSnipFormSave',array(
  65. 'mode' => modSystemEvent::MODE_UPD,
  66. 'id' => $snippet->get('id'),
  67. 'snippet' => &$snippet,
  68. ));
  69. if (is_array($OnBeforeSnipFormSave)) {
  70. $canSave = false;
  71. foreach ($OnBeforeSnipFormSave as $msg) {
  72. if (!empty($msg)) {
  73. $canSave .= $msg."\n";
  74. }
  75. }
  76. } else {
  77. $canSave = $OnBeforeSnipFormSave;
  78. }
  79. if (!empty($canSave)) {
  80. return $modx->error->failure($canSave);
  81. }
  82. /* save snippet */
  83. if ($snippet->save() == false) {
  84. return $modx->error->failure($modx->lexicon('snippet_err_save'));
  85. }
  86. /* invoke OnSnipFormSave event */
  87. $modx->invokeEvent('OnSnipFormSave',array(
  88. 'mode' => modSystemEvent::MODE_UPD,
  89. 'id' => $snippet->get('id'),
  90. 'snippet' => &$snippet,
  91. ));
  92. /* log manager action */
  93. $modx->logManagerAction('snippet_update','modSnippet',$snippet->get('id'));
  94. /* empty cache */
  95. if (!empty($scriptProperties['clearCache'])) {
  96. $modx->cacheManager->refresh();
  97. }
  98. return $modx->error->success('',array_merge($snippet->get(array('id','name','description','category','locked')), array('previous_category' => $previousCategory)));