openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

TreeAction.class.php (10127B)


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