Base for a static organization website

AclNode.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @package Cake.Model
  13. * @since CakePHP(tm) v 0.2.9
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. App::uses('Model', 'Model');
  17. /**
  18. * ACL Node
  19. *
  20. * @package Cake.Model
  21. */
  22. class AclNode extends Model {
  23. /**
  24. * Explicitly disable in-memory query caching for ACL models
  25. *
  26. * @var bool
  27. */
  28. public $cacheQueries = false;
  29. /**
  30. * ACL models use the Tree behavior
  31. *
  32. * @var array
  33. */
  34. public $actsAs = array('Tree' => array('type' => 'nested'));
  35. /**
  36. * Constructor
  37. */
  38. public function __construct() {
  39. $config = Configure::read('Acl.database');
  40. if (isset($config)) {
  41. $this->useDbConfig = $config;
  42. }
  43. parent::__construct();
  44. }
  45. /**
  46. * Retrieves the Aro/Aco node for this model
  47. *
  48. * @param string|array|Model $ref Array with 'model' and 'foreign_key', model object, or string value
  49. * @return array Node found in database
  50. * @throws CakeException when binding to a model that doesn't exist.
  51. */
  52. public function node($ref = null) {
  53. $db = $this->getDataSource();
  54. $type = $this->alias;
  55. $result = null;
  56. if (!empty($this->useTable)) {
  57. $table = $this->useTable;
  58. } else {
  59. $table = Inflector::pluralize(Inflector::underscore($type));
  60. }
  61. if (empty($ref)) {
  62. return null;
  63. } elseif (is_string($ref)) {
  64. $path = explode('/', $ref);
  65. $start = $path[0];
  66. unset($path[0]);
  67. $queryData = array(
  68. 'conditions' => array(
  69. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
  70. $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")),
  71. 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
  72. 'joins' => array(array(
  73. 'table' => $table,
  74. 'alias' => "{$type}0",
  75. 'type' => 'INNER',
  76. 'conditions' => array("{$type}0.alias" => $start)
  77. )),
  78. 'order' => $db->name("{$type}.lft") . ' DESC'
  79. );
  80. $conditionsAfterJoin = array();
  81. foreach ($path as $i => $alias) {
  82. $j = $i - 1;
  83. $queryData['joins'][] = array(
  84. 'table' => $table,
  85. 'alias' => "{$type}{$i}",
  86. 'type' => 'INNER',
  87. 'conditions' => array(
  88. $db->name("{$type}{$i}.alias") . ' = ' . $db->value($alias, 'string')
  89. )
  90. );
  91. // it will be better if this conditions will performs after join operation
  92. $conditionsAfterJoin[] = $db->name("{$type}{$j}.id") . ' = ' . $db->name("{$type}{$i}.parent_id");
  93. $conditionsAfterJoin[] = $db->name("{$type}{$i}.rght") . ' < ' . $db->name("{$type}{$j}.rght");
  94. $conditionsAfterJoin[] = $db->name("{$type}{$i}.lft") . ' > ' . $db->name("{$type}{$j}.lft");
  95. $queryData['conditions'] = array('or' => array(
  96. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght"),
  97. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}{$i}.lft") . ' AND ' . $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}{$i}.rght"))
  98. );
  99. }
  100. $queryData['conditions'] = array_merge($queryData['conditions'], $conditionsAfterJoin);
  101. $result = $db->read($this, $queryData, -1);
  102. $path = array_values($path);
  103. if (!isset($result[0][$type]) ||
  104. (!empty($path) && $result[0][$type]['alias'] != $path[count($path) - 1]) ||
  105. (empty($path) && $result[0][$type]['alias'] != $start)
  106. ) {
  107. return false;
  108. }
  109. } elseif (is_object($ref) && $ref instanceof Model) {
  110. $ref = array('model' => $ref->name, 'foreign_key' => $ref->id);
  111. } elseif (is_array($ref) && !(isset($ref['model']) && isset($ref['foreign_key']))) {
  112. $name = key($ref);
  113. list(, $alias) = pluginSplit($name);
  114. $model = ClassRegistry::init(array('class' => $name, 'alias' => $alias));
  115. if (empty($model)) {
  116. throw new CakeException('cake_dev', "Model class '%s' not found in AclNode::node() when trying to bind %s object", $type, $this->alias);
  117. }
  118. $tmpRef = null;
  119. if (method_exists($model, 'bindNode')) {
  120. $tmpRef = $model->bindNode($ref);
  121. }
  122. if (empty($tmpRef)) {
  123. $ref = array('model' => $alias, 'foreign_key' => $ref[$name][$model->primaryKey]);
  124. } else {
  125. if (is_string($tmpRef)) {
  126. return $this->node($tmpRef);
  127. }
  128. $ref = $tmpRef;
  129. }
  130. }
  131. if (is_array($ref)) {
  132. if (is_array(current($ref)) && is_string(key($ref))) {
  133. $name = key($ref);
  134. $ref = current($ref);
  135. }
  136. foreach ($ref as $key => $val) {
  137. if (strpos($key, $type) !== 0 && strpos($key, '.') === false) {
  138. unset($ref[$key]);
  139. $ref["{$type}0.{$key}"] = $val;
  140. }
  141. }
  142. $queryData = array(
  143. 'conditions' => $ref,
  144. 'fields' => array('id', 'parent_id', 'model', 'foreign_key', 'alias'),
  145. 'joins' => array(array(
  146. 'table' => $table,
  147. 'alias' => "{$type}0",
  148. 'type' => 'INNER',
  149. 'conditions' => array(
  150. $db->name("{$type}.lft") . ' <= ' . $db->name("{$type}0.lft"),
  151. $db->name("{$type}.rght") . ' >= ' . $db->name("{$type}0.rght")
  152. )
  153. )),
  154. 'order' => $db->name("{$type}.lft") . ' DESC'
  155. );
  156. $result = $db->read($this, $queryData, -1);
  157. if (!$result) {
  158. throw new CakeException(__d('cake_dev', "AclNode::node() - Couldn't find %s node identified by \"%s\"", $type, print_r($ref, true)));
  159. }
  160. }
  161. return $result;
  162. }
  163. }