3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
4 * Copyright (C) 2002-2009 The Nucleus Group
\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
13 * This class is used to parse item templates
\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
19 class ItemActions extends BaseActions
\r
22 * ItemActions::$currentItem
\r
23 * item currently being handled (mysql result object, see Blog::showUsingQuery)
\r
25 public $currentItem;
\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
35 * ItemActions::$allowEditAll
\r
36 * true when the current user is a blog admin (and thus allowed to edit all items)
\r
38 private $allowEditAll;
\r
41 * ItemActions::$lastVisit
\r
42 * timestamp of last visit
\r
47 * ItemActions::$blog
\r
48 * reference to the blog currently being displayed
\r
53 * ItemActions::$template
\r
54 * associative array with template info (part name => contents)
\r
59 * ItemActions::$showComments
\r
60 * true when comments need to be displayed
\r
62 private $showComments;
\r
65 * ItemActions::$defined_actions
\r
66 * defined actions in this class
\r
68 static private $defined_actions = array(
\r
97 'syndicate_description',
101 /* actions defined in BodyAction class */
108 * ItemActions::__construct
\r
109 * Enter description here ...
\r
110 * @param unknown_type $blog
\r
112 public function __construct(&$blog)
\r
114 global $catid, $member;
\r
115 // call constructor of superclass first
\r
116 parent::__construct();
118 // extra parameters for created links
\r
121 $this->linkparams = array('catid' => $catid);
\r
124 // check if member is blog admin (and thus allowed to edit all items)
\r
125 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
\r
126 $this->setBlog($blog);
\r
131 * ItemActions::getAvailableActions()
\r
132 * Returns an array with the actions that are defined
\r
133 * in the ItemActions class
\r
138 public function getAvailableActions()
\r
140 return array_merge(self::$defined_actions, parent::getAvailableActions());
\r
144 * ItemActions::setLastVisit()
\r
146 * @param timestamp $lastVisit timestamp of latest visit
\r
149 public function setLastVisit($lastVisit)
\r
151 $this->lastVisit = $lastVisit;
\r
156 * ItemActions::setCurrentItem()
\r
158 * @param object $item instance of Item class
\r
161 public function setCurrentItem(&$item)
\r
163 global $currentitemid;
\r
164 $this->currentItem =& $item;
\r
165 $currentitemid = $this->currentItem->itemid;
\r
170 * ItemActions::setBlog()
\r
172 * @param object &$blog instance of Blog class
\r
175 public function setBlog(&$blog)
\r
177 $this->blog =& $blog;
\r
182 * ItemActions::setTemplate()
\r
184 * @param array $template array including templates
\r
187 public function setTemplate($template)
\r
189 $this->template =& $template;
\r
194 * ItemActions::setShowComments()
\r
196 * @param boolean $val need to be displayed or not
\r
199 public function setShowComments($val)
\r
201 $this->showComments = (boolean) $val;
\r
206 * ItemActions::parse_blogid()
\r
207 * Parse templatevar blogid
\r
212 public function parse_blogid()
\r
214 echo $this->blog->getID();
\r
218 * ItemActions::parse_body()
\r
219 * Parse templatevar body
\r
224 public function parse_body()
\r
226 $this->highlightAndParse($this->currentItem->body);
\r
231 * ItemActions::parse_more()
\r
232 * Parse templatevar more
\r
237 public function parse_more()
\r
239 $this->highlightAndParse($this->currentItem->more);
\r
244 * ItemActions::parse_itemid()
\r
245 * Parse templatevar itemid
\r
250 public function parse_itemid()
\r
252 echo $this->currentItem->itemid;
\r
257 * ItemActions::parse_category()
\r
258 * Parse templatevar category
\r
263 public function parse_category()
\r
265 echo $this->currentItem->category;
\r
270 * ItemActions::parse_categorylink()
\r
271 * Parse templatevar categorylink
\r
276 public function parse_categorylink()
\r
278 echo Link::create_link('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
\r
283 * ItemActions::parse_catid()
\r
284 * Parse templatevar catid
\r
289 public function parse_catid()
\r
291 echo $this->currentItem->catid;
\r
296 * ItemActions::parse_authorid()
\r
297 * Parse templatevar authorid
\r
302 public function parse_authorid()
\r
304 echo $this->currentItem->authorid;
\r
309 * ItemActions::parse_authorlink()
\r
310 * Parse templatevar authorlink
\r
315 public function parse_authorlink()
\r
318 'memberid' => $this->currentItem->authorid,
\r
319 'name' => $this->currentItem->author,
\r
320 'extra' => $this->linkparams
\r
323 echo Link::create_link('member', $data);
\r
328 * ItemActions::parse_query()
\r
329 * Parse templatevar query
\r
334 public function parse_query()
\r
336 echo $this->strHighlight;
\r
341 * ItemActions::parse_itemlink()
\r
342 * Parse templatevar itemlink
\r
347 public function parse_itemlink()
\r
350 'itemid' => $this->currentItem->itemid,
\r
351 'title' => $this->currentItem->title,
\r
352 'timestamp' => $this->currentItem->timestamp,
\r
353 'extra' => $this->linkparams
\r
356 echo Link::create_link('item', $data);
\r
361 * ItemActions::parse_blogurl()
\r
362 * Parse templatevar blogurl
\r
367 public function parse_blogurl()
\r
369 echo $this->blog->getURL();
\r
374 * ItemActions::parse_closed()
\r
375 * Parse templatevar closed
\r
380 public function parse_closed()
\r
382 echo $this->currentItem->closed;
\r
387 * ItemActions::parse_relevance()
\r
388 * Parse templatevar relevance
\r
393 public function parse_relevance()
\r
395 echo round($this->currentItem->score,2);
\r
400 * ItemActions::parse_title()
\r
401 * Parse templatevar title
\r
403 * @param string $format defines in which format the title is shown
\r
406 public function parse_title($format = '')
\r
408 if ( is_array($this->currentItem) )
\r
410 $itemtitle = $this->currentItem['title'];
\r
412 elseif ( is_object($this->currentItem) )
\r
414 $itemtitle = $this->currentItem->title;
\r
419 echo Entity::hen($itemtitle);
\r
422 echo Entity::hsc($itemtitle);
\r
428 $this->highlightAndParse($itemtitle);
\r
435 * ItemActions::parse_karma()
\r
436 * Parse templatevar karma
\r
438 * @param string $type type of data for karma
\r
441 public function parse_karma($type = 'totalscore')
\r
445 // get karma object
\r
446 $karma =& $manager->getKarma($this->currentItem->itemid);
\r
451 echo $karma->getNbPosVotes();
\r
454 echo $karma->getNbNegVotes();
\r
457 echo $karma->getNbOfVotes();
\r
460 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
\r
461 echo number_format($percentage,2), '%';
\r
464 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
\r
465 echo number_format($percentage,2), '%';
\r
469 echo $karma->getTotalScore();
\r
476 * ItemActions::parse_author()
\r
477 * Parse templatevar author
\r
479 * @param string $which key of data for author
\r
482 public function parse_author($which = '')
\r
487 echo $this->currentItem->authorname;
\r
490 echo $this->currentItem->authorid;
\r
493 echo $this->currentItem->authormail;
\r
496 echo $this->currentItem->authorurl;
\r
500 echo $this->currentItem->author;
\r
506 * ItemActions::parse_smartbody()
\r
507 * Parse templatevar smartbody
\r
512 public function parse_smartbody()
\r
514 if ( !$this->currentItem->more )
\r
516 $this->highlightAndParse($this->currentItem->body);
\r
520 $this->highlightAndParse($this->currentItem->more);
\r
526 * ItemActions::parse_morelink()
\r
527 * Parse templatevar morelink
\r
529 public function parse_morelink()
\r
531 if ( $this->currentItem->more )
\r
533 $this->parser->parse($this->template['MORELINK']);
\r
539 * ItemActions::parse_date()
\r
540 * Parse templatevar date
\r
542 * @param string $format format optional strftime format
\r
545 public function parse_date($format = '')
\r
547 if ( $format !== '' )
\r
552 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
\r
554 /* depends on the PHP's current locale */
\r
559 $format = $this->template['FORMAT_DATE'];
\r
565 $offset = $this->blog->getTimeOffset() * 3600;
\r
568 echo i18n::formatted_datetime($format, $this->currentItem->timestamp, $offset);
\r
573 * ItemActions::parse_time()
\r
574 * Parse templatevar time
\r
576 * @param string $format format optional strftime format
\r
579 public function parse_time($format = '')
\r
581 if ( $format !== '' )
\r
586 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
\r
588 /* depends on the PHP's current locale */
\r
593 $format = $this->template['FORMAT_TIME'];
\r
595 echo i18n::formatted_datetime($format, $this->currentItem->timestamp);
\r
600 * ItemActions::parse_syndicate_title()
\r
601 * Parse templatevar syndicate_title
\r
603 * @param string $maxLength maxLength optional maximum length
\r
604 * @return string syndicated title
\r
606 public function parse_syndicate_title($maxLength = 100) {
\r
607 $syndicated = strip_tags($this->currentItem->title);
\r
608 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
\r
612 * ItemActions::parse_syndicate_description()
\r
613 * Parse templatevar syndicate_description
\r
615 * @param stromg $maxLength maxlength optional maximum length
\r
616 * @param string $addHighlight highlighted string
\r
619 public function parse_syndicate_description($maxLength = 250, $addHighlight = 0)
\r
621 $syndicated = strip_tags($this->currentItem->body);
\r
622 if ( $addHighlight )
\r
624 $tmp_highlight = Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
\r
625 echo $this->highlightAndParse($tmp_highlight);
\r
629 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
\r
635 * ItemActions::parse_karmaposlink()
\r
636 * Parse templatevar karmaposlink
\r
638 * @param string $text text element for anchor element
\r
641 public function parse_karmaposlink($text = '')
\r
644 $link = $CONF['ActionURL'] . '?action=votepositive&itemid=' . $this->currentItem->itemid;
\r
647 echo '<a href="'.$link.'">' . $text . '</a>';
\r
658 * ItemActions::parse_karmaneglink()
\r
659 * Parse templatevar karmaneglink
\r
661 * @param string $text text element for anchor element
\r
664 public function parse_karmaneglink($text = '')
\r
667 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem->itemid;
\r
671 echo '<a href="'.$link.'">' . $text . '</a>';
\r
682 * ItemActions::parse_new()
\r
683 * Parse templatevar new
\r
688 public function parse_new()
\r
690 if ( ($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit) )
\r
692 echo $this->template['NEW'];
\r
698 * ItemActions::parse_daylink()
\r
699 * Parse templatevar daylink
\r
704 public function parse_daylink()
\r
706 echo Link::create_archive_link($this->blog->getID(), i18n::formatted_datetime('%Y-%m-%d', $this->currentItem->timestamp), $this->linkparams);
\r
711 * ItemActions::parse_comments(
\r
712 * Parse templatevar comments
\r
714 * @param integer $maxToShow maximum number of comments in a display
\r
717 public function parse_comments($maxToShow = 0)
\r
719 if ( $maxToShow == 0 )
\r
721 $maxToShow = $this->blog->getMaxComments();
\r
725 if ( $this->showComments && $this->blog->commentsEnabled() )
\r
727 $comments = new Comments($this->currentItem->itemid);
\r
728 $comments->setItemActions($this);
\r
729 $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
\r
735 * ItemActions::parse_plugin()
\r
736 * Executes a plugin templatevar
\r
738 * @param string $pluginName name of plugin (without the NP_)
\r
739 * @param extra parameters can be added
\r
742 public function parse_plugin($pluginName)
\r
746 $plugin =& $manager->getPlugin("NP_{$pluginName}");
\r
753 $params = func_get_args();
\r
755 // remove plugin name
\r
756 array_shift($params);
\r
758 // add item reference (array_unshift didn't work)
\r
759 $params = array_merge(array(&$this->currentItem),$params);
\r
761 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
\r
766 * ItemActions::parse_edit()
\r
767 * Parse templatevar edit
\r
772 public function parse_edit()
\r
774 global $member, $CONF;
\r
775 if ( $this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) )
\r
777 $this->parser->parse($this->template['EDITLINK']);
\r
783 * ItemActions::parse_editlink()
\r
784 * Parse templatevar editlink
\r
786 public function parse_editlink()
\r
789 echo $CONF['AdminURL'] . 'bookmarklet.php?action=edit&itemid=' . $this->currentItem->itemid;
\r
794 * ItemActions::parse_editpopupcode()
\r
795 * Parse templatevar editpopupcode
\r
800 public function parse_editpopupcode()
\r
802 echo "if (event && 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
807 * ItemActions::highlightAndParse()
\r
808 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
\r
809 * from hacking your weblog.
\r
810 * 'plugin variables in items' implementation by Andy
\r
812 * @param array $data
\r
815 public function highlightAndParse(&$data)
\r
817 $handler = new BodyActions($this->blog);
818 $handler->setTemplate($this->template);
819 $handler->setHighlight($this->strHighlight);
820 $handler->setCurrentItem($this->currentItem);
822 $parser = new Parser($handler);
\r
823 $parser->parse($handler->highlight($data));
828 * ItemActions::checkCondition()
\r
829 * Checks conditions for if statements
\r
831 * @param string $field type of <%if%>
\r
832 * @param string $name property of field
\r
833 * @param string $value value of property
\r
836 protected function checkCondition($field, $name='', $value = '')
\r
838 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
\r
844 $condition = ($blog && $this->ifCategory($name,$value));
\r
846 case 'itemcategory':
\r
847 $condition = ($this->ifItemCategory($name,$value));
\r
849 case 'blogsetting':
\r
850 $condition = ($blog && ($blog->getSetting($name) == $value));
\r
852 case 'itemblogsetting':
\r
853 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
\r
854 $condition = ($b && ($b->getSetting($name) == $value));
\r
857 $condition = $member->isLoggedIn();
\r
860 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
\r
863 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
\r
866 $condition = ($this->ifAuthor($name,$value));
\r
869 $condition = $this->ifHasPlugin($name, $value);
\r
872 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
\r
879 * ItemActions::ifCategory()
\r
880 * Different checks for a category
\r
882 * @param string $key key of category
\r
883 * @param string $value value for key of category
\r
886 private function ifCategory($key = '', $value = '')
\r
888 global $blog, $catid;
\r
890 // when no parameter is defined, just check if a category is selected
\r
891 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
\r
893 return (boolean) $blog->isValidCategory($catid);
\r
896 // check category name
\r
897 if ( $key == 'catname' )
\r
899 $value = $blog->getCategoryIdFromName($value);
\r
900 if ( $value == $catid )
\r
902 return (boolean) $blog->isValidCategory($catid);
\r
906 // check category id
\r
907 if ( ($key == 'catid') && ($value == $catid) )
\r
909 return (boolean) $blog->isValidCategory($catid);
\r
915 * ItemActions::ifAuthor()
\r
916 * Different checks for an author
\r
918 * @param string $key key of data for author
\r
919 * @param string $value value of data for author
\r
920 * @return boolean correct or not
\r
922 private function ifAuthor($key = '', $value = '')
\r
924 global $member, $manager;
\r
926 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
\r
928 // when no parameter is defined, just check if author is current visitor
\r
929 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
\r
931 return (boolean) ((integer) $member->getID() > 0 && (integer) $member->getID() == (integer) $this->currentItem->authorid);
\r
934 // check author name
\r
935 if ( $key == 'name' )
\r
937 $value = strtolower($value);
\r
938 if ( $value == strtolower($this->currentItem->author) )
\r
944 // check if author is admin
\r
945 if ( ($key == 'isadmin') )
\r
947 $aid = intval($this->currentItem->authorid);
\r
948 $blogid = intval($b->getID());
\r
949 $amember =& $manager->getMember($aid);
\r
950 if ( $amember->isAdmin() )
\r
954 return (boolean) $amember->isBlogAdmin($blogid);
\r
961 * ItemActions::ifItemCategory()
\r
962 * Different checks for a category
\r
964 * @param string $key key of data for category to which item belongs
\r
965 * @param string $value value of data for category to which item belongs
\r
966 * @return boolean correct or not
\r
968 private function ifItemCategory($key = '', $value='')
\r
970 global $catid, $manager;
\r
972 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
\r
974 // when no parameter is defined, just check if a category is selected
\r
975 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
\r
977 return (boolean) $b->isValidCategory($catid);
\r
980 $icatid = $this->currentItem->catid;
\r
982 // check category name
\r
983 if ( $key == 'catname' )
\r
985 $value = $b->getCategoryIdFromName($value);
\r
986 if ( $value == $icatid )
\r
988 return (boolean) $b->isValidCategory($icatid);
\r
992 // check category id
\r
993 if ( ($key == 'catid') && ($value == $icatid) )
\r
995 return (boolean) $b->isValidCategory($icatid);
\r
1002 * ItemActions::ifOnTeam()
\r
1003 * Checks if a member is on the team of a blog and return his rights
\r
1005 * @param string $blogName name of weblog
\r
1006 * @return boolean correct or not
\r
1008 private function ifOnTeam($blogName = '')
\r
1010 global $blog, $member, $manager;
\r
1012 // when no blog found
\r
1013 if ( ($blogName == '') && (!is_object($blog)) )
\r
1018 // explicit blog selection
\r
1019 if ( $blogName != '' )
\r
1021 $blogid = getBlogIDFromName($blogName);
\r
1024 // use current blog
\r
1025 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
\r
1027 $blogid = $blog->getID();
\r
1029 return (boolean) $member->teamRights($blogid);
\r
1033 * ItemActions::ifAdmin()
\r
1034 * Checks if a member is admin of a blog
\r
1036 * @param string $blogName name of weblog
\r
1037 * @return boolean correct or not
\r
1039 private function ifAdmin($blogName = '')
\r
1041 global $blog, $member, $manager;
\r
1043 // when no blog found
\r
1044 if ( ($blogName == '') && (!is_object($blog)) )
\r
1049 // explicit blog selection
\r
1050 if ( $blogName != '' )
\r
1052 $blogid = getBlogIDFromName($blogName);
\r
1055 // use current blog
\r
1056 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
\r
1058 $blogid = $blog->getID();
\r
1060 return (boolean) $member->isBlogAdmin($blogid);
\r
1065 * ItemActions::ifHasPlugin()
\r
1066 * hasplugin,PlugName
\r
1067 * -> checks if plugin exists
\r
1068 * hasplugin,PlugName,OptionName
\r
1069 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
\r
1070 * hasplugin,PlugName,OptionName=value
\r
1071 * -> checks if the option OptionName from plugin PlugName is set to value
\r
1073 * @param string $name name of plugin
\r
1074 * @param string $value key (and value) of plugin option
\r
1075 * @return boolean correct or not
\r
1077 private function ifHasPlugin($name, $value)
\r
1080 $condition = FALSE;
\r
1081 // (pluginInstalled method won't write a message in the actionlog on failure)
\r
1082 if ( $manager->pluginInstalled("NP_{$name}"))
\r
1084 $plugin =& $manager->getPlugin('NP_' . $name);
\r
1085 if ( $plugin != NULL )
\r
1087 if ( $value == "" )
\r
1089 $condition = TRUE;
\r
1093 list($name2, $value2) = preg_split('#=#', $value, 2);
\r
1094 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
\r
1096 $condition = TRUE;
\r
1098 else if ( $plugin->getOption($name2) == $value2 )
\r
1100 $condition = TRUE;
\r
1105 return (boolean) $condition;
\r
1109 * ItemActions::ifPlugin()
\r
1110 * Checks if a plugin exists and call its doIf function
\r
1112 * @param string $name name of plugin
\r
1113 * @param string $key key of plugin option
\r
1114 * @param string $value value of plugin option
\r
1115 * @return boolean callback output from plugin
\r
1117 private function ifPlugin($name, $key = '', $value = '')
\r
1121 $plugin =& $manager->getPlugin("NP_{$name}");
\r
1126 $params = func_get_args();
\r
1127 array_shift($params);
\r
1129 return (boolean) call_user_func_array(array(&$plugin, 'doIf'), $params);
\r