PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/generator/lib/behavior/sortable/SortableBehaviorPeerBuilderModifier.php

https://github.com/trendone/Propel
PHP | 370 lines | 224 code | 36 blank | 110 comment | 18 complexity | acddc489d9d20ee25a140441a9139d65 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * Behavior to add sortable peer methods
  11. *
  12. * @author Franรงois Zaninotto
  13. * @author heltem <heltem@o2php.com>
  14. * @author rozwell
  15. * @package propel.generator.behavior.sortable
  16. */
  17. class SortableBehaviorPeerBuilderModifier
  18. {
  19. protected $behavior, $table, $builder, $objectClassname, $peerClassname;
  20. public function __construct($behavior)
  21. {
  22. $this->behavior = $behavior;
  23. $this->table = $behavior->getTable();
  24. }
  25. protected function getParameter($key)
  26. {
  27. return $this->behavior->getParameter($key);
  28. }
  29. protected function getColumnAttribute($name)
  30. {
  31. return strtolower($this->behavior->getColumnForParameter($name)->getName());
  32. }
  33. protected function getColumnConstant($name)
  34. {
  35. return strtoupper($this->behavior->getColumnForParameter($name)->getName());
  36. }
  37. protected function getColumnPhpName($name)
  38. {
  39. return $this->behavior->getColumnForParameter($name)->getPhpName();
  40. }
  41. protected function setBuilder($builder)
  42. {
  43. $this->builder = $builder;
  44. $this->objectClassname = $builder->getStubObjectBuilder()->getClassname();
  45. $this->peerClassname = $builder->getStubPeerBuilder()->getClassname();
  46. $this->queryClassname = $builder->getStubQueryBuilder()->getClassname();
  47. }
  48. public function staticAttributes($builder)
  49. {
  50. $tableName = $this->table->getName();
  51. $script = "
  52. /**
  53. * rank column
  54. */
  55. const RANK_COL = '" . $tableName . '.' . $this->getColumnConstant('rank_column') . "';
  56. ";
  57. if ($this->behavior->useScope()) {
  58. $script .= "
  59. /**
  60. * Scope column for the set
  61. */
  62. const SCOPE_COL = '" . $tableName . '.' . $this->getColumnConstant('scope_column') . "';
  63. ";
  64. }
  65. return $script;
  66. }
  67. /**
  68. * Static methods
  69. *
  70. * @return string
  71. */
  72. public function staticMethods($builder)
  73. {
  74. $this->setBuilder($builder);
  75. $script = '';
  76. $this->addGetMaxRank($script);
  77. $this->addRetrieveByRank($script);
  78. $this->addReorder($script);
  79. $this->addDoSelectOrderByRank($script);
  80. if ($this->behavior->useScope()) {
  81. $this->addRetrieveList($script);
  82. $this->addCountList($script);
  83. $this->addDeleteList($script);
  84. }
  85. $this->addShiftRank($script);
  86. return $script;
  87. }
  88. protected function addGetMaxRank(&$script)
  89. {
  90. $useScope = $this->behavior->useScope();
  91. $script .= "
  92. /**
  93. * Get the highest rank
  94. * ";
  95. if ($useScope) {
  96. $script .= "
  97. * @param int \$scope Scope to determine which suite to consider";
  98. }
  99. $script .= "
  100. * @param PropelPDO optional connection
  101. *
  102. * @return integer highest position
  103. */
  104. public static function getMaxRank(" . ($useScope ? "\$scope = null, " : "") . "PropelPDO \$con = null)
  105. {
  106. if (\$con === null) {
  107. \$con = Propel::getConnection({$this->peerClassname}::DATABASE_NAME);
  108. }
  109. // shift the objects with a position lower than the one of object
  110. \$c = new Criteria();
  111. \$c->addSelectColumn('MAX(' . {$this->peerClassname}::RANK_COL . ')');";
  112. if ($useScope) {
  113. $script .= "
  114. \$c->add({$this->peerClassname}::SCOPE_COL, \$scope, Criteria::EQUAL);";
  115. }
  116. $script .= "
  117. \$stmt = {$this->peerClassname}::doSelectStmt(\$c, \$con);
  118. return \$stmt->fetchColumn();
  119. }
  120. ";
  121. }
  122. protected function addRetrieveByRank(&$script)
  123. {
  124. $peerClassname = $this->peerClassname;
  125. $useScope = $this->behavior->useScope();
  126. $script .= "
  127. /**
  128. * Get an item from the list based on its rank
  129. *
  130. * @param integer \$rank rank";
  131. if ($useScope) {
  132. $script .= "
  133. * @param int \$scope Scope to determine which suite to consider";
  134. }
  135. $script .= "
  136. * @param PropelPDO \$con optional connection
  137. *
  138. * @return {$this->objectClassname}
  139. */
  140. public static function retrieveByRank(\$rank, " . ($useScope ? "\$scope = null, " : "") . "PropelPDO \$con = null)
  141. {
  142. if (\$con === null) {
  143. \$con = Propel::getConnection($peerClassname::DATABASE_NAME);
  144. }
  145. \$c = new Criteria;
  146. \$c->add($peerClassname::RANK_COL, \$rank);";
  147. if ($useScope) {
  148. $script .= "
  149. \$c->add($peerClassname::SCOPE_COL, \$scope, Criteria::EQUAL);";
  150. }
  151. $script .= "
  152. return $peerClassname::doSelectOne(\$c, \$con);
  153. }
  154. ";
  155. }
  156. protected function addReorder(&$script)
  157. {
  158. $peerClassname = $this->peerClassname;
  159. $columnGetter = 'get' . $this->behavior->getColumnForParameter('rank_column')->getPhpName();
  160. $columnSetter = 'set' . $this->behavior->getColumnForParameter('rank_column')->getPhpName();
  161. $script .= "
  162. /**
  163. * Reorder a set of sortable objects based on a list of id/position
  164. * Beware that there is no check made on the positions passed
  165. * So incoherent positions will result in an incoherent list
  166. *
  167. * @param array \$order id => rank pairs
  168. * @param PropelPDO \$con optional connection
  169. *
  170. * @return boolean true if the reordering took place, false if a database problem prevented it
  171. */
  172. public static function reorder(array \$order, PropelPDO \$con = null)
  173. {
  174. if (\$con === null) {
  175. \$con = Propel::getConnection($peerClassname::DATABASE_NAME);
  176. }
  177. \$con->beginTransaction();
  178. try {
  179. \$ids = array_keys(\$order);
  180. \$objects = $peerClassname::retrieveByPKs(\$ids);
  181. foreach (\$objects as \$object) {
  182. \$pk = \$object->getPrimaryKey();
  183. if (\$object->$columnGetter() != \$order[\$pk]) {
  184. \$object->$columnSetter(\$order[\$pk]);
  185. \$object->save(\$con);
  186. }
  187. }
  188. \$con->commit();
  189. return true;
  190. } catch (PropelException \$e) {
  191. \$con->rollback();
  192. throw \$e;
  193. }
  194. }
  195. ";
  196. }
  197. protected function addDoSelectOrderByRank(&$script)
  198. {
  199. $peerClassname = $this->peerClassname;
  200. $script .= "
  201. /**
  202. * Return an array of sortable objects ordered by position
  203. *
  204. * @param Criteria \$criteria optional criteria object
  205. * @param string \$order sorting order, to be chosen between Criteria::ASC (default) and Criteria::DESC
  206. * @param PropelPDO \$con optional connection
  207. *
  208. * @return array list of sortable objects
  209. */
  210. public static function doSelectOrderByRank(Criteria \$criteria = null, \$order = Criteria::ASC, PropelPDO \$con = null)
  211. {
  212. if (\$con === null) {
  213. \$con = Propel::getConnection($peerClassname::DATABASE_NAME);
  214. }
  215. if (\$criteria === null) {
  216. \$criteria = new Criteria();
  217. } elseif (\$criteria instanceof Criteria) {
  218. \$criteria = clone \$criteria;
  219. }
  220. \$criteria->clearOrderByColumns();
  221. if (\$order == Criteria::ASC) {
  222. \$criteria->addAscendingOrderByColumn($peerClassname::RANK_COL);
  223. } else {
  224. \$criteria->addDescendingOrderByColumn($peerClassname::RANK_COL);
  225. }
  226. return $peerClassname::doSelect(\$criteria, \$con);
  227. }
  228. ";
  229. }
  230. protected function addRetrieveList(&$script)
  231. {
  232. $peerClassname = $this->peerClassname;
  233. $script .= "
  234. /**
  235. * Return an array of sortable objects in the given scope ordered by position
  236. *
  237. * @param int \$scope the scope of the list
  238. * @param string \$order sorting order, to be chosen between Criteria::ASC (default) and Criteria::DESC
  239. * @param PropelPDO \$con optional connection
  240. *
  241. * @return array list of sortable objects
  242. */
  243. public static function retrieveList(\$scope, \$order = Criteria::ASC, PropelPDO \$con = null)
  244. {
  245. \$c = new Criteria();
  246. \$c->add($peerClassname::SCOPE_COL, \$scope);
  247. return $peerClassname::doSelectOrderByRank(\$c, \$order, \$con);
  248. }
  249. ";
  250. }
  251. protected function addCountList(&$script)
  252. {
  253. $peerClassname = $this->peerClassname;
  254. $script .= "
  255. /**
  256. * Return the number of sortable objects in the given scope
  257. *
  258. * @param int \$scope the scope of the list
  259. * @param PropelPDO \$con optional connection
  260. *
  261. * @return array list of sortable objects
  262. */
  263. public static function countList(\$scope, PropelPDO \$con = null)
  264. {
  265. \$c = new Criteria();
  266. \$c->add($peerClassname::SCOPE_COL, \$scope);
  267. return $peerClassname::doCount(\$c, \$con);
  268. }
  269. ";
  270. }
  271. protected function addDeleteList(&$script)
  272. {
  273. $peerClassname = $this->peerClassname;
  274. $script .= "
  275. /**
  276. * Deletes the sortable objects in the given scope
  277. *
  278. * @param int \$scope the scope of the list
  279. * @param PropelPDO \$con optional connection
  280. *
  281. * @return int number of deleted objects
  282. */
  283. public static function deleteList(\$scope, PropelPDO \$con = null)
  284. {
  285. \$c = new Criteria();
  286. \$c->add($peerClassname::SCOPE_COL, \$scope);
  287. return $peerClassname::doDelete(\$c, \$con);
  288. }
  289. ";
  290. }
  291. protected function addShiftRank(&$script)
  292. {
  293. $useScope = $this->behavior->useScope();
  294. $peerClassname = $this->peerClassname;
  295. $script .= "
  296. /**
  297. * Adds \$delta to all Rank values that are >= \$first and <= \$last.
  298. * '\$delta' can also be negative.
  299. *
  300. * @param int \$delta Value to be shifted by, can be negative
  301. * @param int \$first First node to be shifted
  302. * @param int \$last Last node to be shifted";
  303. if ($useScope) {
  304. $script .= "
  305. * @param int \$scope Scope to use for the shift";
  306. }
  307. $script .= "
  308. * @param PropelPDO \$con Connection to use.
  309. */
  310. public static function shiftRank(\$delta, \$first = null, \$last = null, " . ($useScope ? "\$scope = null, " : "") . "PropelPDO \$con = null)
  311. {
  312. if (\$con === null) {
  313. \$con = Propel::getConnection($peerClassname::DATABASE_NAME, Propel::CONNECTION_WRITE);
  314. }
  315. \$whereCriteria = {$this->queryClassname}::create();
  316. if (null !== \$first) {
  317. \$whereCriteria->add($peerClassname::RANK_COL, \$first, Criteria::GREATER_EQUAL);
  318. }
  319. if (null !== \$last) {
  320. \$whereCriteria->addAnd($peerClassname::RANK_COL, \$last, Criteria::LESS_EQUAL);
  321. }";
  322. if ($useScope) {
  323. $script .= "
  324. \$whereCriteria->add($peerClassname::SCOPE_COL, \$scope, Criteria::EQUAL);";
  325. }
  326. $script .= "
  327. \$valuesCriteria = new Criteria($peerClassname::DATABASE_NAME);
  328. \$valuesCriteria->add($peerClassname::RANK_COL, array('raw' => $peerClassname::RANK_COL . ' + ?', 'value' => \$delta), Criteria::CUSTOM_EQUAL);
  329. {$this->builder->getPeerBuilder()->getBasePeerClassname()}::doUpdate(\$whereCriteria, \$valuesCriteria, \$con);
  330. $peerClassname::clearInstancePool();
  331. }
  332. ";
  333. }
  334. }