TreeAction.class.php (10182B)
1 <?php 2 3 namespace cms\action; 4 5 use cms\model\BaseObject; 6 use cms\model\Element; 7 use cms\model\Folder; 8 use cms\model\Group; 9 use cms\model\ModelFactory; 10 use cms\model\Page; 11 use cms\model\Project; 12 use cms\model\Template; 13 use cms\model\User; 14 use cms\model\Value; 15 use util\json\JSON; 16 use util\Tree; 17 use cms\model\Language; 18 use cms\model\Model; 19 20 use util\Session; 21 22 // OpenRat Content Management System 23 // Copyright (C) 2002 Jan Dankert, jandankert@jandankert.de 24 // 25 // This program is free software; you can redistribute it and/or 26 // modify it under the terms of the GNU General Public License 27 // as published by the Free Software Foundation; either version 2 28 // of the License, or (at your option) any later version. 29 // 30 // This program is distributed in the hope that it will be useful, 31 // but WITHOUT ANY WARRANTY; without even the implied warranty of 32 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 33 // GNU General Public License for more details. 34 // 35 // You should have received a copy of the GNU General Public License 36 // along with this program; if not, write to the Free Software 37 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 38 39 /** 40 * Action-Klasse zum Laden/Anzeigen des Navigations-Baumes 41 * @author $Author$ 42 * @version $Revision$ 43 * @package openrat.actions 44 */ 45 46 class TreeAction extends BaseAction 47 { 48 public $security = Action::SECURITY_GUEST; 49 50 public function __construct() 51 { 52 parent::__construct(); 53 } 54 55 /** 56 * Anzeigen des Baumes fuer asynchrone Anfragen. 57 */ 58 public function loadBranchView() 59 { 60 61 $type = $this->getRequestVar('type'); 62 63 $branch = $this->loadTreeBranch( $type ); 64 65 $this->setTemplateVar( 'branch',$branch ); 66 } 67 68 69 70 private function loadTreeBranch( $type ) 71 { 72 $tree = new Tree(); 73 74 try 75 { 76 $method = new \ReflectionMethod($tree,$type); 77 if ( $this->hasRequestVar('id')) 78 $method->invoke($tree, $this->getRequestVar('id') ); 79 else 80 $method->invoke($tree); // <== Executing the Action 81 } 82 catch (\ReflectionException $re) 83 { 84 throw new \LogicException('Treemethod not found: '.$type); 85 } 86 87 88 $branch = array(); 89 foreach($tree->treeElements as $element ) 90 { 91 $branch[] = get_object_vars($element); 92 } 93 94 return $branch; 95 } 96 97 98 /** 99 * Initialer Aufbau des Navigationsbaums. 100 */ 101 public function treeView() 102 { 103 $branch = $this->loadTreeBranch( 'root' ); 104 105 foreach( $branch as $k=>$b ) 106 { 107 if ( !empty($b['type']) ) 108 $branch[$k]['children'] = $this->loadTreeBranch( $b['type'] ); 109 else 110 $branch[$k]['children'] = array(); 111 } 112 113 $this->outputTreeBranch( $branch ); 114 115 //$this->setTemplateVar( 'branch',$branch ); 116 117 exit; // no template available. 118 119 } 120 121 122 /** 123 * The path to an object. 124 */ 125 public function pathView() { 126 127 $type = $this->getRequestVar('type'); 128 $id = $this->getRequestVar('id',OR_FILTER_ALPHANUM); 129 130 $result = $this->calculatePath( $type, $id ); 131 $this->setTemplateVar('path' ,$result ); 132 133 $name = $this->calculateName($type, $id); 134 $this->setTemplateVar('actual',$this->pathItem($type,$id,$name) ); 135 } 136 137 138 /** 139 * The path to an object. 140 */ 141 private function calculatePath($type, $id) { 142 143 switch( $type ) { 144 145 case 'projectlist': 146 return array(); 147 148 case 'configuration': 149 return array(); 150 151 case 'project': 152 return array( 153 $this->pathItem('projectlist',0) 154 ); 155 case 'folder': 156 case 'link' : 157 case 'url' : 158 case 'page' : 159 case 'file' : 160 case 'image' : 161 $o = new BaseObject( $id ); 162 $o->load(); 163 164 $result= array( 165 $this->pathItem('projectlist' ), 166 $this->pathItem('project' , $o->projectid), 167 ); 168 169 $parents = array_keys( $o->parentObjectFileNames(true) ); 170 foreach( $parents as $pid ) 171 { 172 $f = new Folder($pid); 173 $f->load(); 174 $result[] = $this->pathItem('folder' ,$pid,$f->filename ); 175 } 176 return $result; 177 178 case 'pageelement' : 179 180 $ids = explode('_',$id); 181 if ( count($ids) > 1 ) 182 { 183 list( $pageid, $elementid ) = $ids; 184 } 185 186 $p = new Page($pageid); 187 $p->load(); 188 189 $result= array( 190 $this->pathItem('projectlist' ), 191 $this->pathItem('project' , $p->projectid), 192 ); 193 194 $parents = array_keys( $p->parentObjectFileNames(true ) ); 195 foreach( $parents as $pid ) { 196 $f = new Folder($pid); 197 $f->load(); 198 $result[] = $this->pathItem('folder' ,$pid,$f->filename ); 199 } 200 $result[] = $this->pathItem('page' ,$id,$p->filename ); 201 return $result; 202 203 case 'userlist': 204 case 'usergroup': 205 return array( 206 //$this->pathItem('usergroup' ,0) 207 ); 208 case 'user': 209 return array( 210 //$this->pathItem('userandgroups',0), 211 $this->pathItem('userlist',0) 212 ); 213 case 'grouplist': 214 return array( 215 //array('type'=>'userandgroups','action'=>'userandgroups','id'=>0) 216 ); 217 case 'group': 218 return array( 219 //$this->pathItem('userandgroups',0), 220 $this->pathItem('grouplist' ,0) 221 ); 222 223 case 'templatelist': 224 case 'languagelist': 225 case 'modellist': 226 return array( 227 $this->pathItem('projectlist' ,0 ), 228 $this->pathItem('project' ,$id) 229 ); 230 231 case 'template': 232 $t = new Template( $id ); 233 $t->load(); 234 235 return array( 236 $this->pathItem('projectlist' ,0 ), 237 $this->pathItem('project' ,$t->projectid), 238 $this->pathItem('templatelist',$t->projectid) 239 ); 240 241 case 'element': 242 $e = new Element( $id ); 243 $e->load(); 244 $t = new Template( $e->templateid ); 245 $t->load(); 246 247 return array( 248 $this->pathItem('projectlist' ,0 ), 249 $this->pathItem('project' ,$t->projectid ), 250 $this->pathItem('templatelist',$t->projectid ), 251 $this->pathItem('template' ,$t->templateid,$t->name) 252 ); 253 254 case 'language': 255 $l = new Language( $id ); 256 $l->load(); 257 258 return array( 259 $this->pathItem('projectlist' ,0 ), 260 $this->pathItem('project' ,$l->projectid), 261 $this->pathItem('languagelist',$l->projectid) 262 ); 263 264 case 'model': 265 $m = new Model( $id ); 266 $m->load(); 267 268 return array( 269 $this->pathItem('projectlist' ,0 ), 270 $this->pathItem('project' ,$m->projectid), 271 $this->pathItem('modellist' ,$m->projectid) 272 ); 273 274 default: 275 throw new \InvalidArgumentException('Unknown type: '.$type); 276 } 277 } 278 279 280 281 private function outputTreeBranch($branch ) 282 { 283 $json = new JSON(); 284 echo '<ul class="or-navtree-list">'; 285 286 foreach( $branch as $b ) 287 { 288 $hasChildren = isset($b['children']) && !empty($b['children']); 289 290 echo '<li class="or-navtree-node or-navtree-node--'.($hasChildren?'is-open':'is-closed').' or-draggable" data-id="'.$b['internalId'].'" data-type="'.$b['type'].'" data-extra="'.str_replace('"',"'",$json->encode($b['extraId'])).'"><div class="or-navtree-node-control"><i class="tree-icon image-icon image-icon--node-'.($hasChildren?'open':'closed').'"></i></div><div class="clickable"><a href="./#/'.$b['type'].($b['internalId']?'/'.$b['internalId']:'').'" class="entry" data-extra="'.str_replace('"',"'",$json->encode($b['extraId'])).'" data-id="'.$b['internalId'].'" data-action="'.$b['action'].'" data-type="open" title="'.$b['description'].'"><i class="image-icon image-icon--action-'.$b['icon'].'" ></i> '.$b['text'].'</a></div>'; 291 292 if ($hasChildren) 293 { 294 $this->outputTreeBranch($b['children']); 295 } 296 297 echo '</li>'; 298 } 299 300 echo '</ul>'; 301 } 302 303 304 private function pathItem( $action, $id = 0, $name = '' ) { 305 return array('type'=>$this->typeToInternal($action),'action'=>$action ,'id'=>$id,'name'=>$name ); 306 } 307 308 309 private function typeToInternal($type) 310 { 311 switch( $type) { 312 313 case 'projectlist': 314 return 'projects'; 315 316 case 'userlist': 317 return 'users'; 318 319 case 'grouplist': 320 return 'groups'; 321 322 case 'templatelist': 323 return 'templates'; 324 325 case 'languagelist': 326 return 'languages'; 327 328 case 'modellist': 329 return 'models'; 330 331 default: 332 return $type; 333 } 334 335 } 336 337 /** 338 * @param $type 339 * @param $id 340 * @return string 341 */ 342 protected function calculateName($type, $id) 343 { 344 $name = ''; 345 $o = ModelFactory::create($type, $id); 346 347 if ($o) { 348 $o->load(); 349 $name = $o->getName(); 350 } 351 return $name; 352 } 353 354 355 } 356 357 ?>