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::getDefinedActions()
\r
132 * Returns an array with the actions that are defined
\r
133 * in the ItemActions class
\r
139 static public function getDefinedActions()
141 return array_merge(self::$defined_actions, parent::getDefinedActions());
145 * ItemActions::setLastVisit()
\r
147 * @param timestamp $lastVisit timestamp of latest visit
\r
150 public function setLastVisit($lastVisit)
\r
152 $this->lastVisit = $lastVisit;
\r
157 * ItemActions::setParser()
\r
159 * @param object &$parser instance of Parser class
\r
162 public function setParser(&$parser)
\r
164 $this->parser =& $parser;
\r
169 * ItemActions::setCurrentItem()
\r
171 * @param object $item instance of Item class
\r
174 public function setCurrentItem(&$item)
\r
176 global $currentitemid;
\r
177 $this->currentItem =& $item;
\r
178 $currentitemid = $this->currentItem->itemid;
\r
183 * ItemActions::setBlog()
\r
185 * @param object &$blog instance of Blog class
\r
188 public function setBlog(&$blog)
\r
190 $this->blog =& $blog;
\r
195 * ItemActions::setTemplate()
\r
197 * @param array $template array including templates
\r
200 public function setTemplate($template)
\r
202 $this->template =& $template;
\r
207 * ItemActions::setShowComments()
\r
209 * @param boolean $val need to be displayed or not
\r
212 public function setShowComments($val)
\r
214 $this->showComments = (boolean) $val;
\r
219 * ItemActions::parse_blogid()
\r
220 * Parse templatevar blogid
\r
225 public function parse_blogid()
\r
227 echo $this->blog->getID();
\r
231 * ItemActions::parse_body()
\r
232 * Parse templatevar body
\r
237 public function parse_body()
\r
239 $this->highlightAndParse($this->currentItem->body);
\r
244 * ItemActions::parse_more()
\r
245 * Parse templatevar more
\r
250 public function parse_more()
\r
252 $this->highlightAndParse($this->currentItem->more);
\r
257 * ItemActions::parse_itemid()
\r
258 * Parse templatevar itemid
\r
263 public function parse_itemid()
\r
265 echo $this->currentItem->itemid;
\r
270 * ItemActions::parse_category()
\r
271 * Parse templatevar category
\r
276 public function parse_category()
\r
278 echo $this->currentItem->category;
\r
283 * ItemActions::parse_categorylink()
\r
284 * Parse templatevar categorylink
\r
289 public function parse_categorylink()
\r
291 echo Link::create_link('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
\r
296 * ItemActions::parse_catid()
\r
297 * Parse templatevar catid
\r
302 public function parse_catid()
\r
304 echo $this->currentItem->catid;
\r
309 * ItemActions::parse_authorid()
\r
310 * Parse templatevar authorid
\r
315 public function parse_authorid()
\r
317 echo $this->currentItem->authorid;
\r
322 * ItemActions::parse_authorlink()
\r
323 * Parse templatevar authorlink
\r
328 public function parse_authorlink()
\r
331 'memberid' => $this->currentItem->authorid,
\r
332 'name' => $this->currentItem->author,
\r
333 'extra' => $this->linkparams
\r
336 echo Link::create_link('member', $data);
\r
341 * ItemActions::parse_query()
\r
342 * Parse templatevar query
\r
347 public function parse_query()
\r
349 echo $this->strHighlight;
\r
354 * ItemActions::parse_itemlink()
\r
355 * Parse templatevar itemlink
\r
360 public function parse_itemlink()
\r
363 'itemid' => $this->currentItem->itemid,
\r
364 'title' => $this->currentItem->title,
\r
365 'timestamp' => $this->currentItem->timestamp,
\r
366 'extra' => $this->linkparams
\r
369 echo Link::create_link('item', $data);
\r
374 * ItemActions::parse_blogurl()
\r
375 * Parse templatevar blogurl
\r
380 public function parse_blogurl()
\r
382 echo $this->blog->getURL();
\r
387 * ItemActions::parse_closed()
\r
388 * Parse templatevar closed
\r
393 public function parse_closed()
\r
395 echo $this->currentItem->closed;
\r
400 * ItemActions::parse_relevance()
\r
401 * Parse templatevar relevance
\r
406 public function parse_relevance()
\r
408 echo round($this->currentItem->score,2);
\r
413 * ItemActions::parse_title()
\r
414 * Parse templatevar title
\r
416 * @param string $format defines in which format the title is shown
\r
419 public function parse_title($format = '')
\r
421 if ( is_array($this->currentItem) )
\r
423 $itemtitle = $this->currentItem['title'];
\r
425 elseif ( is_object($this->currentItem) )
\r
427 $itemtitle = $this->currentItem->title;
\r
432 echo Entity::hen($itemtitle);
\r
435 echo Entity::hsc($itemtitle);
\r
441 $this->highlightAndParse($itemtitle);
\r
448 * ItemActions::parse_karma()
\r
449 * Parse templatevar karma
\r
451 * @param string $type type of data for karma
\r
454 public function parse_karma($type = 'totalscore')
\r
458 // get karma object
\r
459 $karma =& $manager->getKarma($this->currentItem->itemid);
\r
464 echo $karma->getNbPosVotes();
\r
467 echo $karma->getNbNegVotes();
\r
470 echo $karma->getNbOfVotes();
\r
473 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
\r
474 echo number_format($percentage,2), '%';
\r
477 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
\r
478 echo number_format($percentage,2), '%';
\r
482 echo $karma->getTotalScore();
\r
489 * ItemActions::parse_author()
\r
490 * Parse templatevar author
\r
492 * @param string $which key of data for author
\r
495 public function parse_author($which = '')
\r
500 echo $this->currentItem->authorname;
\r
503 echo $this->currentItem->authorid;
\r
506 echo $this->currentItem->authormail;
\r
509 echo $this->currentItem->authorurl;
\r
513 echo $this->currentItem->author;
\r
519 * ItemActions::parse_smartbody()
\r
520 * Parse templatevar smartbody
\r
525 public function parse_smartbody()
\r
527 if ( !$this->currentItem->more )
\r
529 $this->highlightAndParse($this->currentItem->body);
\r
533 $this->highlightAndParse($this->currentItem->more);
\r
539 * ItemActions::parse_morelink()
\r
540 * Parse templatevar morelink
\r
542 public function parse_morelink()
\r
544 if ( $this->currentItem->more )
\r
546 $this->parser->parse($this->template['MORELINK']);
\r
552 * ItemActions::parse_date()
\r
553 * Parse templatevar date
\r
555 * @param string $format format optional strftime format
\r
558 public function parse_date($format = '')
\r
560 if ( $format !== '' )
\r
565 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
\r
567 /* depends on the PHP's current locale */
\r
572 $format = $this->template['FORMAT_DATE'];
\r
578 $offset = $this->blog->getTimeOffset() * 3600;
\r
581 echo i18n::formatted_datetime($format, $this->currentItem->timestamp, $offset);
\r
586 * ItemActions::parse_time()
\r
587 * Parse templatevar time
\r
589 * @param string $format format optional strftime format
\r
592 public function parse_time($format = '')
\r
594 if ( $format !== '' )
\r
599 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
\r
601 /* depends on the PHP's current locale */
\r
606 $format = $this->template['FORMAT_TIME'];
\r
608 echo i18n::formatted_datetime($format, $this->currentItem->timestamp);
\r
613 * ItemActions::parse_syndicate_title()
\r
614 * Parse templatevar syndicate_title
\r
616 * @param string $maxLength maxLength optional maximum length
\r
617 * @return string syndicated title
\r
619 public function parse_syndicate_title($maxLength = 100) {
\r
620 $syndicated = strip_tags($this->currentItem->title);
\r
621 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
\r
625 * ItemActions::parse_syndicate_description()
\r
626 * Parse templatevar syndicate_description
\r
628 * @param stromg $maxLength maxlength optional maximum length
\r
629 * @param string $addHighlight highlighted string
\r
632 public function parse_syndicate_description($maxLength = 250, $addHighlight = 0)
\r
634 $syndicated = strip_tags($this->currentItem->body);
\r
635 if ( $addHighlight )
\r
637 $tmp_highlight = Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
\r
638 echo $this->highlightAndParse($tmp_highlight);
\r
642 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
\r
648 * ItemActions::parse_karmaposlink()
\r
649 * Parse templatevar karmaposlink
\r
651 * @param string $text text element for anchor element
\r
654 public function parse_karmaposlink($text = '')
\r
657 $link = $CONF['ActionURL'] . '?action=votepositive&itemid=' . $this->currentItem->itemid;
\r
660 echo '<a href="'.$link.'">' . $text . '</a>';
\r
671 * ItemActions::parse_karmaneglink()
\r
672 * Parse templatevar karmaneglink
\r
674 * @param string $text text element for anchor element
\r
677 public function parse_karmaneglink($text = '')
\r
680 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem->itemid;
\r
684 echo '<a href="'.$link.'">' . $text . '</a>';
\r
695 * ItemActions::parse_new()
\r
696 * Parse templatevar new
\r
701 public function parse_new()
\r
703 if ( ($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit) )
\r
705 echo $this->template['NEW'];
\r
711 * ItemActions::parse_daylink()
\r
712 * Parse templatevar daylink
\r
717 public function parse_daylink()
\r
719 echo Link::create_archive_link($this->blog->getID(), i18n::formatted_datetime('%Y-%m-%d', $this->currentItem->timestamp), $this->linkparams);
\r
724 * ItemActions::parse_comments(
\r
725 * Parse templatevar comments
\r
727 * @param integer $maxToShow maximum number of comments in a display
\r
730 public function parse_comments($maxToShow = 0)
\r
732 if ( $maxToShow == 0 )
\r
734 $maxToShow = $this->blog->getMaxComments();
\r
738 if ( $this->showComments && $this->blog->commentsEnabled() )
\r
740 $comments = new Comments($this->currentItem->itemid);
\r
741 $comments->setItemActions($this);
\r
742 $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
\r
748 * ItemActions::parse_plugin()
\r
749 * Executes a plugin templatevar
\r
751 * @param string $pluginName name of plugin (without the NP_)
\r
752 * @param extra parameters can be added
\r
755 public function parse_plugin($pluginName)
\r
759 $plugin =& $manager->getPlugin("NP_{$pluginName}");
\r
766 $params = func_get_args();
\r
768 // remove plugin name
\r
769 array_shift($params);
\r
771 // add item reference (array_unshift didn't work)
\r
772 $params = array_merge(array(&$this->currentItem),$params);
\r
774 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
\r
779 * ItemActions::parse_edit()
\r
780 * Parse templatevar edit
\r
785 public function parse_edit()
\r
787 global $member, $CONF;
\r
788 if ( $this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) )
\r
790 $this->parser->parse($this->template['EDITLINK']);
\r
796 * ItemActions::parse_editlink()
\r
797 * Parse templatevar editlink
\r
799 public function parse_editlink()
\r
802 echo $CONF['AdminURL'] . 'bookmarklet.php?action=edit&itemid=' . $this->currentItem->itemid;
\r
807 * ItemActions::parse_editpopupcode()
\r
808 * Parse templatevar editpopupcode
\r
813 public function parse_editpopupcode()
\r
815 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
820 * ItemActions::highlightAndParse()
\r
821 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
\r
822 * from hacking your weblog.
\r
823 * 'plugin variables in items' implementation by Andy
\r
825 * @param array $data
\r
828 public function highlightAndParse(&$data)
\r
830 $handler = new BodyActions($this->blog);
831 $parser = new Parser($handler->getDefinedActions(), $handler);
832 $handler->setTemplate($this->template);
833 $handler->setHighlight($this->strHighlight);
834 $handler->setCurrentItem($this->currentItem);
835 $parser->parse($handler->highlight($data));
840 * ItemActions::checkCondition()
\r
841 * Checks conditions for if statements
\r
843 * @param string $field type of <%if%>
\r
844 * @param string $name property of field
\r
845 * @param string $value value of property
\r
848 protected function checkCondition($field, $name='', $value = '')
\r
850 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
\r
856 $condition = ($blog && $this->ifCategory($name,$value));
\r
858 case 'itemcategory':
\r
859 $condition = ($this->ifItemCategory($name,$value));
\r
861 case 'blogsetting':
\r
862 $condition = ($blog && ($blog->getSetting($name) == $value));
\r
864 case 'itemblogsetting':
\r
865 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
\r
866 $condition = ($b && ($b->getSetting($name) == $value));
\r
869 $condition = $member->isLoggedIn();
\r
872 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
\r
875 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
\r
878 $condition = ($this->ifAuthor($name,$value));
\r
881 $condition = $this->ifHasPlugin($name, $value);
\r
884 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
\r
891 * ItemActions::ifCategory()
\r
892 * Different checks for a category
\r
894 * @param string $key key of category
\r
895 * @param string $value value for key of category
\r
898 private function ifCategory($key = '', $value = '')
\r
900 global $blog, $catid;
\r
902 // when no parameter is defined, just check if a category is selected
\r
903 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
\r
905 return (boolean) $blog->isValidCategory($catid);
\r
908 // check category name
\r
909 if ( $key == 'catname' )
\r
911 $value = $blog->getCategoryIdFromName($value);
\r
912 if ( $value == $catid )
\r
914 return (boolean) $blog->isValidCategory($catid);
\r
918 // check category id
\r
919 if ( ($key == 'catid') && ($value == $catid) )
\r
921 return (boolean) $blog->isValidCategory($catid);
\r
927 * ItemActions::ifAuthor()
\r
928 * Different checks for an author
\r
930 * @param string $key key of data for author
\r
931 * @param string $value value of data for author
\r
932 * @return boolean correct or not
\r
934 private function ifAuthor($key = '', $value = '')
\r
936 global $member, $manager;
\r
938 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
\r
940 // when no parameter is defined, just check if author is current visitor
\r
941 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
\r
943 return (boolean) ((integer) $member->getID() > 0 && (integer) $member->getID() == (integer) $this->currentItem->authorid);
\r
946 // check author name
\r
947 if ( $key == 'name' )
\r
949 $value = strtolower($value);
\r
950 if ( $value == strtolower($this->currentItem->author) )
\r
956 // check if author is admin
\r
957 if ( ($key == 'isadmin') )
\r
959 $aid = intval($this->currentItem->authorid);
\r
960 $blogid = intval($b->getID());
\r
961 $amember =& $manager->getMember($aid);
\r
962 if ( $amember->isAdmin() )
\r
966 return (boolean) $amember->isBlogAdmin($blogid);
\r
973 * ItemActions::ifItemCategory()
\r
974 * Different checks for a category
\r
976 * @param string $key key of data for category to which item belongs
\r
977 * @param string $value value of data for category to which item belongs
\r
978 * @return boolean correct or not
\r
980 private function ifItemCategory($key = '', $value='')
\r
982 global $catid, $manager;
\r
984 $b =& $manager->getBlog(getBlogIDFromItemID($this->currentItem->itemid));
\r
986 // when no parameter is defined, just check if a category is selected
\r
987 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
\r
989 return (boolean) $b->isValidCategory($catid);
\r
992 $icatid = $this->currentItem->catid;
\r
994 // check category name
\r
995 if ( $key == 'catname' )
\r
997 $value = $b->getCategoryIdFromName($value);
\r
998 if ( $value == $icatid )
\r
1000 return (boolean) $b->isValidCategory($icatid);
\r
1004 // check category id
\r
1005 if ( ($key == 'catid') && ($value == $icatid) )
\r
1007 return (boolean) $b->isValidCategory($icatid);
\r
1014 * ItemActions::ifOnTeam()
\r
1015 * Checks if a member is on the team of a blog and return his rights
\r
1017 * @param string $blogName name of weblog
\r
1018 * @return boolean correct or not
\r
1020 private function ifOnTeam($blogName = '')
\r
1022 global $blog, $member, $manager;
\r
1024 // when no blog found
\r
1025 if ( ($blogName == '') && (!is_object($blog)) )
\r
1030 // explicit blog selection
\r
1031 if ( $blogName != '' )
\r
1033 $blogid = getBlogIDFromName($blogName);
\r
1036 // use current blog
\r
1037 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
\r
1039 $blogid = $blog->getID();
\r
1041 return (boolean) $member->teamRights($blogid);
\r
1045 * ItemActions::ifAdmin()
\r
1046 * Checks if a member is admin of a blog
\r
1048 * @param string $blogName name of weblog
\r
1049 * @return boolean correct or not
\r
1051 private function ifAdmin($blogName = '')
\r
1053 global $blog, $member, $manager;
\r
1055 // when no blog found
\r
1056 if ( ($blogName == '') && (!is_object($blog)) )
\r
1061 // explicit blog selection
\r
1062 if ( $blogName != '' )
\r
1064 $blogid = getBlogIDFromName($blogName);
\r
1067 // use current blog
\r
1068 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
\r
1070 $blogid = $blog->getID();
\r
1072 return (boolean) $member->isBlogAdmin($blogid);
\r
1077 * ItemActions::ifHasPlugin()
\r
1078 * hasplugin,PlugName
\r
1079 * -> checks if plugin exists
\r
1080 * hasplugin,PlugName,OptionName
\r
1081 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
\r
1082 * hasplugin,PlugName,OptionName=value
\r
1083 * -> checks if the option OptionName from plugin PlugName is set to value
\r
1085 * @param string $name name of plugin
\r
1086 * @param string $value key (and value) of plugin option
\r
1087 * @return boolean correct or not
\r
1089 private function ifHasPlugin($name, $value)
\r
1092 $condition = FALSE;
\r
1093 // (pluginInstalled method won't write a message in the actionlog on failure)
\r
1094 if ( $manager->pluginInstalled("NP_{$name}"))
\r
1096 $plugin =& $manager->getPlugin('NP_' . $name);
\r
1097 if ( $plugin != NULL )
\r
1099 if ( $value == "" )
\r
1101 $condition = TRUE;
\r
1105 list($name2, $value2) = preg_split('#=#', $value, 2);
\r
1106 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
\r
1108 $condition = TRUE;
\r
1110 else if ( $plugin->getOption($name2) == $value2 )
\r
1112 $condition = TRUE;
\r
1117 return (boolean) $condition;
\r
1121 * ItemActions::ifPlugin()
\r
1122 * Checks if a plugin exists and call its doIf function
\r
1124 * @param string $name name of plugin
\r
1125 * @param string $key key of plugin option
\r
1126 * @param string $value value of plugin option
\r
1127 * @return boolean callback output from plugin
\r
1129 private function ifPlugin($name, $key = '', $value = '')
\r
1133 $plugin =& $manager->getPlugin("NP_{$name}");
\r
1138 $params = func_get_args();
\r
1139 array_shift($params);
\r
1141 return (boolean) call_user_func_array(array(&$plugin, 'doIf'), $params);
\r