TemplateAddelAction.class.php (1696B)
1 <?php 2 namespace cms\action\template; 3 use cms\action\Action; 4 use cms\action\Method; 5 use cms\action\RequestParams; 6 use cms\action\TemplateAction; 7 use cms\model\Element; 8 use cms\model\Project; 9 use cms\model\Template; 10 use language\Messages; 11 12 13 class TemplateAddelAction extends TemplateAction implements Method { 14 public function view() { 15 // Die verschiedenen Element-Typen 16 $types = array(); 17 18 foreach( Element::getAvailableTypes() as $typeid => $t ) 19 { 20 $types[ $typeid ] = 'EL_'.$t; 21 } 22 23 // Code-Element nur fuer Administratoren (da voller Systemzugriff!) 24 if ( !$this->userIsAdmin() ) 25 unset( $types[Element::ELEMENT_TYPE_CODE] ); 26 27 // Auswahlmoeglichkeiten: 28 $this->setTemplateVar('types',$types); 29 30 // Vorbelegung: 31 $this->setTemplateVar('typeid',Element::ELEMENT_TYPE_TEXT); 32 } 33 public function post() { 34 35 $name = $this->request->getAlphanum('name'); 36 37 if ( empty($name) ) 38 throw new \util\exception\ValidationException('name'); 39 40 $newElement = $this->template->addElement( $name,$this->request->getText('description'),$this->request->getText('typeid') ); 41 42 if ( $this->request->isTrue('addtotemplate') ) 43 { 44 $project = new Project( $this->template->projectid); 45 $modelIds = $project->getModelIds(); 46 47 foreach( $modelIds as $modelId ) 48 { 49 $template = new Template( $this->template->templateid ); 50 $templateModel = $template->loadTemplateModelFor( $modelId ); 51 $templateModel->load(); 52 $templateModel->src .= "\n".'{{'.$newElement->name.'}}'; 53 $templateModel->persist(); 54 } 55 56 } 57 58 $this->addNoticeFor($this->template,Messages::SAVED); 59 } 60 }