OSDN Git Service

Sync ORIGINAL 3.3 Rev.1120
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / ITEMACTIONS.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  * This class to parse item templates
14  *
15  * @license http://nucleuscms.org/license.txt GNU General Public License
16  * @copyright Copyright (C) 2002-2007 The Nucleus Group
17  * @version $Id: ITEMACTIONS.php,v 1.4 2007-03-13 05:04:13 shizuki Exp $
18  * @version $NucleusJP: ITEMACTIONS.php,v 1.3 2007/02/04 06:28:46 kimitake Exp $
19  */
20 class ITEMACTIONS extends BaseActions {
21
22         // contains an assoc array with parameters that need to be included when
23         // generating links to items/archives/... (e.g. catid)
24         var $linkparams;
25
26         // true when the current user is a blog admin (and thus allowed to edit all items)
27         var $allowEditAll;
28
29         // timestamp of last visit
30         var $lastVisit;
31
32         // item currently being handled (mysql result object, see BLOG::showUsingQuery)
33         var $currentItem;
34
35         // reference to the blog currently being displayed
36         var $blog;
37
38         // associative array with template info (part name => contents)
39         var $template;
40
41         // true when comments need to be displayed
42         var $showComments;
43
44         function ITEMACTIONS(&$blog) {
45                 // call constructor of superclass first
46                 $this->BaseActions();
47
48                 // extra parameters for created links
49                 global $catid;
50                 if ($catid)
51                         $this->linkparams = array('catid' => $catid);
52
53                 // check if member is blog admin (and thus allowed to edit all items)
54                 global $member;
55                 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
56                 $this->setBlog($blog);
57         }
58
59         function getDefinedActions() {
60                 return array(
61                         'blogid',
62                         'title',
63                         'body',
64                         'more',
65                         'smartbody',
66                         'itemid',
67                         'morelink',
68                         'category',
69                         'categorylink',
70                         'author',
71                         'authorid',
72                         'authorlink',
73                         'catid',
74                         'karma',
75                         'date',
76                         'time',
77                         'query',
78                         'itemlink',
79                         'blogurl',
80                         'closed',
81                         'syndicate_title',
82                         'syndicate_description',
83                         'karmaposlink',
84                         'karmaneglink',
85                         'new',
86                         'image',
87                         'popup',
88                         'media',
89                         'daylink',
90                         'query',
91                         'include',
92                         'phpinclude',
93                         'parsedinclude',
94                         'skinfile',
95                         'set',
96                         'plugin',
97                         'edit',
98                         'editlink',
99                         'editpopupcode',
100                         'comments',
101                         'relevance'/*,
102                         'if',
103                         'else',
104                         'endif',
105                         'elseif',
106                         'ifnot',
107                         'elseifnot'*/
108                 );
109         }
110
111         function setLastVisit($lastVisit) {
112                 $this->lastVisit = $lastVisit;
113         }
114         
115         function setParser(&$parser) {
116                 $this->parser =& $parser;
117         }
118         
119         function setCurrentItem(&$item) {
120                 $this->currentItem =& $item;
121         }
122         
123         function setBlog(&$blog) {
124                 $this->blog =& $blog;
125         }
126         
127         function setTemplate($template) {
128                 $this->template =& $template;
129         }
130         
131         function setShowComments($val) {
132                 $this->showComments = $val;
133         }
134
135         // methods used by parser to insert content
136
137         function parse_blogid() {
138                 echo $this->blog->getID();
139         }
140         
141         function parse_body() {
142                 $this->highlightAndParse($this->currentItem->body);
143         }
144         
145         function parse_more() {
146                 $this->highlightAndParse($this->currentItem->more);
147         }
148         
149         function parse_itemid() {
150                 echo $this->currentItem->itemid;
151         }
152         
153         function parse_category() {
154                 echo $this->currentItem->category;
155         }
156         
157         function parse_categorylink() {
158                 echo createLink('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
159         }
160         
161         function parse_catid() {
162                 echo $this->currentItem->catid;
163         }
164         
165         function parse_authorid() {
166                 echo $this->currentItem->authorid;
167         }
168         
169         function parse_authorlink() {
170                 echo createLink(
171                         'member',
172                         array(
173                                 'memberid' => $this->currentItem->authorid,
174                                 'name' => $this->currentItem->author,
175                                 'extra' => $this->linkparams
176                         )
177                 );
178         }
179         
180         function parse_query() {
181                 echo $this->strHighlight;
182         }
183         
184         function parse_itemlink() {
185                 echo createLink(
186                         'item',
187                         array(
188                                 'itemid' => $this->currentItem->itemid,
189                                 'title' => $this->currentItem->title,
190                                 'timestamp' => $this->currentItem->timestamp,
191                                 'extra' => $this->linkparams
192                         )
193                 );
194         }
195         
196         function parse_blogurl() {
197                 echo $this->blog->getURL();
198         }
199         
200         function parse_closed() {
201                 echo $this->currentItem->closed;
202         }
203         
204         function parse_relevance() {
205                 echo round($this->currentItem->score,2);
206         }
207
208         function parse_title($format = '') {
209                 switch ($format) {
210                         case 'xml':
211                                 echo stringToXML ($this->currentItem->title);
212                                 break;
213                         case 'attribute':
214                                 echo stringToAttribute ($this->currentItem->title);
215                                 break;
216                         case 'raw':
217                                 echo $this->currentItem->title;
218                                 break;
219                         default:
220                                 $this->highlightAndParse($this->currentItem->title);
221                                 break;
222                 }
223         }
224
225         function parse_karma($type = 'totalscore') {
226                 global $manager;
227
228                 // get karma object
229                 $karma =& $manager->getKarma($this->currentItem->itemid);
230
231                 switch($type) {
232                         case 'pos':
233                                 echo $karma->getNbPosVotes();
234                                 break;
235                         case 'neg':
236                                 echo $karma->getNbNegVotes();
237                                 break;
238                         case 'votes':
239                                 echo $karma->getNbOfVotes();
240                                 break;
241                         case 'posp':
242                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
243                                 echo number_format($percentage,2), '%';
244                                 break;
245                         case 'negp':
246                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
247                                 echo number_format($percentage,2), '%';
248                                 break;
249                         case 'totalscore':
250                         default:
251                                 echo $karma->getTotalScore();
252                                 break;
253                 }
254
255         }
256
257         function parse_author($which = '') {
258                 switch($which)
259                 {
260                         case 'realname':
261                                 echo $this->currentItem->authorname;
262                                 break;
263                         case 'id':
264                                 echo $this->currentItem->authorid;
265                                 break;
266                         case 'email':
267                                 echo $this->currentItem->authormail;
268                                 break;
269                         case 'url':
270                                 echo $this->currentItem->authorurl;
271                                 break;
272                         case 'name':
273                         default:
274                                 echo $this->currentItem->author;
275                 }
276         }
277
278         function parse_smartbody() {
279                 if (!$this->currentItem->more) {
280                         $this->highlightAndParse($this->currentItem->body);
281                 } else {
282                         $this->highlightAndParse($this->currentItem->more);
283                 }
284         }
285
286         function parse_morelink() {
287                 if ($this->currentItem->more)
288                         $this->parser->parse($this->template['MORELINK']);
289         }
290
291         function parse_date($format = '') {
292                 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $this->blog);
293         }
294
295         /**
296           * @param format optional strftime format
297           */
298         function parse_time($format = '') {
299                 echo strftime($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);
300         }
301
302         /**
303           * @param maxLength optional maximum length
304           */
305         function parse_syndicate_title($maxLength = 100) {
306                 $syndicated = strip_tags($this->currentItem->title);
307                 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'));
308         }
309
310         /**
311           * @param maxLength optional maximum length
312           */
313         function parse_syndicate_description($maxLength = 250, $addHighlight = 0) {
314                 $syndicated = strip_tags($this->currentItem->body);
315                 if ($addHighlight) {
316                         $tmp_highlight = htmlspecialchars(shorten($syndicated,$maxLength,'...'));
317                         echo $this->highlightAndParse($tmp_highlight);
318                 } else {
319                         echo htmlspecialchars(shorten($syndicated,$maxLength,'...'));
320                 }
321         }
322
323         function parse_karmaposlink($text = '') {
324                 global $CONF;
325                 $link = $CONF['ActionURL'] . '?action=votepositive&amp;itemid='.$this->currentItem->itemid;
326                 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
327         }
328
329         function parse_karmaneglink($text = '') {
330                 global $CONF;
331                 $link = $CONF['ActionURL'] . '?action=votenegative&amp;itemid='.$this->currentItem->itemid;
332                 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
333         }
334
335         function parse_new() {
336                 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
337                         echo $this->template['NEW'];
338         }
339
340         function parse_daylink() {
341                 echo createArchiveLink($this->blog->getID(), strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
342         }
343
344         function parse_comments($maxToShow = 0) {
345                 if ($maxToShow == 0)
346                         $maxToShow = $this->blog->getMaxComments();
347
348                 // add comments
349                 if ($this->showComments && $this->blog->commentsEnabled()) {
350                         $comments =& new COMMENTS($this->currentItem->itemid);
351                         $comments->setItemActions($this);
352                         $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
353                 }
354         }
355
356         /**
357           * Executes a plugin templatevar
358           *
359           * @param pluginName name of plugin (without the NP_)
360           *
361           * extra parameters can be added
362           */
363         function parse_plugin($pluginName) {
364                 global $manager;
365
366                 // only continue when the plugin is really installed
367                 if (!$manager->pluginInstalled('NP_' . $pluginName))
368                         return;
369
370                 $plugin =& $manager->getPlugin('NP_' . $pluginName);
371                 if (!$plugin) return;
372
373                 // get arguments
374                 $params = func_get_args();
375
376                 // remove plugin name
377                 array_shift($params);
378
379                 // add item reference (array_unshift didn't work)
380                 $params = array_merge(array(&$this->currentItem),$params);
381
382                 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
383         }
384
385         function parse_edit() {
386                 global $member, $CONF;
387                 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {
388                         $this->parser->parse($this->template['EDITLINK']);
389                 }
390         }
391
392         function parse_editlink() {
393                 global $CONF;
394                 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&amp;itemid=',$this->currentItem->itemid;
395         }
396
397         function parse_editpopupcode() {
398                 echo "if (event &amp;&amp; event.preventDefault) event.preventDefault();winbm=window.open(this.href,'nucleusbm','scrollbars=yes,width=600,height=500,left=10,top=10,status=yes,resizable=yes');winbm.focus();return false;";
399         }
400
401         // helper functions
402
403         /**
404          * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
405          * from hacking your weblog.
406          * 'plugin variables in items' implementation by Andy
407          */
408         function highlightAndParse(&$data) {
409                 $actions =& new BODYACTIONS($this->blog);
410                 $parser =& new PARSER($actions->getDefinedActions(), $actions);
411                 $actions->setTemplate($this->template);
412                 $actions->setHighlight($this->strHighlight);
413                 $actions->setCurrentItem($this->currentItem);
414                 //$actions->setParser($parser);
415                 $parser->parse($actions->highlight($data));
416         }
417
418         /*
419         // this is the function previous to the 'plugin variables in items' implementation by Andy
420         function highlightAndParse(&$data) {
421                 // allow only a limited subset of actions (do not allow includes etc, they might be evil)
422                 $this->parser->actions = array('image','media','popup');
423                 $tmp_highlight = $this->highlight($data);
424                 $this->parser->parse($tmp_highlight);
425                 $this->parser->actions = $this->getDefinedActions();
426         }
427         */
428 }
429
430 ?>