OSDN Git Service

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