3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2009 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 1755 2012-04-14 10:05:49Z 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
39 private $action_class;
\r
40 private $event_identifier;
\r
43 * Skin::__construct()
\r
44 * Constructor for a new SKIN object
\r
46 * @param integer $id id of the skin
\r
47 * @param string $action_class name of class extended from BaseActions
\r
48 * @param string $event_identifier event identifier. for example, InitAdminSkinParse if AdminSkin is used
\r
51 public function __construct($id, $action_class='Actions', $event_identifier='Skin')
\r
55 $this->id = (integer) $id;
\r
57 // read skin name/description/content type
\r
58 $query = "SELECT * FROM %s WHERE sdnumber=%d";
\r
59 $query = sprintf($query, sql_table('skin_desc'), (integer) $this->id);
\r
60 $res = sql_query($query);
\r
61 $obj = sql_fetch_object($res);
\r
62 $this->valid = (sql_num_rows($res) > 0);
\r
63 if ( !$this->valid )
\r
69 * NOTE: include needed action class
\r
71 if ( $action_class != 'Actions' )
\r
73 if ( !class_exists($action_class, FALSE)
\r
74 && (!file_exists("{$DIR_LIBS}{$action_class}.php")
\r
75 || !include("{$DIR_LIBS}{$action_class}.php")) )
\r
82 if ( !class_exists('Actions', FALSE)
\r
83 && (!file_exists("{$DIR_LIBS}ACTIONS.php")
\r
84 || !include("{$DIR_LIBS}ACTIONS.php")) )
\r
90 $this->action_class = $action_class;
\r
91 $this->event_identifier = $event_identifier;
\r
93 $this->name = $obj->sdname;
\r
94 $this->description = $obj->sddesc;
\r
95 $this->contentType = $obj->sdtype;
\r
96 $this->includeMode = $obj->sdincmode;
\r
97 $this->includePrefix = $obj->sdincpref;
\r
107 * @return integer id for this skin instance
\r
109 public function getID()
\r
111 return (integer) $this->id;
\r
120 public function isValid()
\r
122 return (boolean) $this->valid;
\r
130 * @return string name of this skin instance
\r
132 public function getName()
\r
134 return (string) $this->name;
\r
138 * Skin::getDescription()
\r
139 * Get SKIN description
\r
142 * @return string description of this skin instance
\r
144 public function getDescription()
\r
146 return (string) $this->description;
\r
150 * Skin::getContentType()
\r
151 * Get SKIN content type
\r
152 * e.g. text/xml, text/html, application/atom+xml
\r
155 * @return string name of this skin instance
\r
157 public function getContentType()
\r
159 return (string) $this->contentType;
\r
163 * Skin::getIncludeMode()
\r
164 * Get include mode of the SKIN
\r
166 * Returns either 'normal' or 'skindir':
\r
167 * 'normal': if a all data of the skin can be found in the databse
\r
168 * 'skindir': if the skin has data in the it's skin driectory
\r
171 * @return string normal/skindir
\r
173 public function getIncludeMode()
\r
175 return (string) $this->includeMode;
\r
179 * Skin::getIncludePrefix()
\r
180 * Get include prefix of the SKIN
\r
182 * Get name of the subdirectory (with trailing slash) where
\r
183 * the files of the current skin can be found (e.g. 'default/')
\r
186 * @return string include prefix of this skin instance
\r
188 public function getIncludePrefix()
\r
190 return (string) $this->includePrefix;
\r
195 * Checks if a skin with a given shortname exists
\r
198 * @param string $name Skin short name
\r
199 * @return integer number of skins with the given ID
\r
201 static public function exists($name)
\r
203 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdname='%s';";
\r
204 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));
\r
205 return (quickQuery($query) > 0);
\r
210 * Checks if a skin with a given ID exists
\r
213 * @param string $id Skin ID
\r
214 * @return integer number of skins with the given ID
\r
216 static public function existsID($id)
\r
218 $query = "SELECT COUNT(*) AS result FROM %s WHERE sdnumber=%d;";
\r
219 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
\r
220 return (quickQuery($query) > 0);
\r
224 * Skin::createFromName()
\r
225 * Returns a skin given its shortname
\r
228 * @param string $name Skin shortname
\r
229 * @return object instance of Skin class
\r
231 static public function createFromName($name)
\r
233 return new SKIN(SKIN::getIdFromName($name));
\r
237 * Skin::getIdFromName()
\r
238 * Returns a skin ID given its shortname
\r
241 * @param string $name Skin shortname
\r
242 * @return integer Skin ID
\r
244 static public function getIdFromName($name)
\r
246 $query = "SELECT sdnumber FROM %s WHERE sdname='%s';";
\r
247 $query = sprintf($query, sql_table('skin_desc'), sql_real_escape_string($name));
\r
248 $res = sql_query($query);
\r
249 $obj = sql_fetch_object($res);
\r
250 return $obj->sdnumber;
\r
254 * Skin::getNameFromId()
\r
255 * Returns a skin shortname given its ID
\r
258 * @param string $name
\r
259 * @return string Skin short name
\r
261 static public function getNameFromId($id)
\r
263 $query = "SELECT sdname AS result FROM %s WHERE sdnumber=%d;";
\r
264 $query = sprintf($query, sql_table('skin_desc'), (integer) $id);
\r
265 return quickQuery($query);
\r
269 * SKIN::createNew()
\r
270 * Creates a new skin, with the given characteristics.
\r
273 * @param String $name value for nucleus_skin.sdname
\r
274 * @param String $desc value for nucleus_skin.sddesc
\r
275 * @param String $type value for nucleus_skin.sdtype
\r
276 * @param String $includeMode value for nucleus_skin.sdinclude
\r
277 * @param String $includePrefix value for nucleus_skin.sdincpref
\r
278 * @return Integer ID for just inserted record
\r
280 public function createNew($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
\r
288 'description' => &$desc,
\r
290 'includeMode' => &$includeMode,
\r
291 'includePrefix' => &$includePrefix
\r
295 $query = "INSERT INTO %s (sdname, sddesc, sdtype, sdincmode, sdincpref) VALUES ('%s', '%s', '%s', '%s', '%s');";
\r
296 $sdname = sql_real_escape_string($name);
\r
297 $sddesc = sql_real_escape_string($desc);
\r
298 $sdtype = sql_real_escape_string($type);
\r
299 $sdincmode = sql_real_escape_string($includeMode);
\r
300 $sdincpref = sql_real_escape_string($includePrefix);
\r
301 $query = sprintf($query, sql_table('skin_desc'), $sdname, $sddesc, $sdtype, $sdincmode, $sdincpref);
\r
303 $newid = sql_insert_id();
\r
308 'skinid' => $newid,
\r
310 'description' => $desc,
\r
312 'includeMode' => $includeMode,
\r
313 'includePrefix' => $includePrefix
\r
323 * @param string $type
\r
326 public function parse($type)
\r
328 global $currentSkinName, $manager, $CONF;
\r
330 $manager->notify("Init{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
\r
333 sendContentType($this->getContentType(), 'skin');
\r
335 // set skin name as global var (so plugins can access it)
\r
336 $currentSkinName = $this->getName();
\r
337 $contents = $this->getContent($type);
\r
341 // use base skin if this skin does not have contents
\r
342 $defskin = new SKIN($CONF['BaseSkin']);
\r
343 $contents = $defskin->getContent($type);
\r
351 $manager->notify("Pre{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type, 'contents' => &$contents));
\r
353 // set IncludeMode properties of parser
\r
354 Parser::setProperty('IncludeMode', $this->getIncludeMode());
\r
355 Parser::setProperty('IncludePrefix', $this->getIncludePrefix());
\r
357 $action_class = $this->action_class;
\r
358 $handler = new $action_class($type);
\r
360 $actions = $handler->getDefinedActions($type);
361 $parser = new Parser($actions, $handler);
\r
362 $handler->setParser($parser);
\r
363 $handler->setSkin($this);
\r
364 $parser->parse($contents);
\r
366 $manager->notify("Post{$this->event_identifier}Parse", array('skin' => &$this, 'type' => $type));
\r
371 * Skin::getContent()
\r
372 * Get content of the skin part from the database
\r
374 * @param string $type type of the skin (e.g. index, item, search ...)
\r
375 * @return string content of scontent
\r
377 public function getContent($type)
\r
379 $query = "SELECT scontent FROM %s WHERE sdesc=%d and stype='%s';";
\r
380 $query = sprintf($query, sql_table('skin'), (integer) $this->id, sql_real_escape_string($type));
\r
381 $res = sql_query($query);
\r
383 if ( sql_num_rows($res) == 0 )
\r
387 return sql_result($res, 0, 0);
\r
392 * Updates the contents for one part of the skin in the database
\r
394 * @param string $type type of the skin part (e.g. index, item, search ...)
\r
395 * @param string $content new content for this skin part
\r
399 public function update($type, $content)
\r
403 $query = "SELECT sdesc FROM %s WHERE stype='%s' and sdesc=%d;";
\r
404 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);
\r
405 $res = sql_query($query);
\r
407 $skintypeexists = sql_fetch_object($res);
\r
408 $skintypevalue = ($content == true);
\r
410 if( $skintypevalue && $skintypeexists )
\r
413 'skinid' => $this->id,
\r
415 'content' => &$content
\r
418 // PreUpdateSkinPart event
\r
419 $manager->notify("PreUpdate{{$this->event_identifier}}Part", $data);
\r
421 else if( $skintypevalue && !$skintypeexists )
\r
424 'skinid' => $this->id,
\r
426 'content' => &$content
\r
429 $manager->notify("PreAdd{$this->event_identifier}Part", $data);
\r
431 else if( !$skintypevalue && $skintypeexists )
\r
434 'skinid' => $this->id,
\r
438 $manager->notify("PreDelete{$this->event_identifier}Part", $data);
\r
441 // delete old thingie
\r
442 $query = "DELETE FROM %s WHERE stype='%s' and sdesc=%d";
\r
443 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($type), (integer) $this->id);
\r
446 // write new thingie
\r
449 $query = "INSERT INTO %s (scontent, stype, sdesc) VALUE ('%s', '%s', %d)";
\r
450 $query = sprintf($query, sql_table('skin'), sql_real_escape_string($content), sql_real_escape_string($type), (integer) $this->id);
\r
454 if( $skintypevalue && $skintypeexists )
\r
457 'skinid' => $this->id,
\r
459 'content' => &$content
\r
462 // PostUpdateSkinPart event
\r
463 $manager->notify("PostUpdate{$this->event_identifier}Part", $data);
\r
465 else if( $skintypevalue && (!$skintypeexists) )
\r
468 'skinid' => $this->id,
\r
470 'content' => &$content
\r
473 // PostAddSkinPart event
\r
474 $manager->notify("PostAdd{$this->event_identifier}Part", $data);
\r
476 else if( (!$skintypevalue) && $skintypeexists )
\r
479 'skinid' => $this->id,
\r
483 $manager->notify("PostDelete{$this->event_identifier}Part", $data);
\r
489 * Skin::deleteAllParts()
\r
490 * Deletes all skin parts from the database
\r
495 public function deleteAllParts()
\r
497 $query = "DELETE FROM %s WHERE sdesc=%d;";
\r
498 $query = sprintf($query, sql_table('skin'), (integer) $this->id);
\r
503 * Skin::updateGeneralInfo()
\r
504 * Updates the general information about the skin
\r
506 * @param string $name name of the skin
\r
507 * @param string $desc description of the skin
\r
508 * @param string $type type of the skin
\r
509 * @param string $includeMode include mode of the skin
\r
510 * @param string $includePrefix include prefix of the skin
\r
513 public function updateGeneralInfo($name, $desc, $type = 'text/html', $includeMode = 'normal', $includePrefix = '')
\r
515 $name = sql_real_escape_string($name);
\r
516 $desc = sql_real_escape_string($desc);
\r
517 $type = sql_real_escape_string($type);
\r
518 $includeMode = sql_real_escape_string($includeMode);
\r
519 $includePrefix = sql_real_escape_string($includePrefix);
\r
521 $query ="UPDATE %s SET sdname='', sddesc='%s', sdtype='%s', sdincmode='%s', sdincpref='%s' WHERE sdnumber=%d:";
\r
522 $query = sprintf($query, $name, $desc, $type, $includeMode, $includePrefix, (integer) $this->id);
\r
529 * Skin::getAllowedActionsForType()
\r
530 * Get the allowed actions for a skin type
\r
531 * returns an array with the allowed actions
\r
533 * @param string $type type of the skin (e.g. index, item, search ...)
\r
534 * @return array allowed action types
\r
536 public function getAllowedActionsForType($type)
\r
539 * NOTE: static method with variable class name is supported since PHP 5.3
\r
540 * So now we utilize eval function.
\r
542 $page_action_names = array();
\r
543 eval("\$defined_actions = {$this->action_class}::getDefinedActions('{$type}');");
\r
544 return $defined_actions;
\r
548 * Skin::getFriendlyNames()
\r
549 * Get an array with the names of possible skin parts
\r
550 * Used to show all possible parts of a skin in the administration backend
\r
553 * @param string $action_class name of action class (optional)
\r
554 * @param array type of the skin
\r
556 static public function getFriendlyNames($action_class='Actions')
\r
561 * NOTE: include needed action class
\r
563 if ( $action_class != 'Actions' )
\r
565 if ( !class_exists($action_class, FALSE)
\r
566 && (!file_exists("{$DIR_LIBS}{$action_class}.php")
\r
567 || !include("{$DIR_LIBS}{$action_class}.php")) )
\r
574 if ( !class_exists('Actions', FALSE)
\r
575 && (!file_exists("{$DIR_LIBS}ACTIONS.php")
\r
576 || !include("{$DIR_LIBS}ACTIONS.php")) )
\r
583 * NOTE: static method with variable class name is supported since PHP 5.3
\r
584 * So now we utilize eval function.
\r
586 eval("\$friendly_names = {$action_class}::getSkinTypeFriendlyNames();");
\r
588 $action_names = array();
\r
589 foreach ( $friendly_names as $action_name => $friendly_name )
\r
591 $action_names[] = $action_name;
\r
594 $query = "SELECT stype FROM %s WHERE stype NOT IN ('%s');";
\r
595 $query = sprintf($query, sql_table('skin'), implode("','", $action_names));
\r
596 $res = sql_query($query);
\r
598 while ( $row = sql_fetch_array($res) )
\r
600 $friendly_names[strtolower($row['stype'])] = $row['stype'];
\r
602 return $friendly_names;
\r