OSDN Git Service

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