OSDN Git Service

6f2b2b2d40084a93196d25814e843ca14af85715
[nucleus-jp/nucleus-jp-ancient.git] / nucleus / libs / PAGEFACTORY.php
1 <?php\r
2 /*\r
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
4  * Copyright (C) 2002-2011 The Nucleus Group\r
5  *\r
6  * This program is free software; you can redistribute it and/or\r
7  * modify it under the terms of the GNU General Public License\r
8  * as published by the Free Software Foundation; either version 2\r
9  * of the License, or (at your option) any later version.\r
10  * (see nucleus/documentation/index.html#license for more info)\r
11  */\r
12 /**\r
13  * @license http://nucleuscms.org/license.txt GNU General Public License\r
14  * @copyright Copyright (C) 2002-2011 The Nucleus Group\r
15  * @version $Id$\r
16  * $NucleusJP: PAGEFACTORY.php,v 1.8.2.2 2007/10/30 16:12:27 shizuki Exp $\r
17  */\r
18 \r
19 /**\r
20  * The formfactory class can be used to insert add/edit item forms into\r
21  * admin area, bookmarklet, skins or any other places where such a form\r
22  * might be needed\r
23  */\r
24 class PAGEFACTORY extends BaseActions {\r
25 \r
26         // ref to the blog object for which an add:edit form is created\r
27         var $blog;\r
28 \r
29         // allowed actions (for parser)\r
30         var $actions;\r
31 \r
32         // allowed types of forms (bookmarklet/admin)\r
33         var $allowedTypes;\r
34         var $type;              // one of the types in $allowedTypes\r
35 \r
36         // 'add' or 'edit'\r
37         var $method;\r
38 \r
39         // info to fill out in the form (e.g. catid, itemid, ...)\r
40         var $variables;\r
41 \r
42         /**\r
43          * creates a new PAGEFACTORY object\r
44          */\r
45         function PAGEFACTORY($blogid) {\r
46                 // call constructor of superclass first\r
47                 $this->BaseActions();\r
48 \r
49                 global $manager;\r
50                 $this->blog =& $manager->getBlog($blogid);\r
51 \r
52                 // TODO: move the definition of actions to the createXForm\r
53                 // methods\r
54                 $this->actions = Array(\r
55                         'actionurl',\r
56                         'title',\r
57                         'body',\r
58                         'more',\r
59                         'blogid',\r
60                         'bloglink',\r
61                         'blogname',\r
62                         'authorname',\r
63                         'checkedonval',\r
64                         'helplink',\r
65                         'currenttime',\r
66                         'itemtime',\r
67                         'init',\r
68                         'text',\r
69                         'jsinput',\r
70                         'jsbuttonbar',\r
71                         'categories',\r
72                         'contents',\r
73                         'ifblogsetting',\r
74                         'ifitemproperty',\r
75                         'else',\r
76                         'endif',\r
77                         'pluginextras',\r
78                         'itemoptions',\r
79                         'extrahead',\r
80                         'ticket',\r
81                         'autosave',\r
82                         'autosaveinfo',\r
83                         'ifautosave'\r
84                 );\r
85 \r
86                 // TODO: maybe add 'skin' later on?\r
87                 // TODO: maybe add other pages from admin area\r
88                 $this->allowedTypes = Array('bookmarklet','admin');\r
89         }\r
90 \r
91         /**\r
92          * creates a "add item" form for a given type of page\r
93          *\r
94          * @param type\r
95          *              'admin' or 'bookmarklet'\r
96          */\r
97         function createAddForm($type, $contents = array()) {\r
98                 if (!in_array($type, $this->allowedTypes))\r
99                         return;\r
100                 $this->type = $type;\r
101                 $this->method = 'add';\r
102 \r
103                 global $manager;\r
104                 $param = array(\r
105                         'contents'      => &$contents,\r
106                         'blog'          => &$this->blog\r
107                 );\r
108                 $manager->notify('PreAddItemForm', $param);\r
109 \r
110                 $this->createForm($contents);\r
111         }\r
112 \r
113         /**\r
114          * creates a "add item" form for a given type of page\r
115          *\r
116          * @param type\r
117          *              'admin' or 'bookmarklet'\r
118          * @param contents\r
119          *              An associative array\r
120          *                      'author' => author\r
121          *                      '' =>\r
122          */\r
123         function createEditForm($type, $contents) {\r
124                 if (!in_array($type, $this->allowedTypes))\r
125                         return;\r
126                 $this->type = $type;\r
127                 $this->method = 'edit';\r
128                 $this->createForm($contents);\r
129         }\r
130 \r
131         /**\r
132          * (private) creates a form for a given type of page\r
133          */\r
134         function createForm($contents) {\r
135                 // save contents\r
136                 $this->variables = $contents;\r
137 \r
138                 // get template to use\r
139                 $template = $this->getTemplateFor($this->type);\r
140 \r
141                 // use the PARSER engine to parse that template\r
142                 $parser = new PARSER($this->actions, $this);\r
143                 $parser->parse($template);\r
144         }\r
145 \r
146         /**\r
147          * returns an appropriate template\r
148          */\r
149         function getTemplateFor($type) {\r
150                 global $DIR_LIBS;\r
151 \r
152                 $filename = $DIR_LIBS . 'include/' . $this->type . '-' . $this->method . '.template';\r
153 \r
154                 if (!file_exists($filename))\r
155                         return '';\r
156 \r
157                 $fsize = filesize($filename);\r
158                 if ($fsize <= 0)\r
159                         return '';\r
160 \r
161                 // read file and return it\r
162                 $fd = fopen ($filename, 'r');\r
163                 $contents = fread ($fd, $fsize);\r
164                 fclose ($fd);\r
165 \r
166                 return $contents;\r
167 \r
168         }\r
169 \r
170         // create category dropdown box\r
171         function parse_categories($startidx = 0) {\r
172                         if ($this->variables['catid'])\r
173                                 $catid = $this->variables['catid'];                             // on edit item\r
174                         else\r
175                                 $catid = $this->blog->getDefaultCategory();             // on add item\r
176 \r
177                         ADMIN::selectBlogCategory('catid',$catid,$startidx,1,$this->blog->getID());\r
178         }\r
179 \r
180         function parse_blogid() {\r
181                 echo $this->blog->getID();\r
182         }\r
183 \r
184         function parse_blogname() {\r
185                 echo $this->blog->getName();\r
186         }\r
187 \r
188         function parse_bloglink() {\r
189                 echo '<a href="'.htmlspecialchars($this->blog->getURL()).'">'.htmlspecialchars($this->blog->getName()).'</a>';\r
190         }\r
191 \r
192         function parse_authorname() {\r
193                 // don't use on add item?\r
194                 global $member;\r
195                 echo $member->getDisplayName();\r
196         }\r
197 \r
198         function parse_title() {\r
199                 echo $this->contents['title'];\r
200         }\r
201 \r
202         /**\r
203          * Indicates the start of a conditional block of data. It will be added to\r
204          * the output only if the blogsetting with the given name equals the\r
205          * given value (default for value = 1 = true)\r
206          *\r
207          * the name of the blogsetting is the column name in the nucleus_blog table\r
208          *\r
209          * the conditional block ends with an <endif> var\r
210          */\r
211         function parse_ifblogsetting($name,$value=1) {\r
212                 $this->_addIfCondition(($this->blog->getSetting($name) == $value));\r
213         }\r
214 \r
215         function parse_ifitemproperty($name,$value=1) {\r
216                 $this->_addIfCondition(($this->variables[$name] == $value));\r
217         }\r
218 \r
219         function parse_ifautosave($name,$value=1) {\r
220                 global $member;\r
221                 $this->_addIfCondition($member->getAutosave() == $value);\r
222         }\r
223 \r
224         function parse_helplink($topic) {\r
225                 help($topic);\r
226         }\r
227 \r
228         // for future items\r
229         function parse_currenttime($what) {\r
230                 $nu = getdate($this->blog->getCorrectTime());\r
231                 echo $nu[$what];\r
232         }\r
233 \r
234         // date change on edit item\r
235         function parse_itemtime($what) {\r
236                 $itemtime = getdate($this->variables['timestamp']);\r
237                 echo $itemtime[$what];\r
238         }\r
239 \r
240         // some init stuff for all forms\r
241         function parse_init() {\r
242                 $authorid = ($this->method == 'edit') ? $this->variables['authorid'] : '';\r
243                 $this->blog->insertJavaScriptInfo($authorid);\r
244         }\r
245 \r
246         // on bookmarklets only: insert extra html header information (by plugins)\r
247         function parse_extrahead() {\r
248                 global $manager;\r
249 \r
250                 $extrahead = '';\r
251 \r
252                 $param = array(\r
253                         'extrahead' => &$extrahead\r
254                 );\r
255                 $manager->notify('BookmarkletExtraHead', $param);\r
256 \r
257                 echo $extrahead;\r
258         }\r
259 \r
260         // inserts some localized text\r
261         function parse_text($which) {\r
262                 // constant($which) only available from 4.0.4 :(\r
263                 if (defined($which)) {\r
264                         eval("echo $which;");\r
265                 } else {\r
266                         echo $which;    // this way we see where definitions are missing\r
267                 }\r
268 \r
269         }\r
270 \r
271         function parse_contents($which) {\r
272                 if (!isset($this->variables[$which])) $this->variables[$which] = '';\r
273                 echo htmlspecialchars($this->variables[$which],ENT_QUOTES);\r
274         }\r
275 \r
276         function parse_checkedonval($value, $name) {\r
277                 if (!isset($this->variables[$name])) $this->variables[$name] = '';\r
278                 if ($this->variables[$name] == $value)\r
279                         echo 'checked="checked"';\r
280         }\r
281 \r
282         // extra javascript for input and textarea fields\r
283         function parse_jsinput($which) {\r
284                 global $CONF, $member;\r
285                 \r
286                 $attributes  = " name=\"{$which}\"";\r
287                 $attributes .= " id=\"input{$which}\"";\r
288                 \r
289                 if ($CONF['DisableJsTools'] != 1) {\r
290                         $attributes .= ' onclick="storeCaret(this);"';\r
291                         $attributes .= ' onselect="storeCaret(this);"';\r
292                         if ($member->getAutosave()) {\r
293                                 $attributes .= " onkeyup=\"storeCaret(this); updPreview('{$which}'); doMonitor();\"";\r
294                         } else {\r
295                                 $attributes .= " onkeyup=\"storeCaret(this); updPreview('{$which}');\"";\r
296                         }\r
297                 }\r
298                 else {\r
299                         if ($CONF['DisableJsTools'] == 0) {\r
300                                 $attributes .= ' onkeypress="shortCuts();"';\r
301                         }\r
302                         if ($member->getAutosave()) {\r
303                                 $attributes .= ' onkeyup="doMonitor();"';\r
304                         }\r
305                 }\r
306                 echo $attributes;\r
307         }\r
308 \r
309         // shows the javascript button bar\r
310         function parse_jsbuttonbar($extrabuttons = "") {\r
311                 global $CONF;\r
312                 switch($CONF['DisableJsTools']) {\r
313 \r
314                         case "0":\r
315                                 echo '<div class="jsbuttonbar">';\r
316 \r
317                                         $this->_jsbutton('cut','cutThis()',_ADD_CUT_TT . " (Ctrl + X)");\r
318                                         $this->_jsbutton('copy','copyThis()',_ADD_COPY_TT . " (Ctrl + C)");\r
319                                         $this->_jsbutton('paste','pasteThis()',_ADD_PASTE_TT . " (Ctrl + V)");\r
320                                         $this->_jsbuttonspacer();\r
321                                         $this->_jsbutton('bold',"boldThis()",_ADD_BOLD_TT ." (Ctrl + Shift + B)");\r
322                                         $this->_jsbutton('italic',"italicThis()",_ADD_ITALIC_TT ." (Ctrl + Shift + I)");\r
323                                         $this->_jsbutton('link',"ahrefThis()",_ADD_HREF_TT ." (Ctrl + Shift + A)");\r
324                                         $this->_jsbuttonspacer();\r
325                                         $this->_jsbutton('alignleft',"alignleftThis()",_ADD_ALIGNLEFT_TT);\r
326                                         $this->_jsbutton('alignright',"alignrightThis()",_ADD_ALIGNRIGHT_TT);\r
327                                         $this->_jsbutton('aligncenter',"aligncenterThis()",_ADD_ALIGNCENTER_TT);\r
328                                         $this->_jsbuttonspacer();\r
329                                         $this->_jsbutton('left',"leftThis()",_ADD_LEFT_TT);\r
330                                         $this->_jsbutton('right',"rightThis()",_ADD_RIGHT_TT);\r
331 \r
332 \r
333                                         if ($extrabuttons) {\r
334                                                 $btns = explode('+',$extrabuttons);\r
335                                                 $this->_jsbuttonspacer();\r
336                                                 foreach ($btns as $button) {\r
337                                                         switch($button) {\r
338                                                                 case "media":\r
339                                                                         $this->_jsbutton('media',"addMedia()",_ADD_MEDIA_TT .   " (Ctrl + Shift + M)");\r
340                                                                         break;\r
341                                                                 case "preview":\r
342                                                                         $this->_jsbutton('preview',"showedit()",_ADD_PREVIEW_TT);\r
343                                                                         break;\r
344                                                         }\r
345                                                 }\r
346                                         }\r
347 \r
348                                 echo '</div>';\r
349 \r
350                                 break;\r
351                         case "2":\r
352                                 echo '<div class="jsbuttonbar">';\r
353 \r
354                                         $this->_jsbutton('bold',"boldThis()",_ADD_BOLD_TT);\r
355                                         $this->_jsbutton('italic',"italicThis()",_ADD_ITALIC_TT);\r
356                                         $this->_jsbutton('link',"ahrefThis()",_ADD_HREF_TT);\r
357                                         $this->_jsbuttonspacer();\r
358                                         $this->_jsbutton('alignleft',"alignleftThis()",_ADD_ALIGNLEFT_TT);\r
359                                         $this->_jsbutton('alignright',"alignrightThis()",_ADD_ALIGNRIGHT_TT);\r
360                                         $this->_jsbutton('aligncenter',"aligncenterThis()",_ADD_ALIGNCENTER_TT);\r
361                                         $this->_jsbuttonspacer();\r
362                                         $this->_jsbutton('left',"leftThis()",_ADD_LEFT_TT);\r
363                                         $this->_jsbutton('right',"rightThis()",_ADD_RIGHT_TT);\r
364 \r
365 \r
366                                         if ($extrabuttons) {\r
367                                                 $btns = explode('+',$extrabuttons);\r
368                                                 $this->_jsbuttonspacer();\r
369                                                 foreach ($btns as $button) {\r
370                                                         switch($button) {\r
371                                                                 case "media":\r
372                                                                         $this->_jsbutton('media',"addMedia()",_ADD_MEDIA_TT);\r
373                                                                         break;\r
374                                                         }\r
375                                                 }\r
376                                         }\r
377 \r
378                                 echo '</div>';\r
379 \r
380                                 break;\r
381                 }\r
382         }\r
383 \r
384         /**\r
385          * Allows plugins to add their own custom fields\r
386          */\r
387         function parse_pluginextras() {\r
388                 global $manager;\r
389 \r
390                 switch ($this->method) {\r
391                         case 'add':\r
392                                 $param = array(\r
393                                         'blog' => &$this->blog\r
394                                 );\r
395                                 $manager->notify('AddItemFormExtras', $param);\r
396                                 break;\r
397                         case 'edit':\r
398                                 $PARAM = array(\r
399                                         'variables'     =>  $this->variables,\r
400                                         'blog'          => &$this->blog,\r
401                                         'itemid'        =>  $this->variables['itemid']\r
402                                 );\r
403                                 $manager->notify('EditItemFormExtras', $param);\r
404                                 break;\r
405                 }\r
406         }\r
407 \r
408         /**\r
409          * Adds the itemOptions of a plugin to a page\r
410          * @author TeRanEX\r
411          */\r
412         function parse_itemoptions() {\r
413                 global $itemid;\r
414                 ADMIN::_insertPluginOptions('item', $itemid);\r
415         }\r
416 \r
417         function parse_ticket() {\r
418                 global $manager;\r
419                 $manager->addTicketHidden();\r
420         }\r
421 \r
422         /**\r
423          * convenience method\r
424          */\r
425         function _jsbutton($type, $code ,$tooltip) {\r
426         ?>\r
427                         <span class="jsbutton"\r
428                                 onmouseover="BtnHighlight(this);"\r
429                                 onmouseout="BtnNormal(this);"\r
430                                 onclick="<?php echo $code?>" >\r
431                                 <img src="images/button-<?php echo $type?>.gif" alt="<?php echo $tooltip?>" title="<?php echo $tooltip?>" width="16" height="16"/>\r
432                         </span>\r
433         <?php   }\r
434         \r
435         function _jsbuttonspacer() {\r
436                 echo '<span class="jsbuttonspacer">&nbsp;</span>';\r
437         }\r
438 \r
439 }\r
440 \r
441 ?>