OSDN Git Service

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