OSDN Git Service

e91e16d058fbf19c6f9e077a9a8f29d0db62ce9e
[nucleus-jp/nucleus-next.git] / nucleus / libs / PAGEFACTORY.php
1 <?php
2 /*
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4  * Copyright (C) 2002-2009 The Nucleus Group
5  *
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)
11  */
12 /**
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 $
16  */
17
18 /**
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
21  * might be needed
22  */
23 class PageFactory extends BaseActions
24 {
25         /**
26          * PageFactory::$blog
27          * Reference to the blog object for which an add:edit form is created
28          */
29         private $blog;
30         
31         /**
32          * PageFactory::$allowed_types
33          * Allowed types of forms (bookmarklet/admin)
34          */
35         private $allowed_types;
36         
37         /**
38          * PageFactory::$type
39          * One of the types in $allowed_types
40          */
41         private $type;
42         
43         /**
44          * PageFactory::$method
45          * 'add' or 'edit'
46          */
47         private $method;
48         
49         /**
50          * PageFactory::$variables
51          * Info to fill out in the form (e.g. catid, itemid, ...)
52          */
53         private $variables;
54         
55         /**
56          * PageFactory::$actions
57          * Allowed actions (for parser)
58          */
59         // TODO: move the definition of actions to the createXForm methods
60         static private $defined_actions = array(
61                 'authorname',
62                 'blogid',
63                 'bloglink',
64                 'blogname',
65                 'categories',
66                 'checkedonval',
67                 'contents',
68                 'currenttime',
69                 'extrahead',
70                 'helplink',
71                 'init',
72                 'itemoptions',
73                 'itemtime',
74                 'jsbuttonbar',
75                 'jsinput',
76                 'pluginextras',
77                 'text',
78                 'ticket',
79                 'title'
80         );
81         
82         /**
83          * PageFactory::__construct()
84          * Creates a new PAGEFACTORY object
85          * 
86          * @param       integer $blog_id
87          * @return      void
88          */
89         public function __construct($blog_id)
90         {
91                 parent::__construct();
92                 
93                 global $manager;
94                 
95                 $this->blog =& $manager->getBlog($blog_id);
96                 
97                 /*
98                  * TODO: maybe add 'skin' later on?
99                  * TODO: maybe add other pages from admin area
100                  */
101                 $this->allowed_types = array('bookmarklet', 'admin');
102                 return;
103         }
104         
105         /**
106          * PageFactory::getDefinedActions()
107          * 
108          * @static
109          * @param       void
110          * @return      array   array for defined action names
111          * 
112          */
113         static public function getDefinedActions()
114         {
115                 return array_merge(self::$defined_actions, parent::getDefinedActions());
116         }
117         
118         /**
119          * PageFactory::createAddForm()
120          * Creates an "add item" form for a given type of page
121          * 
122          * @param       string  $type           'admin' or 'bookmarklet'
123          * @param       array   $contents
124          * @return      void
125          */
126         public function createAddForm($type, $contents = array())
127         {
128                 global $manager;
129                 
130                 // begin if: the $type is not in the allowed types array
131                 if ( !in_array($type, $this->allowed_types) )
132                 {
133                         return;
134                 }
135                 
136                 $this->type = $type;
137                 $this->method = 'add';
138                 
139                 $manager->notify('PreAddItemForm', array('contents' => &$contents, 'blog' => &$this->blog));
140                 
141                 $this->createForm($contents);
142                 return;
143         }
144         
145         /**
146          * PageFactory::createEditForm()
147          * Creates an "edit item" form for a given type of page
148          * 
149          * @param       string  $type           'admin' or 'bookmarklet'
150          * @param       array   $contents
151          * @return      void
152          */
153         public function createEditForm($type, $contents)
154         {
155                 // begin if: the $type is not in the allowed types array
156                 if ( !in_array($type, $this->allowed_types) )
157                 {
158                         return;
159                 }
160                 
161                 $this->type = $type;
162                 $this->method = 'edit';
163                 $this->createForm($contents);
164                 return;
165         }
166         
167         /**
168          * PageFactory::createForm()
169          * creates a form for a given type of page
170          * 
171          * @param       array   $contents
172          * @return      void
173          */
174         private function createForm($contents)
175         {
176                 # save contents
177                 $this->variables = $contents;
178                 
179                 # get template to use
180                 $template = $this->getTemplateFor($this->type);
181                 
182                 # use the PARSER engine to parse that template
183                 $parser = new Parser(self::getDefinedActions(), $this);
184                 $parser->parse($template);
185                 return;
186         }
187         
188         /**
189          * PageFactory::getTemplateFor()
190          * Returns an appropriate template
191          * 
192          * @param       string  $type
193          * @return      string  contents of form template
194          */
195         private function getTemplateFor($type)
196         {
197                 global $DIR_LIBS;
198                 
199                 $filename = $DIR_LIBS . 'include/' . $this->type . '-' . $this->method . '.template';
200                 
201                 // begin if: file doesn't exist
202                 if ( !file_exists($filename) )
203                 {
204                         return '';
205                 } // end if
206                 
207                 $filesize = filesize($filename);
208                 
209                 // begin if: filesize is LTE zero
210                 if ( $filesize <= 0 )
211                 {
212                         return '';
213                 } // end if
214                 
215                 # read file and return it
216                 $fd = fopen ($filename, 'r');
217                 $contents = fread ($fd, $filesize);
218                 fclose ($fd);
219                 
220                 return $contents;
221         }
222         
223         /**
224          * PageFactory::parse_categories()
225          * Create category dropdown box
226          * 
227          * @param       integer $start_index
228          * @return      void
229          */
230         public function parse_categories($start_index = 0)
231         {
232                 // begin if: catid variable is set; use it to select the category
233                 if ( !array_key_exists('catid', $this->variables) )
234                 {
235                         $category_id = $this->blog->getDefaultCategory();
236                 }
237                 // else: get the default category
238                 else
239                 {
240                         $category_id = $this->variables['catid'];
241                 }
242                 
243                 Admin::selectBlogCategory('catid', $category_id, $start_index, 1, $this->blog->getID());
244                 return;
245         }
246         
247         /**
248          * PageFactory::parse_blogid()
249          * Displays the blog ID
250          * 
251          * @param       void
252          * @return      void
253          */
254         public function parse_blogid()
255         {
256                 echo $this->blog->getID();
257                 return;
258         }
259         
260         /**
261          * PageFactory::parse_blogname()
262          * Displays the blog name
263          * 
264          * @param       void
265          * @return      void
266          */
267         public function parse_blogname()
268         {
269                 echo $this->blog->getName();
270                 return;
271         }
272         
273         /**
274          * PageFactory::parse_bloglink()
275          * Displays the blog link
276          * 
277          * @param       void
278          * @return      void
279          */
280         public function parse_bloglink()
281         {
282                 echo '<a href="', Entity::hsc($this->blog->getURL()), '">', Entity::hsc($this->blog->getName()), '</a>';
283                 return;
284         }
285         
286         /**
287          * PageFactory::parse_authorname()
288          * Displays the author's name
289          * 
290          * @param       void
291          * @return      void
292          */
293         function parse_authorname()
294         {
295                 // don't use on add item?
296                 global $member;
297                 echo $member->getDisplayName();
298                 return;
299         }
300         
301         /**
302          * PageFactory::parse_title()
303          * Displays the title
304          * 
305          * @param       void
306          * @return      void
307          */
308         public function parse_title()
309         {
310                 echo $this->contents['title'];
311                 return;
312         }
313         
314         /**
315          * PageFactory::checkCondition()
316          * Checks conditions for if statements
317          *
318          * @param       string  $field  type of <%if%>
319          * @param       string  $name   property of field
320          * @param       string  $value  value of property
321          * @return      boolean
322          */
323         protected function checkCondition($field, $name = '', $value = 1)
324         {
325                 global $member;
326                 
327                 $condition = 0;
328                 switch ( $field )
329                 {
330                         case 'blogsetting':
331                                 $condition = (boolean) ($this->blog->getSetting($name) == $value);
332                                 break;
333                         case 'autosave':
334                                 $condition = (boolean) ($member->getAutosave() == $value);
335                                 break;
336                         case 'itemproperty':
337                                 if ( array_key_exists($name, $this->variables) )
338                                 {
339                                         $condition = (boolean) ($this->variables[$name] == $value);
340                                 }
341                                 break;
342                         default:
343                                 break;
344                 }
345                 return $condition;
346         }
347         
348         /**
349          * PageFactory::parse_helplink()
350          * 
351          * @param       string  $topic
352          * @return      void
353          */
354         function parse_helplink($topic)
355         {
356                 help($topic);
357                 return;
358         }
359         
360         /**
361          * PageFactory::parse_currenttime()
362          * for future items
363          * 
364          * @param       string  $what
365          * @return      void
366          */
367         public function parse_currenttime($what)
368         {
369                 $nu = getdate($this->blog->getCorrectTime());
370                 echo $nu[$what];
371                 return;
372         }
373         
374         /**
375          * PageFactory::parse_itemtime()
376          * date change on edit item
377          * 
378          * @param       string  $what
379          * @return      void
380          */
381         public function parse_itemtime($what)
382         {
383                 $itemtime = getdate($this->variables['timestamp']);
384                 echo $itemtime[$what];
385                 return;
386         }
387         
388         /**
389          * PageFactory::parse_init()
390          * some init stuff for all forms
391          * 
392          * @param       void
393          * @return      void
394          */
395         public function parse_init()
396         {
397                 if ( $this->method != 'edit' )
398                 {
399                         $authorid = '';
400                 }
401                 else
402                 {
403                         $authorid = $this->variables['authorid'];
404                 }
405                 $this->blog->insertJavaScriptInfo($authorid);
406                 return;
407         }
408         
409         /**
410          * PageFactory::parse_extrahead()
411          * on bookmarklets only: insert extra html header information (by plugins)
412          * 
413          * @param       void
414          * @return      void
415          */
416         public function parse_extrahead()
417         {
418                 global $manager;
419                 
420                 $extrahead = '';
421                 $data = array(
422                         'extrahead' => &$extrahead
423                 );
424                 
425                 $manager->notify('BookmarkletExtraHead', $data);
426                 echo $extrahead;
427                 return;
428         }
429         
430         /**
431          * PageFactroy::parse_text()
432          * inserts some localized text
433          * 
434          * @param       string  $which
435          * @return      void
436          */
437         public function parse_text($which)
438         {
439                 if ( !defined($which) )
440                 {
441                         echo $which;
442                 }
443                 else
444                 {
445                         echo constant($which);
446                 }
447                 return;
448         }
449         
450         /**
451          * PageFactory::parse_contents()
452          * 
453          * @param       string  $which
454          * @return      void
455          */
456         public function parse_contents($which)
457         {
458                 if ( !array_key_exists($which, $this->variables) || !isset($this->variables[$which]) )
459                 {
460                         $this->variables[$which] = '';
461                 }
462                 echo Entity::hsc($this->variables[$which]);
463                 return;
464         }
465         
466         /**
467          * PageFactory::parse_checkedonval()
468          * 
469          * @param       string  $value  value for input element with checkbox type
470          * @return      void
471          */
472         public function parse_checkedonval($value, $name)
473         {
474                 if ( !array_key_exists($name, $this->variables) || !isset($this->variables[$name]) )
475                 {
476                         $this->variables[$name] = '';
477                 }
478                 if ( $this->variables[$name] == $value )
479                 {
480                         echo "checked='checked'";
481                 }
482                 return;
483         }
484         
485         /**
486          * Pagefactory::parse_jsinput()
487          * extra javascript for input and textarea fields
488          * 
489          * @param       string  $which  name of JavaScript function
490          * @return      string  attribute for input element
491          */
492         public function parse_jsinput($which)
493         {
494                 global $CONF, $member;
495                 
496                 $attributes  = " name=\"{$which}\"";
497                 $attributes .= " id=\"input{$which}\"";
498                 
499                 if ( $CONF['DisableJsTools'] != 1 )
500                 {
501                         $attributes .= ' onclick="storeCaret(this);"';
502                         $attributes .= ' onselect="storeCaret(this);"';
503                         if ( $member->getAutosave() )
504                         {
505                                 $attributes .= " onkeyup=\"storeCaret(this); updPreview('{$which}'); doMonitor();\"";
506                         }
507                         else
508                         {
509                                 $attributes .= " onkeyup=\"storeCaret(this); updPreview('{$which}');\"";
510                         }
511                 }
512                 else
513                 {
514                         if ( $CONF['DisableJsTools'] == 0 )
515                         {
516                                 $attributes .= ' onkeypress="shortCuts();"';
517                         }
518                         if ( $member->getAutosave() )
519                         {
520                                 $attributes .= ' onkeyup="doMonitor();"';
521                         }
522                 }
523                 echo $attributes;
524                 return;
525         }
526         
527         /**
528          * PageFactory::parse_jsbuttonbar()
529          * shows the javascript button bar
530          * 
531          * @param       string  $extrabuttons   
532          * @return      void
533          */
534         public function parse_jsbuttonbar($extrabuttons = "")
535         {
536                 global $CONF;
537                 switch ( $CONF['DisableJsTools'] )
538                 {
539                         case "0":
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);
555                                 
556                                 if ( $extrabuttons )
557                                 {
558                                         $btns = preg_split('#\+#',$extrabuttons);
559                                         $this->jsbuttonspacer();
560                                         foreach ( $btns as $button )
561                                         {
562                                                 switch ( $button )
563                                                 {
564                                                         case "media":
565                                                                 $this->jsbutton('media', "addMedia()", _ADD_MEDIA_TT .  " (Ctrl + Shift + M)");
566                                                                 break;
567                                                         case "preview":
568                                                                 $this->jsbutton('preview', "showedit()", _ADD_PREVIEW_TT);
569                                                                 break;
570                                                 }
571                                         }
572                                 }
573                                 echo "</div>\n";
574                                 break;
575                         case "2":
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);
587
588                                 if ( $extrabuttons )
589                                 {
590                                         $btns = preg_split('#\+#',$extrabuttons);
591                                         $this->jsbuttonspacer();
592                                         foreach ( $btns as $button )
593                                         {
594                                                 switch ( $button )
595                                                 {
596                                                         case "media":
597                                                                 $this->jsbutton('media', "addMedia()", _ADD_MEDIA_TT);
598                                                                 break;
599                                                 }
600                                         }
601                                 }
602                                 echo "</div>\n";
603                                 break;
604                 }
605                 return;
606         }
607         
608         /**
609          * PageFactory::parse_pluginextras()
610          * Allows plugins to add their own custom fields
611          * 
612          * @param       void
613          * @return      void
614          */
615         public function parse_pluginextras()
616         {
617                 global $manager;
618                 
619                 switch ( $this->method )
620                 {
621                         case 'add':
622                                 $data = array(
623                                         'blog' => &$this->blog
624                                 );
625                                 
626                                 $manager->notify('AddItemFormExtras', $data);
627                                 break;
628                         case 'edit':
629                                 $data = array(
630                                         'variables'     => $this->variables,
631                                         'blog'          => &$this->blog,
632                                         'itemid'        => $this->variables['itemid']
633                                 );
634                                 $manager->notify('EditItemFormExtras', $data);
635                                 break;
636                 }
637                 return;
638         }
639         
640         /**
641          * PageFactory::parse_itemoptions()
642          * Adds the itemOptions of a plugin to a page
643          * 
644          * @param       void
645          * @return      void
646          */
647         public function parse_itemoptions()
648         {
649                 global $itemid;
650                 Admin::_insertPluginOptions('item', $itemid);
651                 return;
652         }
653         
654         /**
655          * PageFactory::parse_ticket()
656          */
657         public function parse_ticket()
658         {
659                 global $manager;
660                 $manager->addTicketHidden();
661                 return;
662         }
663         
664         /**
665          * PageFactory::jsbutton()
666          * convenience method
667          * 
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
671          */
672         private function jsbutton($type, $code, $tooltip)
673         {
674                 echo "<span class=\"jsbutton\" onmouseover=\"BtnHighlight(this);\" onmouseout=\"BtnNormal(this);\" onclick=\"{$code}\" >\n";
675                 echo "<img src=\"images/button-{$type}.gif\" alt=\"{$tooltip}\" title=\"{$tooltip}\" width=\"16\" height=\"16\" />\n";
676                 echo "</span>\n";
677                 return;
678         }
679         
680         /**
681          * PageFactory::jsbuttonspacer()
682          * 
683          * @param       void
684          * @return      void
685          */
686         private function jsbuttonspacer()
687         {
688                 echo '<span class="jsbuttonspacer"></span>';
689                 return;
690         }
691 }