/packages/ezflow_extension/ezextension/ezflow/classes/ezpagezone.php

https://github.com/quochuy/ezflow · PHP · 393 lines · 207 code · 51 blank · 135 comment · 29 complexity · 9479689aa127f2092fc17cd2fa2fa2a2 MD5 · raw file

  1. <?php
  2. //
  3. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  4. // SOFTWARE NAME: eZ Flow
  5. // SOFTWARE RELEASE: 1.1-0
  6. // COPYRIGHT NOTICE: Copyright (C) 1999-2011 eZ Systems AS
  7. // SOFTWARE LICENSE: GNU General Public License v2.0
  8. // NOTICE: >
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of version 2.0 of the GNU General
  11. // Public License as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of version 2.0 of the GNU General
  19. // Public License along with this program; if not, write to the Free
  20. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. // MA 02110-1301, USA.
  22. //
  23. //
  24. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  25. //
  26. class eZPageZone
  27. {
  28. private $attributes = array();
  29. /**
  30. * Constructor
  31. *
  32. * @param string $name
  33. */
  34. function __construct( $name = null )
  35. {
  36. if ( isset( $name ) )
  37. $this->attributes['name'] = $name;
  38. }
  39. /**
  40. * Creates DOMElement with zone data
  41. *
  42. * @param DOMDocument $dom
  43. * @return DOMElement
  44. */
  45. public function toXML( DOMDocument $dom )
  46. {
  47. $zoneNode = $dom->createElement( 'zone' );
  48. foreach ( $this->attributes as $attrName => $attrValue )
  49. {
  50. switch ( $attrName )
  51. {
  52. case 'id':
  53. $zoneNode->setAttribute( 'id', 'id_' . $attrValue );
  54. break;
  55. case 'action':
  56. $zoneNode->setAttribute( 'action', $attrValue );
  57. break;
  58. case 'blocks':
  59. foreach ( $this->attributes['blocks'] as $block )
  60. {
  61. $blockNode = $block->toXML( $dom );
  62. $zoneNode->appendChild( $blockNode );
  63. }
  64. break;
  65. default:
  66. $node = $dom->createElement( $attrName );
  67. $nodeValue = $dom->createTextNode( $attrValue );
  68. $node->appendChild( $nodeValue );
  69. $zoneNode->appendChild( $node );
  70. break;
  71. }
  72. }
  73. return $zoneNode;
  74. }
  75. /**
  76. * Creates and return eZPageZone object from given XML
  77. *
  78. * @static
  79. * @param DOMElement $node
  80. * @return eZPageZone
  81. */
  82. public static function createFromXML( DOMElement $node )
  83. {
  84. $newObj = new eZPageZone();
  85. if ( $node->hasAttributes() )
  86. {
  87. foreach ( $node->attributes as $attr )
  88. {
  89. if ( $attr->name == 'id' )
  90. {
  91. $value = explode( '_', $attr->value );
  92. $newObj->setAttribute( $attr->name, $value[1] );
  93. }
  94. else
  95. {
  96. $newObj->setAttribute( $attr->name, $attr->value );
  97. }
  98. }
  99. }
  100. foreach ( $node->childNodes as $node )
  101. {
  102. if ( $node->nodeType == XML_ELEMENT_NODE && $node->nodeName == 'block' )
  103. {
  104. $blockNode = eZPageBlock::createFromXML( $node );
  105. $newObj->addBlock( $blockNode );
  106. }
  107. elseif ( $node->nodeType == XML_ELEMENT_NODE )
  108. {
  109. $newObj->setAttribute( $node->nodeName, $node->nodeValue );
  110. }
  111. }
  112. return $newObj;
  113. }
  114. /**
  115. * Add new $block to eZPageZone object
  116. *
  117. * @param eZPageBlock $block
  118. * @return eZPageBlock
  119. */
  120. public function addBlock( eZPageBlock $block )
  121. {
  122. $this->attributes['blocks'][] = $block;
  123. return $block;
  124. }
  125. /**
  126. * Sorting blocks for given sort array which contains block ids
  127. *
  128. * @param array $sortArray
  129. */
  130. public function sortBlocks( array $sortArray )
  131. {
  132. $sortedBlocks = array();
  133. foreach( $sortArray as $sortItem )
  134. {
  135. $blocksToBeRemoved = array();
  136. foreach( $this->attributes['blocks'] as $block )
  137. {
  138. if ( $block->attribute('id') === $sortItem )
  139. $sortedBlocks[] = $block;
  140. if ( $block->toBeRemoved() )
  141. $blocksToBeRemoved[] = $block;
  142. }
  143. }
  144. $sortedBlocks = array_merge( $sortedBlocks, $blocksToBeRemoved );
  145. $this->attributes['blocks'] = $sortedBlocks;
  146. }
  147. /**
  148. * Move current block position up
  149. *
  150. * @param integer $currentIndex
  151. * @return bool
  152. */
  153. public function moveBlockUp( $currentIndex )
  154. {
  155. $array =& $this->attributes['blocks'];
  156. $newIndex = $currentIndex - 1;
  157. if ( $newIndex < 0 || $newIndex >= count( $array ) )
  158. return false;
  159. $tmpItem = $array[$newIndex];
  160. $array[$newIndex] =& $array[$currentIndex];
  161. $array[$currentIndex] =& $tmpItem;
  162. if ( $tmpItem->toBeRemoved() )
  163. $this->moveBlockUp( $newIndex );
  164. return true;
  165. }
  166. /**
  167. * Move current block position down
  168. *
  169. * @param integer $currentIndex
  170. * @return bool
  171. */
  172. public function moveBlockDown( $currentIndex )
  173. {
  174. $array =& $this->attributes['blocks'];
  175. $newIndex = $currentIndex + 1;
  176. if ( $newIndex < 0 || $newIndex >= count( $array ) )
  177. return false;
  178. $tmpItem = $array[$newIndex];
  179. $array[$newIndex] =& $array[$currentIndex];
  180. $array[$currentIndex] =& $tmpItem;
  181. if ( $tmpItem->toBeRemoved() )
  182. $this->moveBlockDown( $newIndex );
  183. return true;
  184. }
  185. /**
  186. * Remove block with given $index from eZPageZone object
  187. *
  188. * @param integer $index
  189. */
  190. public function removeBlock( $index )
  191. {
  192. unset( $this->attributes['blocks'][$index] );
  193. }
  194. /**
  195. * Return eZPageZone name attribute
  196. *
  197. * @return string
  198. */
  199. public function getName()
  200. {
  201. return isset( $this->attributes['name'] ) ? $this->attributes['name'] : null;
  202. }
  203. /**
  204. * Return total block count
  205. *
  206. * @return integer
  207. */
  208. public function getBlockCount()
  209. {
  210. return isset( $this->attributes['blocks'] ) ? count( $this->attributes['blocks'] ) : 0;
  211. }
  212. /**
  213. * Return eZPageBlock object by given $index
  214. *
  215. * @return eZPageBlock
  216. * @param integer $index
  217. */
  218. public function getBlock( $index )
  219. {
  220. $block = null;
  221. if ( isset( $this->attributes['blocks'][$index] ) )
  222. $block = $this->attributes['blocks'][$index];
  223. return $block;
  224. }
  225. /**
  226. * Return attributes names
  227. *
  228. * @return array(string)
  229. */
  230. public function attributes()
  231. {
  232. return array_keys( $this->attributes );
  233. }
  234. /**
  235. * Checks if attribute with given $name exists
  236. *
  237. * @param string $name
  238. * @return bool
  239. */
  240. public function hasAttribute( $name )
  241. {
  242. return in_array( $name, array_keys( $this->attributes ) );
  243. }
  244. /**
  245. * Set attribute with given $name to $value
  246. *
  247. * @param string $name
  248. * @param mixed $value
  249. */
  250. public function setAttribute( $name, $value )
  251. {
  252. $this->attributes[$name] = $value;
  253. }
  254. /**
  255. * Return value of attribute with given $name
  256. *
  257. * @return mixed
  258. * @param string $name
  259. */
  260. public function attribute( $name )
  261. {
  262. if ( $this->hasAttribute( $name ) )
  263. {
  264. return $this->attributes[$name];
  265. }
  266. else
  267. {
  268. $value = null;
  269. return $value;
  270. }
  271. }
  272. /**
  273. * Cleanup processed objects, removes action attribute
  274. * removes all blocks marked with "remove" action
  275. *
  276. * @return eZPageZone
  277. */
  278. public function removeProcessed()
  279. {
  280. if ( $this->hasAttribute( 'action' ) )
  281. {
  282. unset( $this->attributes['action'] );
  283. }
  284. if ( $this->getBlockCount() > 0 )
  285. {
  286. foreach ( $this->attributes['blocks'] as $index => $block )
  287. {
  288. if ( $block->toBeRemoved() )
  289. {
  290. $this->removeBlock( $index );
  291. }
  292. else
  293. {
  294. $block->removeProcessed();
  295. }
  296. }
  297. }
  298. return $this;
  299. }
  300. /**
  301. * Checks if current zone is to be removed
  302. *
  303. * @return bool
  304. */
  305. public function toBeRemoved()
  306. {
  307. return isset( $this->attributes['action'] ) && $this->attributes['action'] == 'remove';
  308. }
  309. /**
  310. * Checks if current zone is to be modified
  311. *
  312. * @return bool
  313. */
  314. public function toBeModified()
  315. {
  316. return isset( $this->attributes['action'] ) && $this->attributes['action'] == 'modify';
  317. }
  318. /**
  319. * Checks if current zone is to be added
  320. *
  321. * @return bool
  322. */
  323. public function toBeAdded()
  324. {
  325. return isset( $this->attributes['action'] ) && $this->attributes['action'] == 'add';
  326. }
  327. /**
  328. * Method executed when an object copy is created
  329. * by using the clone keyword
  330. *
  331. */
  332. public function __clone()
  333. {
  334. $this->attributes['id'] = md5( (string)microtime() . (string)mt_rand() );
  335. $this->attributes['action'] = 'add';
  336. foreach ( $this->attributes['blocks'] as $i => $block )
  337. {
  338. $this->attributes['blocks'][$i] = clone $block;
  339. }
  340. }
  341. }
  342. ?>