OSDN Git Service

CHANGE: ENTITYクラスの整備。globalfunctions.phpの整理。
[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 1626 2012-01-09 15:46:54Z 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         /**
27          * Reference to the blog object for which an add:edit form is created
28          */
29         private $blog;
30
31         /**
32          * Allowed actions (for parser)
33          */
34         private $actions;
35
36         /**
37          * Allowed types of forms (bookmarklet/admin)
38          */
39         private $allowed_types;
40
41         /**
42          * One of the types in $allowed_types
43          */
44         private $type;
45
46         /**
47          * 'add' or 'edit'
48          */
49         private $method;
50
51         /**
52          * Info to fill out in the form (e.g. catid, itemid, ...)
53          */
54         private $variables;
55
56
57         /**
58          * Creates a new PAGEFACTORY object
59          * @param int $blog_id
60          */
61         public function __construct($blog_id)
62         {
63                 # Call constructor of superclass first
64                 parent::__construct();
65
66                 global $manager;
67                 $this->blog =& $manager->getBlog($blog_id);
68
69                 // TODO: move the definition of actions to the createXForm methods
70                 $this->actions = array(
71                         'actionurl',
72                         'title',
73                         'body',
74                         'more',
75                         'blogid',
76                         'bloglink',
77                         'blogname',
78                         'authorname',
79                         'checkedonval',
80                         'helplink',
81                         'currenttime',
82                         'itemtime',
83                         'init',
84                         'text',
85                         'jsinput',
86                         'jsbuttonbar',
87                         'categories',
88                         'contents',
89                         'ifblogsetting',
90                         'ifitemproperty',
91                         'else',
92                         'endif',
93                         'pluginextras',
94                         'itemoptions',
95                         'extrahead',
96                         'ticket',
97                         'autosave',
98                         'autosaveinfo',
99                         'ifautosave',
100                 );
101
102                 # TODO: maybe add 'skin' later on?
103                 # TODO: maybe add other pages from admin area
104                 $this->allowed_types = array('bookmarklet', 'admin');
105         }
106
107
108         /**
109          * Creates an "add item" form for a given type of page
110          * @param string $type - 'admin' or 'bookmarklet'
111          * @param array $contents
112          */
113         public function createAddForm($type, $contents = array())
114         {
115
116                 // begin if: the $type is not in the allowed types array
117                 if ( !in_array($type, $this->allowed_types) )
118                 {
119                         return;
120                 } // end if
121
122                 $this->type = $type;
123                 $this->method = 'add';
124
125                 global $manager;
126                 $manager->notify('PreAddItemForm', array('contents' => &$contents, 'blog' => &$this->blog));
127
128                 $this->createForm($contents);
129         }
130
131
132         /**
133          * Creates an "edit item" form for a given type of page
134          * @param string $type 'admin' or 'bookmarklet'
135          * @param array $contents
136          */
137         public function createEditForm($type, $contents)
138         {
139
140                 // begin if: the $type is not in the allowed types array
141                 if ( !in_array($type, $this->allowed_types) )
142                 {
143                         return;
144                 } // end if
145
146                 $this->type = $type;
147                 $this->method = 'edit';
148                 $this->createForm($contents);
149         }
150
151
152         /**
153          * (private) creates a form for a given type of page
154          * @param array $contents
155          */
156         private function createForm($contents)
157         {
158                 # save contents
159                 $this->variables = $contents;
160
161                 # get template to use
162                 $template = $this->getTemplateFor($this->type);
163
164                 # use the PARSER engine to parse that template
165                 $parser = new PARSER($this->actions, $this);
166                 $parser->parse($template);
167         }
168
169
170         /**
171          * Returns an appropriate template
172          * @param string $type
173          */
174         private function getTemplateFor($type)
175         {
176                 global $DIR_LIBS;
177
178                 $filename = $DIR_LIBS . 'include/' . $this->type . '-' . $this->method . '.template';
179
180                 // begin if: file doesn't exist
181                 if ( !file_exists($filename) )
182                 {
183                         return '';
184                 } // end if
185
186                 $filesize = filesize($filename);
187
188                 // begin if: filesize is LTE zero
189                 if ( $filesize <= 0 )
190                 {
191                         return '';
192                 } // end if
193
194                 # read file and return it
195                 $fd = fopen ($filename, 'r');
196                 $contents = fread ($fd, $filesize);
197                 fclose ($fd);
198
199                 return $contents;
200         }
201
202
203         /**
204          * Create category dropdown box
205          * @param int $start_index
206          */
207         function parse_categories($start_index = 0)
208         {
209
210                 // begin if: catid variable is set; use it to select the category
211                 if ( $this->variables['catid'] )
212                 {
213                         $category_id = $this->variables['catid'];
214                 }
215                 // else: get the default category
216                 else
217                 {
218                         $category_id = $this->blog->getDefaultCategory();
219                 } // end if
220
221                 ADMIN::selectBlogCategory('catid', $category_id, $start_index, 1, $this->blog->getID());
222         }
223
224
225         /**
226          * Displays the blog ID
227          */
228         function parse_blogid()
229         {
230                 echo $this->blog->getID();
231         }
232
233
234         /**
235          * Displays the blog name
236          */
237         function parse_blogname()
238         {
239                 echo $this->blog->getName();
240         }
241
242
243         /**
244          * Displays the blog link
245          */
246         function parse_bloglink()
247         {
248                 echo '<a href="', ENTITY::hsc($this->blog->getURL()), '">', ENTITY::hsc($this->blog->getName()), '</a>';
249         }
250
251
252         /**
253          * Displays the author's name
254          */
255         function parse_authorname()
256         {
257                 // don't use on add item?
258                 global $member;
259                 echo $member->getDisplayName();
260         }
261
262
263         /**
264          * Displays the title
265          */
266         function parse_title()
267         {
268                 echo $this->contents['title'];
269         }
270
271
272         /**
273          * Indicates the start of a conditional block of data. It will be added to
274          * the output only if the blogsetting with the given name equals the
275          * given value (default for value = 1 = true)
276          *
277          * the name of the blogsetting is the column name in the nucleus_blog table
278          *
279          * the conditional block ends with an <endif> var
280          */
281         function parse_ifblogsetting($name,$value=1)
282         {
283                 $this->_addIfCondition(($this->blog->getSetting($name) == $value));
284         }
285
286
287         /**
288          *
289          */
290         function parse_ifitemproperty($name,$value=1)
291         {
292                 $this->_addIfCondition(($this->variables[$name] == $value));
293         }
294
295
296         /**
297          *
298          */
299         function parse_ifautosave($name,$value=1)
300         {
301                 global $member;
302                 $this->_addIfCondition($member->getAutosave() == $value);
303         }
304
305
306         /**
307          *
308          */
309         function parse_helplink($topic)
310         {
311                 help($topic);
312         }
313
314
315         /**
316          * for future items
317          */
318         function parse_currenttime($what)
319         {
320                 $nu = getdate($this->blog->getCorrectTime());
321                 echo $nu[$what];
322         }
323
324
325         /**
326          *
327          */
328         // date change on edit item
329         function parse_itemtime($what)
330         {
331                 $itemtime = getdate($this->variables['timestamp']);
332                 echo $itemtime[$what];
333         }
334
335
336         /**
337          * some init stuff for all forms
338          */
339         function parse_init()
340         {
341                 $authorid = ($this->method == 'edit') ? $this->variables['authorid'] : '';
342                 $this->blog->insertJavaScriptInfo($authorid);
343         }
344
345
346         /**
347          * on bookmarklets only: insert extra html header information (by plugins)
348          */
349         function parse_extrahead()
350         {
351                 global $manager;
352
353                 $extrahead = '';
354
355                 $manager->notify(
356                         'BookmarkletExtraHead',
357                         array(
358                                 'extrahead' => &$extrahead
359                         )
360                 );
361
362                 echo $extrahead;
363         }
364
365
366         /**
367          * inserts some localized text
368          */
369         function parse_text($which)
370         {
371                 // constant($which) only available from 4.0.4 :(
372                 if (defined($which)) {
373                         eval("echo $which;");
374                 } else {
375                         echo $which;    // this way we see where definitions are missing
376                 }
377
378         }
379
380
381         /**
382          *
383          */
384         function parse_contents($which)
385         {
386                 if (!isset($this->variables[$which])) $this->variables[$which] = '';
387                 echo ENTITY::hsc($this->variables[$which]);
388         }
389
390
391         /**
392          *
393          */
394         function parse_checkedonval($value, $name)
395         {
396                 if (!isset($this->variables[$name])) $this->variables[$name] = '';
397                 if ($this->variables[$name] == $value)
398                         echo "checked='checked'";
399         }
400
401
402         /**
403          * extra javascript for input and textarea fields
404          */
405         function parse_jsinput($which)
406         {
407                 global $CONF, $member;
408
409                 $attributes  = " name=\"{$which}\"";
410                 $attributes .= " id=\"input{$which}\"";
411
412                 if ($CONF['DisableJsTools'] != 1) {
413                         $attributes .= ' onclick="storeCaret(this);"';
414                         $attributes .= ' onselect="storeCaret(this);"';
415                         if ($member->getAutosave()) {
416                                 $attributes .= " onkeyup=\"storeCaret(this); updPreview('{$which}'); doMonitor();\"";
417                         } else {
418                                 $attributes .= " onkeyup=\"storeCaret(this); updPreview('{$which}');\"";
419                         }
420                 }
421                 else {
422                         if ($CONF['DisableJsTools'] == 0) {
423                                 $attributes .= ' onkeypress="shortCuts();"';
424                         }
425                         if ($member->getAutosave()) {
426                                 $attributes .= ' onkeyup="doMonitor();"';
427                         }
428                 }
429                 echo $attributes;
430         }
431
432
433         /**
434          * shows the javascript button bar
435          */
436         function parse_jsbuttonbar($extrabuttons = "")
437         {
438                 global $CONF;
439                 switch($CONF['DisableJsTools']) {
440
441                         case "0":
442                                 echo '<div class="jsbuttonbar">';
443
444                                         $this->_jsbutton('cut','cutThis()',_ADD_CUT_TT . " (Ctrl + X)");
445                                         $this->_jsbutton('copy','copyThis()',_ADD_COPY_TT . " (Ctrl + C)");
446                                         $this->_jsbutton('paste','pasteThis()',_ADD_PASTE_TT . " (Ctrl + V)");
447                                         $this->_jsbuttonspacer();
448                                         $this->_jsbutton('bold',"boldThis()",_ADD_BOLD_TT ." (Ctrl + Shift + B)");
449                                         $this->_jsbutton('italic',"italicThis()",_ADD_ITALIC_TT ." (Ctrl + Shift + I)");
450                                         $this->_jsbutton('link',"ahrefThis()",_ADD_HREF_TT ." (Ctrl + Shift + A)");
451                                         $this->_jsbuttonspacer();
452                                         $this->_jsbutton('alignleft',"alignleftThis()",_ADD_ALIGNLEFT_TT);
453                                         $this->_jsbutton('alignright',"alignrightThis()",_ADD_ALIGNRIGHT_TT);
454                                         $this->_jsbutton('aligncenter',"aligncenterThis()",_ADD_ALIGNCENTER_TT);
455                                         $this->_jsbuttonspacer();
456                                         $this->_jsbutton('left',"leftThis()",_ADD_LEFT_TT);
457                                         $this->_jsbutton('right',"rightThis()",_ADD_RIGHT_TT);
458
459
460                                         if ($extrabuttons) {
461                                                 $btns = i18n::explode('+',$extrabuttons);
462                                                 $this->_jsbuttonspacer();
463                                                 foreach ($btns as $button) {
464                                                         switch($button) {
465                                                                 case "media":
466                                                                         $this->_jsbutton('media',"addMedia()",_ADD_MEDIA_TT .   " (Ctrl + Shift + M)");
467                                                                         break;
468                                                                 case "preview":
469                                                                         $this->_jsbutton('preview',"showedit()",_ADD_PREVIEW_TT);
470                                                                         break;
471                                                         }
472                                                 }
473                                         }
474
475                                 echo '</div>';
476
477                                 break;
478                         case "2":
479                                 echo '<div class="jsbuttonbar">';
480
481                                         $this->_jsbutton('bold',"boldThis()",_ADD_BOLD_TT);
482                                         $this->_jsbutton('italic',"italicThis()",_ADD_ITALIC_TT);
483                                         $this->_jsbutton('link',"ahrefThis()",_ADD_HREF_TT);
484                                         $this->_jsbuttonspacer();
485                                         $this->_jsbutton('alignleft',"alignleftThis()",_ADD_ALIGNLEFT_TT);
486                                         $this->_jsbutton('alignright',"alignrightThis()",_ADD_ALIGNRIGHT_TT);
487                                         $this->_jsbutton('aligncenter',"aligncenterThis()",_ADD_ALIGNCENTER_TT);
488                                         $this->_jsbuttonspacer();
489                                         $this->_jsbutton('left',"leftThis()",_ADD_LEFT_TT);
490                                         $this->_jsbutton('right',"rightThis()",_ADD_RIGHT_TT);
491
492
493                                         if ($extrabuttons) {
494                                                 $btns = i18n::explode('+',$extrabuttons);
495                                                 $this->_jsbuttonspacer();
496                                                 foreach ($btns as $button) {
497                                                         switch($button) {
498                                                                 case "media":
499                                                                         $this->_jsbutton('media',"addMedia()",_ADD_MEDIA_TT);
500                                                                         break;
501                                                         }
502                                                 }
503                                         }
504
505                                 echo '</div>';
506
507                                 break;
508                 }
509         }
510
511
512         /**
513          * Allows plugins to add their own custom fields
514          */
515         function parse_pluginextras()
516         {
517                 global $manager;
518
519                 switch ($this->method) {
520                         case 'add':
521                                 $manager->notify('AddItemFormExtras',
522                                                 array(
523                                                         'blog' => &$this->blog
524                                                 )
525                                 );
526                                 break;
527                         case 'edit':
528                                 $manager->notify('EditItemFormExtras',
529                                                 array(
530                                                         'variables' => $this->variables,
531                                                         'blog' => &$this->blog,
532                                                         'itemid' => $this->variables['itemid']
533                                                 )
534                                 );
535                                 break;
536                 }
537         }
538
539
540         /**
541          * Adds the itemOptions of a plugin to a page
542          * @author TeRanEX
543          */
544         function parse_itemoptions()
545         {
546                 global $itemid;
547                 ADMIN::_insertPluginOptions('item', $itemid);
548         }
549
550
551         /**
552          *
553          */
554         function parse_ticket()
555         {
556                 global $manager;
557                 $manager->addTicketHidden();
558         }
559
560
561         /**
562          * convenience method
563          */
564         function _jsbutton($type, $code, $tooltip)
565         {
566         ?>
567                         <span class="jsbutton"
568                                 onmouseover="BtnHighlight(this);"
569                                 onmouseout="BtnNormal(this);"
570                                 onclick="<?php echo $code?>" >
571                                 <img src="images/button-<?php echo $type?>.gif" alt="<?php echo $tooltip?>" title="<?php echo $tooltip?>" width="16" height="16"/>
572                         </span>
573         <?php   }
574
575
576         /**
577          *
578          */
579         function _jsbuttonspacer()
580         {
581                 echo '<span class="jsbuttonspacer"></span>';
582         }
583
584 }