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 * @license http://nucleuscms.org/license.txt GNU General Public License
14 * @copyright Copyright (C) 2002-2009 The Nucleus Group
15 * @version $Id: PAGEFACTORY.php 1757 2012-04-15 09:02:32Z sakamocchi $
19 * The formfactory class can be used to insert add/edit item forms into
20 * admin area, bookmarklet, skins or any other places where such a form
23 class PageFactory extends BaseActions
27 * Reference to the blog object for which an add:edit form is created
32 * PageFactory::$allowed_types
33 * Allowed types of forms (bookmarklet/admin)
35 private $allowed_types;
39 * One of the types in $allowed_types
44 * PageFactory::$method
50 * PageFactory::$variables
51 * Info to fill out in the form (e.g. catid, itemid, ...)
56 * PageFactory::$actions
57 * Allowed actions (for parser)
59 // TODO: move the definition of actions to the createXForm methods
60 static private $defined_actions = array(
82 * PageFactory::__construct()
83 * Creates a new PAGEFACTORY object
85 * @param integer $blog_id
88 public function __construct($blog_id)
90 parent::__construct();
94 $this->blog =& $manager->getBlog($blog_id);
97 * TODO: maybe add 'skin' later on?
98 * TODO: maybe add other pages from admin area
100 $this->allowed_types = array('bookmarklet', 'admin');
105 * PageFactory::getDefinedActions()
109 * @return array array for defined action names
112 static public function getDefinedActions()
114 return array_merge(self::$defined_actions, parent::getDefinedActions());
118 * PageFactory::createAddForm()
119 * Creates an "add item" form for a given type of page
121 * @param string $type 'admin' or 'bookmarklet'
122 * @param array $contents
125 public function createAddForm($type, $contents = array())
129 // begin if: the $type is not in the allowed types array
130 if ( !in_array($type, $this->allowed_types) )
136 $this->method = 'add';
138 $manager->notify('PreAddItemForm', array('contents' => &$contents, 'blog' => &$this->blog));
140 $this->createForm($contents);
145 * PageFactory::createEditForm()
146 * Creates an "edit item" form for a given type of page
148 * @param string $type 'admin' or 'bookmarklet'
149 * @param array $contents
152 public function createEditForm($type, $contents)
154 // begin if: the $type is not in the allowed types array
155 if ( !in_array($type, $this->allowed_types) )
161 $this->method = 'edit';
162 $this->createForm($contents);
167 * PageFactory::createForm()
168 * creates a form for a given type of page
170 * @param array $contents
173 private function createForm($contents)
176 $this->variables = $contents;
178 # get template to use
179 $template = $this->getTemplateFor($this->type);
181 # use the PARSER engine to parse that template
182 $parser = new Parser(self::getDefinedActions(), $this);
183 $parser->parse($template);
188 * PageFactory::getTemplateFor()
189 * Returns an appropriate template
191 * @param string $type
192 * @return string contents of form template
194 private function getTemplateFor($type)
198 $filename = $DIR_LIBS . 'include/' . $this->type . '-' . $this->method . '.template';
200 // begin if: file doesn't exist
201 if ( !file_exists($filename) )
206 $filesize = filesize($filename);
208 // begin if: filesize is LTE zero
209 if ( $filesize <= 0 )
214 # read file and return it
215 $fd = fopen ($filename, 'r');
216 $contents = fread ($fd, $filesize);
223 * PageFactory::parse_categories()
224 * Create category dropdown box
226 * @param integer $start_index
229 public function parse_categories($start_index = 0)
231 // begin if: catid variable is set; use it to select the category
232 if ( !array_key_exists('catid', $this->variables) )
234 $category_id = $this->blog->getDefaultCategory();
236 // else: get the default category
239 $category_id = $this->variables['catid'];
242 Admin::selectBlogCategory('catid', $category_id, $start_index, 1, $this->blog->getID());
247 * PageFactory::parse_blogid()
248 * Displays the blog ID
253 public function parse_blogid()
255 echo $this->blog->getID();
260 * PageFactory::parse_blogname()
261 * Displays the blog name
266 public function parse_blogname()
268 echo $this->blog->getName();
273 * PageFactory::parse_bloglink()
274 * Displays the blog link
279 public function parse_bloglink()
281 echo '<a href="', Entity::hsc($this->blog->getURL()), '">', Entity::hsc($this->blog->getName()), '</a>';
286 * PageFactory::parse_authorname()
287 * Displays the author's name
292 function parse_authorname()
294 // don't use on add item?
296 echo $member->getDisplayName();
301 * PageFactory::parse_title()
307 public function parse_title()
309 echo $this->contents['title'];
314 * PageFactory::checkCondition()
315 * Checks conditions for if statements
317 * @param string $field type of <%if%>
318 * @param string $name property of field
319 * @param string $value value of property
322 protected function checkCondition($field, $name = '', $value = 1)
330 $condition = (boolean) ($this->blog->getSetting($name) == $value);
333 $condition = (boolean) ($member->getAutosave() == $value);
336 if ( array_key_exists($name, $this->variables) )
338 $condition = (boolean) ($this->variables[$name] == $value);
348 * PageFactory::parse_helplink()
350 * @param string $topic
353 function parse_helplink($topic)
360 * PageFactory::parse_currenttime()
363 * @param string $what
366 public function parse_currenttime($what)
368 $nu = getdate($this->blog->getCorrectTime());
374 * PageFactory::parse_itemtime()
375 * date change on edit item
377 * @param string $what
380 public function parse_itemtime($what)
382 $itemtime = getdate($this->variables['timestamp']);
383 echo $itemtime[$what];
388 * PageFactory::parse_init()
389 * some init stuff for all forms
394 public function parse_init()
396 if ( $this->method != 'edit' )
402 $authorid = $this->variables['authorid'];
404 $this->blog->insertJavaScriptInfo($authorid);
409 * PageFactory::parse_extrahead()
410 * on bookmarklets only: insert extra html header information (by plugins)
415 public function parse_extrahead()
421 'extrahead' => &$extrahead
424 $manager->notify('BookmarkletExtraHead', $data);
430 * PageFactory::parse_contents()
432 * @param string $which
435 public function parse_contents($which)
437 if ( !array_key_exists($which, $this->variables) || !isset($this->variables[$which]) )
439 $this->variables[$which] = '';
441 echo Entity::hsc($this->variables[$which]);
446 * PageFactory::parse_checkedonval()
448 * @param string $value value for input element with checkbox type
451 public function parse_checkedonval($value, $name)
453 if ( !array_key_exists($name, $this->variables) || !isset($this->variables[$name]) )
455 $this->variables[$name] = '';
457 if ( $this->variables[$name] == $value )
459 echo "checked='checked'";
465 * Pagefactory::parse_jsinput()
466 * extra javascript for input and textarea fields
468 * @param string $which name of JavaScript function
469 * @return string attribute for input element
471 public function parse_jsinput($which)
473 global $CONF, $member;
475 $attributes = " name=\"{$which}\"";
476 $attributes .= " id=\"input{$which}\"";
478 if ( $CONF['DisableJsTools'] != 1 )
480 $attributes .= ' onclick="storeCaret(this);"';
481 $attributes .= ' onselect="storeCaret(this);"';
482 if ( $member->getAutosave() )
484 $attributes .= " onkeyup=\"storeCaret(this); updPreview('{$which}'); doMonitor();\"";
488 $attributes .= " onkeyup=\"storeCaret(this); updPreview('{$which}');\"";
493 if ( $CONF['DisableJsTools'] == 0 )
495 $attributes .= ' onkeypress="shortCuts();"';
497 if ( $member->getAutosave() )
499 $attributes .= ' onkeyup="doMonitor();"';
507 * PageFactory::parse_jsbuttonbar()
508 * shows the javascript button bar
510 * @param string $extrabuttons
513 public function parse_jsbuttonbar($extrabuttons = "")
516 switch ( $CONF['DisableJsTools'] )
519 echo "<div class=\"jsbuttonbar\">\n";
520 $this->jsbutton('cut','cutThis()',_ADD_CUT_TT . " (Ctrl + X)");
521 $this->jsbutton('copy','copyThis()',_ADD_COPY_TT . " (Ctrl + C)");
522 $this->jsbutton('paste','pasteThis()',_ADD_PASTE_TT . " (Ctrl + V)");
523 $this->jsbuttonspacer();
524 $this->jsbutton('bold',"boldThis()", _ADD_BOLD_TT ." (Ctrl + Shift + B)");
525 $this->jsbutton('italic',"italicThis()", _ADD_ITALIC_TT ." (Ctrl + Shift + I)");
526 $this->jsbutton('link',"ahrefThis()", _ADD_HREF_TT ." (Ctrl + Shift + A)");
527 $this->jsbuttonspacer();
528 $this->jsbutton('alignleft',"alignleftThis()", _ADD_ALIGNLEFT_TT);
529 $this->jsbutton('alignright',"alignrightThis()", _ADD_ALIGNRIGHT_TT);
530 $this->jsbutton('aligncenter',"aligncenterThis()", _ADD_ALIGNCENTER_TT);
531 $this->jsbuttonspacer();
532 $this->jsbutton('left',"leftThis()", _ADD_LEFT_TT);
533 $this->jsbutton('right',"rightThis()", _ADD_RIGHT_TT);
537 $btns = preg_split('#\+#',$extrabuttons);
538 $this->jsbuttonspacer();
539 foreach ( $btns as $button )
544 $this->jsbutton('media', "addMedia()", _ADD_MEDIA_TT . " (Ctrl + Shift + M)");
547 $this->jsbutton('preview', "showedit()", _ADD_PREVIEW_TT);
555 echo "<div class=\"jsbuttonbar\">";
556 $this->jsbutton('bold',"boldThis()", _ADD_BOLD_TT);
557 $this->jsbutton('italic',"italicThis()", _ADD_ITALIC_TT);
558 $this->jsbutton('link',"ahrefThis()", _ADD_HREF_TT);
559 $this->jsbuttonspacer();
560 $this->jsbutton('alignleft',"alignleftThis()", _ADD_ALIGNLEFT_TT);
561 $this->jsbutton('alignright',"alignrightThis()", _ADD_ALIGNRIGHT_TT);
562 $this->jsbutton('aligncenter',"aligncenterThis()", _ADD_ALIGNCENTER_TT);
563 $this->jsbuttonspacer();
564 $this->jsbutton('left',"leftThis()", _ADD_LEFT_TT);
565 $this->jsbutton('right',"rightThis()", _ADD_RIGHT_TT);
569 $btns = preg_split('#\+#',$extrabuttons);
570 $this->jsbuttonspacer();
571 foreach ( $btns as $button )
576 $this->jsbutton('media', "addMedia()", _ADD_MEDIA_TT);
581 echo "<span style=\"clear:both;\"></span></div>\n";
588 * PageFactory::parse_pluginextras()
589 * Allows plugins to add their own custom fields
594 public function parse_pluginextras()
598 switch ( $this->method )
602 'blog' => &$this->blog
605 $manager->notify('AddItemFormExtras', $data);
609 'variables' => $this->variables,
610 'blog' => &$this->blog,
611 'itemid' => $this->variables['itemid']
613 $manager->notify('EditItemFormExtras', $data);
620 * PageFactory::parse_itemoptions()
621 * Adds the itemOptions of a plugin to a page
626 public function parse_itemoptions()
629 Admin::_insertPluginOptions('item', $itemid);
634 * PageFactory::parse_ticket()
636 public function parse_ticket()
639 $manager->addTicketHidden();
644 * PageFactory::jsbutton()
647 * @param string $type type of button
648 * @param string $code JavaScript codes for onclick event
649 * @param string $tooltip alternative text attribute for image element
651 private function jsbutton($type, $code, $tooltip)
653 echo "<span class=\"jsbutton\" onmouseover=\"BtnHighlight(this);\" onmouseout=\"BtnNormal(this);\" onclick=\"{$code}\" >";
654 echo "<img src=\"images/button-{$type}.gif\" alt=\"{$tooltip}\" title=\"{$tooltip}\" width=\"16\" height=\"16\" />";
660 * PageFactory::jsbuttonspacer()
665 private function jsbuttonspacer()
667 echo '<span class="jsbuttonspacer"></span>';