openrat-cms

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

TemplateAction.class.php (16441B)


      1 <?php
      2 
      3 namespace cms\action;
      4 
      5 namespace cms\action;
      6 use cms\model\Acl;
      7 use cms\model\Element;
      8 use cms\model\Project;
      9 use cms\model\Template;
     10 use cms\model\Page;
     11 
     12 
     13 use cms\model\TemplateModel;
     14 use cms\publish\PublishPublic;
     15 use Session;
     16 use \Html;
     17 use \Text;
     18 
     19 // OpenRat Content Management System
     20 // Copyright (C) 2002-2009 Jan Dankert
     21 //
     22 // This program is free software; you can redistribute it and/or
     23 // modify it under the terms of the GNU General Public License
     24 // as published by the Free Software Foundation; either version 2
     25 // of the License, or (at your option) any later version.
     26 //
     27 // This program is distributed in the hope that it will be useful,
     28 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     29 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     30 // GNU General Public License for more details.
     31 //
     32 // You should have received a copy of the GNU General Public License
     33 // along with this program; if not, write to the Free Software
     34 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     35 
     36 /**
     37  * Action-Klasse zum Bearbeiten einer Seitenvorlage.
     38  * 
     39  * @author Jan Dankert
     40  * @package openrat.actions
     41  */
     42 
     43 class TemplateAction extends BaseAction
     44 {
     45 	public $security = Action::SECURITY_USER;
     46 	
     47 	var $defaultSubAction = 'show';
     48 
     49     /**
     50      * @var Template
     51      */
     52 	private $template;
     53 	private $element;
     54 
     55 
     56 	function __construct()
     57 	{
     58         parent::__construct();
     59 
     60     }
     61 
     62 
     63     public function init()
     64     {
     65 		$this->template = new Template( $this->getRequestId() );
     66 
     67 		$this->template->modelid = $this->request->getModelId();
     68 		$this->template->load();
     69 
     70 		$this->setTemplateVar( 'templateid',$this->template->templateid );
     71 		$this->setTemplateVar( 'modelid'   ,$this->template->modelid    );
     72 
     73 		if	( intval($this->getRequestVar('elementid')) != 0 )
     74 		{
     75 			$this->element = new Element( $this->getRequestVar('elementid') );
     76 			$this->element->load();
     77 			$this->setTemplateVar( 'elementid',$this->element->elementid );
     78 		}
     79 	}
     80 
     81 
     82     /**
     83      * Save the new template source into the database.
     84      */
     85 	public function srcPost()
     86 	{
     87         $modelId = $this->getRequestId('modelid');
     88 
     89         $templatemodel = new TemplateModel($this->template->templateid, $modelId);
     90         $templatemodel->load();
     91 
     92         $newSource = $this->request->getRequestVar('source',OR_FILTER_RAW);
     93 
     94         /*
     95         // Not useful any more. Technical name of a element should not be changed.
     96         foreach ($this->template->getElementNames() as $elid => $elname) {
     97             $newSource = str_replace('{{' . $elname . '}}', '{{' . $elid . '}}', $newSource);
     98             $newSource = str_replace('{{->' . $elname . '}}', '{{->' . $elid . '}}', $newSource);
     99             $newSource = str_replace('{{' . lang('TEMPLATE_SRC_IFEMPTY') . ':' . $elname . ':' . lang('TEMPLATE_SRC_BEGIN') . '}}', '{{IFEMPTY:' . $elid . ':BEGIN}}', $newSource);
    100             $newSource = str_replace('{{' . lang('TEMPLATE_SRC_IFEMPTY') . ':' . $elname . ':' . lang('TEMPLATE_SRC_END') . '}}', '{{IFEMPTY:' . $elid . ':END}}', $newSource);
    101             $newSource = str_replace('{{' . lang('TEMPLATE_SRC_IFNOTEMPTY') . ':' . $elname . ':' . lang('TEMPLATE_SRC_BEGIN') . '}}', '{{IFNOTEMPTY:' . $elid . ':BEGIN}}', $newSource);
    102             $newSource = str_replace('{{' . lang('TEMPLATE_SRC_IFNOTEMPTY') . ':' . $elname . ':' . lang('TEMPLATE_SRC_END') . '}}', '{{IFNOTEMPTY:' . $elid . ':END}}', $newSource);
    103         }
    104         */
    105 
    106         $templatemodel->src = $newSource;
    107 
    108         if ( !$templatemodel->isPersistent() )
    109             $templatemodel->add();
    110         else
    111             $templatemodel->save();
    112 
    113 		$this->addNotice('template',$this->template->name,'SAVED',OR_NOTICE_OK);
    114 	}
    115 
    116 
    117 
    118 
    119 	function srcelementPost()
    120 	{
    121 		$text = $this->template->src;
    122 
    123 		switch( $this->getRequestVar('type') )
    124 		{
    125 			case 'addelement':
    126 				$text .= "\n".'{{'.$this->getRequestVar('elementid').'}}';
    127 				break;
    128 		
    129 			case 'addicon':
    130 				$text .= "\n".'{{->'.$this->getRequestVar('writable_elementid').'}}';
    131 				break;
    132 
    133 			case 'addifempty':
    134 				$text .= "\n".'{{IFEMPTY:'.$this->getRequestVar('writable_elementid').':BEGIN}}  {{IFEMPTY:'.$this->getRequestVar('writable_elementid').':END}}';
    135 				break;
    136 
    137 			case 'addifnotempty':
    138 				$text .= "\n".'{{IFNOTEMPTY:'.$this->getRequestVar('writable_elementid').':BEGIN}}  {{IFNOTEMPTY:'.$this->getRequestVar('writable_elementid').':END}}';
    139 				break;
    140 		
    141 			default:
    142 				$this->addValidationError('type');
    143 				$this->callSubAction('srcelement');
    144 				return;
    145 		}
    146 		
    147 		$this->template->src = $text;
    148 
    149 		$this->template->save();
    150 		$this->template->load();
    151 
    152 		$this->addNotice('template',$this->template->name,'SAVED',OR_NOTICE_OK);
    153 	}
    154 
    155 
    156 	// Speichern der Template-Eigenschaftens
    157 	//
    158 	function propPost()
    159 	{
    160 		
    161 		if	($this->getRequestVar('name') == "")
    162 		{
    163 			$this->addValidationError('name');
    164 			$this->callSubAction('name');
    165 			return;
    166 		}
    167 		else
    168 		{
    169 			$this->template->name = $this->getRequestVar('name');
    170 			$this->template->save();
    171 			$this->addNotice('template',$this->template->name,'SAVED',OR_NOTICE_OK);
    172 		}
    173 	}
    174 
    175 
    176 	// Speichern der Template-Eigenschaftens
    177 	//
    178 	function removePost()
    179 	{
    180 		if   ( $this->getRequestVar('delete') != '' )
    181 		{
    182 			$this->template->delete();
    183 			$this->addNotice('template',$this->template->name,'DELETED',OR_NOTICE_OK);
    184 		}
    185 		else
    186 		{
    187 			$this->addNotice('template',$this->template->name,'CANCELED',OR_NOTICE_WARN);
    188 		}
    189 	}
    190 
    191 
    192 	/**
    193 	 * Entfernen der Vorlage
    194 	 */
    195 	function removeView()
    196 	{
    197 		$this->setTemplateVar('name',$this->template->name);
    198 	}
    199 
    200 
    201 	/**
    202 	 * Anzeigen aller Seiten der Vorlage.
    203 	 */
    204 	function infoView()
    205 	{
    206 		$pages = array();
    207 		$pageids = $this->template->getDependentObjectIds();
    208 		
    209 		foreach( $pageids as $pageid )
    210 		{
    211 			$page = new Page($pageid);
    212 			$page->load();
    213 			
    214 			$pages[$pageid] = $page->name;
    215 		}
    216 		
    217 		$this->setTemplateVar('pages',$pages);
    218 		$this->setTemplateVar('id'   ,$this->template->templateid);
    219 	}
    220 
    221 
    222 	/**
    223      * Speichern der Dateiendung
    224      */
    225 	public function extensionPost()
    226 	{
    227         $project = new Project( $this->template->projectid );
    228         $models = $project->getModels();
    229 
    230         $extensions = array();
    231         foreach( $models as $modelId => $modelName ) {
    232 
    233             $input = $this->getRequestVar( $modelName );
    234 
    235             // Validierung: Werte dürfen nicht doppelt vorkommen.
    236             if ( in_array($input, $extensions) )
    237             {
    238                 $this->addNotice('template',$this->template->name,'DUPLICATE_INPUT','error');
    239                 throw new \ValidationException( $modelName );
    240             }
    241 
    242             $extensions[ $modelId ] = $input;
    243         }
    244 
    245         foreach( $models as $modelId => $modelName ) {
    246 
    247             $templatemodel = new TemplateModel($this->template->templateid, $modelId);
    248             $templatemodel->load();
    249 
    250             $templatemodel->extension = $extensions[ $modelId ];
    251 
    252             if ( !$templatemodel->isPersistent() )
    253                 $templatemodel->add();
    254             else
    255                 $templatemodel->save();
    256         }
    257 
    258 		$this->addNotice('template',$this->template->name,'SAVED','ok');
    259 	}
    260 
    261 
    262 
    263 	function addelView()
    264 	{
    265 		// Die verschiedenen Element-Typen
    266 		$types = array();
    267 
    268 		foreach( Element::getAvailableTypes() as $typeid => $t )
    269 		{
    270 			$types[ $typeid ] = 'EL_'.$t;
    271 		}
    272 
    273 		// Code-Element nur fuer Administratoren (da voller Systemzugriff!)		
    274 		if	( !$this->userIsAdmin() )
    275 			unset( $types[Element::ELEMENT_TYPE_CODE] );
    276 
    277 		// Auswahlmoeglichkeiten:
    278 		$this->setTemplateVar('types',$types);
    279 
    280 		// Vorbelegung:
    281 		$this->setTemplateVar('typeid',Element::ELEMENT_TYPE_TEXT);
    282 	}
    283 	
    284 	
    285 	
    286 	/*
    287 	 * Neues Element hinzufuegen.
    288 	 */
    289 	function addelPost()
    290 	{
    291 
    292 		$name = $this->getRequestVar('name',OR_FILTER_ALPHANUM);
    293 
    294 		if  ( empty($name) )
    295 		    throw new \ValidationException('name');
    296 
    297 		$newElement = $this->template->addElement( $name,$this->getRequestVar('description'),$this->getRequestVar('typeid') );
    298 
    299 		if	( $this->hasRequestVar('addtotemplate') )
    300 		{
    301 		    $project  = new Project( $this->template->projectid);
    302 		    $modelIds = $project->getModelIds();
    303 
    304 		    foreach( $modelIds as $modelId )
    305             {
    306                 $template = new Template( $this->template->templateid );
    307                 $template->modelid = $modelId;
    308                 $template->load();
    309                 $template->src .= "\n".'{{'.$newElement->elementid.'}}';
    310                 $template->save();
    311             }
    312 
    313 		}
    314 
    315 		$this->addNotice('template',$this->template->name,'SAVED',OR_NOTICE_OK);
    316 	}
    317 
    318 
    319 	
    320 	/**
    321 	 * Eigenschaften einer Vorlage anzeigen
    322 	 */
    323 	function propView()
    324 	{
    325 		$this->setTemplateVar('name'     ,$this->template->name       );
    326 		$this->setTemplateVar('extension',$this->template->extension  );
    327 		$this->setTemplateVar('mime_type',$this->template->mimeType() );
    328 	}
    329 
    330 
    331 
    332 	/**
    333 	 * Extension einer Vorlage anzeigen
    334 	 */
    335 	function extensionView()
    336 	{
    337         $project = new Project( $this->template->projectid );
    338         $models = $project->getModels();
    339 
    340         $modelSrc = array();
    341 
    342         foreach( $models as $modelId => $modelName )
    343         {
    344             $templatemodel = new TemplateModel( $this->template->templateid, $modelId );
    345             $templatemodel->load();
    346 
    347             $modelSrc[ $modelId ] = array(
    348                 'name'     =>$modelName,
    349                 'extension'=>$templatemodel->extension
    350             );
    351         }
    352 
    353         $this->setTemplateVar( 'extension',$modelSrc );
    354 	}
    355 
    356 	
    357 	
    358 	/**
    359 	 * Anzeigen des Inhaltes, der Inhalt wird samt Header direkt
    360 	 * auf die Standardausgabe geschrieben
    361 	 */
    362 	function previewView()
    363 	{
    364 	    $project = new Project( $this->template->projectid);
    365 
    366         $this->setTemplateVar('models',$project->getModels() );
    367 
    368         if   ( ! $this->template->modelid )
    369             $this->template->modelid = $project->getDefaultModelId();
    370 
    371         $this->setTemplateVar('modelid'   ,$this->template->modelid );
    372 
    373 		$this->setTemplateVar('preview_url',Html::url('template','show',$this->template->templateid,array('target'=>'none','modelid'=>$this->template->modelid ) ) );
    374 	}
    375 	
    376 	
    377 
    378 
    379 	/**
    380 	 * Voransicht einer Vorlage
    381 	 */
    382 	function showView()
    383 	{
    384 		header('Content-Type: '.$this->template->mimeType().'; charset=UTF-8' );
    385 		$text = $this->template->src;
    386 	
    387 		foreach( $this->template->getElementIds() as $elid )
    388 		{
    389 			$element = new Element( $elid );
    390 			$element->load();
    391 			$url = Html::url( 'element','edit',$this->template->templateid,array('elementid'=>$elid));
    392 			
    393 			$text = str_replace('{{'.$elid.'}}',$element->name,
    394 			                    $text );
    395 			$text = str_replace('{{->'.$elid.'}}','',
    396 			                    $text );
    397 
    398 			$text = str_replace('{{IFEMPTY:'.$elid.':BEGIN}}','',
    399 			                    $text );
    400 			$text = str_replace('{{IFEMPTY:'.$elid.':END}}','',
    401 			                    $text );
    402 
    403 			$text = str_replace('{{IFNOTEMPTY:'.$elid.':BEGIN}}','',
    404 			                    $text );
    405 			$text = str_replace('{{IFNOTEMPTY:'.$elid.':END}}','',
    406 			                    $text );
    407 			                    
    408 			unset( $element );
    409 		}
    410 	
    411 		echo $text;
    412 		
    413 		exit();
    414 	}
    415 
    416 
    417 	/**
    418 	 * Bearbeiten einer Vorlage
    419 	 */
    420 	function editView()
    421 	{
    422 		// Elemente laden 
    423 		$list = array();
    424 	
    425 		foreach( $this->template->getElementIds() as $elid )
    426 		{
    427 			$element = new Element( $elid );
    428 			$element->load();
    429 
    430 			$list[$elid] = array();
    431 			$list[$elid]['id'         ] = $elid;
    432 			$list[$elid]['name'       ] = $element->name;
    433 			$list[$elid]['description'] = $element->desc;
    434 			$list[$elid]['type'       ] = $element->getTypeName();
    435 			$list[$elid]['typeid'     ] = $element->typeid;
    436 
    437 			unset( $element );
    438 		}
    439 		$this->setTemplateVar('elements',$list);	
    440 		
    441 		
    442         $project = new Project( $this->template->projectid );
    443 
    444 
    445         $models = array();
    446 
    447         foreach( $project->getModels() as $modelId => $modelName )
    448         {
    449             $templatemodel = new TemplateModel( $this->template->templateid, $modelId );
    450             $templatemodel->load();
    451 
    452             $text = $templatemodel->src;
    453 
    454             foreach( $this->template->getElementIds() as $elid )
    455             {
    456                 $element = new Element( $elid );
    457                 $element->load();
    458 
    459                 // Fix old stuff:
    460                 $text = str_replace('{{'.$elid.'}}',
    461                     '{{'.$element->name.'}}',
    462                     $text );
    463                 $text = str_replace('{{->'.$elid.'}}',
    464                     '{{goto.'.$element->name.'}}',
    465                     $text );
    466                 $text = str_replace('{{IFEMPTY:'.$elid.':BEGIN}}',
    467                     '{{^'.$element->name.'}}',
    468                     $text );
    469                 $text = str_replace('{{IFEMPTY:'.$elid.':END}}',
    470                     '{{/'.$element->name.'}}',
    471                     $text );
    472                 $text = str_replace('{{IFNOTEMPTY:'.$elid.':BEGIN}}',
    473                     '{{#'.$element->name.'}}',
    474                     $text );
    475                 $text = str_replace('{{IFNOTEMPTY:'.$elid.':END}}',
    476                     '{{/'.$element->name.'}}',
    477                     $text );
    478             }
    479 
    480             $models[ $modelId ] = array(
    481                 'name'    => $modelName,
    482                 'source'  => $text,
    483                 'modelid' => $modelId
    484             );
    485         }
    486 
    487         $this->setTemplateVar( 'models',$models );
    488 
    489 
    490     }
    491 
    492 
    493 	function srcelementView()
    494 	{
    495 		$elements           = array();
    496 		$writable_elements = array();
    497 	
    498 		foreach( $this->template->getElementIds() as $elid )
    499 		{
    500 			$element = new Element( $elid );
    501 			$element->load();
    502 
    503 			$elements[$elid] = $element->name;
    504 
    505 			if	( $element->isWritable() )
    506 				$writable_elements[$elid] = $element->name;
    507 		}
    508 
    509 		$this->setTemplateVar('elements'         ,$elements         );
    510 		$this->setTemplateVar('writable_elements',$writable_elements);
    511 	}
    512 	
    513 	
    514 	
    515 	/**
    516 	  * Anzeigen des Template-Quellcodes
    517 	  */
    518 	function srcView()
    519 	{
    520 	    $project = new Project( $this->template->projectid );
    521 	    $modelId = $this->getRequestId('modelid');
    522 
    523 	    $modelSrc = array();
    524 
    525         $templatemodel = new TemplateModel( $this->template->templateid, $modelId );
    526         $templatemodel->load();
    527 
    528         $text = $templatemodel->src;
    529 
    530         foreach( $this->template->getElementIds() as $elid )
    531         {
    532             $element = new Element( $elid );
    533             $element->load();
    534 
    535             // Fix old stuff:
    536             $text = str_replace('{{'.$elid.'}}',
    537                 '{{'.$element->name.'}}',
    538                 $text );
    539             $text = str_replace('{{->'.$elid.'}}',
    540                 '{{goto.'.$element->name.'}}',
    541                 $text );
    542             $text = str_replace('{{IFEMPTY:'.$elid.':BEGIN}}',
    543                 '{{^'.$element->name.'}}',
    544                 $text );
    545             $text = str_replace('{{IFEMPTY:'.$elid.':END}}',
    546                 '{{/'.$element->name.'}}',
    547                 $text );
    548             $text = str_replace('{{IFNOTEMPTY:'.$elid.':BEGIN}}',
    549                 '{{#'.$element->name.'}}',
    550                 $text );
    551             $text = str_replace('{{IFNOTEMPTY:'.$elid.':END}}',
    552                 '{{/'.$element->name.'}}',
    553                 $text );
    554         }
    555 
    556         $this->setTemplateVar( 'modelid',$modelId );
    557         $this->setTemplateVar( 'source' ,$text );
    558 
    559     }
    560 
    561 
    562 	// Anzeigen aller Templates
    563 	//
    564 	function listingView()
    565 	{
    566 		global $conf_php;
    567 
    568 		$list = array();
    569 
    570         $project = new Project( $this->template->projectid );
    571 
    572 		foreach( $project->getTemplates() as $id=>$name )
    573 		{
    574 			$list[$id] = array();
    575 			$list[$id]['name'] = $name;
    576 			$list[$id]['url' ] = Html::url('template','el',$id,array());
    577 		}
    578 		
    579 //		$var['templatemodelid'] = htmlentities( $id   );
    580 //		$var['text']            = htmlentities( $text );
    581 		$this->setTemplateVar('templates',$list);
    582 	}
    583 
    584 	
    585 	/**
    586 	 * Anzeigen der Maske zum Veröffentlichen.
    587 	 */
    588 	public function pubView()
    589 	{
    590 		
    591 	}
    592 	
    593 	
    594 	
    595 	/**
    596 	 * Veröffentlichen.
    597 	 */
    598 	public function pubPost()
    599 	{
    600 		$objectIds = $this->template->getDependentObjectIds();
    601 
    602 		Session::close();
    603 
    604 		$publisher = new PublishPublic( $this->template->projectid );
    605 		
    606 		foreach( $objectIds as $objectid )
    607 		{
    608 			$page = new Page( $objectid );
    609 			$page->load();
    610 			
    611 			if	( !$page->hasRight( Acl::ACL_PUBLISH ) )
    612 				continue;
    613 			
    614 			$page->publisher = $publisher;
    615 			$page->publish();
    616         }
    617 
    618         $this->addNotice( 'template',
    619             $this->template->name,
    620             'PUBLISHED',
    621             OR_NOTICE_OK,
    622             array(),
    623             array_map( function($obj) {
    624                 return $obj['full_filename'];
    625             },$publisher->publishedObjects) );
    626 
    627         $publisher->close();
    628     }
    629 	
    630 	
    631 
    632 }