OSDN Git Service

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