[ Index ]

PHP Cross Reference of Nucleus CMS v3.51 code documentation

title

Body

[close]

/nucleus/libs/ -> ITEMACTIONS.php (source)

   1  <?php
   2  /*

   3   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)

   4   * Copyright (C) 2002-2009 The Nucleus Group

   5   *

   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)

  11   */
  12  /**

  13   * This class is used to parse item templates

  14   *

  15   * @license http://nucleuscms.org/license.txt GNU General Public License

  16   * @copyright Copyright (C) 2002-2009 The Nucleus Group

  17   * @version $Id: ITEMACTIONS.php 1388 2009-07-18 06:31:28Z shizuki $

  18   */
  19  class ITEMACTIONS extends BaseActions {
  20  
  21      // contains an assoc array with parameters that need to be included when

  22      // generating links to items/archives/... (e.g. catid)

  23      var $linkparams;
  24  
  25      // true when the current user is a blog admin (and thus allowed to edit all items)

  26      var $allowEditAll;
  27  
  28      // timestamp of last visit

  29      var $lastVisit;
  30  
  31      // item currently being handled (mysql result object, see BLOG::showUsingQuery)

  32      var $currentItem;
  33  
  34      // reference to the blog currently being displayed

  35      var $blog;
  36  
  37      // associative array with template info (part name => contents)

  38      var $template;
  39  
  40      // true when comments need to be displayed

  41      var $showComments;
  42  
  43  	function ITEMACTIONS(&$blog) {
  44          // call constructor of superclass first

  45          $this->BaseActions();
  46  
  47          // extra parameters for created links

  48          global $catid;
  49          if ($catid)
  50              $this->linkparams = array('catid' => $catid);
  51  
  52          // check if member is blog admin (and thus allowed to edit all items)

  53          global $member;
  54          $this->allowEditAll = ($member->isLoggedIn() && $member->blogAdminRights($blog->getID()));
  55          $this->setBlog($blog);
  56      }
  57  
  58      /**

  59        * Returns an array with the actions that are defined

  60        * in the ITEMACTIONS class

  61        */
  62  	function getDefinedActions() {
  63          return array(
  64              'blogid',
  65              'title',
  66              'body',
  67              'more',
  68              'smartbody',
  69              'itemid',
  70              'morelink',
  71              'category',
  72              'categorylink',
  73              'author',
  74              'authorid',
  75              'authorlink',
  76              'catid',
  77              'karma',
  78              'date',
  79              'time',
  80              'query',
  81              'itemlink',
  82              'blogurl',
  83              'closed',
  84              'syndicate_title',
  85              'syndicate_description',
  86              'karmaposlink',
  87              'karmaneglink',
  88              'new',
  89              'image',
  90              'popup',
  91              'media',
  92              'daylink',
  93              'query',
  94              'include',
  95              'phpinclude',
  96              'parsedinclude',
  97              'skinfile',
  98              'set',
  99              'plugin',
 100              'edit',
 101              'editlink',
 102              'editpopupcode',
 103              'comments',
 104              'relevance'/*,

 105              'if',

 106              'else',

 107              'endif',

 108              'elseif',

 109              'ifnot',

 110              'elseifnot'*/
 111          );
 112      }
 113  
 114  	function setLastVisit($lastVisit) {
 115          $this->lastVisit = $lastVisit;
 116      }
 117  
 118  	function setParser(&$parser) {
 119          $this->parser =& $parser;
 120      }
 121  
 122  	function setCurrentItem(&$item) {
 123          $this->currentItem =& $item;
 124      }
 125  
 126  	function setBlog(&$blog) {
 127          $this->blog =& $blog;
 128      }
 129  
 130  	function setTemplate($template) {
 131          $this->template =& $template;
 132      }
 133  
 134  	function setShowComments($val) {
 135          $this->showComments = $val;
 136      }
 137  
 138      // methods used by parser to insert content

 139  
 140  
 141      /**

 142       * Parse templatevar blogid

 143       */
 144  	function parse_blogid() {
 145          echo $this->blog->getID();
 146      }
 147  
 148      /**

 149       * Parse templatevar body

 150       */
 151  	function parse_body() {
 152          $this->highlightAndParse($this->currentItem->body);
 153      }
 154  
 155      /**

 156       * Parse templatevar more

 157       */
 158  	function parse_more() {
 159          $this->highlightAndParse($this->currentItem->more);
 160      }
 161  
 162      /**

 163       * Parse templatevar itemid

 164       */
 165  	function parse_itemid() {
 166          echo $this->currentItem->itemid;
 167      }
 168  
 169      /**

 170       * Parse templatevar category

 171       */
 172  	function parse_category() {
 173          echo $this->currentItem->category;
 174      }
 175  
 176      /**

 177       * Parse templatevar categorylink

 178       */
 179  	function parse_categorylink() {
 180          echo createLink('category', array('catid' => $this->currentItem->catid, 'name' => $this->currentItem->category));
 181      }
 182  
 183      /**

 184       * Parse templatevar catid

 185       */
 186  	function parse_catid() {
 187          echo $this->currentItem->catid;
 188      }
 189  
 190      /**

 191       * Parse templatevar authorid

 192       */
 193  	function parse_authorid() {
 194          echo $this->currentItem->authorid;
 195      }
 196  
 197      /**

 198       * Parse templatevar authorlink

 199       */
 200  	function parse_authorlink() {
 201          echo createLink(
 202              'member',
 203              array(
 204                  'memberid' => $this->currentItem->authorid,
 205                  'name' => $this->currentItem->author,
 206                  'extra' => $this->linkparams
 207              )
 208          );
 209      }
 210  
 211      /**

 212       * Parse templatevar query

 213       */
 214  	function parse_query() {
 215          echo $this->strHighlight;
 216      }
 217  
 218      /**

 219       * Parse templatevar itemlink

 220       */
 221  	function parse_itemlink() {
 222          echo createLink(
 223              'item',
 224              array(
 225                  'itemid' => $this->currentItem->itemid,
 226                  'title' => $this->currentItem->title,
 227                  'timestamp' => $this->currentItem->timestamp,
 228                  'extra' => $this->linkparams
 229              )
 230          );
 231      }
 232  
 233      /**

 234       * Parse templatevar blogurl

 235       */
 236  	function parse_blogurl() {
 237          echo $this->blog->getURL();
 238      }
 239  
 240      /**

 241       * Parse templatevar closed

 242       */
 243  	function parse_closed() {
 244          echo $this->currentItem->closed;
 245      }
 246  
 247      /**

 248       * Parse templatevar relevance

 249       */
 250  	function parse_relevance() {
 251          echo round($this->currentItem->score,2);
 252      }
 253  
 254      /**

 255       * Parse templatevar title

 256       *

 257       * @param string $format defines in which format the title is shown

 258       */
 259  	function parse_title($format = '') {
 260          if (is_array($this->currentItem)) {
 261              $itemtitle = $this->currentItem['title'];
 262          } elseif (is_object($this->currentItem)) {
 263              $itemtitle = $this->currentItem->title;
 264          }
 265          switch ($format) {
 266              case 'xml':
 267  //                echo stringToXML ($this->currentItem->title);

 268                  echo stringToXML ($itemtitle);
 269                  break;
 270              case 'attribute':
 271  //                echo stringToAttribute ($this->currentItem->title);

 272                  echo stringToAttribute ($itemtitle);
 273                  break;
 274              case 'raw':
 275  //                echo $this->currentItem->title;

 276                  echo $itemtitle;
 277                  break;
 278              default:
 279  //                $this->highlightAndParse($this->currentItem->title);

 280                  $this->highlightAndParse($itemtitle);
 281                  break;
 282          }
 283      }
 284  
 285      /**

 286       * Parse templatevar karma

 287       */
 288  	function parse_karma($type = 'totalscore') {
 289          global $manager;
 290  
 291          // get karma object

 292          $karma =& $manager->getKarma($this->currentItem->itemid);
 293  
 294          switch($type) {
 295              case 'pos':
 296                  echo $karma->getNbPosVotes();
 297                  break;
 298              case 'neg':
 299                  echo $karma->getNbNegVotes();
 300                  break;
 301              case 'votes':
 302                  echo $karma->getNbOfVotes();
 303                  break;
 304              case 'posp':
 305                  $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbPosVotes() / $karma->getNbOfVotes()) : 50;
 306                  echo number_format($percentage,2), '%';
 307                  break;
 308              case 'negp':
 309                  $percentage = $karma->getNbOfVotes() ? 100 * ($karma->getNbNegVotes() / $karma->getNbOfVotes()) : 50;
 310                  echo number_format($percentage,2), '%';
 311                  break;
 312              case 'totalscore':
 313              default:
 314                  echo $karma->getTotalScore();
 315                  break;
 316          }
 317  
 318      }
 319  
 320      /**

 321       * Parse templatevar author

 322       */
 323  	function parse_author($which = '') {
 324          switch($which)
 325          {
 326              case 'realname':
 327                  echo $this->currentItem->authorname;
 328                  break;
 329              case 'id':
 330                  echo $this->currentItem->authorid;
 331                  break;
 332              case 'email':
 333                  echo $this->currentItem->authormail;
 334                  break;
 335              case 'url':
 336                  echo $this->currentItem->authorurl;
 337                  break;
 338              case 'name':
 339              default:
 340                  echo $this->currentItem->author;
 341          }
 342      }
 343  
 344      /**

 345       * Parse templatevar smartbody

 346       */
 347  	function parse_smartbody() {
 348          if (!$this->currentItem->more) {
 349              $this->highlightAndParse($this->currentItem->body);
 350          } else {
 351              $this->highlightAndParse($this->currentItem->more);
 352          }
 353      }
 354  
 355      /**

 356       * Parse templatevar morelink

 357       */
 358  	function parse_morelink() {
 359          if ($this->currentItem->more)
 360              $this->parser->parse($this->template['MORELINK']);
 361      }
 362  
 363      /**

 364       * Parse templatevar date

 365       *

 366       * @param format optional strftime format

 367       */
 368  	function parse_date($format = '') {
 369          if (!isset($this->template['FORMAT_DATE'])) $this->template['FORMAT_DATE'] = '';
 370          echo formatDate($format, $this->currentItem->timestamp, $this->template['FORMAT_DATE'], $this->blog);
 371      }
 372  
 373      /**

 374        * Parse templatevar time

 375        *

 376        * @param format optional strftime format

 377        */
 378  	function parse_time($format = '') {
 379          if (!isset($this->template['FORMAT_TIME'])) $this->template['FORMAT_TIME'] = '';
 380          echo strftime($format ? $format : $this->template['FORMAT_TIME'],$this->currentItem->timestamp);
 381      }
 382  
 383      /**

 384        * Parse templatevar syndicate_title

 385        *

 386        * @param maxLength optional maximum length

 387        */
 388  	function parse_syndicate_title($maxLength = 100) {
 389          $syndicated = strip_tags($this->currentItem->title);
 390          echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
 391      }
 392  
 393      /**

 394        * Parse templatevar syndicate_description

 395        *

 396        * @param maxLength optional maximum length

 397        */
 398  	function parse_syndicate_description($maxLength = 250, $addHighlight = 0) {
 399          $syndicated = strip_tags($this->currentItem->body);
 400          if ($addHighlight) {
 401              $tmp_highlight = htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
 402              echo $this->highlightAndParse($tmp_highlight);
 403          } else {
 404              echo htmlspecialchars(shorten($syndicated,$maxLength,'...'),ENT_QUOTES);
 405          }
 406      }
 407  
 408      /**

 409        * Parse templatevar karmaposlink

 410        *

 411        * @param string text

 412        */
 413  	function parse_karmaposlink($text = '') {
 414          global $CONF;
 415          $link = $CONF['ActionURL'] . '?action=votepositive&amp;itemid='.$this->currentItem->itemid;
 416          echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
 417      }
 418  
 419      /**

 420        * Parse templatevar karmaneglink

 421        *

 422        * @param string text

 423        */
 424  	function parse_karmaneglink($text = '') {
 425          global $CONF;
 426          $link = $CONF['ActionURL'] . '?action=votenegative&amp;itemid='.$this->currentItem->itemid;
 427          echo $text ? '<a href="'.$link.'">'.$text.'</a>' : $link;
 428      }
 429  
 430      /**

 431        * Parse templatevar new

 432        */
 433  	function parse_new() {
 434          if (($this->lastVisit != 0) && ($this->currentItem->timestamp > $this->lastVisit))
 435              echo $this->template['NEW'];
 436      }
 437  
 438      /**

 439        * Parse templatevar daylink

 440        */
 441  	function parse_daylink() {
 442          echo createArchiveLink($this->blog->getID(), strftime('%Y-%m-%d',$this->currentItem->timestamp), $this->linkparams);
 443      }
 444  
 445      /**

 446        * Parse templatevar comments

 447        */
 448  	function parse_comments($maxToShow = 0) {
 449          if ($maxToShow == 0)
 450              $maxToShow = $this->blog->getMaxComments();
 451  
 452          // add comments

 453          if ($this->showComments && $this->blog->commentsEnabled()) {
 454              $comments =& new COMMENTS($this->currentItem->itemid);
 455              $comments->setItemActions($this);
 456              $comments->showComments($this->template, $maxToShow, $this->currentItem->closed ? 0 : 1, $this->strHighlight);
 457          }
 458      }
 459  
 460      /**

 461        * Executes a plugin templatevar

 462        *

 463        * @param pluginName name of plugin (without the NP_)

 464        *

 465        * extra parameters can be added

 466        */
 467  	function parse_plugin($pluginName) {
 468          global $manager;
 469  
 470          // should be already tested from the parser (PARSER.php)

 471          // only continue when the plugin is really installed

 472          /*if (!$manager->pluginInstalled('NP_' . $pluginName))

 473              return;*/
 474  
 475          $plugin =& $manager->getPlugin('NP_' . $pluginName);
 476          if (!$plugin) return;
 477  
 478          // get arguments

 479          $params = func_get_args();
 480  
 481          // remove plugin name

 482          array_shift($params);
 483  
 484          // add item reference (array_unshift didn't work)

 485          $params = array_merge(array(&$this->currentItem),$params);
 486  
 487          call_user_func_array(array(&$plugin,'doTemplateVar'), $params);
 488      }
 489  
 490      /**

 491        * Parse templatevar edit

 492        */
 493  	function parse_edit() {
 494          global $member, $CONF;
 495          if ($this->allowEditAll || ($member->isLoggedIn() && ($member->getID() == $this->currentItem->authorid)) ) {
 496              $this->parser->parse($this->template['EDITLINK']);
 497          }
 498      }
 499  
 500      /**

 501        * Parse templatevar editlink

 502        */
 503  	function parse_editlink() {
 504          global $CONF;
 505          echo $CONF['AdminURL'],'bookmarklet.php?action=edit&amp;itemid=',$this->currentItem->itemid;
 506      }
 507  
 508      /**

 509        * Parse templatevar editpopupcode

 510        */
 511  	function parse_editpopupcode() {
 512          echo "if (event &amp;&amp; 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;";
 513      }
 514  
 515      // helper functions

 516  
 517      /**

 518       * Parses highlighted text, with limited actions only (to prevent not fully trusted team members

 519       * from hacking your weblog.

 520       * 'plugin variables in items' implementation by Andy

 521       */
 522  	function highlightAndParse(&$data) {
 523          $actions =& new BODYACTIONS($this->blog);
 524          $parser =& new PARSER($actions->getDefinedActions(), $actions);
 525          $actions->setTemplate($this->template);
 526          $actions->setHighlight($this->strHighlight);
 527          $actions->setCurrentItem($this->currentItem);
 528          //$actions->setParser($parser);

 529          $parser->parse($actions->highlight($data));
 530      }
 531  
 532      /*

 533      // this is the function previous to the 'plugin variables in items' implementation by Andy

 534      function highlightAndParse(&$data) {

 535          // allow only a limited subset of actions (do not allow includes etc, they might be evil)

 536          $this->parser->actions = array('image','media','popup');

 537          $tmp_highlight = $this->highlight($data);

 538          $this->parser->parse($tmp_highlight);

 539          $this->parser->actions = $this->getDefinedActions();

 540      }

 541      */
 542  
 543  }
 544  
 545  ?>


Generated: Sun Aug 1 03:56:06 2010
Open Source related documentation for developers.