3 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
4 * Copyright (C) 2002-2009 The Nucleus Group
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)
13 * This class is used to parse item templates
15 * @license http://nucleuscms.org/license.txt GNU General Public License
16 * @copyright Copyright (C) 2002-2009 The Nucleus Group
18 * @version $Id: ITEMACTIONS.php 1757 2012-04-15 09:02:32Z sakamocchi $
20 * @version $Id: ITEMACTIONS.php 1886 2012-06-17 08:27:27Z sakamocchi $
21 >>>>>>> skinnable-master
23 class ItemActions extends BaseActions
26 * ItemActions::$currentItem
27 * item currently being handled (mysql result object, see Blog::showUsingQuery)
32 * ItemActions::$linkparams
33 * contains an assoc array with parameters that need to be included when
34 * generating links to items/archives/... (e.g. catid)
39 * ItemActions::$allowEditAll
40 * true when the current user is a blog admin (and thus allowed to edit all items)
42 private $allowEditAll;
45 * ItemActions::$lastVisit
46 * timestamp of last visit
52 * reference to the blog currently being displayed
57 * ItemActions::$template
58 * associative array with template info (part name => contents)
63 * ItemActions::$showComments
64 * true when comments need to be displayed
66 private $showComments;
69 * ItemActions::$defined_actions
70 * defined actions in this class
72 static private $defined_actions = array(
101 'syndicate_description',
105 /* actions defined in BodyAction class */
112 >>>>>>> skinnable-master
116 * ItemActions::__construct
117 * Enter description here ...
118 * @param unknown_type $blog
120 public function __construct(&$blog)
122 global $catid, $member;
124 // call constructor of superclass first
125 parent::__construct();
127 // extra parameters for created links
130 $this->linkparams = array('catid' => $catid);
133 // check if member is blog admin (and thus allowed to edit all items)
134 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
135 $this->setBlog($blog);
140 * ItemActions::getAvailableActions()
141 * Returns an array with the actions that are defined
142 * in the ItemActions class
147 public function getAvailableActions()
149 return array_merge(self::$defined_actions, parent::getAvailableActions());
153 * ItemActions::setLastVisit()
155 * @param timestamp $lastVisit timestamp of latest visit
158 public function setLastVisit($lastVisit)
160 $this->lastVisit = $lastVisit;
165 * ItemActions::setCurrentItem()
167 * @param object $item instance of Item class
170 public function setCurrentItem(&$item)
172 global $currentitemid;
173 $this->currentItem =& $item;
174 $currentitemid = $this->currentItem['itemid'];
179 * ItemActions::setBlog()
181 * @param object &$blog instance of Blog class
184 public function setBlog(&$blog)
186 $this->blog =& $blog;
191 * ItemActions::setTemplate()
193 * @param array $template array including templates
196 public function setTemplate($template)
198 $this->template =& $template;
203 * ItemActions::setShowComments()
205 * @param boolean $val need to be displayed or not
208 public function setShowComments($val)
210 $this->showComments = (boolean) $val;
215 * ItemActions::parse_blogid()
216 * Parse templatevar blogid
221 public function parse_blogid()
223 echo $this->blog->getID();
227 * ItemActions::parse_body()
228 * Parse templatevar body
233 public function parse_body()
235 $this->highlightAndParse($this->currentItem['body']);
240 * ItemActions::parse_more()
241 * Parse templatevar more
246 public function parse_more()
248 $this->highlightAndParse($this->currentItem['more']);
253 * ItemActions::parse_itemid()
254 * Parse templatevar itemid
259 public function parse_itemid()
261 echo $this->currentItem['itemid'];
266 * ItemActions::parse_category()
267 * Parse templatevar category
272 public function parse_category()
274 echo $this->currentItem['category'];
279 * ItemActions::parse_categorylink()
280 * Parse templatevar categorylink
285 public function parse_categorylink()
287 echo Link::create_link('category', array('catid' => $this->currentItem['catid'], 'name' => $this->currentItem['category']));
292 * ItemActions::parse_catid()
293 * Parse templatevar catid
298 public function parse_catid()
300 echo $this->currentItem['catid'];
305 * ItemActions::parse_authorid()
306 * Parse templatevar authorid
311 public function parse_authorid()
313 echo $this->currentItem['authorid'];
318 * ItemActions::parse_authorlink()
319 * Parse templatevar authorlink
324 public function parse_authorlink()
327 'memberid' => $this->currentItem['authorid'],
328 'name' => $this->currentItem['author'],
329 'extra' => $this->linkparams
332 echo Link::create_link('member', $data);
337 * ItemActions::parse_query()
338 * Parse templatevar query
343 public function parse_query()
345 echo $this->strHighlight;
350 * ItemActions::parse_itemlink()
351 * Parse templatevar itemlink
356 public function parse_itemlink()
359 'itemid' => $this->currentItem['itemid'],
360 'title' => $this->currentItem['title'],
361 'timestamp' => $this->currentItem['timestamp'],
362 'extra' => $this->linkparams
365 echo Link::create_link('item', $data);
370 * ItemActions::parse_blogurl()
371 * Parse templatevar blogurl
376 public function parse_blogurl()
378 echo $this->blog->getURL();
383 * ItemActions::parse_closed()
384 * Parse templatevar closed
389 public function parse_closed()
391 echo $this->currentItem['closed'];
396 * ItemActions::parse_relevance()
397 * Parse templatevar relevance
402 public function parse_relevance()
404 echo round($this->currentItem['score'], 2);
409 * ItemActions::parse_title()
410 * Parse templatevar title
412 * @param string $format defines in which format the title is shown
415 public function parse_title($format = '')
417 if ( is_array($this->currentItem) )
419 $itemtitle = $this->currentItem['title'];
424 echo Entity::hen($itemtitle);
427 echo Entity::hsc($itemtitle);
433 $this->highlightAndParse($itemtitle);
440 * ItemActions::parse_karma()
441 * Parse templatevar karma
443 * @param string $type type of data for karma
446 public function parse_karma($type = 'totalscore')
451 $karma =& $manager->getKarma($this->currentItem['itemid']);
456 echo $karma->getNbPosVotes();
459 echo $karma->getNbNegVotes();
462 echo $karma->getNbOfVotes();
465 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
466 echo number_format($percentage,2), '%';
469 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
470 echo number_format($percentage,2), '%';
474 echo $karma->getTotalScore();
481 * ItemActions::parse_author()
482 * Parse templatevar author
484 * @param string $which key of data for author
487 public function parse_author($which = '')
492 echo $this->currentItem['authorname'];
495 echo $this->currentItem['authorid'];
498 echo $this->currentItem['authormail'];
501 echo $this->currentItem['authorurl'];
505 echo $this->currentItem['author'];
511 * ItemActions::parse_smartbody()
512 * Parse templatevar smartbody
517 public function parse_smartbody()
519 if ( !$this->currentItem['more'] )
521 $this->highlightAndParse($this->currentItem['body']);
525 $this->highlightAndParse($this->currentItem['more']);
531 * ItemActions::parse_morelink()
532 * Parse templatevar morelink
534 public function parse_morelink()
536 if ( $this->currentItem['more'] )
538 $this->parser->parse($this->template['MORELINK']);
544 * ItemActions::parse_date()
545 * Parse templatevar date
547 * @param string $format format optional strftime format
550 public function parse_date($format = '')
552 if ( $format !== '' )
557 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
559 /* depends on the PHP's current locale */
564 $format = $this->template['FORMAT_DATE'];
570 $offset = $this->blog->getTimeOffset() * 3600;
573 echo i18n::formatted_datetime($format, $this->currentItem['timestamp'], $offset);
578 * ItemActions::parse_time()
579 * Parse templatevar time
581 * @param string $format format optional strftime format
584 public function parse_time($format = '')
586 if ( $format !== '' )
591 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
593 /* depends on the PHP's current locale */
598 $format = $this->template['FORMAT_TIME'];
600 echo i18n::formatted_datetime($format, $this->currentItem['timestamp']);
605 * ItemActions::parse_syndicate_title()
606 * Parse templatevar syndicate_title
608 * @param string $maxLength maxLength optional maximum length
609 * @return string syndicated title
611 public function parse_syndicate_title($maxLength = 100) {
612 $syndicated = strip_tags($this->currentItem['title']);
613 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
617 * ItemActions::parse_syndicate_description()
618 * Parse templatevar syndicate_description
620 * @param stromg $maxLength maxlength optional maximum length
621 * @param string $addHighlight highlighted string
624 public function parse_syndicate_description($maxLength = 250, $addHighlight = 0)
626 $syndicated = strip_tags($this->currentItem['body']);
629 $tmp_highlight = Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
630 echo $this->highlightAndParse($tmp_highlight);
634 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
640 * ItemActions::parse_karmaposlink()
641 * Parse templatevar karmaposlink
643 * @param string $text text element for anchor element
646 public function parse_karmaposlink($text = '')
649 $link = $CONF['ActionURL'] . '?action=votepositive&itemid=' . $this->currentItem['itemid'];
652 echo '<a href="'.$link.'">' . $text . '</a>';
663 * ItemActions::parse_karmaneglink()
664 * Parse templatevar karmaneglink
666 * @param string $text text element for anchor element
669 public function parse_karmaneglink($text = '')
672 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem['itemid'];
676 echo '<a href="'.$link.'">' . $text . '</a>';
687 * ItemActions::parse_new()
688 * Parse templatevar new
693 public function parse_new()
695 if ( ($this->lastVisit != 0) && ($this->currentItem['timestamp'] > $this->lastVisit) )
697 echo $this->template['NEW'];
703 * ItemActions::parse_daylink()
704 * Parse templatevar daylink
709 public function parse_daylink()
711 echo Link::create_archive_link($this->blog->getID(), i18n::formatted_datetime('%Y-%m-%d', $this->currentItem['timestamp']), $this->linkparams);
716 * ItemActions::parse_comments(
717 * Parse templatevar comments
719 * @param integer $maxToShow maximum number of comments in a display
722 public function parse_comments($maxToShow = 0)
724 if ( $maxToShow == 0 )
726 $maxToShow = $this->blog->getMaxComments();
730 if ( $this->showComments && $this->blog->commentsEnabled() )
732 $comments = new Comments($this->currentItem['itemid']);
733 $comments->setItemActions($this);
734 $comments->showComments($this->template, $maxToShow, $this->currentItem['closed'] ? 0 : 1, $this->strHighlight);
740 * ItemActions::parse_plugin()
741 * Executes a plugin templatevar
743 * @param string $pluginName name of plugin (without the NP_)
744 * @param extra parameters can be added
747 public function parse_plugin($pluginName)
751 $plugin =& $manager->getPlugin("NP_{$pluginName}");
758 $params = func_get_args();
760 // remove plugin name
761 array_shift($params);
763 // add item reference (array_unshift didn't work)
765 $params = array_merge(array(&$this->currentItem),$params);
767 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
769 $target = array(&$this->currentItem);
770 $params = array_merge($target,$params);
772 call_user_func_array(array($plugin,'doTemplateVar'), $params);
773 >>>>>>> skinnable-master
778 * ItemActions::parse_edit()
779 * Parse templatevar edit
784 public function parse_edit()
786 global $member, $CONF;
787 if ( $this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem['authorid'])) )
789 $this->parser->parse($this->template['EDITLINK']);
795 * ItemActions::parse_editlink()
796 * Parse templatevar editlink
798 public function parse_editlink()
801 echo $CONF['AdminURL'] . 'bookmarklet.php?action=edit&itemid=' . $this->currentItem['itemid'];
806 * ItemActions::parse_editpopupcode()
807 * Parse templatevar editpopupcode
812 public function parse_editpopupcode()
814 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;";
819 * ItemActions::highlightAndParse()
820 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
821 * from hacking your weblog.
822 * 'plugin variables in items' implementation by Andy
827 public function highlightAndParse(&$data)
829 $handler = new BodyActions($this->blog);
830 $handler->setTemplate($this->template);
831 $handler->setHighlight($this->strHighlight);
832 $handler->setCurrentItem($this->currentItem);
834 $parser = new Parser($handler);
836 $parser->parse($handler->highlight($data));
838 $highlight = $handler->highlight($data);
839 $parser->parse($highlight);
840 >>>>>>> skinnable-master
845 * ItemActions::checkCondition()
846 * Checks conditions for if statements
848 * @param string $field type of <%if%>
849 * @param string $name property of field
850 * @param string $value value of property
853 protected function checkCondition($field, $name='', $value = '')
855 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
861 $condition = ($blog && $this->ifCategory($name,$value));
864 $condition = ($this->ifItemCategory($name,$value));
867 $condition = ($blog && ($blog->getSetting($name) == $value));
869 case 'itemblogsetting':
871 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem['itemid']));
872 $condition = ($b && ($b->getSetting($name) == $value));
874 $item =& $manager->getItem($this->currentItem['itemid'], 1, 1);
875 $t_blog =& $manager->getBlog($item['blogid']);
876 $condition = ($t_blog && ($t_blog->getSetting($name) == $value));
877 >>>>>>> skinnable-master
880 $condition = $member->isLoggedIn();
883 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
886 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
889 $condition = ($this->ifAuthor($name,$value));
892 $condition = $this->ifHasPlugin($name, $value);
895 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
902 * ItemActions::ifCategory()
903 * Different checks for a category
905 * @param string $key key of category
906 * @param string $value value for key of category
909 private function ifCategory($key = '', $value = '')
911 global $blog, $catid;
913 // when no parameter is defined, just check if a category is selected
914 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
916 return (boolean) $blog->isValidCategory($catid);
919 // check category name
920 if ( $key == 'catname' )
922 $value = $blog->getCategoryIdFromName($value);
923 if ( $value == $catid )
925 return (boolean) $blog->isValidCategory($catid);
930 if ( ($key == 'catid') && ($value == $catid) )
932 return (boolean) $blog->isValidCategory($catid);
938 * ItemActions::ifAuthor()
939 * Different checks for an author
941 * @param string $key key of data for author
942 * @param string $value value of data for author
943 * @return boolean correct or not
945 private function ifAuthor($key = '', $value = '')
947 global $member, $manager;
950 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem['itemid']));
953 >>>>>>> skinnable-master
954 // when no parameter is defined, just check if author is current visitor
955 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
957 return (boolean) ((integer) $member->getID() > 0 && (integer) $member->getID() == (integer) $this->currentItem['authorid']);
961 if ( $key == 'name' )
963 $value = strtolower($value);
964 if ( $value == strtolower($this->currentItem['author']) )
970 // check if author is admin
971 if ( ($key == 'isadmin') )
974 $aid = intval($this->currentItem['authorid']);
975 $blogid = intval($b->getID());
976 $amember =& $manager->getMember($aid);
977 if ( $amember->isAdmin() )
981 return (boolean) $amember->isBlogAdmin($blogid);
983 $i_author =& $manager->getMember($this->currentItem['authorid']);
984 if ( $i_author->isAdmin() )
988 return (boolean) $i_author->isBlogAdmin($this->currentItem['blogid']);
989 >>>>>>> skinnable-master
996 * ItemActions::ifItemCategory()
997 * Different checks for a category
999 * @param string $key key of data for category to which item belongs
1000 * @param string $value value of data for category to which item belongs
1001 * @return boolean correct or not
1003 private function ifItemCategory($key = '', $value='')
1005 global $catid, $manager;
1008 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem['itemid']));
1010 $blog =& $manager->getBlog($this->currentItem['blogid']);
1011 >>>>>>> skinnable-master
1013 // when no parameter is defined, just check if a category is selected
1014 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
1017 return (boolean) $b->isValidCategory($catid);
1019 return (boolean) $blog->isValidCategory($catid);
1020 >>>>>>> skinnable-master
1023 $icatid = $this->currentItem['catid'];
1025 // check category name
1026 if ( $key == 'catname' )
1029 $value = $b->getCategoryIdFromName($value);
1030 if ( $value == $icatid )
1032 return (boolean) $b->isValidCategory($icatid);
1034 $value = $blog->getCategoryIdFromName($value);
1035 if ( $value == $icatid )
1037 return (boolean) $blog->isValidCategory($icatid);
1038 >>>>>>> skinnable-master
1042 // check category id
1043 if ( ($key == 'catid') && ($value == $icatid) )
1046 return (boolean) $b->isValidCategory($icatid);
1048 return (boolean) $blog->isValidCategory($icatid);
1049 >>>>>>> skinnable-master
1056 * ItemActions::ifOnTeam()
1057 * Checks if a member is on the team of a blog and return his rights
1059 * @param string $blogName name of weblog
1060 * @return boolean correct or not
1062 private function ifOnTeam($blogName = '')
1064 global $blog, $member, $manager;
1066 // when no blog found
1067 if ( ($blogName == '') && (!is_object($blog)) )
1072 // explicit blog selection
1073 if ( $blogName != '' )
1075 $blogid = getBlogIDFromName($blogName);
1079 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
1081 $blogid = $blog->getID();
1083 return (boolean) $member->teamRights($blogid);
1087 * ItemActions::ifAdmin()
1088 * Checks if a member is admin of a blog
1090 * @param string $blogName name of weblog
1091 * @return boolean correct or not
1093 private function ifAdmin($blogName = '')
1095 global $blog, $member, $manager;
1097 // when no blog found
1098 if ( ($blogName == '') && (!is_object($blog)) )
1103 // explicit blog selection
1104 if ( $blogName != '' )
1106 $blogid = getBlogIDFromName($blogName);
1110 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
1112 $blogid = $blog->getID();
1114 return (boolean) $member->isBlogAdmin($blogid);
1119 * ItemActions::ifHasPlugin()
1120 * hasplugin,PlugName
1121 * -> checks if plugin exists
1122 * hasplugin,PlugName,OptionName
1123 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
1124 * hasplugin,PlugName,OptionName=value
1125 * -> checks if the option OptionName from plugin PlugName is set to value
1127 * @param string $name name of plugin
1128 * @param string $value key (and value) of plugin option
1129 * @return boolean correct or not
1131 private function ifHasPlugin($name, $value)
1135 // (pluginInstalled method won't write a message in the actionlog on failure)
1136 if ( $manager->pluginInstalled("NP_{$name}"))
1138 $plugin =& $manager->getPlugin('NP_' . $name);
1139 if ( $plugin != NULL )
1147 list($name2, $value2) = preg_split('#=#', $value, 2);
1148 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
1152 else if ( $plugin->getOption($name2) == $value2 )
1159 return (boolean) $condition;
1163 * ItemActions::ifPlugin()
1164 * Checks if a plugin exists and call its doIf function
1166 * @param string $name name of plugin
1167 * @param string $key key of plugin option
1168 * @param string $value value of plugin option
1169 * @return boolean callback output from plugin
1171 private function ifPlugin($name, $key = '', $value = '')
1175 $plugin =& $manager->getPlugin("NP_{$name}");
1180 $params = func_get_args();
1181 array_shift($params);
1184 return (boolean) call_user_func_array(array(&$plugin, 'doIf'), $params);
1186 return (boolean) call_user_func_array(array($plugin, 'doIf'), $params);
1187 >>>>>>> skinnable-master