OSDN Git Service

CHANGE: getBlogIDFromItemID()を使わずにManager::getItem()を使うよう変更
[nucleus-jp/nucleus-next.git] / nucleus / libs / ITEMACTIONS.php
1 <?php\r
2 /*\r
3  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
4  * Copyright (C) 2002-2009 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  * This class is used to parse item templates\r
14  *\r
15  * @license http://nucleuscms.org/license.txt GNU General Public License\r
16  * @copyright Copyright (C) 2002-2009 The Nucleus Group\r
17  * @version $Id: ITEMACTIONS.php 1757 2012-04-15 09:02:32Z sakamocchi $\r
18  */\r
19 class ItemActions extends BaseActions\r
20 {\r
21         /**\r
22          * ItemActions::$currentItem\r
23          * item currently being handled (mysql result object, see Blog::showUsingQuery)\r
24          */\r
25         public $currentItem;\r
26         \r
27         /**\r
28          * ItemActions::$linkparams\r
29          * contains an assoc array with parameters that need to be included when\r
30          * generating links to items/archives/... (e.g. catid)\r
31          */\r
32         public $linkparams;\r
33         \r
34         /**\r
35          * ItemActions::$allowEditAll\r
36          * true when the current user is a blog admin (and thus allowed to edit all items) \r
37          */\r
38         private $allowEditAll;\r
39         \r
40         /**\r
41          * ItemActions::$lastVisit\r
42          * timestamp of last visit\r
43          */\r
44         private $lastVisit;\r
45         \r
46         /**\r
47          * ItemActions::$blog\r
48          * reference to the blog currently being displayed\r
49          */\r
50         public $blog;
51         \r
52         /**\r
53          * ItemActions::$template\r
54          * associative array with template info (part name => contents)\r
55          */\r
56         private $template;\r
57         \r
58         /**\r
59          * ItemActions::$showComments\r
60          * true when comments need to be displayed\r
61          */\r
62         private $showComments;\r
63         \r
64         /**\r
65          * ItemActions::$defined_actions\r
66          * defined actions in this class\r
67          */\r
68         static private $defined_actions = array(\r
69                 'author',
70                 'authorid',
71                 'authorlink',
72                 'blogid',\r
73                 'blogurl',
74                 'body',\r
75                 'category',\r
76                 'categorylink',\r
77                 'catid',\r
78                 'closed',
79                 'comments',
80                 'date',\r
81                 'daylink',
82                 'edit',
83                 'editlink',
84                 'editpopupcode',
85                 'itemid',
86                 'itemlink',\r
87                 'karma',
88                 'karmaneglink',
89                 'karmaposlink',\r
90                 'more',
91                 'morelink',
92                 'new',\r
93                 'plugin',
94                 'query',
95                 'relevance',
96                 'smartbody',\r
97                 'syndicate_description',\r
98                 'syndicate_title',\r
99                 'time',\r
100                 'title',\r
101         /* actions defined in BodyAction class */\r
102                 'image',\r
103                 'media',\r
104                 'popup'\r
105         );\r
106         \r
107         /**\r
108          * ItemActions::__construct\r
109          * Enter description here ...\r
110          * @param unknown_type $blog\r
111          */\r
112         public function __construct(&$blog)\r
113         {\r
114                 global $catid, $member;\r
115                 \r
116                 // call constructor of superclass first\r
117                 parent::__construct();
118                 \r
119                 // extra parameters for created links\r
120                 if ( $catid )\r
121                 {\r
122                         $this->linkparams = array('catid' => $catid);\r
123                 }\r
124                 \r
125                 // check if member is blog admin (and thus allowed to edit all items)\r
126                 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));\r
127                 $this->setBlog($blog);\r
128                 return;\r
129         }\r
130         \r
131         /**\r
132          * ItemActions::getAvailableActions()\r
133          * Returns an array with the actions that are defined\r
134          * in the ItemActions class\r
135          * \r
136          * @param       void\r
137          * @return      void\r
138          */\r
139         public function getAvailableActions()\r
140         {\r
141                 return array_merge(self::$defined_actions, parent::getAvailableActions());\r
142         }\r
143         \r
144         /**\r
145          * ItemActions::setLastVisit()\r
146          * \r
147          * @param       timestamp       $lastVisit      timestamp of latest visit\r
148          * @return      void\r
149          */\r
150         public function setLastVisit($lastVisit)\r
151         {\r
152                 $this->lastVisit = $lastVisit;\r
153                 return;\r
154         }\r
155         \r
156         /**\r
157          * ItemActions::setCurrentItem()\r
158          * \r
159          * @param       object  $item   instance of Item class\r
160          * @return      void\r
161          */\r
162         public function setCurrentItem(&$item)\r
163         {\r
164                 global $currentitemid;\r
165                 $this->currentItem =& $item;\r
166                 $currentitemid = $this->currentItem['itemid'];\r
167                 return;\r
168         }\r
169         \r
170         /**\r
171          * ItemActions::setBlog()\r
172          * \r
173          * @param       object  &$blog  instance of Blog class\r
174          * @return      void\r
175          */\r
176         public function setBlog(&$blog)\r
177         {\r
178                 $this->blog =& $blog;\r
179                 return;\r
180         }\r
181         \r
182         /**\r
183          * ItemActions::setTemplate()\r
184          * \r
185          * @param       array   $template       array including templates\r
186          * @return      void\r
187          */\r
188         public function setTemplate($template)\r
189         {\r
190                 $this->template =& $template;\r
191                 return;\r
192         }\r
193         \r
194         /**\r
195          * ItemActions::setShowComments()\r
196          * \r
197          * @param       boolean $val    need to be displayed or not\r
198          * @return      void\r
199          */\r
200         public function setShowComments($val)\r
201         {\r
202                 $this->showComments = (boolean) $val;\r
203                 return;\r
204         }\r
205         \r
206         /**\r
207          * ItemActions::parse_blogid()\r
208          * Parse templatevar blogid\r
209          * \r
210          * @param       void\r
211          * @return      void\r
212          */\r
213         public function parse_blogid()\r
214         {\r
215                 echo $this->blog->getID();\r
216         }\r
217 \r
218         /**\r
219          * ItemActions::parse_body()\r
220          * Parse templatevar body\r
221          * \r
222          * @param       void\r
223          * @return      void\r
224          */\r
225         public function parse_body()\r
226         {\r
227                 $this->highlightAndParse($this->currentItem['body']);\r
228                 return;\r
229         }\r
230         \r
231         /**\r
232          * ItemActions::parse_more()\r
233          * Parse templatevar more\r
234          * \r
235          * @param       void\r
236          * @return      void\r
237          */\r
238         public function parse_more()\r
239         {\r
240                 $this->highlightAndParse($this->currentItem['more']);\r
241                 return;\r
242         }\r
243         \r
244         /**\r
245          * ItemActions::parse_itemid()\r
246          * Parse templatevar itemid\r
247          * \r
248          * @param       void\r
249          * @return      void\r
250          */\r
251         public function parse_itemid()\r
252         {\r
253                 echo $this->currentItem['itemid'];\r
254                 return;\r
255         }\r
256         \r
257         /**\r
258          * ItemActions::parse_category()\r
259          * Parse templatevar category\r
260          * \r
261          * @param       void\r
262          * @return      void\r
263          */\r
264         public function parse_category()\r
265         {\r
266                 echo $this->currentItem['category'];\r
267                 return;\r
268         }\r
269         \r
270         /**\r
271          * ItemActions::parse_categorylink()\r
272          * Parse templatevar categorylink\r
273          * \r
274          * @param       void\r
275          * @return      void\r
276          */\r
277         public function parse_categorylink()\r
278         {\r
279                 echo Link::create_link('category', array('catid' => $this->currentItem['catid'], 'name' => $this->currentItem['category']));\r
280                 return;\r
281         }\r
282         \r
283         /**\r
284          * ItemActions::parse_catid()\r
285          * Parse templatevar catid\r
286          * \r
287          * @param       void\r
288          * @return      void\r
289          */\r
290         public function parse_catid()\r
291         {\r
292                 echo $this->currentItem['catid'];\r
293                 return;\r
294         }\r
295         \r
296         /**\r
297          * ItemActions::parse_authorid()\r
298          * Parse templatevar authorid\r
299          * \r
300          * @param       void\r
301          * @return      void\r
302          */\r
303         public function parse_authorid()\r
304         {\r
305                 echo $this->currentItem['authorid'];\r
306                 return;\r
307         }\r
308         \r
309         /**\r
310          * ItemActions::parse_authorlink()\r
311          * Parse templatevar authorlink\r
312          * \r
313          * @param       void\r
314          * @return      void\r
315          */\r
316         public function parse_authorlink()\r
317         {\r
318                 $data = array(\r
319                                 'memberid' => $this->currentItem['authorid'],\r
320                                 'name' => $this->currentItem['author'],\r
321                                 'extra' => $this->linkparams\r
322                         );\r
323                 \r
324                 echo Link::create_link('member', $data);\r
325                 return;\r
326         }\r
327         \r
328         /**\r
329          * ItemActions::parse_query()\r
330          * Parse templatevar query\r
331          * \r
332          * @param       void\r
333          * @return      void\r
334          */\r
335         public function parse_query()\r
336         {\r
337                 echo $this->strHighlight;\r
338                 return;\r
339         }\r
340         \r
341         /**\r
342          * ItemActions::parse_itemlink()\r
343          * Parse templatevar itemlink\r
344          * \r
345          * @param       void\r
346          * @return      void\r
347          */\r
348         public function parse_itemlink()\r
349         {\r
350                 $data = array(\r
351                         'itemid'        => $this->currentItem['itemid'],\r
352                         'title'         => $this->currentItem['title'],\r
353                         'timestamp'     => $this->currentItem['timestamp'],\r
354                         'extra'         => $this->linkparams\r
355                 );\r
356                 \r
357                 echo Link::create_link('item', $data);\r
358                 return;\r
359         }\r
360         \r
361         /**\r
362          * ItemActions::parse_blogurl()\r
363          * Parse templatevar blogurl\r
364          * \r
365          * @param       void\r
366          * @return      void\r
367          */\r
368         public function parse_blogurl()\r
369         {\r
370                 echo $this->blog->getURL();\r
371                 return;\r
372         }\r
373         \r
374         /**\r
375          * ItemActions::parse_closed()\r
376          * Parse templatevar closed\r
377          * \r
378          * @param       void\r
379          * @return      void\r
380          */\r
381         public function parse_closed()\r
382         {\r
383                 echo $this->currentItem['closed'];\r
384                 return;\r
385         }\r
386         \r
387         /**\r
388          * ItemActions::parse_relevance()\r
389          * Parse templatevar relevance\r
390          * \r
391          * @param       void\r
392          * @return      void\r
393          */\r
394         public function parse_relevance()\r
395         {\r
396                 echo round($this->currentItem['score'], 2);\r
397                 return;\r
398         }\r
399         \r
400         /**\r
401          * ItemActions::parse_title()\r
402          * Parse templatevar title\r
403          *\r
404          * @param       string  $format defines in which format the title is shown\r
405          * @return      void\r
406          */\r
407         public function parse_title($format = '')\r
408         {\r
409                 if ( is_array($this->currentItem) )\r
410                 {\r
411                         $itemtitle = $this->currentItem['title'];\r
412                 }\r
413                 switch ( $format )\r
414                 {\r
415                         case 'xml':\r
416                                 echo Entity::hen($itemtitle);\r
417                                 break;\r
418                         case 'attribute':\r
419                                 echo Entity::hsc($itemtitle);\r
420                                 break;\r
421                         case 'raw':\r
422                                 echo $itemtitle;\r
423                                 break;\r
424                         default:\r
425                                 $this->highlightAndParse($itemtitle);\r
426                                 break;\r
427                 }\r
428                 return;\r
429         }\r
430         \r
431         /**\r
432          * ItemActions::parse_karma()\r
433          * Parse templatevar karma\r
434          * \r
435          * @param       string  $type   type of data for karma\r
436          * @return      void\r
437          */\r
438         public function parse_karma($type = 'totalscore')\r
439         {\r
440                 global $manager;\r
441                 \r
442                 // get karma object\r
443                 $karma =& $manager->getKarma($this->currentItem['itemid']);\r
444                 \r
445                 switch ( $type )\r
446                 {\r
447                         case 'pos':\r
448                                 echo $karma->getNbPosVotes();\r
449                                 break;\r
450                         case 'neg':\r
451                                 echo $karma->getNbNegVotes();\r
452                                 break;\r
453                         case 'votes':\r
454                                 echo $karma->getNbOfVotes();\r
455                                 break;\r
456                         case 'posp':\r
457                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;\r
458                                 echo number_format($percentage,2), '%';\r
459                                 break;\r
460                         case 'negp':\r
461                                 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;\r
462                                 echo number_format($percentage,2), '%';\r
463                                 break;\r
464                         case 'totalscore':\r
465                         default:\r
466                                 echo $karma->getTotalScore();\r
467                                 break;\r
468                 }\r
469                 return;\r
470         }\r
471         \r
472         /**\r
473          * ItemActions::parse_author()\r
474          * Parse templatevar author\r
475          * \r
476          * @param       string  $which  key of data for author\r
477          * @return      void\r
478          */\r
479         public function parse_author($which = '')\r
480         {\r
481                 switch ( $which )\r
482                 {\r
483                         case 'realname':\r
484                                 echo $this->currentItem['authorname'];\r
485                                 break;\r
486                         case 'id':\r
487                                 echo $this->currentItem['authorid'];\r
488                                 break;\r
489                         case 'email':\r
490                                 echo $this->currentItem['authormail'];\r
491                                 break;\r
492                         case 'url':\r
493                                 echo $this->currentItem['authorurl'];\r
494                                 break;\r
495                         case 'name':\r
496                         default:\r
497                                 echo $this->currentItem['author'];\r
498                 }\r
499                 return;\r
500         }\r
501         \r
502         /**\r
503          * ItemActions::parse_smartbody()\r
504          * Parse templatevar smartbody\r
505          * \r
506          * @param       void\r
507          * @return      void\r
508          */\r
509         public function parse_smartbody()\r
510         {\r
511                 if ( !$this->currentItem['more'] )\r
512                 {\r
513                         $this->highlightAndParse($this->currentItem['body']);\r
514                 }\r
515                 else\r
516                 {\r
517                         $this->highlightAndParse($this->currentItem['more']);\r
518                 }\r
519                 return;\r
520         }\r
521         \r
522         /**\r
523          * ItemActions::parse_morelink()\r
524          * Parse templatevar morelink\r
525          */\r
526         public function parse_morelink()\r
527         {\r
528                 if ( $this->currentItem['more'] )\r
529                 {\r
530                         $this->parser->parse($this->template['MORELINK']);\r
531                 }\r
532                 return;\r
533         }\r
534         \r
535         /**\r
536          * ItemActions::parse_date()\r
537          * Parse templatevar date\r
538          *\r
539          * @param       string  $format format optional strftime format\r
540          * @return      void\r
541          */\r
542         public function parse_date($format = '')\r
543         {\r
544                 if ( $format !== '' )\r
545                 {\r
546                         /* do nothing */\r
547                         ;\r
548                 }\r
549                 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )\r
550                 {\r
551                         /* depends on the PHP's current locale */\r
552                         $format = '%X';\r
553                 }\r
554                 else\r
555                 {\r
556                         $format = $this->template['FORMAT_DATE'];\r
557                 }\r
558                 \r
559                 $offset = 0;\r
560                 if ( $this->blog )\r
561                 {\r
562                         $offset = $this->blog->getTimeOffset() * 3600;\r
563                 }\r
564                 \r
565                 echo i18n::formatted_datetime($format, $this->currentItem['timestamp'], $offset);\r
566                 return;\r
567         }\r
568         \r
569         /**\r
570          * ItemActions::parse_time()\r
571          * Parse templatevar time\r
572          *\r
573          * @param       string  $format format optional strftime format\r
574          * @return      void\r
575          */\r
576         public function parse_time($format = '')\r
577         {\r
578                 if ( $format !== '' )\r
579                 {\r
580                         /* do nothing */\r
581                         ;\r
582                 }\r
583                 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )\r
584                 {\r
585                         /* depends on the PHP's current locale */\r
586                         $format = '%x';\r
587                 }\r
588                 else\r
589                 {\r
590                         $format = $this->template['FORMAT_TIME'];\r
591                 }\r
592                 echo i18n::formatted_datetime($format, $this->currentItem['timestamp']);\r
593                 return;\r
594         }\r
595         \r
596         /**\r
597          * ItemActions::parse_syndicate_title()\r
598          * Parse templatevar syndicate_title\r
599          *\r
600          * @param       string  $maxLength      maxLength optional maximum length\r
601          * @return      string  syndicated      title\r
602          */\r
603         public function parse_syndicate_title($maxLength = 100) {\r
604                 $syndicated = strip_tags($this->currentItem['title']);\r
605                 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));\r
606         }\r
607         \r
608         /**\r
609          * ItemActions::parse_syndicate_description()\r
610          * Parse templatevar syndicate_description\r
611          *\r
612          * @param       stromg  $maxLength              maxlength optional maximum length\r
613          * @param       string  $addHighlight   highlighted string\r
614          * @return      void\r
615          */\r
616         public function parse_syndicate_description($maxLength = 250, $addHighlight = 0)\r
617         {\r
618                 $syndicated = strip_tags($this->currentItem['body']);\r
619                 if ( $addHighlight )\r
620                 {\r
621                         $tmp_highlight = Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));\r
622                         echo $this->highlightAndParse($tmp_highlight);\r
623                 }\r
624                 else\r
625                 {\r
626                         echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));\r
627                 }\r
628                 return;\r
629         }\r
630         \r
631         /**\r
632          * ItemActions::parse_karmaposlink()\r
633          * Parse templatevar karmaposlink\r
634          *\r
635          * @param       string  $text   text element for anchor element\r
636          * @return      void\r
637          */\r
638         public function parse_karmaposlink($text = '')\r
639         {\r
640                 global $CONF;\r
641                 $link = $CONF['ActionURL'] . '?action=votepositive&amp;itemid=' . $this->currentItem['itemid'];\r
642                 if ( !$text )\r
643                 {\r
644                         echo '<a href="'.$link.'">' . $text . '</a>';\r
645                 }\r
646                 else\r
647                 {\r
648                         echo $link;\r
649                 }\r
650                 \r
651                 return;\r
652         }\r
653         \r
654         /**\r
655          * ItemActions::parse_karmaneglink()\r
656          * Parse templatevar karmaneglink\r
657          *\r
658          * @param       string $text    text element for anchor element\r
659          * @return      void\r
660          */\r
661         public function parse_karmaneglink($text = '')\r
662         {\r
663                 global $CONF;\r
664                 $link = $CONF['ActionURL'] . '?action=votenegative&amp;itemid='.$this->currentItem['itemid'];\r
665                 \r
666                 if ( !$text )\r
667                 {\r
668                         echo '<a href="'.$link.'">' . $text . '</a>';\r
669                 }\r
670                 else\r
671                 {\r
672                         echo $link;\r
673                 }\r
674                 \r
675                 return;\r
676         }\r
677 \r
678         /**\r
679          * ItemActions::parse_new()\r
680          * Parse templatevar new\r
681          * \r
682          * @param       void\r
683          * @return      void\r
684          */\r
685         public function parse_new()\r
686         {\r
687                 if ( ($this->lastVisit != 0) && ($this->currentItem['timestamp'] > $this->lastVisit) )\r
688                 {\r
689                         echo $this->template['NEW'];\r
690                 }\r
691                 return;\r
692         }\r
693         \r
694         /**\r
695          * ItemActions::parse_daylink()\r
696          * Parse templatevar daylink\r
697          * \r
698          * @param       void\r
699          * @return      void\r
700          */\r
701         public function parse_daylink()\r
702         {\r
703                 echo Link::create_archive_link($this->blog->getID(), i18n::formatted_datetime('%Y-%m-%d', $this->currentItem['timestamp']), $this->linkparams);\r
704                 return;\r
705         }\r
706         \r
707         /**\r
708          * ItemActions::parse_comments(\r
709          * Parse templatevar comments\r
710          * \r
711          * @param       integer $maxToShow      maximum number of comments in a display\r
712          * @return      void\r
713          */\r
714         public function parse_comments($maxToShow = 0)\r
715         {\r
716                 if ( $maxToShow == 0 )\r
717                 {\r
718                         $maxToShow = $this->blog->getMaxComments();\r
719                 }\r
720                 \r
721                 // add comments\r
722                 if ( $this->showComments && $this->blog->commentsEnabled() )\r
723                 {\r
724                         $comments = new Comments($this->currentItem['itemid']);\r
725                         $comments->setItemActions($this);\r
726                         $comments->showComments($this->template, $maxToShow, $this->currentItem['closed'] ? 0 : 1, $this->strHighlight);\r
727                 }\r
728                 return;\r
729         }\r
730         \r
731         /**\r
732          * ItemActions::parse_plugin()\r
733          * Executes a plugin templatevar\r
734          *\r
735          * @param       string  $pluginName     name of plugin (without the NP_)\r
736          * @param       extra parameters can be added\r
737          * @return      void\r
738          */\r
739         public function parse_plugin($pluginName)\r
740         {\r
741                 global $manager;\r
742                 \r
743                 $plugin =& $manager->getPlugin("NP_{$pluginName}");\r
744                 if ( !$plugin )\r
745                 {\r
746                         return;\r
747                 }\r
748                 \r
749                 // get arguments\r
750                 $params = func_get_args();\r
751                 \r
752                 // remove plugin name\r
753                 array_shift($params);\r
754                 \r
755                 // add item reference (array_unshift didn't work)\r
756                 $params = array_merge(array(&$this->currentItem),$params);\r
757                 \r
758                 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);\r
759                 return;\r
760         }\r
761         \r
762         /**\r
763          * ItemActions::parse_edit()\r
764          * Parse templatevar edit\r
765          * \r
766          * @param       void\r
767          * @return      void\r
768          */\r
769         public function parse_edit()\r
770         {\r
771                 global $member, $CONF;\r
772                 if ( $this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem['authorid'])) )\r
773                 {\r
774                         $this->parser->parse($this->template['EDITLINK']);\r
775                 }\r
776                 return;\r
777         }\r
778         \r
779         /**\r
780          * ItemActions::parse_editlink()\r
781          * Parse templatevar editlink\r
782          */\r
783         public function parse_editlink()\r
784         {\r
785                 global $CONF;\r
786                 echo $CONF['AdminURL'] . 'bookmarklet.php?action=edit&amp;itemid=' . $this->currentItem['itemid'];\r
787                 return;\r
788         }\r
789         \r
790         /**\r
791          * ItemActions::parse_editpopupcode()\r
792          * Parse templatevar editpopupcode\r
793          * \r
794          * @param       void\r
795          * @return      void\r
796          */\r
797         public function parse_editpopupcode()\r
798         {\r
799                 echo "if (event &amp;&amp; event.preventDefault) event.preventDefault();winbm=window.open(this.href,'nucleusbm','scrollbars=yes,width=600,height=550,left=10,top=10,status=yes,resizable=yes');winbm.focus();return false;";\r
800                 return;\r
801         }\r
802         \r
803         /**\r
804          * ItemActions::highlightAndParse()\r
805          * Parses highlighted text, with limited actions only (to prevent not fully trusted team members\r
806          * from hacking your weblog.\r
807          * 'plugin variables in items' implementation by Andy\r
808          * \r
809          * @param       array   $data   \r
810          * @return      void\r
811          */\r
812         public function highlightAndParse(&$data)\r
813         {\r
814                 $handler = new BodyActions($this->blog);
815                 $handler->setTemplate($this->template);
816                 $handler->setHighlight($this->strHighlight);
817                 $handler->setCurrentItem($this->currentItem);
818                 \r
819                 $parser = new Parser($handler);\r
820                 $parser->parse($handler->highlight($data));
821                 return;\r
822         }\r
823         \r
824         /**\r
825          * ItemActions::checkCondition()\r
826          * Checks conditions for if statements\r
827          *\r
828          * @param       string  $field  type of <%if%>\r
829          * @param       string  $name   property of field\r
830          * @param       string  $value  value of property\r
831          * @return      boolean\r
832          */\r
833         protected function checkCondition($field, $name='', $value = '')\r
834         {\r
835                 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;\r
836                 \r
837                 $condition = 0;\r
838                 switch ( $field )\r
839                 {\r
840                         case 'category':\r
841                                 $condition = ($blog && $this->ifCategory($name,$value));\r
842                                 break;\r
843                         case 'itemcategory':\r
844                                 $condition = ($this->ifItemCategory($name,$value));\r
845                                 break;\r
846                         case 'blogsetting':\r
847                                 $condition = ($blog && ($blog->getSetting($name) == $value));\r
848                                 break;\r
849                         case 'itemblogsetting':\r
850                                 $item =& $manager->getItem($this->currentItem['itemid'], 1, 1);\r
851                                 $t_blog =& $manager->getBlog($item['blogid']);\r
852                                 $condition = ($t_blog && ($t_blog->getSetting($name) == $value));\r
853                                 break;\r
854                         case 'loggedin':\r
855                                 $condition = $member->isLoggedIn();\r
856                                 break;\r
857                         case 'onteam':\r
858                                 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);\r
859                                 break;\r
860                         case 'admin':\r
861                                 $condition = $member->isLoggedIn() && $this->ifAdmin($name);\r
862                                 break;\r
863                         case 'author':\r
864                                 $condition = ($this->ifAuthor($name,$value));\r
865                                 break;\r
866                         case 'hasplugin':\r
867                                 $condition = $this->ifHasPlugin($name, $value);\r
868                                 break;\r
869                         default:\r
870                                 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);\r
871                                 break;\r
872                 }\r
873                 return $condition;\r
874         }       \r
875         \r
876         /**\r
877          * ItemActions::ifCategory()\r
878          *  Different checks for a category\r
879          *  \r
880          * @param       string  $key    key of category\r
881          * @param       string  $value  value for key of category\r
882          * @return      boolean\r
883          */\r
884         private function ifCategory($key = '', $value = '')\r
885         {\r
886                 global $blog, $catid;\r
887                 \r
888                 // when no parameter is defined, just check if a category is selected\r
889                 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )\r
890                 {\r
891                         return (boolean) $blog->isValidCategory($catid);\r
892                 }\r
893                 \r
894                 // check category name\r
895                 if ( $key == 'catname' )\r
896                 {\r
897                         $value = $blog->getCategoryIdFromName($value);\r
898                         if ( $value == $catid )\r
899                         {\r
900                                 return (boolean) $blog->isValidCategory($catid);\r
901                         }\r
902                 }\r
903                 \r
904                 // check category id\r
905                 if ( ($key == 'catid') && ($value == $catid) )\r
906                 {\r
907                         return (boolean) $blog->isValidCategory($catid);\r
908                 }\r
909                 return FALSE;\r
910         }\r
911         \r
912         /**\r
913          * ItemActions::ifAuthor()\r
914          * Different checks for an author\r
915          * \r
916          * @param       string  $key    key of data for author\r
917          * @param       string  $value  value of data for author\r
918          * @return      boolean correct or not\r
919          */\r
920         private function ifAuthor($key = '', $value = '')\r
921         {\r
922                 global $member, $manager;\r
923                 \r
924                 // when no parameter is defined, just check if author is current visitor\r
925                 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )\r
926                 {\r
927                         return (boolean) ((integer) $member->getID() > 0 && (integer) $member->getID() == (integer) $this->currentItem['authorid']);\r
928                 }\r
929                 \r
930                 // check author name\r
931                 if ( $key == 'name' )\r
932                 {\r
933                         $value = strtolower($value);\r
934                         if ( $value == strtolower($this->currentItem['author']) )\r
935                         {\r
936                                 return TRUE;\r
937                         }\r
938                 }\r
939                 \r
940                 // check if author is admin\r
941                 if ( ($key == 'isadmin') )\r
942                 {\r
943                         $i_author =& $manager->getMember($this->currentItem['authorid']);\r
944                         if ( $i_author->isAdmin() )\r
945                         {\r
946                                 return TRUE;\r
947                         }\r
948                         return (boolean) $i_author->isBlogAdmin($this->currentItem['blogid']);\r
949                 }\r
950                 \r
951                 return FALSE;\r
952         }\r
953         \r
954         /**\r
955          * ItemActions::ifItemCategory()\r
956          * Different checks for a category\r
957          * \r
958          * @param       string  $key    key of data for category to which item belongs\r
959          * @param       string  $value  value of data for category to which item belongs\r
960          * @return boolean      correct or not\r
961          */\r
962         private function ifItemCategory($key = '', $value='')\r
963         {\r
964                 global $catid, $manager;\r
965                 \r
966                 $blog =& $manager->getBlog($this->currentItem['blogid']);\r
967                 \r
968                 // when no parameter is defined, just check if a category is selected\r
969                 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )\r
970                 {\r
971                         return (boolean) $blog->isValidCategory($catid);\r
972                 }\r
973                 \r
974                 $icatid = $this->currentItem['catid'];\r
975                 \r
976                 // check category name\r
977                 if ( $key == 'catname' )\r
978                 {\r
979                         $value = $blog->getCategoryIdFromName($value);\r
980                         if ( $value == $icatid )\r
981                         {\r
982                                 return (boolean) $blog->isValidCategory($icatid);\r
983                         }\r
984                 }\r
985                 \r
986                 // check category id\r
987                 if ( ($key == 'catid') && ($value == $icatid) )\r
988                 {\r
989                         return (boolean) $blog->isValidCategory($icatid);\r
990                 }\r
991                 return FALSE;\r
992         }\r
993 \r
994         \r
995         /**\r
996          * ItemActions::ifOnTeam()\r
997          * Checks if a member is on the team of a blog and return his rights\r
998          * \r
999          * @param       string  $blogName       name of weblog\r
1000          * @return      boolean correct or not\r
1001          */\r
1002         private function ifOnTeam($blogName = '')\r
1003         {\r
1004                 global $blog, $member, $manager;\r
1005                 \r
1006                 // when no blog found\r
1007                 if ( ($blogName == '') && (!is_object($blog)) )\r
1008                 {\r
1009                         return 0;\r
1010                 }\r
1011                 \r
1012                 // explicit blog selection\r
1013                 if ( $blogName != '' )\r
1014                 {\r
1015                         $blogid = getBlogIDFromName($blogName);\r
1016                 }\r
1017                 \r
1018                 // use current blog\r
1019                 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )\r
1020                 {\r
1021                         $blogid = $blog->getID();\r
1022                 }\r
1023                 return (boolean) $member->teamRights($blogid);\r
1024         }\r
1025         \r
1026         /**\r
1027          * ItemActions::ifAdmin()\r
1028          * Checks if a member is admin of a blog\r
1029          * \r
1030          * @param       string  $blogName       name of weblog\r
1031          * @return      boolean correct or not\r
1032          */\r
1033         private function ifAdmin($blogName = '')\r
1034         {\r
1035                 global $blog, $member, $manager;\r
1036                 \r
1037                 // when no blog found\r
1038                 if ( ($blogName == '') && (!is_object($blog)) )\r
1039                 {\r
1040                         return 0;\r
1041                 }\r
1042                 \r
1043                 // explicit blog selection\r
1044                 if ( $blogName != '' )\r
1045                 {\r
1046                         $blogid = getBlogIDFromName($blogName);\r
1047                 }\r
1048                 \r
1049                 // use current blog\r
1050                 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )\r
1051                 {\r
1052                         $blogid = $blog->getID();\r
1053                 }\r
1054                 return (boolean) $member->isBlogAdmin($blogid);\r
1055         }\r
1056         \r
1057         \r
1058         /**\r
1059          * ItemActions::ifHasPlugin()\r
1060          *      hasplugin,PlugName\r
1061          *         -> checks if plugin exists\r
1062          *      hasplugin,PlugName,OptionName\r
1063          *         -> checks if the option OptionName from plugin PlugName is not set to 'no'\r
1064          *      hasplugin,PlugName,OptionName=value\r
1065          *         -> checks if the option OptionName from plugin PlugName is set to value\r
1066          *\r
1067          * @param       string  $name   name of plugin\r
1068          * @param       string  $value  key (and value) of plugin option\r
1069          * @return      boolean correct or not\r
1070          */\r
1071         private function ifHasPlugin($name, $value)\r
1072         {\r
1073                 global $manager;\r
1074                 $condition = FALSE;\r
1075                 // (pluginInstalled method won't write a message in the actionlog on failure)\r
1076                 if ( $manager->pluginInstalled("NP_{$name}"))\r
1077                 {\r
1078                         $plugin =& $manager->getPlugin('NP_' . $name);\r
1079                         if ( $plugin != NULL )\r
1080                         {\r
1081                                 if ( $value == "" )\r
1082                                 {\r
1083                                         $condition = TRUE;\r
1084                                 }\r
1085                                 else\r
1086                                 {\r
1087                                         list($name2, $value2) = preg_split('#=#', $value, 2);\r
1088                                         if ( $value2 == "" && $plugin->getOption($name2) != 'no' )\r
1089                                         {\r
1090                                                 $condition = TRUE;\r
1091                                         }\r
1092                                         else if ( $plugin->getOption($name2) == $value2 )\r
1093                                         {\r
1094                                                 $condition = TRUE;\r
1095                                         }\r
1096                                 }\r
1097                         }\r
1098                 }\r
1099                 return (boolean) $condition;\r
1100         }\r
1101         \r
1102         /**\r
1103          * ItemActions::ifPlugin()\r
1104          * Checks if a plugin exists and call its doIf function\r
1105          * \r
1106          * @param       string  $name   name of plugin\r
1107          * @param       string  $key    key of plugin option\r
1108          * @param       string  $value  value of plugin option\r
1109          * @return      boolean callback output from plugin\r
1110          */\r
1111         private function ifPlugin($name, $key = '', $value = '')\r
1112         {\r
1113                 global $manager;\r
1114                 \r
1115                 $plugin =& $manager->getPlugin("NP_{$name}");\r
1116                 if ( !$plugin )\r
1117                 {\r
1118                         return;\r
1119                 }\r
1120                 $params = func_get_args();\r
1121                 array_shift($params);\r
1122                 \r
1123                 return (boolean) call_user_func_array(array(&$plugin, 'doIf'), $params);\r
1124         }\r
1125 }