3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2012 The Nucleus Group
\r
6 * This program is free software; you can redistribute it and/or
\r
7 * modify it under the terms of the GNU General Public License
\r
8 * as published by the Free Software Foundation; either version 2
\r
9 * of the License, or (at your option) any later version.
\r
10 * (see nucleus/documentation/index.html#license for more info)
\r
13 * Class representing a skin
\r
15 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
16 * @copyright Copyright (C) 2002-2009 The Nucleus Group
\r
17 * @version $Id: SKIN.php 1727 2012-04-07 02:54:22Z sakamocchi $
\r
20 if ( !function_exists('requestVar') )
\r
27 // after creating a SKIN object, evaluates to true when the skin exists
\r
30 // skin characteristics. Use the getXXX methods rather than accessing directly
\r
32 private $description;
\r
33 private $contentType;
\r
34 private $includeMode; // either 'normal' or 'skindir'
\r
35 private $includePrefix;
\r
40 * whether to be used by Admin class or not
\r
41 * the other way is preferrable instead of this
\r
42 * for example, adding admindir to normal/skindir in nucleus_skin_desc.sdincmode
\r
45 private $admin = FALSE;
\r
48 * Skin::__construct()
\r
49 * Constructor for a new SKIN object
\r
51 * @param integer $id id of the skin
\r
52 * @param boolean $admin for admin pages or not
\r
55 public function __construct($id, $admin=FALSE)
\r
59 $this->id = (integer) $id;
\r
61 // read skin name/description/content type
\r
62 $query = "SELECT * FROM %s WHERE sdnumber=%d";
\r
63 $query = sprintf($query, sql_table('skin_desc'), (integer) $this->id);
\r
64 $res = sql_query($query);
\r
65 $obj = sql_fetch_object($res);
\r
66 $this->valid = (sql_num_rows($res) > 0);
\r
67 if ( !$this->valid )
\r
72 $this->name = $obj->sdname;
\r
73 $this->description = $obj->sddesc;
\r
74 $this->contentType = $obj->sdtype;
\r
75 $this->includeMode = $obj->sdincmode;
\r
76 $this->includePrefix = $obj->sdincpref;
\r
78 * FIXME: this key should be replaced by other way
\r
79 * such that adding admin key into skin table and so on
\r
83 if ( !class_exists('Actions', FALSE) )
\r
85 include("{$DIR_LIBS}ACTIONS.php");
\r
87 $this->admin = FALSE;
\r
91 if ( !class_exists('AdminActions', FALSE) )
\r
93 include("{$DIR_LIBS}AdminActions.php");
\r
95 $this->admin = TRUE;
\r
106 * @return integer id for this skin instance
\r
108 public function getID()
\r
110 return (integer) $this->id;
\r
119 public function isValid()
\r
121 return (boolean) $this->valid;
\r
129 * @return string name of this skin instance
\r
131 public function getName()
\r
133 return (string) $this->name;
\r
137 * Skin::getDescription()
\r
138 * Get SKIN description
\r
141 * @return string description of this skin instance
\r
143 public function getDescription()
\r
145 return (string) $this->description;
\r
149 * Skin::getContentType()
\r
150 * Get SKIN content type
\r
151 * e.g. text/xml, text/html, application/atom+xml
\r
154 * @return string name of this skin instance
\r
156 public function getContentType()
\r
158 return (string) $this->contentType;
\r
162 * Skin::getIncludeMode()
\r
163 * Get include mode of the SKIN
\r
165 * Returns either 'normal' or 'skindir':
\r
166 * 'normal': if a all data of the skin can be found in the databse
\r
167 * 'skindir': if the skin has data in the it's skin driectory
\r
170 * @return string normal/skindir
\r
172 public function getIncludeMode()
\r
174 return (string) $this->includeMode;
\r
178 * Skin::getIncludePrefix()
\r
179 * Get include prefix of the SKIN
\r
181 * Get name of the subdirectory (with trailing slash) where
\r
182 * the files of the current skin can be found (e.g. 'default/')
\r
185 * @return string include prefix of this skin instance
\r
187 public function getIncludePrefix()
\r
189 return (string) $this->includePrefix;
\r
194 * Checks if a skin with a given shortname exists
\r
197 * @param string $name Skin short name
\r
198 * @return integer number of skins with the given ID
\r
200 static public function exists($name)
\r
202 $query = "SELECT COUNT (*) AS result FROM %s WHERE sdname='%s';";
\r
203 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));
\r
204 return (quickQuery($query) > 0);
\r
209 * Checks if a skin with a given ID exists
\r
212 * @param string $id Skin ID
\r
213 * @return integer number of skins with the given ID
\r
215 static public function existsID($id)
\r
217 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";
\r
218 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
\r
219 return (quickQuery($query) > 0);
\r
223 * Skin::createFromName()
\r
224 * Returns a skin given its shortname
\r
227 * @param string $name Skin shortname
\r
228 * @return object instance of Skin class
\r
230 static public function createFromName($name)
\r
232 return new SKIN(SKIN::getIdFromName($name));
\r
236 * Skin::getIdFromName()
\r
237 * Returns a skin ID given its shortname
\r
240 * @param string $name Skin shortname
\r
241 * @return integer Skin ID
\r
243 static public function getIdFromName($name)
\r
245 $query = "SELECT sdnumber FROM %s WHERE sdname='%s';";
\r
246 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));
\r
247 $res = sql_query($query);
\r
248 $obj = sql_fetch_object($res);
\r
249 return $obj->sdnumber;
\r
253 * Skin::getNameFromId()
\r
254 * Returns a skin shortname given its ID
\r
257 * @param string $name
\r
258 * @return string Skin short name
\r
260 static public function getNameFromId($id)
\r
262 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";
\r
263 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
\r
264 return quickQuery($query);
\r
268 * SKIN::createNew()
\r
269 * Creates a new skin, with the given characteristics.
\r
272 * @param String $name value for nucleus_skin.sdname
\r
273 * @param String $desc value for nucleus_skin.sddesc
\r
274 * @param String $type value for nucleus_skin.sdtype
\r
275 * @param String $includeMode value for nucleus_skin.sdinclude
\r
276 * @param String $includePrefix value for nucleus_skin.sdincpref
\r
277 * @return Integer ID for just inserted record
\r
279 public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
\r
287 'description' => &$desc,
\r
289 'includeMode' => &$includeMode,
\r
290 'includePrefix' => &$includePrefix
\r
294 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES ('%s', '%s', '%s', '%s', '%s');";
\r
295 $sdname = sql_real_escape_string($name);
\r
296 $sddesc = sql_real_escape_string($desc);
\r
297 $sdtype = sql_real_escape_string($type);
\r
298 $sdincmode = sql_real_escape_string($includeMode);
\r
299 $sdincpref = sql_real_escape_string($includePrefix);
\r
300 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);
\r
302 $newid = sql_insert_id();
\r
307 'skinid' => $newid,
\r
309 'description' => $desc,
\r
311 'includeMode' => $includeMode,
\r
312 'includePrefix' => $includePrefix
\r
322 * @param string $type
\r
325 public function parse($type)
\r
327 global $currentSkinName, $manager, $CONF;
\r
329 if ( !$this->admin )
\r
331 $manager->notify('InitSkinParse',array('skin' => &$this, 'type' => $type));
\r
335 $manager->notify('InitAdminSkinParse',array('skin' => &$this, 'type' => $type));
\r
339 sendContentType($this->getContentType(), 'skin');
\r
341 // set skin name as global var (so plugins can access it)
\r
342 $currentSkinName = $this->getName();
\r
343 $contents = $this->getContent($type);
\r
347 // use base skin if this skin does not have contents
\r
348 $defskin = new SKIN($CONF['BaseSkin']);
\r
349 $contents = $defskin->getContent($type);
\r
357 $actions = $this->getAllowedActionsForType($type);
\r
359 if ( !$this->admin )
\r
361 $manager->notify('PreSkinParse',array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
\r
365 $manager->notify('PreAdminSkinParse',array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
\r
368 // set IncludeMode properties of parser
\r
369 Parser::setProperty('IncludeMode', $this->getIncludeMode());
\r
370 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
\r
372 if ( !$this->admin )
\r
374 $handler = new Actions($type);
\r
378 $handler = new AdminActions($type);
\r
381 $parser = new Parser($actions, $handler);
\r
382 $handler->setParser($parser);
\r
383 $handler->setSkin($this);
\r
384 $parser->parse($contents);
\r
386 if ( !$this->admin )
\r
388 $manager->notify('PostSkinParse',array('skin' => &$this, 'type' => $type));
\r
392 $manager->notify('PostSkinParse',array('skin' => &$this, 'type' => $type));
\r
398 * Skin::getContent()
\r
399 * Get content of the skin part from the database
\r
401 * @param string $type type of the skin (e.g. index, item, search ...)
\r
402 * @return string content of scontent
\r
404 public function getContent($type)
\r
406 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype='%s';";
\r
407 $query = sprintf($query, sql_table('skin'), (integer) $this->id, sql_real_escape_string($type));
\r
408 $res = sql_query($query);
\r
410 if ( sql_num_rows($res) == 0 )
\r
414 return sql_result($res, 0, 0);
\r
419 * Updates the contents for one part of the skin in the database
\r
421 * @param string $type type of the skin part (e.g. index, item, search ...)
\r
422 * @param string $content new content for this skin part
\r
426 public function update($type, $content)
\r
430 $query = "SELECT sdesc FROM %s WHERE stype='%s' and sdesc=%d;";
\r
431 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);
\r
432 $res = sql_query($query);
\r
434 $skintypeexists = sql_fetch_object($res);
\r
435 $skintypevalue = ($content == true);
\r
437 if( $skintypevalue && $skintypeexists )
\r
440 'skinid' => $this->id,
\r
442 'content' => &$content
\r
445 // PreUpdateSkinPart event
\r
446 if ( !$this->admin )
\r
448 $manager->notify('PreUpdateSkinPart', $data);
\r
452 $manager->notify('PreUpdateAdminSkinPart', $data);
\r
455 else if( $skintypevalue && !$skintypeexists )
\r
458 'skinid' => $this->id,
\r
460 'content' => &$content
\r
463 // PreAddSkinPart event
\r
464 if ( !$this->admin )
\r
466 $manager->notify('PreAddSkinPart', $data);
\r
470 $manager->notify('PreAddAdminSkinPart', $data);
\r
473 else if( !$skintypevalue && $skintypeexists )
\r
476 'skinid' => $this->id,
\r
480 // PreDeleteSkinPart event
\r
481 if ( !$this->admin )
\r
483 $manager->notify('PreDeleteSkinPart', $data);
\r
487 $manager->notify('PreDeleteAdminSkinPart', $data);
\r
491 // delete old thingie
\r
492 $query = "DELETE FROM %s WHERE stype='%s' and sdesc=%d";
\r
493 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);
\r
496 // write new thingie
\r
499 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE ('%s', '%s', %d)";
\r
500 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->id);
\r
504 if( $skintypevalue && $skintypeexists )
\r
507 'skinid' => $this->id,
\r
509 'content' => &$content
\r
512 // PostUpdateSkinPart event
\r
513 if ( !$this->admin )
\r
515 $manager->notify('PostUpdateSkinPart', $data);
\r
519 $manager->notify('PostUpdateAdminSkinPart', $data);
\r
522 else if( $skintypevalue && (!$skintypeexists) )
\r
525 'skinid' => $this->id,
\r
527 'content' => &$content
\r
530 // PostAddSkinPart event
\r
531 if ( !$this->admin )
\r
533 $manager->notify('PostAddSkinPart', $data);
\r
537 $manager->notify('PostAddAdminSkinPart', $data);
\r
540 else if( (!$skintypevalue) && $skintypeexists )
\r
543 'skinid' => $this->id,
\r
547 // PostDeleteSkinPart event
\r
548 if ( !$this->admin )
\r
550 $manager->notify('PostDeleteSkinPart', $data);
\r
554 $manager->notify('PostDeleteAdminSkinPart', $data);
\r
561 * Skin::deleteAllParts()
\r
562 * Deletes all skin parts from the database
\r
567 public function deleteAllParts()
\r
569 $query = "DELETE FROM %s WHERE sdesc=%d;";
\r
570 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
\r
575 * Skin::updateGeneralInfo()
\r
576 * Updates the general information about the skin
\r
578 * @param string $name name of the skin
\r
579 * @param string $desc description of the skin
\r
580 * @param string $type type of the skin
\r
581 * @param string $includeMode include mode of the skin
\r
582 * @param string $includePrefix include prefix of the skin
\r
585 public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
\r
587 $name = sql_real_escape_string($name);
\r
588 $desc = sql_real_escape_string($desc);
\r
589 $type = sql_real_escape_string($type);
\r
590 $includeMode = sql_real_escape_string($includeMode);
\r
591 $includePrefix = sql_real_escape_string($includePrefix);
\r
593 $query ="UPDATE %s SET sdname='', sddesc='%s', sdtype='%s', sdincmode='%s', sdincpref='%s' WHERE sdnumber=%d:";
\r
594 $query = sprintf($query, $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);
\r
601 * Skin::getFriendlyNames()
\r
602 * Get an array with the names of possible skin parts
\r
603 * Used to show all possible parts of a skin in the administration backend
\r
606 * @param array type of the skin
\r
608 static public function getFriendlyNames()
\r
610 if ( !$this->$admin )
\r
612 $friendly_names = Actions::get_page_type_friendly_names();
\r
613 $action_names = Actions::get_page_action_names();
\r
617 $friendly_names = AdminActions::get_page_type_friendly_names();
\r
618 $action_names = AdminActions::get_page_action_names();
\r
621 $query = "SELECT stype FROM %s WHERE stype NOT IN ('%s');";
\r
622 $query = sprintf($query, sql_table('skin'), implode("','", $action_names));
\r
623 $res = sql_query($query);
\r
625 while ( $row = sql_fetch_array($res) )
\r
627 $friendly_names[strtolower($row['stype'])] = $row['stype'];
\r
629 return $friendly_names;
\r
633 * Skin::getAllowedActionsForType()
\r
634 * Get the allowed actions for a skin type
\r
635 * returns an array with the allowed actions
\r
637 * @param string $type type of the skin (e.g. index, item, search ...)
\r
638 * @return array allowed action types
\r
640 public function getAllowedActionsForType($type)
\r
642 if ( !$this->admin )
\r
644 return Actions::get_allowed_actions_for_type($type);
\r
648 return AdminActions::get_allowed_actions_for_type($type);
\r