1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
<?php
/*-----8<--------------------------------------------------------------------
*
* BEdita - a semantic content management framework
*
* Copyright 2008 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* BEdita is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with BEdita (see LICENSE.LGPL).
* If not, see <http://gnu.org/licenses/lgpl-3.0.html>.
*
*------------------------------------------------------------------->8-----
*/
/**
* Management of the tree of the contents.
*
* Permits with bits representation (bedita.ini.php):
* BEDITA_PERMS_READ 0x1
* BEDITA_PERMS_MODIFY 0x2
*
*
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $LastChangedDate$
*
* $Id$
*/
class BeTreeComponent extends Object {
public $controller = null;
public $filter = array();
protected $Tree = null;
protected $BEObject = null;
protected $Section = null;
function __construct() {
$this->Tree = ClassRegistry::init('Tree');
$this->BEObject = ClassRegistry::init('BEObject');
$this->Section = ClassRegistry::init('Section');
}
/**
* Initialize BeTree component
*
* @param Controller $controller
*/
function initialize(Controller $controller) {
$this->controller = $controller;
}
/**
* clean filter
*/
public function cleanFilter() {
$this->filter = array();
}
/**
* Tree of publications and sections for user connected
*
* @return array
*/
public function getSectionsTree() {
$conf = Configure::getInstance() ;
// Get connected user
$userid = $this->controller->BeAuth->userid();
$filter = array(
"object_type_id" => array($conf->objectTypes['area']['id'],$conf->objectTypes['section']['id']) ,
"count_permission" => true
);
$tree = $this->Tree->getAll(null, $userid, null, $filter) ;
return $tree ;
}
/**
* Tree of a publication and sections for user connected
*
* @return array
*/
public function getPublicationTree($id) {
$conf = Configure::getInstance() ;
// Get connected user
$userid = $this->controller->BeAuth->userid();
$filter = array(
"object_type_id" => array($conf->objectTypes['area']['id'],$conf->objectTypes['section']['id']) ,
"count_permission" => true
);
$tree = $this->Tree->getAll($id, $userid, null, $filter) ;
return $tree ;
}
/**
* get publication (area) data for the section $section_id
*
* @param int $section_id
* @return array
*/
public function getAreaForSection($section_id) {
$area = ClassRegistry::init('Area');
$area->containLevel("minimum");
$area_id = $this->Tree->getRootForSection($section_id);
return $area->findById($area_id);
}
/**
* Get tree with one branch expanded (the branch where object $id is)
*
* @param integer $id
* @return array
*/
public function expandOneBranch($id = null) {
$tree = $this->getSectionsTree() ;
for($i=0; $i < count($tree) ; $i++) {
if(!isset($id) || !$this->_searchRootDeleteOther($tree[$i], $id)) {
unset($tree[$i]['children']) ;
}
}
return $tree ;
}
/**
* Get children for node $id
*
* @param integer $id node ID
* @param mixed $status
* @param mixed $filter
* @param mixed $order
* @param boolean $dir
* @param int $page
* @param int $dim
* @param array $excludeIds
* @return array
*/
public function getChildren($id = null, $status = null, $filter = false, $order = null, $dir = true, $page = 1, $dim = null, $excludeIds = array()) {
$conf = Configure::getInstance() ;
// Get user connected
$userid = $this->controller->BeAuth->userid();
if (empty($userid)) {
$userid = null;
}
$filter = ($filter)? array_merge($this->filter, $filter) : $this->filter;
$objs = & $this->Tree->getChildren($id, $userid, $status, $filter, $order, $dir, $page, $dim, $excludeIds) ;
return $objs ;
}
/**
* Return descendants of a tree node
*
* @param integer $id node ID
* @param mixed $status
* @param mixed $filter
* @param mixed $order
* @param boolean $dir
* @param int $page
* @param int $dim
* @param array $excludeIds
* @return array
*/
public function getDescendants($id = null, $status = null, $filter = false, $order = null, $dir = true, $page = 1, $dim = null, $excludeIds = array()) {
$conf = Configure::getInstance() ;
// Get user data
$userid = $this->controller->BeAuth->userid();
if (empty($userid)) {
$userid = null;
}
$filter = ($filter)? array_merge($this->filter, $filter) : $this->filter;
$objs = & $this->Tree->getDescendants($id, $userid, $status, $filter, $order, $dir, $page, $dim, $excludeIds) ;
return $objs ;
}
/**
* TODO: remove this method
* Array of parent objects of $id...
*
* @param integer $id
* @return array parents ids
*/
public function getParents($id = null, $area_id=null, $status = array()) {
return $this->Tree->getParents($id, $area_id, $status);
}
/**
* TODO: remove this method. Used only in some addons shell scripts
* update tree position of object $id with new $destination array
*
* @param integer $id
* @param array $destination
*/
public function updateTree($id, $destination) {
return $this->Tree->updateTree($id, $destination);
}
/**
* setup array of tree destinations (parent ids)
* if some parents is forbidden to user (backend_private permission)
* then add it to $destination because user can't edit that destination
*
* @param int $objectId
* @param array $destination array of parent ids
*/
public function setupForSave($objectId, &$destination = array()) {
$permission = ClassRegistry::init('Permission');
$parentIds = $this->Tree->find('list', array(
'fields' => array('parent_id'),
'conditions' => array('id' => $objectId)
));
$userData = $this->controller->BeAuth->getUserSession();
foreach ($parentIds as $parent_id) {
if ($permission->isForbidden($parent_id, $userData) && !in_array($parent_id, $destination)) {
$destination[] = $parent_id;
}
}
}
/////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
/**
* Delete branches that don't contain object $id
*
* @param array $tree tree where to perform search
* @param integer $id object to search
* @return boolean
*/
private function _searchRootDeleteOther(&$tree, $id) {
// If it's the root...
if($tree['id'] == $id) {
for($i=0; $i < count($tree['children']) ; $i++) {
unset($tree['children'][$i]['children']) ;
}
return true ;
}
// Search in children trees
$found = null ;
for($i=0; $i < count($tree['children']) ; $i++) {
if($this->_searchRootDeleteOther($tree['children'][$i], $id)) {
$found = $i ;
}
}
// If branches to exclude were found, delete them
if(isset($found)) {
$tmp = $tree['children'][$found] ;
unset($tree['children']) ;
$tree['children'] = array($tmp) ;
return true ;
}
return false ;
}
}
?>