OSDN Git Service

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