openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Page.class.php (8426B)


      1 <?php
      2 namespace cms\model;
      3 // OpenRat Content Management System
      4 // Copyright (C) 2002-2012 Jan Dankert, cms@jandankert.de
      5 //
      6 // This program is free software; you can redistribute it and/or
      7 // modify it under the terms of the GNU General Public License
      8 // as published by the Free Software Foundation; either version 2
      9 // of the License, or (at your option) any later version.
     10 //
     11 // This program is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 //
     16 // You should have received a copy of the GNU General Public License
     17 // along with this program; if not, write to the Free Software
     18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     19 use cms\base\DB as Db;
     20 use cms\generator\PageContext;
     21 use phpseclib\Math\BigInteger;
     22 
     23 
     24 /**
     25  * Darstellen einer Seite
     26  *
     27  * @author Jan Dankert
     28  * @package openrat.objects
     29  */
     30 
     31 class Page extends BaseObject
     32 {
     33 	/**
     34 	 * Page-Id
     35 	 *
     36 	 * Hint: This is not the object-id!
     37 	 *
     38 	 * @var Integer
     39 	 */
     40 	public $pageid;
     41 
     42 	/**
     43 	 * Template-Id.
     44 	 *
     45 	 * The id of the underlying template. Every page must have a template.
     46 	 *
     47 	 * @var Integer
     48 	 */
     49 	public $templateid;
     50 
     51 
     52 	function __construct( $objectid='' )
     53 	{
     54 		parent::__construct( $objectid );
     55 		$this->isPage = true;
     56 		$this->typeid = BaseObject::TYPEID_PAGE;
     57     }
     58 
     59 
     60 
     61 	/**
     62 	  * Ermitteln aller Eigenschaften
     63 	  *
     64 	  * @return array
     65 	  */
     66 	function getProperties()
     67 	{
     68 		return array_merge( parent::getProperties(),
     69 		                    array('full_filename'=>'',
     70 		                          'pageid'       =>$this->pageid,
     71 		                          'templateid'   =>$this->templateid) );
     72 	}
     73 
     74 
     75 	/**
     76 	 * Eine Seite hinzufuegen
     77 	 */
     78 	protected function add()
     79 	{
     80 		parent::add(); // Hinzuf?gen von Objekt (dabei wird Objekt-ID ermittelt)
     81 
     82 		$sql = Db::sql('SELECT MAX(id) FROM {{page}}');
     83 		$this->pageid = intval($sql->getOne())+1;
     84 
     85 		$sql = Db::sql(<<<SQL
     86 	INSERT INTO {{page}}
     87 	            (id,objectid,templateid)
     88 	       VALUES( {pageid},{objectid},{templateid} )
     89 SQL
     90 		);
     91 		$sql->setInt   ('pageid'    ,$this->pageid     );
     92 		$sql->setInt   ('objectid'  ,$this->objectid   );
     93 		$sql->setInt   ('templateid',$this->templateid );
     94 
     95 		$sql->execute();
     96 	}
     97 
     98 
     99 	/**
    100 	  * Seite laden
    101 	  */
    102 	public function load()
    103 	{
    104 		$sql  = Db::sql( 'SELECT * FROM {{page}} '.
    105 		                 '  WHERE objectid={objectid}' );
    106 		$sql->setInt('objectid',$this->objectid);
    107 		$row = $sql->getRow();
    108 
    109 		if   ( count($row)==0 )
    110 			throw new \util\exception\ObjectNotFoundException("Page with Id $this->objectid not found.");
    111 
    112 		$this->pageid      = $row['id'        ];
    113 		$this->templateid  = $row['templateid'];
    114 
    115 		parent::load();
    116 	}
    117 
    118 
    119 	public function delete()
    120 	{
    121 		$this->deleteContent();
    122 		// Delete the page
    123 		$sql = DB::sql( <<<SQL
    124 			DELETE FROM {{page}}
    125 			 WHERE objectid={objectid}
    126 SQL
    127 		);
    128 		$sql->setInt('objectid',$this->objectid);
    129 		$sql->execute();
    130 
    131 		parent::delete();
    132 	}
    133 
    134 
    135 	/**
    136 	 * Copy another page.
    137 	 *
    138 	 * Copies the contents of another page and merge it into this one.
    139 	 *
    140 	 * @param $otherpageid integer ID of the page which should be copied
    141 	 */
    142 	function copyValuesFromPage( $otherpageid )
    143 	{
    144 		$this->load();
    145 
    146 		foreach( $this->getElementIds() as $elementid )
    147 		{
    148 			$project = $this->getProject();
    149 			foreach( $project->getLanguages() as $lid=>$lname )
    150 			{
    151 				$pageContent = new PageContent();
    152 				$pageContent->pageId     = $this->pageid;
    153 				$pageContent->languageid = $lid;
    154 				$pageContent->elementId  = $elementid;
    155 				$pageContent->load();
    156 
    157 				$value = new Value();
    158 				$value->contentid = $pageContent->contentId;
    159 				$value->load();
    160 
    161 				$otherPageContent = new PageContent();
    162 				$otherPageContent->pageId     = $this->pageid;
    163 				$otherPageContent->languageid = $lid;
    164 				$otherPageContent->elementId  = $elementid;
    165 				$otherPageContent->load();
    166 
    167 				if   ( $otherPageContent->isPersistent() ) {
    168 
    169 					$value = new Value();
    170 					$value->contentid = $otherPageContent->contentId;
    171 					$value->load();
    172 
    173 					if ( $value->isPersistent() ) {
    174 						if   ( !$pageContent->isPersistent() )
    175 							$pageContent->persist();
    176 
    177 						$value->contentid = $pageContent->contentId;
    178 						$value->persist();
    179 					}
    180 				}
    181 			}
    182 
    183 		}
    184 	}
    185 
    186 
    187 	public function save()
    188 	{
    189 		$db = \cms\base\DB::get();
    190 
    191 		$sql = $db->sql('UPDATE {{page}}'.
    192 		               '  SET templateid ={templateid}'.
    193 		               '  WHERE objectid={objectid}' );
    194 		$sql->setInt('templateid' ,$this->templateid);
    195 		$sql->setInt('objectid'   ,$this->objectid  );
    196 		$sql->execute();
    197 
    198 		parent::save();
    199 	}
    200 
    201 
    202 	
    203 	public function replaceTemplate( $newTemplateId,$replaceElementMap )
    204 	{
    205 		$oldTemplateId = $this->templateid;
    206 
    207 		$db = \cms\base\DB::get();
    208 
    209 		// Template-id dieser Seite aendern
    210 		$this->templateid = $newTemplateId;
    211 
    212 		$sql = $db->sql('UPDATE {{page}}'.
    213 		               '  SET templateid ={templateid}'.
    214 		               '  WHERE objectid={objectid}' );
    215 		$sql->setInt('templateid' ,$this->templateid);
    216 		$sql->setInt('objectid'   ,$this->objectid  );
    217 		$sql->execute();
    218 
    219 
    220 		// Inhalte umschluesseln, d.h. die Element-Ids aendern
    221 		$template = new Template( $oldTemplateId );
    222 		foreach( $template->getElementIds() as $oldElementId )
    223 		{
    224 			if	( !isset($replaceElementMap[$oldElementId])  ||
    225 			      intval($replaceElementMap[$oldElementId]) < 1 )
    226 			{
    227 				\logger\Logger::debug( 'deleting value of elementid '.$oldElementId );
    228 				$sql = $db->sql('DELETE FROM {{value}}'.
    229 				               '  WHERE pageid={pageid}'.
    230 				               '    AND elementid={elementid}' );
    231 				$sql->setInt('pageid'   ,$this->pageid);
    232 				$sql->setInt('elementid',$oldElementId      );
    233 				
    234 				$sql->execute();
    235 			}
    236 			else
    237 			{
    238 				$newElementId = intval($replaceElementMap[$oldElementId]);
    239 
    240 				\logger\Logger::debug( 'updating elementid '.$oldElementId.' -> '.$newElementId );
    241 				$sql = $db->sql('UPDATE {{value}}'.
    242 				               '  SET elementid ={newelementid}'.
    243 				               '  WHERE pageid   ={pageid}'.
    244 				               '    AND elementid={oldelementid}' );
    245 				$sql->setInt('pageid'      ,$this->pageid);
    246 				$sql->setInt('oldelementid',$oldElementId      );
    247 				$sql->setInt('newelementid',$newElementId      );
    248 				$sql->execute();
    249 			}
    250 		}
    251 	}
    252 
    253 
    254 
    255 
    256 	/**
    257 	  * Get all elements from this page.
    258 	 * @return array
    259 	 */
    260 	public function getElementIds()
    261 	{
    262 		$t = new Template( $this->templateid );
    263 		
    264 		return $t->getElementIds();
    265 	}
    266 
    267 
    268 	/**
    269 	 * Gets the template.
    270 	 *
    271 	 * @return Template
    272 	 */
    273 	public function getTemplate() {
    274 		return new Template( $this->templateid );
    275 	}
    276 
    277 
    278 
    279 	/**
    280 	 * Ermittelt den Mime-Type zu dieser Seite
    281 	 *
    282 	 * @return String Mime-Type
    283 	 * @deprecated this is model-dependant! Use the same method in PageGenerator.
    284 	 */
    285 	function mimeType()
    286 	{
    287 		$templateModel = new TemplateModel( $this->templateid,$this->getProject()->getDefaultModelId() );
    288 		$templateModel->load();
    289 
    290 		$this->mime_type = $templateModel->mimeType();
    291 			
    292 		return( $this->mime_type );
    293 	}
    294 
    295 	
    296 	
    297 	/**
    298 	 * Returns a page with default values.
    299 	 *
    300 	 * If a value is empty, then the value should be loaded from this referenced object.
    301 	 *
    302 	 * @return Page|null
    303 	 */
    304 	public function getPageAsDefault() {
    305 
    306 		$defaultObjectId = $this->getPageIdForDefault();
    307 
    308 		if   ( $defaultObjectId ) {
    309 			$page = new Page( $defaultObjectId );
    310 			return $page;
    311 		}
    312 
    313 		return null;
    314 	}
    315 
    316 	/**
    317 	 * Returns a pageid with default values.
    318 	 *
    319 	 * If a value is empty, then the value should be loaded from this referenced object.
    320 	 *
    321 	 * @return int|null
    322 	 */
    323 	public function getPageIdForDefault() {
    324 
    325 		$settings = $this->getTotalSettings();
    326 		return @$settings['copy-default-values-from'];
    327 	}
    328 
    329 
    330 
    331     public function __toString()
    332     {
    333     	return 'Page#'.$this->pageid.' (filename='.$this->filename.', templateid='.$this->templateid.')';
    334     }
    335 
    336 
    337 	/**
    338 	 * Deletes all content of the page
    339 	 */
    340 	private function deleteContent()
    341 	{
    342 		// Delete all page contents.
    343 		$project = $this->getProject();
    344 		$languageIds = $project->getLanguageIds();
    345 		$elementIds  = $this->getElementIds();
    346 
    347 		foreach( $languageIds as $languageId )
    348 			foreach ( $elementIds as $elementId ) {
    349 				$pageContent = new PageContent();
    350 				$pageContent->pageId     = $this->pageid;
    351 				$pageContent->elementId  = $elementId;
    352 				$pageContent->languageid = $languageId;
    353 				$pageContent->load();
    354 				$pageContent->delete();
    355 			}
    356 	}
    357 }