OSDN Git Service

CHANGE/REMOVE: <%text%>タグをスキンのどのコンテクストでも利用可能に。NP_Textの廃止。
[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                 'ticket',
78                 'title'
79         );
80         
81         /**
82          * PageFactory::__construct()
83          * Creates a new PAGEFACTORY object
84          * 
85          * @param       integer $blog_id
86          * @return      void
87          */
88         public function __construct($blog_id)
89         {
90                 parent::__construct();
91                 
92                 global $manager;
93                 
94                 $this->blog =& $manager->getBlog($blog_id);
95                 
96                 /*
97                  * TODO: maybe add 'skin' later on?
98                  * TODO: maybe add other pages from admin area
99                  */
100                 $this->allowed_types = array('bookmarklet', 'admin');
101                 return;
102         }
103         
104         /**
105          * PageFactory::getDefinedActions()
106          * 
107          * @static
108          * @param       void
109          * @return      array   array for defined action names
110          * 
111          */
112         static public function getDefinedActions()
113         {
114                 return array_merge(self::$defined_actions, parent::getDefinedActions());
115         }
116         
117         /**
118          * PageFactory::createAddForm()
119          * Creates an "add item" form for a given type of page
120          * 
121          * @param       string  $type           'admin' or 'bookmarklet'
122          * @param       array   $contents
123          * @return      void
124          */
125         public function createAddForm($type, $contents = array())
126         {
127                 global $manager;
128                 
129                 // begin if: the $type is not in the allowed types array
130                 if ( !in_array($type, $this->allowed_types) )
131                 {
132                         return;
133                 }
134                 
135                 $this->type = $type;
136                 $this->method = 'add';
137                 
138                 $manager->notify('PreAddItemForm', array('contents' => &$contents, 'blog' => &$this->blog));
139                 
140                 $this->createForm($contents);
141                 return;
142         }
143         
144         /**
145          * PageFactory::createEditForm()
146          * Creates an "edit item" form for a given type of page
147          * 
148          * @param       string  $type           'admin' or 'bookmarklet'
149          * @param       array   $contents
150          * @return      void
151          */
152         public function createEditForm($type, $contents)
153         {
154                 // begin if: the $type is not in the allowed types array
155                 if ( !in_array($type, $this->allowed_types) )
156                 {
157                         return;
158                 }
159                 
160                 $this->type = $type;
161                 $this->method = 'edit';
162                 $this->createForm($contents);
163                 return;
164         }
165         
166         /**
167          * PageFactory::createForm()
168          * creates a form for a given type of page
169          * 
170          * @param       array   $contents
171          * @return      void
172          */
173         private function createForm($contents)
174         {
175                 # save contents
176                 $this->variables = $contents;
177                 
178                 # get template to use
179                 $template = $this->getTemplateFor($this->type);
180                 
181                 # use the PARSER engine to parse that template
182                 $parser = new Parser(self::getDefinedActions(), $this);
183                 $parser->parse($template);
184                 return;
185         }
186         
187         /**
188          * PageFactory::getTemplateFor()
189          * Returns an appropriate template
190          * 
191          * @param       string  $type
192          * @return      string  contents of form template
193          */
194         private function getTemplateFor($type)
195         {
196                 global $DIR_LIBS;
197                 
198                 $filename = $DIR_LIBS . 'include/' . $this->type . '-' . $this->method . '.template';
199                 
200                 // begin if: file doesn't exist
201                 if ( !file_exists($filename) )
202                 {
203                         return '';
204                 } // end if
205                 
206                 $filesize = filesize($filename);
207                 
208                 // begin if: filesize is LTE zero
209                 if ( $filesize <= 0 )
210                 {
211                         return '';
212                 } // end if
213                 
214                 # read file and return it
215                 $fd = fopen ($filename, 'r');
216                 $contents = fread ($fd, $filesize);
217                 fclose ($fd);
218                 
219                 return $contents;
220         }
221         
222         /**
223          * PageFactory::parse_categories()
224          * Create category dropdown box
225          * 
226          * @param       integer $start_index
227          * @return      void
228          */
229         public function parse_categories($start_index = 0)
230         {
231                 // begin if: catid variable is set; use it to select the category
232                 if ( !array_key_exists('catid', $this->variables) )
233                 {
234                         $category_id = $this->blog->getDefaultCategory();
235                 }
236                 // else: get the default category
237                 else
238                 {
239                         $category_id = $this->variables['catid'];
240                 }
241                 
242                 Admin::selectBlogCategory('catid', $category_id, $start_index, 1, $this->blog->getID());
243                 return;
244         }
245         
246         /**
247          * PageFactory::parse_blogid()
248          * Displays the blog ID
249          * 
250          * @param       void
251          * @return      void
252          */
253         public function parse_blogid()
254         {
255                 echo $this->blog->getID();
256                 return;
257         }
258         
259         /**
260          * PageFactory::parse_blogname()
261          * Displays the blog name
262          * 
263          * @param       void
264          * @return      void
265          */
266         public function parse_blogname()
267         {
268                 echo $this->blog->getName();
269                 return;
270         }
271         
272         /**
273          * PageFactory::parse_bloglink()
274          * Displays the blog link
275          * 
276          * @param       void
277          * @return      void
278          */
279         public function parse_bloglink()
280         {
281                 echo '<a href="', Entity::hsc($this->blog->getURL()), '">', Entity::hsc($this->blog->getName()), '</a>';
282                 return;
283         }
284         
285         /**
286          * PageFactory::parse_authorname()
287          * Displays the author's name
288          * 
289          * @param       void
290          * @return      void
291          */
292         function parse_authorname()
293         {
294                 // don't use on add item?
295                 global $member;
296                 echo $member->getDisplayName();
297                 return;
298         }
299         
300         /**
301          * PageFactory::parse_title()
302          * Displays the title
303          * 
304          * @param       void
305          * @return      void
306          */
307         public function parse_title()
308         {
309                 echo $this->contents['title'];
310                 return;
311         }
312         
313         /**
314          * PageFactory::checkCondition()
315          * Checks conditions for if statements
316          *
317          * @param       string  $field  type of <%if%>
318          * @param       string  $name   property of field
319          * @param       string  $value  value of property
320          * @return      boolean
321          */
322         protected function checkCondition($field, $name = '', $value = 1)
323         {
324                 global $member;
325                 
326                 $condition = 0;
327                 switch ( $field )
328                 {
329                         case 'blogsetting':
330                                 $condition = (boolean) ($this->blog->getSetting($name) == $value);
331                                 break;
332                         case 'autosave':
333                                 $condition = (boolean) ($member->getAutosave() == $value);
334                                 break;
335                         case 'itemproperty':
336                                 if ( array_key_exists($name, $this->variables) )
337                                 {
338                                         $condition = (boolean) ($this->variables[$name] == $value);
339                                 }
340                                 break;
341                         default:
342                                 break;
343                 }
344                 return $condition;
345         }
346         
347         /**
348          * PageFactory::parse_helplink()
349          * 
350          * @param       string  $topic
351          * @return      void
352          */
353         function parse_helplink($topic)
354         {
355                 help($topic);
356                 return;
357         }
358         
359         /**
360          * PageFactory::parse_currenttime()
361          * for future items
362          * 
363          * @param       string  $what
364          * @return      void
365          */
366         public function parse_currenttime($what)
367         {
368                 $nu = getdate($this->blog->getCorrectTime());
369                 echo $nu[$what];
370                 return;
371         }
372         
373         /**
374          * PageFactory::parse_itemtime()
375          * date change on edit item
376          * 
377          * @param       string  $what
378          * @return      void
379          */
380         public function parse_itemtime($what)
381         {
382                 $itemtime = getdate($this->variables['timestamp']);
383                 echo $itemtime[$what];
384                 return;
385         }
386         
387         /**
388          * PageFactory::parse_init()
389          * some init stuff for all forms
390          * 
391          * @param       void
392          * @return      void
393          */
394         public function parse_init()
395         {
396                 if ( $this->method != 'edit' )
397                 {
398                         $authorid = '';
399                 }
400                 else
401                 {
402                         $authorid = $this->variables['authorid'];
403                 }
404                 $this->blog->insertJavaScriptInfo($authorid);
405                 return;
406         }
407         
408         /**
409          * PageFactory::parse_extrahead()
410          * on bookmarklets only: insert extra html header information (by plugins)
411          * 
412          * @param       void
413          * @return      void
414          */
415         public function parse_extrahead()
416         {
417                 global $manager;
418                 
419                 $extrahead = '';
420                 $data = array(
421                         'extrahead' => &$extrahead
422                 );
423                 
424                 $manager->notify('BookmarkletExtraHead', $data);
425                 echo $extrahead;
426                 return;
427         }
428         
429         /**
430          * PageFactory::parse_contents()
431          * 
432          * @param       string  $which
433          * @return      void
434          */
435         public function parse_contents($which)
436         {
437                 if ( !array_key_exists($which, $this->variables) || !isset($this->variables[$which]) )
438                 {
439                         $this->variables[$which] = '';
440                 }
441                 echo Entity::hsc($this->variables[$which]);
442                 return;
443         }
444         
445         /**
446          * PageFactory::parse_checkedonval()
447          * 
448          * @param       string  $value  value for input element with checkbox type
449          * @return      void
450          */
451         public function parse_checkedonval($value, $name)
452         {
453                 if ( !array_key_exists($name, $this->variables) || !isset($this->variables[$name]) )
454                 {
455                         $this->variables[$name] = '';
456                 }
457                 if ( $this->variables[$name] == $value )
458                 {
459                         echo "checked='checked'";
460                 }
461                 return;
462         }
463         
464         /**
465          * Pagefactory::parse_jsinput()
466          * extra javascript for input and textarea fields
467          * 
468          * @param       string  $which  name of JavaScript function
469          * @return      string  attribute for input element
470          */
471         public function parse_jsinput($which)
472         {
473                 global $CONF, $member;
474                 
475                 $attributes  = " name=\"{$which}\"";
476                 $attributes .= " id=\"input{$which}\"";
477                 
478                 if ( $CONF['DisableJsTools'] != 1 )
479                 {
480                         $attributes .= ' onclick="storeCaret(this);"';
481                         $attributes .= ' onselect="storeCaret(this);"';
482                         if ( $member->getAutosave() )
483                         {
484                                 $attributes .= " onkeyup=\"storeCaret(this); updPreview('{$which}'); doMonitor();\"";
485                         }
486                         else
487                         {
488                                 $attributes .= " onkeyup=\"storeCaret(this); updPreview('{$which}');\"";
489                         }
490                 }
491                 else
492                 {
493                         if ( $CONF['DisableJsTools'] == 0 )
494                         {
495                                 $attributes .= ' onkeypress="shortCuts();"';
496                         }
497                         if ( $member->getAutosave() )
498                         {
499                                 $attributes .= ' onkeyup="doMonitor();"';
500                         }
501                 }
502                 echo $attributes;
503                 return;
504         }
505         
506         /**
507          * PageFactory::parse_jsbuttonbar()
508          * shows the javascript button bar
509          * 
510          * @param       string  $extrabuttons   
511          * @return      void
512          */
513         public function parse_jsbuttonbar($extrabuttons = "")
514         {
515                 global $CONF;
516                 switch ( $CONF['DisableJsTools'] )
517                 {
518                         case "0":
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);
534                                 
535                                 if ( $extrabuttons )
536                                 {
537                                         $btns = preg_split('#\+#',$extrabuttons);
538                                         $this->jsbuttonspacer();
539                                         foreach ( $btns as $button )
540                                         {
541                                                 switch ( $button )
542                                                 {
543                                                         case "media":
544                                                                 $this->jsbutton('media', "addMedia()", _ADD_MEDIA_TT .  " (Ctrl + Shift + M)");
545                                                                 break;
546                                                         case "preview":
547                                                                 $this->jsbutton('preview', "showedit()", _ADD_PREVIEW_TT);
548                                                                 break;
549                                                 }
550                                         }
551                                 }
552                                 echo "</div>\n";
553                                 break;
554                         case "2":
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);
566
567                                 if ( $extrabuttons )
568                                 {
569                                         $btns = preg_split('#\+#',$extrabuttons);
570                                         $this->jsbuttonspacer();
571                                         foreach ( $btns as $button )
572                                         {
573                                                 switch ( $button )
574                                                 {
575                                                         case "media":
576                                                                 $this->jsbutton('media', "addMedia()", _ADD_MEDIA_TT);
577                                                                 break;
578                                                 }
579                                         }
580                                 }
581                                 echo "<span style=\"clear:both;\"></span></div>\n";
582                                 break;
583                 }
584                 return;
585         }
586         
587         /**
588          * PageFactory::parse_pluginextras()
589          * Allows plugins to add their own custom fields
590          * 
591          * @param       void
592          * @return      void
593          */
594         public function parse_pluginextras()
595         {
596                 global $manager;
597                 
598                 switch ( $this->method )
599                 {
600                         case 'add':
601                                 $data = array(
602                                         'blog' => &$this->blog
603                                 );
604                                 
605                                 $manager->notify('AddItemFormExtras', $data);
606                                 break;
607                         case 'edit':
608                                 $data = array(
609                                         'variables'     => $this->variables,
610                                         'blog'          => &$this->blog,
611                                         'itemid'        => $this->variables['itemid']
612                                 );
613                                 $manager->notify('EditItemFormExtras', $data);
614                                 break;
615                 }
616                 return;
617         }
618         
619         /**
620          * PageFactory::parse_itemoptions()
621          * Adds the itemOptions of a plugin to a page
622          * 
623          * @param       void
624          * @return      void
625          */
626         public function parse_itemoptions()
627         {
628                 global $itemid;
629                 Admin::_insertPluginOptions('item', $itemid);
630                 return;
631         }
632         
633         /**
634          * PageFactory::parse_ticket()
635          */
636         public function parse_ticket()
637         {
638                 global $manager;
639                 $manager->addTicketHidden();
640                 return;
641         }
642         
643         /**
644          * PageFactory::jsbutton()
645          * convenience method
646          * 
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
650          */
651         private function jsbutton($type, $code, $tooltip)
652         {
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\" />";
655                 echo "</span>\n";
656                 return;
657         }
658         
659         /**
660          * PageFactory::jsbuttonspacer()
661          * 
662          * @param       void
663          * @return      void
664          */
665         private function jsbuttonspacer()
666         {
667                 echo '<span class="jsbuttonspacer"></span>';
668                 return;
669         }
670 }