3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2009 The Nucleus Group
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 * (see nucleus/documentation/index.html#license for more info)
13 * Class representing a skin
15 * @license http://nucleuscms.org/license.txt GNU General Public License
16 * @copyright Copyright (C) 2002-2009 The Nucleus Group
17 * @version $Id: SKIN.php 1755 2012-04-14 10:05:49Z sakamocchi $
20 if ( !function_exists('requestVar') )
27 // after creating a SKIN object, evaluates to true when the skin exists
30 // skin characteristics. Use the getXXX methods rather than accessing directly
34 private $includeMode; // either 'normal' or 'skindir'
35 private $includePrefix;
39 private $action_class;
40 private $event_identifier;
44 * Constructor for a new SKIN object
46 * @param integer $id id of the skin
47 * @param string $action_class name of class extended from BaseActions
48 * @param string $event_identifier event identifier. for example, InitAdminSkinParse if AdminSkin is used
51 public function __construct($id, $action_class='Actions', $event_identifier='Skin')
55 $this->id = (integer) $id;
57 // read skin name/description/content type
58 $query = "SELECT * FROM %s WHERE sdnumber=%d";
59 $query = sprintf($query, sql_table('skin_desc'), (integer) $this->id);
60 $res = sql_query($query);
61 $obj = sql_fetch_object($res);
62 $this->valid = (sql_num_rows($res) > 0);
69 * NOTE: include needed action class
71 if ( $action_class != 'Actions' )
73 if ( !class_exists($action_class, FALSE)
74 && (!file_exists("{$DIR_LIBS}{$action_class}.php")
75 || !include("{$DIR_LIBS}{$action_class}.php")) )
82 if ( !class_exists('Actions', FALSE)
83 && (!file_exists("{$DIR_LIBS}ACTIONS.php")
84 || !include("{$DIR_LIBS}ACTIONS.php")) )
90 $this->action_class = $action_class;
91 $this->event_identifier = $event_identifier;
93 $this->name = $obj->sdname;
94 $this->description = $obj->sddesc;
95 $this->contentType = $obj->sdtype;
96 $this->includeMode = $obj->sdincmode;
97 $this->includePrefix = $obj->sdincpref;
107 * @return integer id for this skin instance
109 public function getID()
111 return (integer) $this->id;
120 public function isValid()
122 return (boolean) $this->valid;
130 * @return string name of this skin instance
132 public function getName()
134 return (string) $this->name;
138 * Skin::getDescription()
139 * Get SKIN description
142 * @return string description of this skin instance
144 public function getDescription()
146 return (string) $this->description;
150 * Skin::getContentType()
151 * Get SKIN content type
152 * e.g. text/xml, text/html, application/atom+xml
155 * @return string name of this skin instance
157 public function getContentType()
159 return (string) $this->contentType;
163 * Skin::getIncludeMode()
164 * Get include mode of the SKIN
166 * Returns either 'normal' or 'skindir':
167 * 'normal': if a all data of the skin can be found in the databse
168 * 'skindir': if the skin has data in the it's skin driectory
171 * @return string normal/skindir
173 public function getIncludeMode()
175 return (string) $this->includeMode;
179 * Skin::getIncludePrefix()
180 * Get include prefix of the SKIN
182 * Get name of the subdirectory (with trailing slash) where
183 * the files of the current skin can be found (e.g. 'default/')
186 * @return string include prefix of this skin instance
188 public function getIncludePrefix()
190 return (string) $this->includePrefix;
195 * Checks if a skin with a given shortname exists
198 * @param string $name Skin short name
199 * @return integer number of skins with the given ID
201 static public function exists($name)
203 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname='%s';";
204 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));
205 return (quickQuery($query) > 0);
210 * Checks if a skin with a given ID exists
213 * @param string $id Skin ID
214 * @return integer number of skins with the given ID
216 static public function existsID($id)
218 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";
219 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
220 return (quickQuery($query) > 0);
224 * Skin::createFromName()
225 * Returns a skin given its shortname
228 * @param string $name Skin shortname
229 * @return object instance of Skin class
231 static public function createFromName($name)
233 return new SKIN(SKIN::getIdFromName($name));
237 * Skin::getIdFromName()
238 * Returns a skin ID given its shortname
241 * @param string $name Skin shortname
242 * @return integer Skin ID
244 static public function getIdFromName($name)
246 $query = "SELECT sdnumber FROM %s WHERE sdname='%s';";
247 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));
248 $res = sql_query($query);
249 $obj = sql_fetch_object($res);
250 return $obj->sdnumber;
254 * Skin::getNameFromId()
255 * Returns a skin shortname given its ID
258 * @param string $name
259 * @return string Skin short name
261 static public function getNameFromId($id)
263 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";
264 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
265 return quickQuery($query);
270 * Creates a new skin, with the given characteristics.
273 * @param String $name value for nucleus_skin.sdname
274 * @param String $desc value for nucleus_skin.sddesc
275 * @param String $type value for nucleus_skin.sdtype
276 * @param String $includeMode value for nucleus_skin.sdinclude
277 * @param String $includePrefix value for nucleus_skin.sdincpref
278 * @return Integer ID for just inserted record
280 public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
288 'description' => &$desc,
290 'includeMode' => &$includeMode,
291 'includePrefix' => &$includePrefix
295 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES ('%s', '%s', '%s', '%s', '%s');";
296 $sdname = sql_real_escape_string($name);
297 $sddesc = sql_real_escape_string($desc);
298 $sdtype = sql_real_escape_string($type);
299 $sdincmode = sql_real_escape_string($includeMode);
300 $sdincpref = sql_real_escape_string($includePrefix);
301 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);
303 $newid = sql_insert_id();
310 'description' => $desc,
312 'includeMode' => $includeMode,
313 'includePrefix' => $includePrefix
323 * @param string $type
326 public function parse($type)
328 global $currentSkinName, $manager, $CONF;
330 $manager->notify("Init{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
333 sendContentType($this->getContentType(), 'skin');
335 // set skin name as global var (so plugins can access it)
336 $currentSkinName = $this->getName();
337 $contents = $this->getContent($type);
341 // use base skin if this skin does not have contents
342 $defskin = new SKIN($CONF['BaseSkin']);
343 $contents = $defskin->getContent($type);
351 $actions = $this->getAllowedActionsForType($type);
353 $manager->notify("Pre{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
355 // set IncludeMode properties of parser
356 Parser::setProperty('IncludeMode', $this->getIncludeMode());
357 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
359 $action_class = $this->action_class;
360 $handler = new $action_class($type);
362 $parser = new Parser($actions, $handler);
363 $handler->setParser($parser);
364 $handler->setSkin($this);
365 $parser->parse($contents);
367 $manager->notify("Post{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
373 * Get content of the skin part from the database
375 * @param string $type type of the skin (e.g. index, item, search ...)
376 * @return string content of scontent
378 public function getContent($type)
380 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype='%s';";
381 $query = sprintf($query, sql_table('skin'), (integer) $this->id, sql_real_escape_string($type));
382 $res = sql_query($query);
384 if ( sql_num_rows($res) == 0 )
388 return sql_result($res, 0, 0);
393 * Updates the contents for one part of the skin in the database
395 * @param string $type type of the skin part (e.g. index, item, search ...)
396 * @param string $content new content for this skin part
400 public function update($type, $content)
404 $query = "SELECT sdesc FROM %s WHERE stype='%s' and sdesc=%d;";
405 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);
406 $res = sql_query($query);
408 $skintypeexists = sql_fetch_object($res);
409 $skintypevalue = ($content == true);
411 if( $skintypevalue && $skintypeexists )
414 'skinid' => $this->id,
416 'content' => &$content
419 // PreUpdateSkinPart event
420 $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);
422 else if( $skintypevalue && !$skintypeexists )
425 'skinid' => $this->id,
427 'content' => &$content
430 $manager->notify("PreAdd{$this->event_identifier}Part", $data);
432 else if( !$skintypevalue && $skintypeexists )
435 'skinid' => $this->id,
439 $manager->notify("PreDelete{$this->event_identifier}Part", $data);
442 // delete old thingie
443 $query = "DELETE FROM %s WHERE stype='%s' and sdesc=%d";
444 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);
450 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE ('%s', '%s', %d)";
451 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->id);
455 if( $skintypevalue && $skintypeexists )
458 'skinid' => $this->id,
460 'content' => &$content
463 // PostUpdateSkinPart event
464 $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
466 else if( $skintypevalue && (!$skintypeexists) )
469 'skinid' => $this->id,
471 'content' => &$content
474 // PostAddSkinPart event
475 $manager->notify("PostAdd{$this->event_identifier}Part", $data);
477 else if( (!$skintypevalue) && $skintypeexists )
480 'skinid' => $this->id,
484 $manager->notify("PostDelete{$this->event_identifier}Part", $data);
490 * Skin::deleteAllParts()
491 * Deletes all skin parts from the database
496 public function deleteAllParts()
498 $query = "DELETE FROM %s WHERE sdesc=%d;";
499 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
504 * Skin::updateGeneralInfo()
505 * Updates the general information about the skin
507 * @param string $name name of the skin
508 * @param string $desc description of the skin
509 * @param string $type type of the skin
510 * @param string $includeMode include mode of the skin
511 * @param string $includePrefix include prefix of the skin
514 public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
516 $name = sql_real_escape_string($name);
517 $desc = sql_real_escape_string($desc);
518 $type = sql_real_escape_string($type);
519 $includeMode = sql_real_escape_string($includeMode);
520 $includePrefix = sql_real_escape_string($includePrefix);
522 $query ="UPDATE %s SET sdname='', sddesc='%s', sdtype='%s', sdincmode='%s', sdincpref='%s' WHERE sdnumber=%d:";
523 $query = sprintf($query, $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);
530 * Skin::getAllowedActionsForType()
531 * Get the allowed actions for a skin type
532 * returns an array with the allowed actions
534 * @param string $type type of the skin (e.g. index, item, search ...)
535 * @return array allowed action types
537 public function getAllowedActionsForType($type)
540 * NOTE: static method with variable class name is supported since PHP 5.3
541 * So now we utilize eval function.
543 $page_action_names = array();
544 eval("\$defined_actions = {$this->action_class}::getDefinedActions('{$type}');");
545 return $defined_actions;
549 * Skin::getFriendlyNames()
550 * Get an array with the names of possible skin parts
551 * Used to show all possible parts of a skin in the administration backend
554 * @param string $action_class name of action class (optional)
555 * @param array type of the skin
557 static public function getFriendlyNames($action_class='Actions')
562 * NOTE: include needed action class
564 if ( $action_class != 'Actions' )
566 if ( !class_exists($action_class, FALSE)
567 && (!file_exists("{$DIR_LIBS}{$action_class}.php")
568 || !include("{$DIR_LIBS}{$action_class}.php")) )
575 if ( !class_exists('Actions', FALSE)
576 && (!file_exists("{$DIR_LIBS}ACTIONS.php")
577 || !include("{$DIR_LIBS}ACTIONS.php")) )
584 * NOTE: static method with variable class name is supported since PHP 5.3
585 * So now we utilize eval function.
587 eval("\$friendly_names = {$action_class}::getSkinTypeFriendlyNames();");
589 $action_names = array();
590 foreach ( $friendly_names as $action_name => $friendly_name )
592 $action_names[] = $action_name;
595 $query = "SELECT stype FROM %s WHERE stype NOT IN ('%s');";
596 $query = sprintf($query, sql_table('skin'), implode("','", $action_names));
597 $res = sql_query($query);
599 while ( $row = sql_fetch_array($res) )
601 $friendly_names[strtolower($row['stype'])] = $row['stype'];
603 return $friendly_names;