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',
\r
101 /* actions defined in BodyAction class */
\r
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
116 // call constructor of superclass first
\r
117 parent::__construct();
119 // extra parameters for created links
\r
122 $this->linkparams = array('catid' => $catid);
\r
125 // check if member is blog admin (and thus allowed to edit all items)
\r
126 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
\r
127 $this->setBlog($blog);
\r
132 * ItemActions::getAvailableActions()
\r
133 * Returns an array with the actions that are defined
\r
134 * in the ItemActions class
\r
139 public function getAvailableActions()
\r
141 return array_merge(self::$defined_actions, parent::getAvailableActions());
\r
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::setCurrentItem()
\r
159 * @param object $item instance of Item class
\r
162 public function setCurrentItem(&$item)
\r
164 global $currentitemid;
\r
165 $this->currentItem =& $item;
\r
166 $currentitemid = $this->currentItem['itemid'];
\r
171 * ItemActions::setBlog()
\r
173 * @param object &$blog instance of Blog class
\r
176 public function setBlog(&$blog)
\r
178 $this->blog =& $blog;
\r
183 * ItemActions::setTemplate()
\r
185 * @param array $template array including templates
\r
188 public function setTemplate($template)
\r
190 $this->template =& $template;
\r
195 * ItemActions::setShowComments()
\r
197 * @param boolean $val need to be displayed or not
\r
200 public function setShowComments($val)
\r
202 $this->showComments = (boolean) $val;
\r
207 * ItemActions::parse_blogid()
\r
208 * Parse templatevar blogid
\r
213 public function parse_blogid()
\r
215 echo $this->blog->getID();
\r
219 * ItemActions::parse_body()
\r
220 * Parse templatevar body
\r
225 public function parse_body()
\r
227 $this->highlightAndParse($this->currentItem['body']);
\r
232 * ItemActions::parse_more()
\r
233 * Parse templatevar more
\r
238 public function parse_more()
\r
240 $this->highlightAndParse($this->currentItem['more']);
\r
245 * ItemActions::parse_itemid()
\r
246 * Parse templatevar itemid
\r
251 public function parse_itemid()
\r
253 echo $this->currentItem['itemid'];
\r
258 * ItemActions::parse_category()
\r
259 * Parse templatevar category
\r
264 public function parse_category()
\r
266 echo $this->currentItem['category'];
\r
271 * ItemActions::parse_categorylink()
\r
272 * Parse templatevar categorylink
\r
277 public function parse_categorylink()
\r
279 echo Link::create_link('category', array('catid' => $this->currentItem['catid'], 'name' => $this->currentItem['category']));
\r
284 * ItemActions::parse_catid()
\r
285 * Parse templatevar catid
\r
290 public function parse_catid()
\r
292 echo $this->currentItem['catid'];
\r
297 * ItemActions::parse_authorid()
\r
298 * Parse templatevar authorid
\r
303 public function parse_authorid()
\r
305 echo $this->currentItem['authorid'];
\r
310 * ItemActions::parse_authorlink()
\r
311 * Parse templatevar authorlink
\r
316 public function parse_authorlink()
\r
319 'memberid' => $this->currentItem['authorid'],
\r
320 'name' => $this->currentItem['author'],
\r
321 'extra' => $this->linkparams
\r
324 echo Link::create_link('member', $data);
\r
329 * ItemActions::parse_query()
\r
330 * Parse templatevar query
\r
335 public function parse_query()
\r
337 echo $this->strHighlight;
\r
342 * ItemActions::parse_itemlink()
\r
343 * Parse templatevar itemlink
\r
348 public function parse_itemlink()
\r
351 'itemid' => $this->currentItem['itemid'],
\r
352 'title' => $this->currentItem['title'],
\r
353 'timestamp' => $this->currentItem['timestamp'],
\r
354 'extra' => $this->linkparams
\r
357 echo Link::create_link('item', $data);
\r
362 * ItemActions::parse_blogurl()
\r
363 * Parse templatevar blogurl
\r
368 public function parse_blogurl()
\r
370 echo $this->blog->getURL();
\r
375 * ItemActions::parse_closed()
\r
376 * Parse templatevar closed
\r
381 public function parse_closed()
\r
383 echo $this->currentItem['closed'];
\r
388 * ItemActions::parse_relevance()
\r
389 * Parse templatevar relevance
\r
394 public function parse_relevance()
\r
396 echo round($this->currentItem['score'], 2);
\r
401 * ItemActions::parse_title()
\r
402 * Parse templatevar title
\r
404 * @param string $format defines in which format the title is shown
\r
407 public function parse_title($format = '')
\r
409 if ( is_array($this->currentItem) )
\r
411 $itemtitle = $this->currentItem['title'];
\r
416 echo Entity::hen($itemtitle);
\r
419 echo Entity::hsc($itemtitle);
\r
425 $this->highlightAndParse($itemtitle);
\r
432 * ItemActions::parse_karma()
\r
433 * Parse templatevar karma
\r
435 * @param string $type type of data for karma
\r
438 public function parse_karma($type = 'totalscore')
\r
442 // get karma object
\r
443 $karma =& $manager->getKarma($this->currentItem['itemid']);
\r
448 echo $karma->getNbPosVotes();
\r
451 echo $karma->getNbNegVotes();
\r
454 echo $karma->getNbOfVotes();
\r
457 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
\r
458 echo number_format($percentage,2), '%';
\r
461 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
\r
462 echo number_format($percentage,2), '%';
\r
466 echo $karma->getTotalScore();
\r
473 * ItemActions::parse_author()
\r
474 * Parse templatevar author
\r
476 * @param string $which key of data for author
\r
479 public function parse_author($which = '')
\r
484 echo $this->currentItem['authorname'];
\r
487 echo $this->currentItem['authorid'];
\r
490 echo $this->currentItem['authormail'];
\r
493 echo $this->currentItem['authorurl'];
\r
497 echo $this->currentItem['author'];
\r
503 * ItemActions::parse_smartbody()
\r
504 * Parse templatevar smartbody
\r
509 public function parse_smartbody()
\r
511 if ( !$this->currentItem['more'] )
\r
513 $this->highlightAndParse($this->currentItem['body']);
\r
517 $this->highlightAndParse($this->currentItem['more']);
\r
523 * ItemActions::parse_morelink()
\r
524 * Parse templatevar morelink
\r
526 public function parse_morelink()
\r
528 if ( $this->currentItem['more'] )
\r
530 $this->parser->parse($this->template['MORELINK']);
\r
536 * ItemActions::parse_date()
\r
537 * Parse templatevar date
\r
539 * @param string $format format optional strftime format
\r
542 public function parse_date($format = '')
\r
544 if ( $format !== '' )
\r
549 else if ( !array_key_exists('FORMAT_DATE', $this->template) || $this->template['FORMAT_DATE'] === '' )
\r
551 /* depends on the PHP's current locale */
\r
556 $format = $this->template['FORMAT_DATE'];
\r
562 $offset = $this->blog->getTimeOffset() * 3600;
\r
565 echo i18n::formatted_datetime($format, $this->currentItem['timestamp'], $offset);
\r
570 * ItemActions::parse_time()
\r
571 * Parse templatevar time
\r
573 * @param string $format format optional strftime format
\r
576 public function parse_time($format = '')
\r
578 if ( $format !== '' )
\r
583 else if ( !array_key_exists('FORMAT_TIME', $this->template) || $this->template['FORMAT_TIME'] === '' )
\r
585 /* depends on the PHP's current locale */
\r
590 $format = $this->template['FORMAT_TIME'];
\r
592 echo i18n::formatted_datetime($format, $this->currentItem['timestamp']);
\r
597 * ItemActions::parse_syndicate_title()
\r
598 * Parse templatevar syndicate_title
\r
600 * @param string $maxLength maxLength optional maximum length
\r
601 * @return string syndicated title
\r
603 public function parse_syndicate_title($maxLength = 100) {
\r
604 $syndicated = strip_tags($this->currentItem['title']);
\r
605 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
\r
609 * ItemActions::parse_syndicate_description()
\r
610 * Parse templatevar syndicate_description
\r
612 * @param stromg $maxLength maxlength optional maximum length
\r
613 * @param string $addHighlight highlighted string
\r
616 public function parse_syndicate_description($maxLength = 250, $addHighlight = 0)
\r
618 $syndicated = strip_tags($this->currentItem['body']);
\r
619 if ( $addHighlight )
\r
621 $tmp_highlight = Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
\r
622 echo $this->highlightAndParse($tmp_highlight);
\r
626 echo Entity::hsc(Entity::shorten($syndicated,$maxLength,'...'));
\r
632 * ItemActions::parse_karmaposlink()
\r
633 * Parse templatevar karmaposlink
\r
635 * @param string $text text element for anchor element
\r
638 public function parse_karmaposlink($text = '')
\r
641 $link = $CONF['ActionURL'] . '?action=votepositive&itemid=' . $this->currentItem['itemid'];
\r
644 echo '<a href="'.$link.'">' . $text . '</a>';
\r
655 * ItemActions::parse_karmaneglink()
\r
656 * Parse templatevar karmaneglink
\r
658 * @param string $text text element for anchor element
\r
661 public function parse_karmaneglink($text = '')
\r
664 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem['itemid'];
\r
668 echo '<a href="'.$link.'">' . $text . '</a>';
\r
679 * ItemActions::parse_new()
\r
680 * Parse templatevar new
\r
685 public function parse_new()
\r
687 if ( ($this->lastVisit != 0) && ($this->currentItem['timestamp'] > $this->lastVisit) )
\r
689 echo $this->template['NEW'];
\r
695 * ItemActions::parse_daylink()
\r
696 * Parse templatevar daylink
\r
701 public function parse_daylink()
\r
703 echo Link::create_archive_link($this->blog->getID(), i18n::formatted_datetime('%Y-%m-%d', $this->currentItem['timestamp']), $this->linkparams);
\r
708 * ItemActions::parse_comments(
\r
709 * Parse templatevar comments
\r
711 * @param integer $maxToShow maximum number of comments in a display
\r
714 public function parse_comments($maxToShow = 0)
\r
716 if ( $maxToShow == 0 )
\r
718 $maxToShow = $this->blog->getMaxComments();
\r
722 if ( $this->showComments && $this->blog->commentsEnabled() )
\r
724 $comments = new Comments($this->currentItem['itemid']);
\r
725 $comments->setItemActions($this);
\r
726 $comments->showComments($this->template, $maxToShow, $this->currentItem['closed'] ? 0 : 1, $this->strHighlight);
\r
732 * ItemActions::parse_plugin()
\r
733 * Executes a plugin templatevar
\r
735 * @param string $pluginName name of plugin (without the NP_)
\r
736 * @param extra parameters can be added
\r
739 public function parse_plugin($pluginName)
\r
743 $plugin =& $manager->getPlugin("NP_{$pluginName}");
\r
750 $params = func_get_args();
\r
752 // remove plugin name
\r
753 array_shift($params);
\r
755 // add item reference (array_unshift didn't work)
\r
756 $params = array_merge(array(&$this->currentItem),$params);
\r
758 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
\r
763 * ItemActions::parse_edit()
\r
764 * Parse templatevar edit
\r
769 public function parse_edit()
\r
771 global $member, $CONF;
\r
772 if ( $this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem['authorid'])) )
\r
774 $this->parser->parse($this->template['EDITLINK']);
\r
780 * ItemActions::parse_editlink()
\r
781 * Parse templatevar editlink
\r
783 public function parse_editlink()
\r
786 echo $CONF['AdminURL'] . 'bookmarklet.php?action=edit&itemid=' . $this->currentItem['itemid'];
\r
791 * ItemActions::parse_editpopupcode()
\r
792 * Parse templatevar editpopupcode
\r
797 public function parse_editpopupcode()
\r
799 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
804 * ItemActions::highlightAndParse()
\r
805 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
\r
806 * from hacking your weblog.
\r
807 * 'plugin variables in items' implementation by Andy
\r
809 * @param array $data
\r
812 public function highlightAndParse(&$data)
\r
814 $handler = new BodyActions($this->blog);
815 $handler->setTemplate($this->template);
816 $handler->setHighlight($this->strHighlight);
817 $handler->setCurrentItem($this->currentItem);
819 $parser = new Parser($handler);
\r
820 $parser->parse($handler->highlight($data));
825 * ItemActions::checkCondition()
\r
826 * Checks conditions for if statements
\r
828 * @param string $field type of <%if%>
\r
829 * @param string $name property of field
\r
830 * @param string $value value of property
\r
833 protected function checkCondition($field, $name='', $value = '')
\r
835 global $catid, $blog, $member, $itemidnext, $itemidprev, $manager, $archiveprevexists, $archivenextexists;
\r
841 $condition = ($blog && $this->ifCategory($name,$value));
\r
843 case 'itemcategory':
\r
844 $condition = ($this->ifItemCategory($name,$value));
\r
846 case 'blogsetting':
\r
847 $condition = ($blog && ($blog->getSetting($name) == $value));
\r
849 case 'itemblogsetting':
\r
850 $item =& $manager->getItem($this->currentItem['itemid'], 1, 1);
\r
851 $t_blog =& $manager->getBlog($item['blogid']);
\r
852 $condition = ($t_blog && ($t_blog->getSetting($name) == $value));
\r
855 $condition = $member->isLoggedIn();
\r
858 $condition = $member->isLoggedIn() && $this->ifOnTeam($name);
\r
861 $condition = $member->isLoggedIn() && $this->ifAdmin($name);
\r
864 $condition = ($this->ifAuthor($name,$value));
\r
867 $condition = $this->ifHasPlugin($name, $value);
\r
870 $condition = $manager->pluginInstalled('NP_' . $field) && $this->ifPlugin($field, $name, $value);
\r
877 * ItemActions::ifCategory()
\r
878 * Different checks for a category
\r
880 * @param string $key key of category
\r
881 * @param string $value value for key of category
\r
884 private function ifCategory($key = '', $value = '')
\r
886 global $blog, $catid;
\r
888 // when no parameter is defined, just check if a category is selected
\r
889 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
\r
891 return (boolean) $blog->isValidCategory($catid);
\r
894 // check category name
\r
895 if ( $key == 'catname' )
\r
897 $value = $blog->getCategoryIdFromName($value);
\r
898 if ( $value == $catid )
\r
900 return (boolean) $blog->isValidCategory($catid);
\r
904 // check category id
\r
905 if ( ($key == 'catid') && ($value == $catid) )
\r
907 return (boolean) $blog->isValidCategory($catid);
\r
913 * ItemActions::ifAuthor()
\r
914 * Different checks for an author
\r
916 * @param string $key key of data for author
\r
917 * @param string $value value of data for author
\r
918 * @return boolean correct or not
\r
920 private function ifAuthor($key = '', $value = '')
\r
922 global $member, $manager;
\r
924 // when no parameter is defined, just check if author is current visitor
\r
925 if ( ($key != 'isadmin' && $key != 'name') || ($key == 'name' && $value == '') )
\r
927 return (boolean) ((integer) $member->getID() > 0 && (integer) $member->getID() == (integer) $this->currentItem['authorid']);
\r
930 // check author name
\r
931 if ( $key == 'name' )
\r
933 $value = strtolower($value);
\r
934 if ( $value == strtolower($this->currentItem['author']) )
\r
940 // check if author is admin
\r
941 if ( ($key == 'isadmin') )
\r
943 $i_author =& $manager->getMember($this->currentItem['authorid']);
\r
944 if ( $i_author->isAdmin() )
\r
948 return (boolean) $i_author->isBlogAdmin($this->currentItem['blogid']);
\r
955 * ItemActions::ifItemCategory()
\r
956 * Different checks for a category
\r
958 * @param string $key key of data for category to which item belongs
\r
959 * @param string $value value of data for category to which item belongs
\r
960 * @return boolean correct or not
\r
962 private function ifItemCategory($key = '', $value='')
\r
964 global $catid, $manager;
\r
966 $blog =& $manager->getBlog($this->currentItem['blogid']);
\r
968 // when no parameter is defined, just check if a category is selected
\r
969 if ( ($key != 'catname' && $key != 'catid') || ($value == '') )
\r
971 return (boolean) $blog->isValidCategory($catid);
\r
974 $icatid = $this->currentItem['catid'];
\r
976 // check category name
\r
977 if ( $key == 'catname' )
\r
979 $value = $blog->getCategoryIdFromName($value);
\r
980 if ( $value == $icatid )
\r
982 return (boolean) $blog->isValidCategory($icatid);
\r
986 // check category id
\r
987 if ( ($key == 'catid') && ($value == $icatid) )
\r
989 return (boolean) $blog->isValidCategory($icatid);
\r
996 * ItemActions::ifOnTeam()
\r
997 * Checks if a member is on the team of a blog and return his rights
\r
999 * @param string $blogName name of weblog
\r
1000 * @return boolean correct or not
\r
1002 private function ifOnTeam($blogName = '')
\r
1004 global $blog, $member, $manager;
\r
1006 // when no blog found
\r
1007 if ( ($blogName == '') && (!is_object($blog)) )
\r
1012 // explicit blog selection
\r
1013 if ( $blogName != '' )
\r
1015 $blogid = getBlogIDFromName($blogName);
\r
1018 // use current blog
\r
1019 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
\r
1021 $blogid = $blog->getID();
\r
1023 return (boolean) $member->teamRights($blogid);
\r
1027 * ItemActions::ifAdmin()
\r
1028 * Checks if a member is admin of a blog
\r
1030 * @param string $blogName name of weblog
\r
1031 * @return boolean correct or not
\r
1033 private function ifAdmin($blogName = '')
\r
1035 global $blog, $member, $manager;
\r
1037 // when no blog found
\r
1038 if ( ($blogName == '') && (!is_object($blog)) )
\r
1043 // explicit blog selection
\r
1044 if ( $blogName != '' )
\r
1046 $blogid = getBlogIDFromName($blogName);
\r
1049 // use current blog
\r
1050 if ( ($blogName == '') || !$manager->existsBlogID($blogid) )
\r
1052 $blogid = $blog->getID();
\r
1054 return (boolean) $member->isBlogAdmin($blogid);
\r
1059 * ItemActions::ifHasPlugin()
\r
1060 * hasplugin,PlugName
\r
1061 * -> checks if plugin exists
\r
1062 * hasplugin,PlugName,OptionName
\r
1063 * -> checks if the option OptionName from plugin PlugName is not set to 'no'
\r
1064 * hasplugin,PlugName,OptionName=value
\r
1065 * -> checks if the option OptionName from plugin PlugName is set to value
\r
1067 * @param string $name name of plugin
\r
1068 * @param string $value key (and value) of plugin option
\r
1069 * @return boolean correct or not
\r
1071 private function ifHasPlugin($name, $value)
\r
1074 $condition = FALSE;
\r
1075 // (pluginInstalled method won't write a message in the actionlog on failure)
\r
1076 if ( $manager->pluginInstalled("NP_{$name}"))
\r
1078 $plugin =& $manager->getPlugin('NP_' . $name);
\r
1079 if ( $plugin != NULL )
\r
1081 if ( $value == "" )
\r
1083 $condition = TRUE;
\r
1087 list($name2, $value2) = preg_split('#=#', $value, 2);
\r
1088 if ( $value2 == "" && $plugin->getOption($name2) != 'no' )
\r
1090 $condition = TRUE;
\r
1092 else if ( $plugin->getOption($name2) == $value2 )
\r
1094 $condition = TRUE;
\r
1099 return (boolean) $condition;
\r
1103 * ItemActions::ifPlugin()
\r
1104 * Checks if a plugin exists and call its doIf function
\r
1106 * @param string $name name of plugin
\r
1107 * @param string $key key of plugin option
\r
1108 * @param string $value value of plugin option
\r
1109 * @return boolean callback output from plugin
\r
1111 private function ifPlugin($name, $key = '', $value = '')
\r
1115 $plugin =& $manager->getPlugin("NP_{$name}");
\r
1120 $params = func_get_args();
\r
1121 array_shift($params);
\r
1123 return (boolean) call_user_func_array(array(&$plugin, 'doIf'), $params);
\r