modpropertyset.class.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 reusable set of properties for elements.
  12. *
  13. * Each named property set can be associated with one or more element instances
  14. * and can be called via a tag syntax or programatically.
  15. *
  16. * @property string $name A name for the set
  17. * @property int $category Optional. The category this Set belongs to
  18. * @property string $description A description of the set
  19. * @property array $properties An array of properties contained in this Property Set
  20. * @package modx
  21. * @extends xPDOSimpleObject
  22. */
  23. class modPropertySet extends xPDOSimpleObject {
  24. /**
  25. * The property value array for the element.
  26. * @var array
  27. * @access protected
  28. */
  29. protected $_properties= null;
  30. /**
  31. * Get all the modElement instances this property set is available to.
  32. *
  33. * @access public
  34. * @return array An array of modElement instances.
  35. */
  36. public function getElements() {
  37. $elements = array();
  38. $links = $this->getMany('Elements');
  39. foreach ($links as $link) {
  40. $element = $link->getOne('Element');
  41. if ($element) $elements[] = $element;
  42. }
  43. return $elements;
  44. }
  45. /**
  46. * Overrides xPDOObject::get to handle when retrieving the properties field
  47. * for an Element.
  48. *
  49. * {@inheritdoc}
  50. */
  51. public function get($k, $format= null, $formatTemplate= null) {
  52. $value = parent :: get($k, $format, $formatTemplate);
  53. /* automatically translate lexicon descriptions */
  54. if ($k == 'properties' && !empty($value) && is_array($value)
  55. && is_object($this->xpdo) && $this->xpdo instanceof modX && $this->xpdo->lexicon) {
  56. foreach ($value as &$property) {
  57. if (!empty($property['lexicon'])) {
  58. if (strpos($property['lexicon'],':') !== false) {
  59. $this->xpdo->lexicon->load('en:'.$property['lexicon']);
  60. } else {
  61. $this->xpdo->lexicon->load('en:core:'.$property['lexicon']);
  62. }
  63. $this->xpdo->lexicon->load($property['lexicon']);
  64. }
  65. $property['desc_trans'] = $this->xpdo->lexicon($property['desc']);
  66. $property['area'] = !empty($property['area']) ? $property['area'] : '';
  67. if (!empty($property['options'])) {
  68. foreach ($property['options'] as &$option) {
  69. if (empty($option['text']) && !empty($option['name'])) {
  70. $option['text'] = $option['name'];
  71. unset($option['name']);
  72. }
  73. if (empty($option['value']) && !empty($option[0])) {
  74. $option['value'] = $option[0];
  75. unset($option[0]);
  76. }
  77. $option['name'] = $this->xpdo->lexicon($option['text']);
  78. }
  79. }
  80. }
  81. }
  82. return $value;
  83. }
  84. /**
  85. * Get the properties for this element instance for processing.
  86. *
  87. * @access public
  88. * @param array|string $properties An array or string of properties to
  89. * apply.
  90. * @return array A simple array of properties ready to use for processing.
  91. */
  92. public function getProperties($properties = null) {
  93. $this->xpdo->getParser();
  94. $this->_properties= $this->xpdo->parser->parseProperties($this->get('properties'));
  95. if (!empty($properties)) {
  96. $this->_properties= array_merge($this->_properties, $this->xpdo->parser->parseProperties($properties));
  97. }
  98. return $this->_properties;
  99. }
  100. /**
  101. * Set properties for this modPropertySet instance.
  102. *
  103. * @access public
  104. * @param array|string $properties A property array or property string.
  105. * @param boolean $merge Indicates if properties should be merged with
  106. * existing ones.
  107. * @return boolean true if the properties are set.
  108. */
  109. public function setProperties($properties, $merge = false) {
  110. $set = false;
  111. $propertiesArray = array();
  112. if (is_string($properties)) {
  113. $properties = $this->xpdo->parser->parsePropertyString($properties);
  114. }
  115. if (is_array($properties)) {
  116. foreach ($properties as $propKey => $property) {
  117. if (is_array($property) && isset($property[5])) {
  118. $key = $property[0];
  119. $propertyArray = array(
  120. 'name' => $property[0],
  121. 'desc' => $property[1],
  122. 'type' => $property[2],
  123. 'options' => $property[3],
  124. 'value' => $property[4],
  125. 'lexicon' => !empty($property[5]) ? $property[5] : null,
  126. 'area' => !empty($property[6]) ? $property[6] : '',
  127. );
  128. } elseif (is_array($property) && isset($property['value'])) {
  129. $key = $property['name'];
  130. $propertyArray = array(
  131. 'name' => $property['name'],
  132. 'desc' => isset($property['description']) ? $property['description'] : (isset($property['desc']) ? $property['desc'] : ''),
  133. 'type' => isset($property['xtype']) ? $property['xtype'] : (isset($property['type']) ? $property['type'] : 'textfield'),
  134. 'options' => isset($property['options']) ? $property['options'] : array(),
  135. 'value' => $property['value'],
  136. 'lexicon' => !empty($property['lexicon']) ? $property['lexicon'] : null,
  137. 'area' => !empty($property['area']) ? $property['area'] : '',
  138. );
  139. } else {
  140. $key = $propKey;
  141. $propertyArray = array(
  142. 'name' => $propKey,
  143. 'desc' => '',
  144. 'type' => 'textfield',
  145. 'options' => array(),
  146. 'value' => $property,
  147. 'lexicon' => null,
  148. 'area' => '',
  149. );
  150. }
  151. if ($propertyArray['type'] == 'combo-boolean' && is_numeric($propertyArray['value'])) {
  152. $propertyArray['value'] = (boolean)$propertyArray['value'];
  153. }
  154. /* handle translations of properties (temp fix until modLocalizableObject in 2.1 and beyond) */
  155. /*if (!empty($propertyArray['lexicon'])) {
  156. $this->xpdo->lexicon->load($propertyArray['lexicon']);
  157. $propertyArray['desc'] = $this->xpdo->lexicon($propertyArray['desc']);
  158. if (is_array($propertyArray['options'])) {
  159. foreach ($propertyArray['options'] as $optionKey => &$option) {
  160. if (!empty($option['text'])) {
  161. $option['text'] = $this->xpdo->lexicon($option['text']);
  162. }
  163. }
  164. }
  165. }*/
  166. $propertiesArray[$key] = $propertyArray;
  167. }
  168. if ($merge && !empty($propertiesArray)) {
  169. $existing = $this->get('properties');
  170. if (is_array($existing) && !empty($existing)) {
  171. $propertiesArray = array_merge($existing, $propertiesArray);
  172. }
  173. }
  174. $set = $this->set('properties', $propertiesArray);
  175. }
  176. return $set;
  177. }
  178. /**
  179. * Overrides xPDOObject::save to fire modX-specific events.
  180. *
  181. * {@inheritDoc}
  182. */
  183. public function save($cacheFlag = null) {
  184. $isNew = $this->isNew();
  185. if ($this->xpdo instanceof modX) {
  186. $this->xpdo->invokeEvent('OnPropertySetBeforeSave',array(
  187. 'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
  188. 'propertySet' => &$this,
  189. 'cacheFlag' => $cacheFlag,
  190. ));
  191. }
  192. $saved = parent :: save($cacheFlag);
  193. if ($saved && $this->xpdo instanceof modX) {
  194. $this->xpdo->invokeEvent('OnPropertySetSave',array(
  195. 'mode' => $isNew ? modSystemEvent::MODE_NEW : modSystemEvent::MODE_UPD,
  196. 'propertySet' => &$this,
  197. 'cacheFlag' => $cacheFlag,
  198. ));
  199. }
  200. return $saved;
  201. }
  202. /**
  203. * Overrides xPDOObject::remove to fire modX-specific events.
  204. *
  205. * {@inheritDoc}
  206. */
  207. public function remove(array $ancestors = array()) {
  208. if ($this->xpdo instanceof modX) {
  209. $this->xpdo->invokeEvent('OnPropertySetBeforeRemove',array(
  210. 'propertySet' => &$this,
  211. 'ancestors' => $ancestors,
  212. ));
  213. }
  214. $removed = parent :: remove($ancestors);
  215. if ($removed && $this->xpdo instanceof modX) {
  216. $this->xpdo->invokeEvent('OnPropertySetRemove',array(
  217. 'propertySet' => &$this,
  218. 'ancestors' => $ancestors,
  219. ));
  220. }
  221. return $removed;
  222. }
  223. }