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

/generator/lib/behavior/nestedset/NestedSetBehaviorQueryBuilderModifier.php

https://github.com/1989gaurav/Propel
PHP | 379 lines | 238 code | 22 blank | 119 comment | 14 complexity | d7d2e75efa70c81632962ecdc2b4fb3c 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 adds nested set tree structure columns and abilities
  11. *
  12. * @author François Zaninotto
  13. * @package propel.generator.behavior.nestedset
  14. */
  15. class NestedSetBehaviorQueryBuilderModifier
  16. {
  17. protected $behavior, $table, $builder, $objectClassname, $peerClassname;
  18. public function __construct($behavior)
  19. {
  20. $this->behavior = $behavior;
  21. $this->table = $behavior->getTable();
  22. }
  23. protected function getParameter($key)
  24. {
  25. return $this->behavior->getParameter($key);
  26. }
  27. protected function getColumn($name)
  28. {
  29. return $this->behavior->getColumnForParameter($name);
  30. }
  31. protected function setBuilder($builder)
  32. {
  33. $this->builder = $builder;
  34. $this->objectClassname = $builder->getStubObjectBuilder()->getClassname();
  35. $this->queryClassname = $builder->getStubQueryBuilder()->getClassname();
  36. $this->peerClassname = $builder->getStubPeerBuilder()->getClassname();
  37. }
  38. public function queryMethods($builder)
  39. {
  40. $this->setBuilder($builder);
  41. $script = '';
  42. // select filters
  43. if ($this->behavior->useScope()) {
  44. $this->addTreeRoots($script);
  45. $this->addInTree($script);
  46. }
  47. $this->addDescendantsOf($script);
  48. $this->addBranchOf($script);
  49. $this->addChildrenOf($script);
  50. $this->addSiblingsOf($script);
  51. $this->addAncestorsOf($script);
  52. $this->addRootsOf($script);
  53. // select orders
  54. $this->addOrderByBranch($script);
  55. $this->addOrderByLevel($script);
  56. // select termination methods
  57. $this->addFindRoot($script);
  58. if ($this->behavior->useScope()) {
  59. $this->addFindRoots($script);
  60. }
  61. $this->addFindTree($script);
  62. return $script;
  63. }
  64. protected function addTreeRoots(&$script)
  65. {
  66. $script .= "
  67. /**
  68. * Filter the query to restrict the result to root objects
  69. *
  70. * @return {$this->queryClassname} The current query, for fluid interface
  71. */
  72. public function treeRoots()
  73. {
  74. return \$this->addUsingAlias({$this->peerClassname}::LEFT_COL, 1, Criteria::EQUAL);
  75. }
  76. ";
  77. }
  78. protected function addInTree(&$script)
  79. {
  80. $script .= "
  81. /**
  82. * Returns the objects in a certain tree, from the tree scope
  83. *
  84. * @param int \$scope Scope to determine which objects node to return
  85. *
  86. * @return {$this->queryClassname} The current query, for fluid interface
  87. */
  88. public function inTree(\$scope = null)
  89. {
  90. return \$this->addUsingAlias({$this->peerClassname}::SCOPE_COL, \$scope, Criteria::EQUAL);
  91. }
  92. ";
  93. }
  94. protected function addDescendantsOf(&$script)
  95. {
  96. $objectName = '$' . $this->table->getStudlyPhpName();
  97. $script .= "
  98. /**
  99. * Filter the query to restrict the result to descendants of an object
  100. *
  101. * @param {$this->objectClassname} $objectName The object to use for descendant search
  102. *
  103. * @return {$this->queryClassname} The current query, for fluid interface
  104. */
  105. public function descendantsOf($objectName)
  106. {
  107. return \$this";
  108. if ($this->behavior->useScope()) {
  109. $script .= "
  110. ->inTree({$objectName}->getScopeValue())";
  111. }
  112. $script .= "
  113. ->addUsingAlias({$this->peerClassname}::LEFT_COL, {$objectName}->getLeftValue(), Criteria::GREATER_THAN)
  114. ->addUsingAlias({$this->peerClassname}::LEFT_COL, {$objectName}->getRightValue(), Criteria::LESS_THAN);
  115. }
  116. ";
  117. }
  118. protected function addBranchOf(&$script)
  119. {
  120. $objectName = '$' . $this->table->getStudlyPhpName();
  121. $script .= "
  122. /**
  123. * Filter the query to restrict the result to the branch of an object.
  124. * Same as descendantsOf(), except that it includes the object passed as parameter in the result
  125. *
  126. * @param {$this->objectClassname} $objectName The object to use for branch search
  127. *
  128. * @return {$this->queryClassname} The current query, for fluid interface
  129. */
  130. public function branchOf($objectName)
  131. {
  132. return \$this";
  133. if ($this->behavior->useScope()) {
  134. $script .= "
  135. ->inTree({$objectName}->getScopeValue())";
  136. }
  137. $script .= "
  138. ->addUsingAlias({$this->peerClassname}::LEFT_COL, {$objectName}->getLeftValue(), Criteria::GREATER_EQUAL)
  139. ->addUsingAlias({$this->peerClassname}::LEFT_COL, {$objectName}->getRightValue(), Criteria::LESS_EQUAL);
  140. }
  141. ";
  142. }
  143. protected function addChildrenOf(&$script)
  144. {
  145. $objectName = '$' . $this->table->getStudlyPhpName();
  146. $script .= "
  147. /**
  148. * Filter the query to restrict the result to children of an object
  149. *
  150. * @param {$this->objectClassname} $objectName The object to use for child search
  151. *
  152. * @return {$this->queryClassname} The current query, for fluid interface
  153. */
  154. public function childrenOf($objectName)
  155. {
  156. return \$this
  157. ->descendantsOf($objectName)
  158. ->addUsingAlias({$this->peerClassname}::LEVEL_COL, {$objectName}->getLevel() + 1, Criteria::EQUAL);
  159. }
  160. ";
  161. }
  162. protected function addSiblingsOf(&$script)
  163. {
  164. $objectName = '$' . $this->table->getStudlyPhpName();
  165. $script .= "
  166. /**
  167. * Filter the query to restrict the result to siblings of an object.
  168. * The result does not include the object passed as parameter.
  169. *
  170. * @param {$this->objectClassname} $objectName The object to use for sibling search
  171. * @param PropelPDO \$con Connection to use.
  172. *
  173. * @return {$this->queryClassname} The current query, for fluid interface
  174. */
  175. public function siblingsOf($objectName, PropelPDO \$con = null)
  176. {
  177. if ({$objectName}->isRoot()) {
  178. return \$this->
  179. add({$this->peerClassname}::LEVEL_COL, '1<>1', Criteria::CUSTOM);
  180. } else {
  181. return \$this
  182. ->childrenOf({$objectName}->getParent(\$con))
  183. ->prune($objectName);
  184. }
  185. }
  186. ";
  187. }
  188. protected function addAncestorsOf(&$script)
  189. {
  190. $objectName = '$' . $this->table->getStudlyPhpName();
  191. $script .= "
  192. /**
  193. * Filter the query to restrict the result to ancestors of an object
  194. *
  195. * @param {$this->objectClassname} $objectName The object to use for ancestors search
  196. *
  197. * @return {$this->queryClassname} The current query, for fluid interface
  198. */
  199. public function ancestorsOf($objectName)
  200. {
  201. return \$this";
  202. if ($this->behavior->useScope()) {
  203. $script .= "
  204. ->inTree({$objectName}->getScopeValue())";
  205. }
  206. $script .= "
  207. ->addUsingAlias({$this->peerClassname}::LEFT_COL, {$objectName}->getLeftValue(), Criteria::LESS_THAN)
  208. ->addUsingAlias({$this->peerClassname}::RIGHT_COL, {$objectName}->getRightValue(), Criteria::GREATER_THAN);
  209. }
  210. ";
  211. }
  212. protected function addRootsOf(&$script)
  213. {
  214. $objectName = '$' . $this->table->getStudlyPhpName();
  215. $script .= "
  216. /**
  217. * Filter the query to restrict the result to roots of an object.
  218. * Same as ancestorsOf(), except that it includes the object passed as parameter in the result
  219. *
  220. * @param {$this->objectClassname} $objectName The object to use for roots search
  221. *
  222. * @return {$this->queryClassname} The current query, for fluid interface
  223. */
  224. public function rootsOf($objectName)
  225. {
  226. return \$this";
  227. if ($this->behavior->useScope()) {
  228. $script .= "
  229. ->inTree({$objectName}->getScopeValue())";
  230. }
  231. $script .= "
  232. ->addUsingAlias({$this->peerClassname}::LEFT_COL, {$objectName}->getLeftValue(), Criteria::LESS_EQUAL)
  233. ->addUsingAlias({$this->peerClassname}::RIGHT_COL, {$objectName}->getRightValue(), Criteria::GREATER_EQUAL);
  234. }
  235. ";
  236. }
  237. protected function addOrderByBranch(&$script)
  238. {
  239. $script .= "
  240. /**
  241. * Order the result by branch, i.e. natural tree order
  242. *
  243. * @param bool \$reverse if true, reverses the order
  244. *
  245. * @return {$this->queryClassname} The current query, for fluid interface
  246. */
  247. public function orderByBranch(\$reverse = false)
  248. {
  249. if (\$reverse) {
  250. return \$this
  251. ->addDescendingOrderByColumn({$this->peerClassname}::LEFT_COL);
  252. } else {
  253. return \$this
  254. ->addAscendingOrderByColumn({$this->peerClassname}::LEFT_COL);
  255. }
  256. }
  257. ";
  258. }
  259. protected function addOrderByLevel(&$script)
  260. {
  261. $script .= "
  262. /**
  263. * Order the result by level, the closer to the root first
  264. *
  265. * @param bool \$reverse if true, reverses the order
  266. *
  267. * @return {$this->queryClassname} The current query, for fluid interface
  268. */
  269. public function orderByLevel(\$reverse = false)
  270. {
  271. if (\$reverse) {
  272. return \$this
  273. ->addAscendingOrderByColumn({$this->peerClassname}::RIGHT_COL);
  274. } else {
  275. return \$this
  276. ->addDescendingOrderByColumn({$this->peerClassname}::RIGHT_COL);
  277. }
  278. }
  279. ";
  280. }
  281. protected function addFindRoot(&$script)
  282. {
  283. $useScope = $this->behavior->useScope();
  284. $script .= "
  285. /**
  286. * Returns " . ($useScope ? 'a' : 'the') ." root node for the tree
  287. *";
  288. if($useScope) {
  289. $script .= "
  290. * @param int \$scope Scope to determine which root node to return";
  291. }
  292. $script .= "
  293. * @param PropelPDO \$con Connection to use.
  294. *
  295. * @return {$this->objectClassname} The tree root object
  296. */
  297. public function findRoot(" . ($useScope ? "\$scope = null, " : "") . "\$con = null)
  298. {
  299. return \$this
  300. ->addUsingAlias({$this->peerClassname}::LEFT_COL, 1, Criteria::EQUAL)";
  301. if ($useScope) {
  302. $script .= "
  303. ->inTree(\$scope)";
  304. }
  305. $script .= "
  306. ->findOne(\$con);
  307. }
  308. ";
  309. }
  310. protected function addFindRoots(&$script)
  311. {
  312. $script .= "
  313. /**
  314. * Returns the root objects for all trees.
  315. *
  316. * @param PropelPDO \$con Connection to use.
  317. *
  318. * @return mixed the list of results, formatted by the current formatter
  319. */
  320. public function findRoots(\$con = null)
  321. {
  322. return \$this
  323. ->treeRoots()
  324. ->find(\$con);
  325. }
  326. ";
  327. }
  328. protected function addFindTree(&$script)
  329. {
  330. $useScope = $this->behavior->useScope();
  331. $script .= "
  332. /**
  333. * Returns " . ($useScope ? 'a' : 'the') ." tree of objects
  334. *";
  335. if($useScope) {
  336. $script .= "
  337. * @param int \$scope Scope to determine which tree node to return";
  338. }
  339. $script .= "
  340. * @param PropelPDO \$con Connection to use.
  341. *
  342. * @return mixed the list of results, formatted by the current formatter
  343. */
  344. public function findTree(" . ($useScope ? "\$scope = null, " : "") . "\$con = null)
  345. {
  346. return \$this";
  347. if ($useScope) {
  348. $script .= "
  349. ->inTree(\$scope)";
  350. }
  351. $script .= "
  352. ->orderByBranch()
  353. ->find(\$con);
  354. }
  355. ";
  356. }
  357. }