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(
83 * PageFactory::__construct()
84 * Creates a new PAGEFACTORY object
86 * @param integer $blog_id
89 public function __construct($blog_id)
91 parent::__construct();
95 $this->blog =& $manager->getBlog($blog_id);
98 * TODO: maybe add 'skin' later on?
99 * TODO: maybe add other pages from admin area
101 $this->allowed_types = array('bookmarklet', 'admin');
106 * PageFactory::getDefinedActions()
110 * @return array array for defined action names
113 static public function getDefinedActions()
115 return array_merge(self::$defined_actions, parent::getDefinedActions());
119 * PageFactory::createAddForm()
120 * Creates an "add item" form for a given type of page
122 * @param string $type 'admin' or 'bookmarklet'
123 * @param array $contents
126 public function createAddForm($type, $contents = array())
130 // begin if: the $type is not in the allowed types array
131 if ( !in_array($type, $this->allowed_types) )
137 $this->method = 'add';
139 $manager->notify('PreAddItemForm', array('contents' => &$contents, 'blog' => &$this->blog));
141 $this->createForm($contents);
146 * PageFactory::createEditForm()
147 * Creates an "edit item" form for a given type of page
149 * @param string $type 'admin' or 'bookmarklet'
150 * @param array $contents
153 public function createEditForm($type, $contents)
155 // begin if: the $type is not in the allowed types array
156 if ( !in_array($type, $this->allowed_types) )
162 $this->method = 'edit';
163 $this->createForm($contents);
168 * PageFactory::createForm()
169 * creates a form for a given type of page
171 * @param array $contents
174 private function createForm($contents)
177 $this->variables = $contents;
179 # get template to use
180 $template = $this->getTemplateFor($this->type);
182 # use the PARSER engine to parse that template
183 $parser = new Parser(self::getDefinedActions(), $this);
184 $parser->parse($template);
189 * PageFactory::getTemplateFor()
190 * Returns an appropriate template
192 * @param string $type
193 * @return string contents of form template
195 private function getTemplateFor($type)
199 $filename = $DIR_LIBS . 'include/' . $this->type . '-' . $this->method . '.template';
201 // begin if: file doesn't exist
202 if ( !file_exists($filename) )
207 $filesize = filesize($filename);
209 // begin if: filesize is LTE zero
210 if ( $filesize <= 0 )
215 # read file and return it
216 $fd = fopen ($filename, 'r');
217 $contents = fread ($fd, $filesize);
224 * PageFactory::parse_categories()
225 * Create category dropdown box
227 * @param integer $start_index
230 public function parse_categories($start_index = 0)
232 // begin if: catid variable is set; use it to select the category
233 if ( !array_key_exists('catid', $this->variables) )
235 $category_id = $this->blog->getDefaultCategory();
237 // else: get the default category
240 $category_id = $this->variables['catid'];
243 Admin::selectBlogCategory('catid', $category_id, $start_index, 1, $this->blog->getID());
248 * PageFactory::parse_blogid()
249 * Displays the blog ID
254 public function parse_blogid()
256 echo $this->blog->getID();
261 * PageFactory::parse_blogname()
262 * Displays the blog name
267 public function parse_blogname()
269 echo $this->blog->getName();
274 * PageFactory::parse_bloglink()
275 * Displays the blog link
280 public function parse_bloglink()
282 echo '<a href="', Entity::hsc($this->blog->getURL()), '">', Entity::hsc($this->blog->getName()), '</a>';
287 * PageFactory::parse_authorname()
288 * Displays the author's name
293 function parse_authorname()
295 // don't use on add item?
297 echo $member->getDisplayName();
302 * PageFactory::parse_title()
308 public function parse_title()
310 echo $this->contents['title'];
315 * PageFactory::checkCondition()
316 * Checks conditions for if statements
318 * @param string $field type of <%if%>
319 * @param string $name property of field
320 * @param string $value value of property
323 protected function checkCondition($field, $name = '', $value = 1)
331 $condition = (boolean) ($this->blog->getSetting($name) == $value);
334 $condition = (boolean) ($member->getAutosave() == $value);
337 if ( array_key_exists($name, $this->variables) )
339 $condition = (boolean) ($this->variables[$name] == $value);
349 * PageFactory::parse_helplink()
351 * @param string $topic
354 function parse_helplink($topic)
361 * PageFactory::parse_currenttime()
364 * @param string $what
367 public function parse_currenttime($what)
369 $nu = getdate($this->blog->getCorrectTime());
375 * PageFactory::parse_itemtime()
376 * date change on edit item
378 * @param string $what
381 public function parse_itemtime($what)
383 $itemtime = getdate($this->variables['timestamp']);
384 echo $itemtime[$what];
389 * PageFactory::parse_init()
390 * some init stuff for all forms
395 public function parse_init()
397 if ( $this->method != 'edit' )
403 $authorid = $this->variables['authorid'];
405 $this->blog->insertJavaScriptInfo($authorid);
410 * PageFactory::parse_extrahead()
411 * on bookmarklets only: insert extra html header information (by plugins)
416 public function parse_extrahead()
422 'extrahead' => &$extrahead
425 $manager->notify('BookmarkletExtraHead', $data);
431 * PageFactroy::parse_text()
432 * inserts some localized text
434 * @param string $which
437 public function parse_text($which)
439 if ( !defined($which) )
445 echo constant($which);
451 * PageFactory::parse_contents()
453 * @param string $which
456 public function parse_contents($which)
458 if ( !array_key_exists($which, $this->variables) || !isset($this->variables[$which]) )
460 $this->variables[$which] = '';
462 echo Entity::hsc($this->variables[$which]);
467 * PageFactory::parse_checkedonval()
469 * @param string $value value for input element with checkbox type
472 public function parse_checkedonval($value, $name)
474 if ( !array_key_exists($name, $this->variables) || !isset($this->variables[$name]) )
476 $this->variables[$name] = '';
478 if ( $this->variables[$name] == $value )
480 echo "checked='checked'";
486 * Pagefactory::parse_jsinput()
487 * extra javascript for input and textarea fields
489 * @param string $which name of JavaScript function
490 * @return string attribute for input element
492 public function parse_jsinput($which)
494 global $CONF, $member;
496 $attributes = " name=\"{$which}\"";
497 $attributes .= " id=\"input{$which}\"";
499 if ( $CONF['DisableJsTools'] != 1 )
501 $attributes .= ' onclick="storeCaret(this);"';
502 $attributes .= ' onselect="storeCaret(this);"';
503 if ( $member->getAutosave() )
505 $attributes .= " onkeyup=\"storeCaret(this); updPreview('{$which}'); doMonitor();\"";
509 $attributes .= " onkeyup=\"storeCaret(this); updPreview('{$which}');\"";
514 if ( $CONF['DisableJsTools'] == 0 )
516 $attributes .= ' onkeypress="shortCuts();"';
518 if ( $member->getAutosave() )
520 $attributes .= ' onkeyup="doMonitor();"';
528 * PageFactory::parse_jsbuttonbar()
529 * shows the javascript button bar
531 * @param string $extrabuttons
534 public function parse_jsbuttonbar($extrabuttons = "")
537 switch ( $CONF['DisableJsTools'] )
540 echo "<div class=\"jsbuttonbar\">\n";
541 $this->jsbutton('cut','cutThis()',_ADD_CUT_TT . " (Ctrl + X)");
542 $this->jsbutton('copy','copyThis()',_ADD_COPY_TT . " (Ctrl + C)");
543 $this->jsbutton('paste','pasteThis()',_ADD_PASTE_TT . " (Ctrl + V)");
544 $this->jsbuttonspacer();
545 $this->jsbutton('bold',"boldThis()", _ADD_BOLD_TT ." (Ctrl + Shift + B)");
546 $this->jsbutton('italic',"italicThis()", _ADD_ITALIC_TT ." (Ctrl + Shift + I)");
547 $this->jsbutton('link',"ahrefThis()", _ADD_HREF_TT ." (Ctrl + Shift + A)");
548 $this->jsbuttonspacer();
549 $this->jsbutton('alignleft',"alignleftThis()", _ADD_ALIGNLEFT_TT);
550 $this->jsbutton('alignright',"alignrightThis()", _ADD_ALIGNRIGHT_TT);
551 $this->jsbutton('aligncenter',"aligncenterThis()", _ADD_ALIGNCENTER_TT);
552 $this->jsbuttonspacer();
553 $this->jsbutton('left',"leftThis()", _ADD_LEFT_TT);
554 $this->jsbutton('right',"rightThis()", _ADD_RIGHT_TT);
558 $btns = preg_split('#\+#',$extrabuttons);
559 $this->jsbuttonspacer();
560 foreach ( $btns as $button )
565 $this->jsbutton('media', "addMedia()", _ADD_MEDIA_TT . " (Ctrl + Shift + M)");
568 $this->jsbutton('preview', "showedit()", _ADD_PREVIEW_TT);
576 echo "<div class=\"jsbuttonbar\">";
577 $this->jsbutton('bold',"boldThis()", _ADD_BOLD_TT);
578 $this->jsbutton('italic',"italicThis()", _ADD_ITALIC_TT);
579 $this->jsbutton('link',"ahrefThis()", _ADD_HREF_TT);
580 $this->jsbuttonspacer();
581 $this->jsbutton('alignleft',"alignleftThis()", _ADD_ALIGNLEFT_TT);
582 $this->jsbutton('alignright',"alignrightThis()", _ADD_ALIGNRIGHT_TT);
583 $this->jsbutton('aligncenter',"aligncenterThis()", _ADD_ALIGNCENTER_TT);
584 $this->jsbuttonspacer();
585 $this->jsbutton('left',"leftThis()", _ADD_LEFT_TT);
586 $this->jsbutton('right',"rightThis()", _ADD_RIGHT_TT);
590 $btns = preg_split('#\+#',$extrabuttons);
591 $this->jsbuttonspacer();
592 foreach ( $btns as $button )
597 $this->jsbutton('media', "addMedia()", _ADD_MEDIA_TT);
602 echo "<span style=\"clear:both;\"></span></div>\n";
609 * PageFactory::parse_pluginextras()
610 * Allows plugins to add their own custom fields
615 public function parse_pluginextras()
619 switch ( $this->method )
623 'blog' => &$this->blog
626 $manager->notify('AddItemFormExtras', $data);
630 'variables' => $this->variables,
631 'blog' => &$this->blog,
632 'itemid' => $this->variables['itemid']
634 $manager->notify('EditItemFormExtras', $data);
641 * PageFactory::parse_itemoptions()
642 * Adds the itemOptions of a plugin to a page
647 public function parse_itemoptions()
650 Admin::_insertPluginOptions('item', $itemid);
655 * PageFactory::parse_ticket()
657 public function parse_ticket()
660 $manager->addTicketHidden();
665 * PageFactory::jsbutton()
668 * @param string $type type of button
669 * @param string $code JavaScript codes for onclick event
670 * @param string $tooltip alternative text attribute for image element
672 private function jsbutton($type, $code, $tooltip)
674 echo "<span class=\"jsbutton\" onmouseover=\"BtnHighlight(this);\" onmouseout=\"BtnNormal(this);\" onclick=\"{$code}\" >";
675 echo "<img src=\"images/button-{$type}.gif\" alt=\"{$tooltip}\" title=\"{$tooltip}\" width=\"16\" height=\"16\" />";
681 * PageFactory::jsbuttonspacer()
686 private function jsbuttonspacer()
688 echo '<span class="jsbuttonspacer"></span>';