File modules/cms/action/templatelist/TemplatelistAddAction.class.php
Last commit: Mon Jun 13 22:00:06 2022 +0200 Jan Dankert Fix: Element must be copied if the template is copied.
1 <?php 2 namespace cms\action\templatelist; 3 use cms\action\Method; 4 use cms\action\TemplatelistAction; 5 use cms\model\Element; 6 use cms\model\Template; 7 use language\Messages; 8 9 class TemplatelistAddAction extends TemplatelistAction implements Method { 10 11 12 public function view() { 13 $this->setTemplateVar( 'templates',$this->project->getTemplates() ); 14 $this->setTemplateVar( 'copytemplateid','' ); 15 16 /* 17 $examples = array(); 18 $dir = opendir( 'examples/templates'); 19 while( $file = readdir($dir) ) 20 { 21 if ( substr($file,0,1) != '.') 22 { 23 $examples[$file] = $file; 24 } 25 } 26 27 $this->setTemplateVar( 'examples',$examples ); 28 $this->setTemplateVar( 'example','' ); 29 */ 30 31 } 32 33 34 public function post() { 35 36 $name = $this->request->getText('name'); 37 38 // create a new template. 39 $template = new Template(); 40 $template->projectid = $this->project->projectid; 41 $template->name = $name; 42 $template->persist(); 43 44 $this->addNoticeFor($template, Messages::ADDED); 45 46 $copytemplateid = $this->request->getNumber('copytemplateid'); 47 if ( $copytemplateid ) { 48 49 // Template kopieren. 50 $copyTemplate = new Template( $copytemplateid ); 51 $copyTemplate->load(); 52 53 // Copy all elements 54 foreach( $copyTemplate->getElements() as $element ) 55 { 56 /* @type $element Element */ 57 $element->load(); 58 $element->elementid = null; // we want to copy this element 59 60 $element->templateid = $template->templateid; 61 $element->persist(); 62 } 63 64 // copy all template models 65 foreach( $this->project->getModelIds() as $modelid ) 66 { 67 // Template laden 68 $copyTemplate->load(); 69 70 $copyTemplateModel = $copyTemplate->loadTemplateModelFor( $modelid ); 71 72 $newTemplateModel = $template->loadTemplateModelFor( $modelid ); 73 $newTemplateModel->src = $copyTemplateModel->src; 74 $newTemplateModel->extension = $copyTemplateModel->extension; 75 $newTemplateModel->persist(); 76 } 77 78 $this->addNoticeFor( $copyTemplate, Messages::COPIED); 79 80 /* 81 case 'example': 82 83 // Neues Template anlegen. 84 $template = new Template(); 85 $template->projectid = $this->project->projectid; 86 87 $template->add( $this->request->getRequestVar('name') ); 88 89 $templateModel = $template->loadTemplateModelFor( $this->project->getDefaultModelId() ); 90 91 // FIXME 92 $example = parse_ini_file('examples/templates/'.$this->request->getRequestVar('example'),true); 93 94 foreach( $example as $exampleKey=>$exampleElement ) 95 { 96 if ( !is_array($exampleElement) ) 97 { 98 $template->$exampleKey = $exampleElement; 99 } 100 else 101 { 102 $element = new Element(); 103 $element->templateid = $template->templateid; 104 $element->name = $exampleKey; 105 $element->writable = true; 106 $element->add(); 107 108 foreach( $exampleElement as $ePropName=>$ePropValue) 109 $element->$ePropName = $ePropValue; 110 111 $element->defaultText = str_replace(';',"\n",$element->defaultText); 112 $element->save(); 113 } 114 } 115 $template->name = $this->request->getRequestVar('name'); 116 $templateModel->src = str_replace(';',"\n",$templateModel->src); 117 118 foreach( $template->getElementNames() as $elid=>$elname ) 119 { 120 $templateModel->src = str_replace('{{'.$elname.'}}' ,'{{'.$elid.'}}' ,$templateModel->src ); 121 $templateModel->src = str_replace('{{->'.$elname.'}}','{{->'.$elid.'}}',$templateModel->src ); 122 } 123 124 $template->save(); 125 $templateModel->save(); 126 127 $this->addNotice('template', 0, $template->name, 'ADDED', 'ok'); 128 129 break; 130 */ 131 } 132 133 } 134 }
Downloadmodules/cms/action/templatelist/TemplatelistAddAction.class.php
History Mon, 13 Jun 2022 22:00:06 +0200 Jan Dankert Fix: Element must be copied if the template is copied. Fri, 26 Feb 2021 01:06:01 +0100 Jan Dankert Refactoring accessing the request parameter values. Wed, 18 Nov 2020 01:46:36 +0100 Jan Dankert Refactoring of model classes: New method persist() and some other cleanups. Tue, 17 Nov 2020 23:51:00 +0100 Jan Dankert Refactoring: Every Actionmethod has now its own class.