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 $NucleusJP: ITEMACTIONS.php,v 1.5.2.3 2007/10/30 19:05:20 kmorimatsu Exp $
20 class ITEMACTIONS extends BaseActions {
\r
22 // contains an assoc array with parameters that need to be included when
\r
23 // generating links to items/archives/... (e.g. catid)
\r
26 // true when the current user is a blog admin (and thus allowed to edit all items)
\r
29 // timestamp of last visit
\r
32 // item currently being handled (mysql result object, see BLOG::showUsingQuery)
\r
35 // reference to the blog currently being displayed
\r
38 // associative array with template info (part name => contents)
\r
41 // true when comments need to be displayed
\r
44 function ITEMACTIONS(&$blog) {
\r
45 // call constructor of superclass first
\r
46 $this->BaseActions();
\r
48 // extra parameters for created links
\r
51 $this->linkparams = array('catid' => $catid);
\r
53 // check if member is blog admin (and thus allowed to edit all items)
\r
55 $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
\r
56 $this->setBlog($blog);
\r
60 * Returns an array with the actions that are defined
\r
61 * in the ITEMACTIONS class
\r
63 function getDefinedActions() {
\r
86 'syndicate_description',
\r
115 function setLastVisit($lastVisit) {
\r
116 $this->lastVisit = $lastVisit;
\r
119 function setParser(&$parser) {
\r
120 $this->parser =& $parser;
\r
123 function setCurrentItem(&$item) {
\r
124 $this->currentItem =& $item;
\r
127 function setBlog(&$blog) {
\r
128 $this->blog =& $blog;
\r
131 function setTemplate($template) {
\r
132 $this->template =& $template;
\r
135 function setShowComments($val) {
\r
136 $this->showComments = $val;
\r
139 // methods used by parser to insert content
\r
143 * Parse templatevar blogid
\r
145 function parse_blogid() {
\r
146 echo $this->blog->getID();
\r
150 * Parse templatevar body
\r
152 function parse_body() {
\r
153 $this->highlightAndParse($this->currentItem->body);
\r
157 * Parse templatevar more
\r
159 function parse_more() {
\r
160 $this->highlightAndParse($this->currentItem->more);
\r
164 * Parse templatevar itemid
\r
166 function parse_itemid() {
\r
167 echo $this->currentItem->itemid;
\r
171 * Parse templatevar category
\r
173 function parse_category() {
\r
174 echo $this->currentItem->category;
\r
178 * Parse templatevar categorylink
\r
180 function parse_categorylink() {
\r
181 echo createLink('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
\r
185 * Parse templatevar catid
\r
187 function parse_catid() {
\r
188 echo $this->currentItem->catid;
\r
192 * Parse templatevar authorid
\r
194 function parse_authorid() {
\r
195 echo $this->currentItem->authorid;
\r
199 * Parse templatevar authorlink
\r
201 function parse_authorlink() {
\r
205 'memberid' => $this->currentItem->authorid,
\r
206 'name' => $this->currentItem->author,
\r
207 'extra' => $this->linkparams
\r
213 * Parse templatevar query
\r
215 function parse_query() {
\r
216 echo $this->strHighlight;
\r
220 * Parse templatevar itemlink
\r
222 function parse_itemlink() {
\r
226 'itemid' => $this->currentItem->itemid,
\r
227 'title' => $this->currentItem->title,
\r
228 'timestamp' => $this->currentItem->timestamp,
\r
229 'extra' => $this->linkparams
\r
235 * Parse templatevar blogurl
\r
237 function parse_blogurl() {
\r
238 echo $this->blog->getURL();
\r
242 * Parse templatevar closed
\r
244 function parse_closed() {
\r
245 echo $this->currentItem->closed;
\r
249 * Parse templatevar relevance
\r
251 function parse_relevance() {
\r
252 echo round($this->currentItem->score,2);
\r
256 * Parse templatevar title
\r
258 * @param string $format defines in which format the title is shown
\r
260 function parse_title($format = '') {
\r
261 if (is_array($this->currentItem)) {
\r
262 $itemtitle = $this->currentItem['title'];
\r
263 } elseif (is_object($this->currentItem)) {
\r
264 $itemtitle = $this->currentItem->title;
\r
268 // echo stringToXML ($this->currentItem->title);
\r
269 echo stringToXML ($itemtitle);
\r
272 // echo stringToAttribute ($this->currentItem->title);
\r
273 echo stringToAttribute ($itemtitle);
\r
276 // echo $this->currentItem->title;
\r
280 // $this->highlightAndParse($this->currentItem->title);
\r
281 $this->highlightAndParse($itemtitle);
\r
287 * Parse templatevar karma
\r
289 function parse_karma($type = 'totalscore') {
\r
292 // get karma object
\r
293 $karma =& $manager->getKarma($this->currentItem->itemid);
\r
297 echo $karma->getNbPosVotes();
\r
300 echo $karma->getNbNegVotes();
\r
303 echo $karma->getNbOfVotes();
\r
306 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
\r
307 echo number_format($percentage,2), '%';
\r
310 $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
\r
311 echo number_format($percentage,2), '%';
\r
315 echo $karma->getTotalScore();
\r
322 * Parse templatevar author
\r
324 function parse_author($which = '') {
\r
328 echo $this->currentItem->authorname;
\r
331 echo $this->currentItem->authorid;
\r
334 echo $this->currentItem->authormail;
\r
337 echo $this->currentItem->authorurl;
\r
341 echo $this->currentItem->author;
\r
346 * Parse templatevar smartbody
\r
348 function parse_smartbody() {
\r
349 if (!$this->currentItem->more) {
\r
350 $this->highlightAndParse($this->currentItem->body);
\r
352 $this->highlightAndParse($this->currentItem->more);
\r
357 * Parse templatevar morelink
\r
359 function parse_morelink() {
\r
360 if ($this->currentItem->more)
\r
361 $this->parser->parse($this->template['MORELINK']);
\r
365 * Parse templatevar date
\r
367 * @param format optional strftime format
\r
369 function parse_date($format = '') {
\r
370 if (!isset($this->template['FORMAT_DATE'])) $this->template['FORMAT_DATE'] = '';
371 echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $this->blog);
\r
375 * Parse templatevar time
\r
377 * @param format optional strftime format
\r
379 function parse_time($format = '') {
\r
380 if (!isset($this->template['FORMAT_TIME'])) $this->template['FORMAT_TIME'] = '';
381 echo strftime($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);
\r
385 * Parse templatevar syndicate_title
\r
387 * @param maxLength optional maximum length
\r
389 function parse_syndicate_title($maxLength = 100) {
\r
390 $syndicated = strip_tags($this->currentItem->title);
\r
391 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
\r
395 * Parse templatevar syndicate_description
\r
397 * @param maxLength optional maximum length
\r
399 function parse_syndicate_description($maxLength = 250, $addHighlight = 0) {
\r
400 $syndicated = strip_tags($this->currentItem->body);
\r
401 if ($addHighlight) {
\r
402 $tmp_highlight = htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
\r
403 echo $this->highlightAndParse($tmp_highlight);
\r
405 echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
\r
410 * Parse templatevar karmaposlink
\r
412 * @param string text
\r
414 function parse_karmaposlink($text = '') {
\r
416 $link = $CONF['ActionURL'] . '?action=votepositive&itemid='.$this->currentItem->itemid;
\r
417 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
\r
421 * Parse templatevar karmaneglink
\r
423 * @param string text
\r
425 function parse_karmaneglink($text = '') {
\r
427 $link = $CONF['ActionURL'] . '?action=votenegative&itemid='.$this->currentItem->itemid;
\r
428 echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
\r
432 * Parse templatevar new
\r
434 function parse_new() {
\r
435 if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
\r
436 echo $this->template['NEW'];
\r
440 * Parse templatevar daylink
\r
442 function parse_daylink() {
\r
443 echo createArchiveLink($this->blog->getID(), strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
\r
447 * Parse templatevar comments
\r
449 function parse_comments($maxToShow = 0) {
\r
450 if ($maxToShow == 0)
\r
451 $maxToShow = $this->blog->getMaxComments();
\r
454 if ($this->showComments && $this->blog->commentsEnabled()) {
\r
455 $comments =& new COMMENTS($this->currentItem->itemid);
\r
456 $comments->setItemActions($this);
\r
457 $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
\r
462 * Executes a plugin templatevar
\r
464 * @param pluginName name of plugin (without the NP_)
\r
466 * extra parameters can be added
\r
468 function parse_plugin($pluginName) {
\r
471 // should be already tested from the parser (PARSER.php)
\r
472 // only continue when the plugin is really installed
\r
473 /*if (!$manager->pluginInstalled('NP_' . $pluginName))
\r
476 $plugin =& $manager->getPlugin('NP_' . $pluginName);
\r
477 if (!$plugin) return;
\r
480 $params = func_get_args();
\r
482 // remove plugin name
\r
483 array_shift($params);
\r
485 // add item reference (array_unshift didn't work)
\r
486 $params = array_merge(array(&$this->currentItem),$params);
\r
488 call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
\r
492 * Parse templatevar edit
\r
494 function parse_edit() {
\r
495 global $member, $CONF;
\r
496 if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {
\r
497 $this->parser->parse($this->template['EDITLINK']);
\r
502 * Parse templatevar editlink
\r
504 function parse_editlink() {
\r
506 echo $CONF['AdminURL'],'bookmarklet.php?action=edit&itemid=',$this->currentItem->itemid;
\r
510 * Parse templatevar editpopupcode
\r
512 function parse_editpopupcode() {
\r
513 echo "if (event && event.preventDefault) event.preventDefault();winbm=window.open(this.href,'nucleusbm','scrollbars=no,width=600,height=550,left=10,top=10,status=no,resizable=yes');winbm.focus();return false;";
\r
516 // helper functions
\r
519 * Parses highlighted text, with limited actions only (to prevent not fully trusted team members
\r
520 * from hacking your weblog.
\r
521 * 'plugin variables in items' implementation by Andy
\r
523 function highlightAndParse(&$data) {
\r
524 $actions =& new BODYACTIONS($this->blog);
\r
525 $parser =& new PARSER($actions->getDefinedActions(), $actions);
\r
526 $actions->setTemplate($this->template);
\r
527 $actions->setHighlight($this->strHighlight);
\r
528 $actions->setCurrentItem($this->currentItem);
\r
529 //$actions->setParser($parser);
\r
530 $parser->parse($actions->highlight($data));
\r
534 // this is the function previous to the 'plugin variables in items' implementation by Andy
\r
535 function highlightAndParse(&$data) {
\r
536 // allow only a limited subset of actions (do not allow includes etc, they might be evil)
\r
537 $this->parser->actions = array('image','media','popup');
\r
538 $tmp_highlight = $this->highlight($data);
\r
539 $this->parser->parse($tmp_highlight);
\r
540 $this->parser->actions = $this->getDefinedActions();
\r