OSDN Git Service

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