modchunk.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * Represents a chunk of static HTML content.
  12. *
  13. * @property string $name The name of the Chunk.
  14. * @property string $description A user-provided description of the Chunk
  15. * @property int $editor_type Deprecated
  16. * @property int $category The ID of the Category this chunk resides in. Defaults to 0.
  17. * @property boolean $cache_type Deprecated
  18. * @property string $content The contents of the Chunk
  19. * @property boolean $locked Whether or not this chunk can only be edited by Administrators
  20. * @property array $properties An array of default properties for this Chunk
  21. *
  22. * @package modx
  23. */
  24. class modChunk extends modElement {
  25. /**
  26. * Overrides modElement::__construct to set the tag token for this Element
  27. * @param xPDO $xpdo A reference to the xPDO|modX instance
  28. */
  29. function __construct(& $xpdo) {
  30. parent :: __construct($xpdo);
  31. $this->setToken('$');
  32. }
  33. /**
  34. * Overrides modElement::save to add custom error logging and fire
  35. * modX-specific events.
  36. *
  37. * {@inheritdoc}
  38. */
  39. public function save($cacheFlag = null) {
  40. $isNew = $this->isNew();
  41. if ($this->xpdo instanceof modX) {
  42. $this->xpdo->invokeEvent('OnChunkBeforeSave',array(
  43. 'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
  44. 'chunk' => &$this,
  45. 'cacheFlag' => $cacheFlag,
  46. ));
  47. }
  48. $saved = parent::save($cacheFlag);
  49. if ($saved && $this->xpdo instanceof modX) {
  50. $this->xpdo->invokeEvent('OnChunkSave',array(
  51. 'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
  52. 'chunk' => &$this,
  53. 'cacheFlag' => $cacheFlag,
  54. ));
  55. } else if (!$saved && !empty($this->xpdo->lexicon)) {
  56. $msg = $isNew ? $this->xpdo->lexicon('chunk_err_create') : $this->xpdo->lexicon('chunk_err_save');
  57. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,$msg.' '.print_r($this->toArray(),true));
  58. }
  59. return $saved;
  60. }
  61. /**
  62. * Overrides modElement::remove to add custom error logging and fire
  63. * modX-specific events.
  64. *
  65. * {@inheritdoc}
  66. */
  67. public function remove(array $ancestors= array ()) {
  68. if ($this->xpdo instanceof modX) {
  69. $this->xpdo->invokeEvent('OnChunkBeforeRemove',array(
  70. 'chunk' => &$this,
  71. 'ancestors' => $ancestors,
  72. ));
  73. }
  74. $removed = parent :: remove($ancestors);
  75. if ($removed && $this->xpdo instanceof modX) {
  76. $this->xpdo->invokeEvent('OnChunkRemove',array(
  77. 'chunk' => &$this,
  78. 'ancestors' => $ancestors,
  79. ));
  80. } else if (!$removed && !empty($this->xpdo->lexicon)) {
  81. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,$this->xpdo->lexicon('chunk_err_remove').$this->toArray());
  82. }
  83. return $removed;
  84. }
  85. /**
  86. * Overrides modElement::process to initialize the Chunk into the element cache,
  87. * as well as set placeholders and filter the output.
  88. *
  89. * {@inheritdoc}
  90. */
  91. public function process($properties= null, $content= null) {
  92. parent :: process($properties, $content);
  93. if (!$this->_processed || !$this->isCacheable()) {
  94. /* copy the content into the output buffer */
  95. $this->_output= $this->_content;
  96. if (is_string($this->_output) && !empty ($this->_output)) {
  97. /* turn the processed properties into placeholders */
  98. $scope = $this->xpdo->toPlaceholders($this->_properties, '', '.', true);
  99. /* collect element tags in the output and process them */
  100. $maxIterations= intval($this->xpdo->getOption('parser_max_iterations',null,10));
  101. $this->xpdo->parser->processElementTags(
  102. $this->_tag,
  103. $this->_output,
  104. $this->xpdo->parser->isProcessingUncacheable(),
  105. $this->xpdo->parser->isRemovingUnprocessed(),
  106. '[[',
  107. ']]',
  108. array(),
  109. $maxIterations
  110. );
  111. /* remove the placeholders set from the properties of this element and restore global values */
  112. if (isset($scope['keys'])) $this->xpdo->unsetPlaceholders($scope['keys']);
  113. if (isset($scope['restore'])) $this->xpdo->toPlaceholders($scope['restore']);
  114. }
  115. $this->filterOutput();
  116. $this->cache();
  117. $this->_processed= true;
  118. }
  119. $this->xpdo->parser->setProcessingElement(false);
  120. /* finally, return the processed element content */
  121. return $this->_output;
  122. }
  123. }