TemplatelistAction.class.php (5914B)
1 <?php 2 3 namespace cms\action; 4 5 use cms\model\Element; 6 use cms\model\Project; 7 use cms\model\Template; 8 use Session; 9 10 // OpenRat Content Management System 11 // Copyright (C) 2002-2009 Jan Dankert 12 // 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of the GNU General Public License 15 // as published by the Free Software Foundation; either version 2 16 // of the License, or (at your option) any later version. 17 // 18 // This program is distributed in the hope that it will be useful, 19 // but WITHOUT ANY WARRANTY; without even the implied warranty of 20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 // GNU General Public License for more details. 22 // 23 // You should have received a copy of the GNU General Public License 24 // along with this program; if not, write to the Free Software 25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 26 27 /** 28 * Action-Klasse zum Bearbeiten einer Seitenvorlage. 29 * 30 * @author Jan Dankert 31 * @package openrat.actions 32 */ 33 34 class TemplatelistAction extends BaseAction 35 { 36 public $security = Action::SECURITY_USER; 37 38 /** 39 * @var Project 40 */ 41 private $project; 42 43 44 function __construct() 45 { 46 parent::__construct(); 47 48 } 49 50 51 public function init() 52 { 53 $this->project = new Project( $this->request->getRequestId()); 54 } 55 56 57 58 /** 59 * Bearbeiten einer Vorlage 60 */ 61 function editView() 62 { 63 $this->nextSubAction('show'); 64 } 65 66 67 68 // Anzeigen aller Templates 69 // 70 function showView() 71 { 72 $list = array(); 73 74 foreach( $this->project->getTemplates() as $id=>$name ) 75 { 76 $list[$id] = array(); 77 $list[$id]['name'] = $name; 78 $list[$id]['id' ] = $id; 79 } 80 81 $this->setTemplateVar('templates',$list); 82 } 83 84 85 86 /** 87 * Vorlage hinzuf�gen. 88 */ 89 function addView() 90 { 91 $this->setTemplateVar( 'templates',array() /*Template::getAll()*/ ); 92 93 $examples = array(); 94 $dir = opendir( 'examples/templates'); 95 while( $file = readdir($dir) ) 96 { 97 if ( substr($file,0,1) != '.') 98 { 99 $examples[$file] = $file; 100 } 101 } 102 103 $this->setTemplateVar( 'examples',$examples ); 104 $this->setTemplateVar( 'templateid','' ); 105 $this->setTemplateVar( 'example','' ); 106 } 107 108 109 110 function addPost() 111 { 112 // Hinzufuegen eines Templates 113 if ( $this->getRequestVar('name') == '' ) 114 throw new \ValidationException('name'); 115 116 // Hinzufuegen eines Templates 117 switch( $this->getRequestVar('type') ) 118 { 119 case 'empty': 120 121 // Neues Template anlegen. 122 $template = new Template(); 123 $template->projectid = $this->project->projectid; 124 $template->add( $this->getRequestVar('name') ); 125 $this->addNotice('template',$template->name,'ADDED','ok'); 126 break; 127 128 case 'copy': 129 130 $copy_templateid = intval($this->getRequestVar('templateid') ); 131 132 if ( $copy_templateid == 0 ) 133 { 134 $this->addValidationError('templateid'); 135 return; 136 } 137 138 // Neues Template anlegen. 139 $template = new Template(); 140 $template->projectid = $this->project->projectid; 141 $template->add( $this->getRequestVar('name') ); 142 $this->addNotice('template',$template->name,'ADDED','ok'); 143 144 // Template kopieren. 145 $copy_template = new Template( $copy_templateid ); 146 $copy_template->load(); 147 $elementMapping = array(); 148 foreach( $copy_template->getElements() as $element ) 149 { 150 /* @type $element Element */ 151 $element->load(); 152 $oldelementId = $element->elementid; 153 $element->templateid = $template->templateid; 154 $element->add(); 155 $element->save(); 156 157 $elementMapping[$oldelementId] = $element->elementid; 158 } 159 160 $project = new Project( $this->getRequestId('projectid') ); 161 foreach( $project->getModelIds() as $modelid ) 162 { 163 // Template laden 164 $copy_template->modelid = $modelid; 165 $copy_template->load(); 166 167 $template->modelid = $modelid; 168 $src = $copy_template->src; 169 170 // Elemente im Quelltext an die geänderten Element-Idn anpassen. 171 foreach( $elementMapping as $oldId=>$newId) 172 $src = str_replace('{{'.$oldId.'}}','{{'.$newId.'}}',$src); 173 174 $template->src = $src; 175 $template->extension = $copy_template->extension; 176 $template->save(); 177 } 178 179 $this->addNotice('template',$copy_template->name,'COPIED','ok'); 180 181 break; 182 183 case 'example': 184 185 // Neues Template anlegen. 186 $template = new Template(); 187 $template->projectid = $this->project->projectid; 188 189 $template->add( $this->getRequestVar('name') ); 190 191 $example = parse_ini_file('examples/templates/'.$this->getRequestVar('example'),true); 192 193 foreach( $example as $exampleKey=>$exampleElement ) 194 { 195 if ( !is_array($exampleElement) ) 196 { 197 $template->$exampleKey = $exampleElement; 198 } 199 else 200 { 201 $element = new Element(); 202 $element->templateid = $template->templateid; 203 $element->name = $exampleKey; 204 $element->writable = true; 205 $element->add(); 206 207 foreach( $exampleElement as $ePropName=>$ePropValue) 208 $element->$ePropName = $ePropValue; 209 210 $element->defaultText = str_replace(';',"\n",$element->defaultText); 211 $element->save(); 212 // Html::debug($element,"Element"); 213 } 214 } 215 // Html::debug($template,"Template"); 216 $template->name = $this->getRequestVar('name'); 217 $template->src = str_replace(';',"\n",$template->src); 218 219 foreach( $template->getElementNames() as $elid=>$elname ) 220 { 221 $template->src = str_replace('{{'.$elname.'}}' ,'{{'.$elid.'}}' ,$template->src ); 222 $template->src = str_replace('{{->'.$elname.'}}','{{->'.$elid.'}}',$template->src ); 223 } 224 225 $template->save(); 226 $this->addNotice('template',$template->name,'ADDED','ok'); 227 228 break; 229 default: 230 $this->addValidationError('type'); 231 $this->callSubAction('add'); 232 return; 233 } 234 235 236 $this->setTemplateVar('tree_refresh',true); 237 } 238 239 240 }