OSDN Git Service

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