TreePathAction.class.php (5697B)
1 <?php 2 namespace cms\ui\action\tree; 3 use cms\action\RequestParams; 4 use cms\model\BaseObject; 5 use cms\model\Element; 6 use cms\model\Folder; 7 use cms\model\Language; 8 use cms\model\Model; 9 use cms\model\ModelFactory; 10 use cms\model\Page; 11 use cms\model\Template; 12 use cms\ui\action\TreeAction; 13 use cms\action\Method; 14 15 /** 16 * Calculating a path to an object, normally used for a breadcrumb navigation. 17 */ 18 class TreePathAction extends TreeAction implements Method { 19 20 public function view() { 21 $type = $this->request->getAlphanum('type'); 22 $id = $this->request->getAlphanum('id' ); 23 24 // Calculating the path to the actual object 25 $result = $this->calculatePath($type, $id); 26 $this->setTemplateVar('path', $result); 27 28 // The parent object 29 $this->setTemplateVar('parent', end($result ) ); 30 31 // The actual object 32 $name = $this->calculateName($type, $id); 33 $this->setTemplateVar('actual', $this->pathItem($type, $id, $name)); 34 } 35 36 37 public function post() { 38 } 39 40 41 /** 42 * The path to an object. 43 */ 44 protected function calculatePath($type, $id) { 45 46 switch( $type ) { 47 48 case 'index': 49 return array( 50 ); 51 52 case 'projectlist': 53 return array( 54 $this->pathItem('index',0) 55 ); 56 57 case 'configuration': 58 return array( 59 $this->pathItem('index',0) 60 ); 61 62 case 'project': 63 return array( 64 $this->pathItem('index' ,0 ), 65 $this->pathItem('projectlist',0) 66 ); 67 case 'folder': 68 case 'link' : 69 case 'url' : 70 case 'alias' : 71 case 'page' : 72 case 'file' : 73 case 'image' : 74 case 'text' : 75 $o = new BaseObject( $id ); 76 $o->load(); 77 78 $result= array( 79 $this->pathItem('index' ,0 ), 80 $this->pathItem('projectlist' ), 81 $this->pathItem('project' , $o->projectid), 82 ); 83 84 $parents = array_keys( $o->parentObjectFileNames(true) ); 85 foreach( $parents as $pid ) 86 { 87 $f = new Folder($pid); 88 $f->load(); 89 $result[] = $this->pathItem('folder' ,$pid,$f->filename ); 90 } 91 return $result; 92 93 case 'pageelement' : 94 95 $ids = explode('_',$id); 96 if ( count($ids) > 1 ) 97 { 98 list( $pageid, $elementid ) = $ids; 99 } 100 101 $p = new Page($pageid); 102 $p->load(); 103 104 $result= array( 105 $this->pathItem('index' ,0 ), 106 $this->pathItem('projectlist' ), 107 $this->pathItem('project' , $p->projectid), 108 ); 109 110 $parents = array_keys( $p->parentObjectFileNames(true ) ); 111 foreach( $parents as $pid ) { 112 $f = new Folder($pid); 113 $f->load(); 114 $result[] = $this->pathItem('folder' ,$pid,$f->filename ); 115 } 116 $result[] = $this->pathItem('page' ,$pageid,$p->filename ); 117 return $result; 118 119 case 'userlist': 120 return array( 121 $this->pathItem('index' ,0 ), 122 $this->pathItem('usergroup' ,0) 123 ); 124 case 'usergroup': 125 return array( 126 $this->pathItem('index' ,0) 127 ); 128 case 'user': 129 return array( 130 $this->pathItem('index' ,0 ), 131 $this->pathItem('usergroup' ,0), 132 $this->pathItem('userlist' ,0) 133 ); 134 case 'grouplist': 135 return array( 136 $this->pathItem('index' ,0 ), 137 $this->pathItem('usergroup' ,0) 138 ); 139 case 'group': 140 return array( 141 $this->pathItem('index' ,0 ), 142 $this->pathItem('usergroup' ,0), 143 $this->pathItem('grouplist' ,0), 144 ); 145 146 case 'templatelist': 147 case 'languagelist': 148 case 'modellist': 149 return array( 150 $this->pathItem('index' ,0 ), 151 $this->pathItem('projectlist' ,0 ), 152 $this->pathItem('project' ,$id) 153 ); 154 155 case 'template': 156 $t = new Template( $id ); 157 $t->load(); 158 159 return array( 160 $this->pathItem('index' ,0 ), 161 $this->pathItem('projectlist' ,0 ), 162 $this->pathItem('project' ,$t->projectid), 163 $this->pathItem('templatelist',$t->projectid) 164 ); 165 166 case 'element': 167 $e = new Element( $id ); 168 $e->load(); 169 $t = new Template( $e->templateid ); 170 $t->load(); 171 172 return array( 173 $this->pathItem('index' ,0 ), 174 $this->pathItem('projectlist' ,0 ), 175 $this->pathItem('project' ,$t->projectid ), 176 $this->pathItem('templatelist',$t->projectid ), 177 $this->pathItem('template' ,$t->templateid,$t->name) 178 ); 179 180 case 'language': 181 $l = new Language( $id ); 182 $l->load(); 183 184 return array( 185 $this->pathItem('index' ,0 ), 186 $this->pathItem('projectlist' ,0 ), 187 $this->pathItem('project' ,$l->projectid), 188 $this->pathItem('languagelist',$l->projectid) 189 ); 190 191 case 'model': 192 $m = new Model( $id ); 193 $m->load(); 194 195 return array( 196 $this->pathItem('index' ,0 ), 197 $this->pathItem('projectlist' ,0 ), 198 $this->pathItem('project' ,$m->projectid), 199 $this->pathItem('modellist' ,$m->projectid) 200 ); 201 202 default: 203 throw new \InvalidArgumentException('Unknown type: '.$type); 204 } 205 } 206 207 208 protected function pathItem($action, $id = 0, $name = '' ) { 209 return array('type'=>$this->typeToInternal($action),'action'=>$action ,'id'=>$id,'name'=>$name ); 210 } 211 212 213 private function typeToInternal($type) 214 { 215 switch( $type) { 216 217 case 'projectlist': 218 return 'projects'; 219 220 case 'userlist': 221 return 'users'; 222 223 case 'grouplist': 224 return 'groups'; 225 226 case 'templatelist': 227 return 'templates'; 228 229 case 'languagelist': 230 return 'languages'; 231 232 case 'modellist': 233 return 'models'; 234 235 default: 236 return $type; 237 } 238 239 } 240 241 /** 242 * @param $type 243 * @param $id 244 * @return string 245 */ 246 protected function calculateName($type, $id) 247 { 248 $o = ModelFactory::create($type, $id); 249 250 if ($o) { 251 $o->load(); 252 $name = $o->getName(); 253 } 254 else{ 255 //$name = \cms\base\Language::lang($type); 256 $name = ''; 257 } 258 return $name; 259 } 260 261 }