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

https://github.com/quochuy/ezflow · PHP · 207 lines · 120 code · 21 blank · 66 comment · 9 complexity · 352572ed03c407647115b291c34aa6bc 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 eZFlowPool
  27. {
  28. /**
  29. * Return waiting items for block with given $blockID
  30. *
  31. * @static
  32. * @param string $blockID
  33. * @return array
  34. */
  35. static function waitingItems( $blockID )
  36. {
  37. $db = eZDB::instance();
  38. $queue = $db->arrayQuery( "SELECT *
  39. FROM ezm_pool
  40. WHERE block_id='$blockID'
  41. AND ts_visible=0
  42. AND ts_hidden=0
  43. ORDER BY ts_publication ASC, priority ASC" );
  44. return $queue;
  45. }
  46. /**
  47. * Return valid items for block with given $blockID
  48. *
  49. * @static
  50. * @param string $blockID
  51. * @return array
  52. */
  53. static function validItems( $blockID )
  54. {
  55. $db = eZDB::instance();
  56. $valid = $db->arrayQuery( "SELECT *
  57. FROM ezm_pool
  58. WHERE block_id='$blockID'
  59. AND ts_visible>0
  60. AND ts_hidden=0
  61. ORDER BY priority DESC" );
  62. return $valid;
  63. }
  64. /**
  65. * Return valid items for block with given $blockID
  66. *
  67. * @static
  68. * @param string $blockID
  69. * @param bool $asObject
  70. * @return array(eZContentObjectTreeNode)
  71. */
  72. static function validNodes( $blockID, $asObject = true )
  73. {
  74. if ( isset( $GLOBALS['eZFlowPool'] ) === false )
  75. $GLOBALS['eZFlowPool'] = array();
  76. if ( isset( $GLOBALS['eZFlowPool'][$blockID] ) )
  77. return $GLOBALS['eZFlowPool'][$blockID];
  78. $db = eZDB::instance();
  79. $validNodes = $db->arrayQuery( "SELECT *
  80. FROM ezm_pool, ezcontentobject_tree
  81. WHERE ezm_pool.block_id='$blockID'
  82. AND ezm_pool.ts_visible>0
  83. AND ezm_pool.ts_hidden=0
  84. AND ezcontentobject_tree.node_id = ezm_pool.node_id
  85. ORDER BY ezm_pool.priority DESC" );
  86. if ( $asObject && !empty( $validNodes ) )
  87. {
  88. $validNodesObjects = array();
  89. foreach( $validNodes as $node )
  90. {
  91. $nodeID = $node['node_id'];
  92. $validNodesObjects[] = eZContentObjectTreeNode::fetch( $nodeID );
  93. }
  94. $GLOBALS['eZFlowPool'][$blockID] = $validNodesObjects;
  95. return $validNodesObjects;
  96. }
  97. else
  98. {
  99. return $validNodes;
  100. }
  101. }
  102. /**
  103. * Return archived items for block with given $blockID
  104. *
  105. * @static
  106. * @param string $blockID
  107. * @return array
  108. */
  109. static function archivedItems( $blockID )
  110. {
  111. $db = eZDB::instance();
  112. $archived = $db->arrayQuery( "SELECT *
  113. FROM ezm_pool
  114. WHERE block_id='$blockID'
  115. AND ts_hidden>0
  116. ORDER BY ts_hidden ASC" );
  117. return $archived;
  118. }
  119. /**
  120. * Insert items in the pool
  121. *
  122. * @param array $items Array of items to insert in the pool.
  123. * The following information must appear for every item:
  124. * blockID, objectID, nodeID, priority and timestamp.
  125. *
  126. * @return bool Returns true if the operation suceeded, false otherwise.
  127. */
  128. static function insertItems( array $items )
  129. {
  130. // Checking the validity of items.
  131. foreach ( $items as $item )
  132. {
  133. if ( !isset( $item['blockID'], $item['objectID'], $item['nodeID'], $item['priority'], $item['timestamp'] ) )
  134. {
  135. eZDebug::writeError( "Pool item is missing one of the following information: blockID, objectID, nodeID, priority or timestamp", __METHOD__ );
  136. return false;
  137. }
  138. }
  139. $db = eZDB::instance();
  140. if ( $db->databaseName() === 'mysql' )
  141. {
  142. // MySQL permits inserting elements without complaining about duplicates thanks to "INSERT IGNORE".
  143. // additionally, it support multiple inserts which may improve performance a lot.
  144. // @see #017120
  145. $values = array();
  146. foreach ( $items as $item )
  147. {
  148. $values[] = "( '" . $db->escapeString( $item['blockID'] ) . "', " .
  149. (int)$item['objectID'] . ", " .
  150. (int)$item['nodeID'] . ", " .
  151. (int)$item['priority'] . ", " .
  152. (int)$item['timestamp'] . " )";
  153. }
  154. if ( !empty( $values ) )
  155. {
  156. $db->query( "INSERT IGNORE INTO ezm_pool ( block_id, object_id, node_id, priority, ts_publication ) VALUES " .
  157. implode( ',', $values ) );
  158. }
  159. }
  160. else
  161. {
  162. $db->lock( 'ezm_pool' );
  163. foreach ( $items as $item )
  164. {
  165. $escapedBlockID = $db->escapeString( $item['blockID'] );
  166. $itemCount = $db->arrayQuery(
  167. "SELECT COUNT( * ) as count " .
  168. "FROM ezm_pool " .
  169. "WHERE block_id='$escapedBlockID' AND object_id=" . (int)$item['objectID'] );
  170. if ( $itemCount[0]['count'] == 0 )
  171. {
  172. $db->query( "INSERT INTO ezm_pool ( block_id, object_id, node_id, priority, ts_publication ) " .
  173. "VALUES ( '$escapedBlockID', " .
  174. (int)$item['objectID'] . ", " .
  175. (int)$item['nodeID'] . ", " .
  176. (int)$item['priority'] . ", " .
  177. (int)$item['timestamp'] .
  178. " )" );
  179. }
  180. }
  181. $db->unlock();
  182. }
  183. return true;
  184. }
  185. }
  186. ?>