OSDN Git Service

41235b4b1f3897257850ae390ed4016a91715792
[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-2009 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-2009 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                 $manager->notify('PreAddItemForm', array('contents' => &$contents, 'blog' => &$this->blog));\r
105 \r
106                 $this->createForm($contents);\r
107         }\r
108 \r
109         /**\r
110          * creates a "add item" form for a given type of page\r
111          *\r
112          * @param type\r
113          *              'admin' or 'bookmarklet'\r
114          * @param contents\r
115          *              An associative array\r
116          *                      'author' => author\r
117          *                      '' =>\r
118          */\r
119         function createEditForm($type, $contents) {\r
120                 if (!in_array($type, $this->allowedTypes))\r
121                         return;\r
122                 $this->type = $type;\r
123                 $this->method = 'edit';\r
124                 $this->createForm($contents);\r
125         }\r
126 \r
127         /**\r
128          * (private) creates a form for a given type of page\r
129          */\r
130         function createForm($contents) {\r
131                 // save contents\r
132                 $this->variables = $contents;\r
133 \r
134                 // get template to use\r
135                 $template = $this->getTemplateFor($this->type);\r
136 \r
137                 // use the PARSER engine to parse that template\r
138                 $parser =& new PARSER($this->actions, $this);\r
139                 $parser->parse($template);\r
140         }\r
141 \r
142         /**\r
143          * returns an appropriate template\r
144          */\r
145         function getTemplateFor($type) {\r
146                 global $DIR_LIBS;\r
147 \r
148                 $filename = $DIR_LIBS . 'include/' . $this->type . '-' . $this->method . '.template';\r
149 \r
150                 if (!file_exists($filename))\r
151                         return '';\r
152 \r
153                 $fsize = filesize($filename);\r
154                 if ($fsize <= 0)\r
155                         return '';\r
156 \r
157                 // read file and return it\r
158                 $fd = fopen ($filename, 'r');\r
159                 $contents = fread ($fd, $fsize);\r
160                 fclose ($fd);\r
161 \r
162                 return $contents;\r
163 \r
164         }\r
165 \r
166         // create category dropdown box\r
167         function parse_categories($startidx = 0) {\r
168                         if ($this->variables['catid'])\r
169                                 $catid = $this->variables['catid'];                             // on edit item\r
170                         else\r
171                                 $catid = $this->blog->getDefaultCategory();             // on add item\r
172 \r
173                         ADMIN::selectBlogCategory('catid',$catid,$startidx,1,$this->blog->getID());\r
174         }\r
175 \r
176         function parse_blogid() {\r
177                 echo $this->blog->getID();\r
178         }\r
179 \r
180         function parse_blogname() {\r
181                 echo $this->blog->getName();\r
182         }\r
183 \r
184         function parse_bloglink() {\r
185                 echo '<a href="'.htmlspecialchars($this->blog->getURL()).'">'.htmlspecialchars($this->blog->getName()).'</a>';\r
186         }\r
187 \r
188         function parse_authorname() {\r
189                 // don't use on add item?\r
190                 global $member;\r
191                 echo $member->getDisplayName();\r
192         }\r
193 \r
194         function parse_title() {\r
195                 echo $this->contents['title'];\r
196         }\r
197 \r
198         /**\r
199          * Indicates the start of a conditional block of data. It will be added to\r
200          * the output only if the blogsetting with the given name equals the\r
201          * given value (default for value = 1 = true)\r
202          *\r
203          * the name of the blogsetting is the column name in the nucleus_blog table\r
204          *\r
205          * the conditional block ends with an <endif> var\r
206          */\r
207         function parse_ifblogsetting($name,$value=1) {\r
208                 $this->_addIfCondition(($this->blog->getSetting($name) == $value));\r
209         }\r
210 \r
211         function parse_ifitemproperty($name,$value=1) {\r
212                 $this->_addIfCondition(($this->variables[$name] == $value));\r
213         }\r
214 \r
215         function parse_ifautosave($name,$value=1) {\r
216                 global $member;\r
217                 $this->_addIfCondition($member->getAutosave() == $value);\r
218         }\r
219 \r
220         function parse_helplink($topic) {\r
221                 help($topic);\r
222         }\r
223 \r
224         // for future items\r
225         function parse_currenttime($what) {\r
226                 $nu = getdate($this->blog->getCorrectTime());\r
227                 echo $nu[$what];\r
228         }\r
229 \r
230         // date change on edit item\r
231         function parse_itemtime($what) {\r
232                 $itemtime = getdate($this->variables['timestamp']);\r
233                 echo $itemtime[$what];\r
234         }\r
235 \r
236         // some init stuff for all forms\r
237         function parse_init() {\r
238                 $authorid = ($this->method == 'edit') ? $this->variables['authorid'] : '';\r
239                 $this->blog->insertJavaScriptInfo($authorid);\r
240         }\r
241 \r
242         // on bookmarklets only: insert extra html header information (by plugins)\r
243         function parse_extrahead() {\r
244                 global $manager;\r
245 \r
246                 $extrahead = '';\r
247 \r
248                 $manager->notify(\r
249                         'BookmarkletExtraHead',\r
250                         array(\r
251                                 'extrahead' => &$extrahead\r
252                         )\r
253                 );\r
254 \r
255                 echo $extrahead;\r
256         }\r
257 \r
258         // inserts some localized text\r
259         function parse_text($which) {\r
260                 // constant($which) only available from 4.0.4 :(\r
261                 if (defined($which)) {\r
262                         eval("echo $which;");\r
263                 } else {\r
264                         echo $which;    // this way we see where definitions are missing\r
265                 }\r
266 \r
267         }\r
268 \r
269         function parse_contents($which) {\r
270                 echo htmlspecialchars($this->variables[$which],ENT_QUOTES);\r
271         }\r
272 \r
273         function parse_checkedonval($value, $name) {\r
274                 if ($this->variables[$name] == $value)\r
275                         echo 'checked="checked"';\r
276         }\r
277 \r
278         // extra javascript for input and textarea fields\r
279         function parse_jsinput($which) {\r
280                 global $CONF;\r
281                 $out = 'name="' . $which . '" id="input' . $which . '"';\r
282                 if ($CONF['DisableJsTools'] != 1) {\r
283                         $out .= 'onkeyup="storeCaret(this); updPreview(' . $which . '); doMonitor();"'\r
284                                   . 'onclick="storeCaret(this);"'\r
285                                   . 'onselect="storeCaret(this);"';\r
286                 } elseif ($CONF['DisableJsTools'] == 0) {\r
287                         $out .= ' onkeyup="doMonitor();" onkeypress="shortCuts();"';\r
288                 } else {\r
289                         $out .= ' onkeyup="doMonitor();"';\r
290                 }\r
291                 echo $out;\r
292         }\r
293 \r
294         // shows the javascript button bar\r
295         function parse_jsbuttonbar($extrabuttons = "") {\r
296                 global $CONF;\r
297                 switch($CONF['DisableJsTools']) {\r
298 \r
299                         case "0":\r
300                                 echo '<div class="jsbuttonbar">';\r
301 \r
302                                         $this->_jsbutton('cut','cutThis()',_ADD_CUT_TT . " (Ctrl + X)");\r
303                                         $this->_jsbutton('copy','copyThis()',_ADD_COPY_TT . " (Ctrl + C)");\r
304                                         $this->_jsbutton('paste','pasteThis()',_ADD_PASTE_TT . " (Ctrl + V)");\r
305                                         $this->_jsbuttonspacer();\r
306                                         $this->_jsbutton('bold',"boldThis()",_ADD_BOLD_TT ." (Ctrl + Shift + B)");\r
307                                         $this->_jsbutton('italic',"italicThis()",_ADD_ITALIC_TT ." (Ctrl + Shift + I)");\r
308                                         $this->_jsbutton('link',"ahrefThis()",_ADD_HREF_TT ." (Ctrl + Shift + A)");\r
309                                         $this->_jsbuttonspacer();\r
310                                         $this->_jsbutton('alignleft',"alignleftThis()",_ADD_ALIGNLEFT_TT);\r
311                                         $this->_jsbutton('alignright',"alignrightThis()",_ADD_ALIGNRIGHT_TT);\r
312                                         $this->_jsbutton('aligncenter',"aligncenterThis()",_ADD_ALIGNCENTER_TT);\r
313                                         $this->_jsbuttonspacer();\r
314                                         $this->_jsbutton('left',"leftThis()",_ADD_LEFT_TT);\r
315                                         $this->_jsbutton('right',"rightThis()",_ADD_RIGHT_TT);\r
316 \r
317 \r
318                                         if ($extrabuttons) {\r
319                                                 $btns = explode('+',$extrabuttons);\r
320                                                 $this->_jsbuttonspacer();\r
321                                                 foreach ($btns as $button) {\r
322                                                         switch($button) {\r
323                                                                 case "media":\r
324                                                                         $this->_jsbutton('media',"addMedia()",_ADD_MEDIA_TT .   " (Ctrl + Shift + M)");\r
325                                                                         break;\r
326                                                                 case "preview":\r
327                                                                         $this->_jsbutton('preview',"showedit()",_ADD_PREVIEW_TT);\r
328                                                                         break;\r
329                                                         }\r
330                                                 }\r
331                                         }\r
332 \r
333                                 echo '</div>';\r
334 \r
335                                 break;\r
336                         case "2":\r
337                                 echo '<div class="jsbuttonbar">';\r
338 \r
339                                         $this->_jsbutton('bold',"boldThis()",_ADD_BOLD_TT);\r
340                                         $this->_jsbutton('italic',"italicThis()",_ADD_ITALIC_TT);\r
341                                         $this->_jsbutton('link',"ahrefThis()",_ADD_HREF_TT);\r
342                                         $this->_jsbuttonspacer();\r
343                                         $this->_jsbutton('alignleft',"alignleftThis()",_ADD_ALIGNLEFT_TT);\r
344                                         $this->_jsbutton('alignright',"alignrightThis()",_ADD_ALIGNRIGHT_TT);\r
345                                         $this->_jsbutton('aligncenter',"aligncenterThis()",_ADD_ALIGNCENTER_TT);\r
346                                         $this->_jsbuttonspacer();\r
347                                         $this->_jsbutton('left',"leftThis()",_ADD_LEFT_TT);\r
348                                         $this->_jsbutton('right',"rightThis()",_ADD_RIGHT_TT);\r
349 \r
350 \r
351                                         if ($extrabuttons) {\r
352                                                 $btns = explode('+',$extrabuttons);\r
353                                                 $this->_jsbuttonspacer();\r
354                                                 foreach ($btns as $button) {\r
355                                                         switch($button) {\r
356                                                                 case "media":\r
357                                                                         $this->_jsbutton('media',"addMedia()",_ADD_MEDIA_TT);\r
358                                                                         break;\r
359                                                         }\r
360                                                 }\r
361                                         }\r
362 \r
363                                 echo '</div>';\r
364 \r
365                                 break;\r
366                 }\r
367         }\r
368 \r
369         /**\r
370          * Allows plugins to add their own custom fields\r
371          */\r
372         function parse_pluginextras() {\r
373                 global $manager;\r
374 \r
375                 switch ($this->method) {\r
376                         case 'add':\r
377                                 $manager->notify('AddItemFormExtras',\r
378                                                 array(\r
379                                                         'blog' => &$this->blog\r
380                                                 )\r
381                                 );\r
382                                 break;\r
383                         case 'edit':\r
384                                 $manager->notify('EditItemFormExtras',\r
385                                                 array(\r
386                                                         'variables' => $this->variables,\r
387                                                         'blog' => &$this->blog,\r
388                                                         'itemid' => $this->variables['itemid']\r
389                                                 )\r
390                                 );\r
391                                 break;\r
392                 }\r
393         }\r
394 \r
395         /**\r
396          * Adds the itemOptions of a plugin to a page\r
397          * @author TeRanEX\r
398          */\r
399         function parse_itemoptions() {\r
400                 global $itemid;\r
401                 ADMIN::_insertPluginOptions('item', $itemid);\r
402         }\r
403 \r
404         function parse_ticket() {\r
405                 global $manager;\r
406                 $manager->addTicketHidden();\r
407         }\r
408 \r
409         /**\r
410          * convenience method\r
411          */\r
412         function _jsbutton($type, $code ,$tooltip) {\r
413                 echo <<<__JSBUTTON__\r
414                         <span class="jsbutton" onmouseover="BtnHighlight(this);" onmouseout="BtnNormal(this);" onclick="{$code}">\r
415                                 <img src="images/button-{$type}.gif" alt="{$tooltip}" width="16" height="16" />\r
416                         </span>\r
417 \r
418 __JSBUTTON__;\r
419         }\r
420 \r
421         function _jsbuttonspacer() {\r
422                 echo '<span class="jsbuttonspacer">&nbsp;</span>';\r
423         }\r
424 \r
425 }\r
426 \r
427 ?>