modsnippet.class.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. * A modScript derivative representing a MODX PHP code snippet.
  12. *
  13. * @property boolean $cache_type Deprecated
  14. * @property string $snippet The PHP code of the Snippet
  15. * @property boolean $locked Whether or not this Snippet can only be edited by Administrators
  16. * @property array $properties An array of default properties for the Snippet
  17. * @property string $moduleguid Deprecated
  18. * @package modx
  19. * @extends modScript
  20. */
  21. class modSnippet extends modScript {
  22. /**
  23. * Overrides modElement::save to add custom error logging and fire
  24. * modX-specific events.
  25. *
  26. * {@inheritDoc}
  27. */
  28. public function save($cacheFlag = null) {
  29. $isNew = $this->isNew();
  30. if ($this->xpdo instanceof modX) {
  31. $this->xpdo->invokeEvent('OnSnippetBeforeSave',array(
  32. 'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
  33. 'snippet' => &$this,
  34. 'cacheFlag' => $cacheFlag,
  35. ));
  36. }
  37. $saved = parent::save($cacheFlag);
  38. if ($saved && $this->xpdo instanceof modX) {
  39. $this->xpdo->invokeEvent('OnSnippetSave',array(
  40. 'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
  41. 'snippet' => &$this,
  42. 'cacheFlag' => $cacheFlag,
  43. ));
  44. } else if (!$saved && !empty($this->xpdo->lexicon)) {
  45. $msg = $isNew ? $this->xpdo->lexicon('snippet_err_create') : $this->xpdo->lexicon('snippet_err_save');
  46. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,$msg.$this->toArray());
  47. }
  48. return $saved;
  49. }
  50. /**
  51. * Overrides modElement::remove to add custom error logging and fire
  52. * modX-specific events.
  53. *
  54. * {@inheritDoc}
  55. */
  56. public function remove(array $ancestors= array ()) {
  57. if ($this->xpdo instanceof modX) {
  58. $this->xpdo->invokeEvent('OnSnippetBeforeRemove',array(
  59. 'snippet' => &$this,
  60. 'ancestors' => $ancestors,
  61. ));
  62. }
  63. $removed = parent :: remove($ancestors);
  64. if ($removed && $this->xpdo instanceof modX) {
  65. $this->xpdo->invokeEvent('OnSnippetRemove',array(
  66. 'snippet' => &$this,
  67. 'ancestors' => $ancestors,
  68. ));
  69. } else if (!$removed && !empty($this->xpdo->lexicon)) {
  70. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,$this->xpdo->lexicon('snippet_err_remove').$this->toArray());
  71. }
  72. return $removed;
  73. }
  74. }