[ Index ]

PHP Cross Reference of Nucleus CMS v3.51 code documentation

title

Body

[close]

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

   1  <?php
   2  
   3  /*

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

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

   6   *

   7   * This program is free software; you can redistribute it and/or

   8   * modify it under the terms of the GNU General Public License

   9   * as published by the Free Software Foundation; either version 2

  10   * of the License, or (at your option) any later version.

  11   * (see nucleus/documentation/index.html#license for more info)

  12   */
  13  /**

  14   * A class representing an item

  15   *

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

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

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

  19   */
  20  class ITEM {
  21  
  22      var $itemid;
  23  
  24      /**

  25        * Constructor of an ITEM object

  26        * 

  27        * @param integer $itemid id of the item

  28        */
  29  	function ITEM($itemid) {
  30          $this->itemid = $itemid;
  31      }
  32  
  33      /**

  34        * Returns one item with the specific itemid

  35        * 

  36        * @param integer $itemid id of the item

  37        * @param boolean $allowdraft

  38        * @param boolean $allowfuture                        

  39        * @static

  40        */
  41  	function getitem($itemid, $allowdraft, $allowfuture) {
  42          global $manager;
  43  
  44          $itemid = intval($itemid);
  45  
  46          $query =  'SELECT i.idraft as draft, i.inumber as itemid, i.iclosed as closed, '
  47                 . ' i.ititle as title, i.ibody as body, m.mname as author, '
  48                 . ' i.iauthor as authorid, i.itime, i.imore as more, i.ikarmapos as karmapos, '
  49                 . ' i.ikarmaneg as karmaneg, i.icat as catid, i.iblog as blogid '
  50                 . ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, ' . sql_table('blog') . ' as b '
  51                 . ' WHERE i.inumber=' . $itemid
  52                 . ' and i.iauthor=m.mnumber '
  53                 . ' and i.iblog=b.bnumber';
  54  
  55          if (!$allowdraft)
  56              $query .= ' and i.idraft=0';
  57  
  58          if (!$allowfuture) {
  59              $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
  60              $query .= ' and i.itime <=' . mysqldate($blog->getCorrectTime());
  61          }
  62  
  63          $query .= ' LIMIT 1';
  64  
  65          $res = sql_query($query);
  66  
  67          if (sql_num_rows($res) == 1)
  68          {
  69              $aItemInfo = sql_fetch_assoc($res);
  70              $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
  71              return $aItemInfo;
  72          } else {
  73              return 0;
  74          }
  75  
  76      }
  77  
  78      /**

  79       * Tries to create an item from the data in the current request (comes from

  80       * bookmarklet or admin area

  81       *

  82       * Returns an array with status info:

  83       * status = 'added', 'error', 'newcategory'

  84       *

  85       * @static

  86       */
  87  	function createFromRequest() {
  88           global $member, $manager;
  89  
  90           $i_author =         $member->getID();
  91           $i_body =             postVar('body');
  92           $i_title =            postVar('title');
  93           $i_more =             postVar('more');
  94           $i_actiontype =     postVar('actiontype');
  95           $i_closed =         intPostVar('closed');
  96           $i_hour =             intPostVar('hour');
  97           $i_minutes =         intPostVar('minutes');
  98           $i_month =         intPostVar('month');
  99           $i_day =             intPostVar('day');
 100           $i_year =             intPostVar('year');
 101  
 102           $i_catid =         postVar('catid');
 103  
 104           $i_draftid =         intPostVar('draftid');
 105  
 106           if (!$member->canAddItem($i_catid))
 107              return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
 108  
 109           if (!$i_actiontype) $i_actiontype = 'addnow';
 110  
 111           switch ($i_actiontype) {
 112              case 'adddraft':
 113                  $i_draft = 1;
 114                  break;
 115              case 'addfuture':
 116              case 'addnow':
 117              default:
 118                  $i_draft = 0;
 119           }
 120  
 121           if (!trim($i_body))
 122              return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
 123  
 124          // create new category if needed

 125          if (strstr($i_catid,'newcat')) {
 126              // get blogid

 127              list($i_blogid) = sscanf($i_catid,"newcat-%d");
 128  
 129              // create

 130              $blog =& $manager->getBlog($i_blogid);
 131              $i_catid = $blog->createNewCategory();
 132  
 133              // show error when sth goes wrong

 134              if (!$i_catid)
 135                  return array('status' => 'error','message' => 'Could not create new category');
 136          } else {
 137              // force blogid (must be same as category id)

 138              $i_blogid = getBlogIDFromCatID($i_catid);
 139              $blog =& $manager->getBlog($i_blogid);
 140          }
 141  
 142          if ($i_actiontype == 'addfuture') {
 143              $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
 144  
 145              // make sure the date is in the future, unless we allow past dates

 146              if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()))
 147                  $posttime = $blog->getCorrectTime();
 148          } else {
 149              // time with offset, or 0 for drafts

 150              $posttime = $i_draft ? 0 : $blog->getCorrectTime();
 151          }
 152  
 153          if ($posttime > $blog->getCorrectTime()) {
 154              $posted = 0;
 155              $blog->setFuturePost();
 156          }
 157          else {
 158              $posted = 1;
 159          }
 160  
 161          $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft,$posted);
 162  
 163          //Setting the itemOptions

 164          $aOptions = requestArray('plugoption');
 165          NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
 166          $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $i_title, 'body' => $i_body, 'more' => $i_more, 'closed' => $i_closed, 'catid' => $i_catid)));
 167  
 168          if ($i_draftid > 0) {
 169              // delete permission is checked inside ITEM::delete()

 170              ITEM::delete($i_draftid);
 171          }
 172  
 173          // success

 174          if ($i_catid != intRequestVar('catid'))
 175              return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
 176          else
 177              return array('status' => 'added', 'itemid' => $itemid);
 178      }
 179  
 180  
 181      /**

 182        * Updates an item

 183        * 

 184        * @static

 185        */
 186  	function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
 187          global $manager;
 188  
 189          $itemid = intval($itemid);
 190  
 191          // make sure value is 1 or 0

 192          if ($closed != 1) $closed = 0;
 193  
 194          // get destination blogid

 195          $new_blogid = getBlogIDFromCatID($catid);
 196          $old_blogid = getBlogIDFromItemID($itemid);
 197  
 198          // move will be done on end of method

 199          if ($new_blogid != $old_blogid)
 200              $moveNeeded = 1;
 201  
 202          // add <br /> before newlines

 203          $blog =& $manager->getBlog($new_blogid);
 204          if ($blog->convertBreaks()) {
 205              $body = addBreaks($body);
 206              $more = addBreaks($more);
 207          }
 208  
 209          // call plugins

 210          $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid));
 211  
 212          // update item itsself

 213          $query =  'UPDATE '.sql_table('item')
 214                 . ' SET'
 215                 . " ibody='". addslashes($body) ."',"
 216                 . " ititle='" . addslashes($title) . "',"
 217                 . " imore='" . addslashes($more) . "',"
 218                 . " iclosed=" . intval($closed) . ","
 219                 . " icat=" . intval($catid);
 220  
 221          // if we received an updated timestamp in the past, but past posting is not allowed,

 222          // reject that date change (timestamp = 0 will make sure the current date is kept)

 223          if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
 224                  $timestamp = 0;
 225  
 226          if ($timestamp > $blog->getCorrectTime(time())) {
 227              $isFuture = 1;
 228              $query .= ', iposted=0';
 229          }
 230          else {
 231              $isFuture = 0;
 232              $query .= ', iposted=1';
 233          }
 234  
 235          if ($wasdraft && $publish) {
 236              // set timestamp to current date only if it's not a future item

 237              // draft items have timestamp == 0

 238              // don't allow timestamps in the past (unless otherwise defined in blogsettings)

 239              $query .= ', idraft=0';
 240  
 241              if ($timestamp == 0)
 242                  $timestamp = $blog->getCorrectTime();
 243  
 244              // send new item notification

 245              if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
 246                  $blog->sendNewItemNotification($itemid, $title, $body);
 247          }
 248          
 249          // save back to drafts        

 250          if (!$wasdraft && !$publish) {
 251              $query .= ', idraft=1';
 252              // set timestamp back to zero for a draft

 253              $query .= ", itime=" . mysqldate($timestamp);
 254          }
 255  
 256          // update timestamp when needed

 257          if ($timestamp != 0)
 258              $query .= ", itime=" . mysqldate($timestamp);
 259  
 260          // make sure the correct item is updated

 261          $query .= ' WHERE inumber=' . $itemid;
 262  
 263          // off we go!

 264          sql_query($query);
 265  
 266          $manager->notify('PostUpdateItem',array('itemid' => $itemid));
 267  
 268          // when needed, move item and comments to new blog

 269          if ($moveNeeded)
 270              ITEM::move($itemid, $catid);
 271  
 272          //update the itemOptions

 273          $aOptions = requestArray('plugoption');
 274          NucleusPlugin::_applyPluginOptions($aOptions);
 275          $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $title, 'body' => $body, 'more' => $more, 'closed' => $closed, 'catid' => $catid)));
 276  
 277      }
 278  
 279      /**

 280       * Move an item to another blog (no checks)

 281       *

 282       * @static

 283       */                   
 284  	function move($itemid, $new_catid) {
 285          global $manager;
 286  
 287          $itemid = intval($itemid);
 288          $new_catid = intval($new_catid);
 289  
 290          $new_blogid = getBlogIDFromCatID($new_catid);
 291  
 292          $manager->notify(
 293              'PreMoveItem',
 294              array(
 295                  'itemid' => $itemid,
 296                  'destblogid' => $new_blogid,
 297                  'destcatid' => $new_catid
 298              )
 299          );
 300  
 301  
 302          // update item table

 303          $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
 304          sql_query($query);
 305  
 306          // update comments

 307          $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
 308          sql_query($query);
 309  
 310          $manager->notify(
 311              'PostMoveItem',
 312              array(
 313                  'itemid' => $itemid,
 314                  'destblogid' => $new_blogid,
 315                  'destcatid' => $new_catid
 316              )
 317          );
 318      }
 319  
 320      /**

 321        * Deletes an item

 322        */
 323  	function delete($itemid) {
 324          global $manager, $member;
 325  
 326          $itemid = intval($itemid);
 327  
 328          // check to ensure only those allow to alter the item can

 329          // proceed

 330          if (!$member->canAlterItem($itemid)) {
 331              return 1;
 332          }
 333  
 334  
 335          $manager->notify('PreDeleteItem', array('itemid' => $itemid));
 336  
 337          // delete item

 338          $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
 339          sql_query($query);
 340  
 341          // delete the comments associated with the item

 342          $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
 343          sql_query($query);
 344  
 345          // delete all associated plugin options

 346          NucleusPlugin::_deleteOptionValues('item', $itemid);
 347  
 348          $manager->notify('PostDeleteItem', array('itemid' => $itemid));
 349  
 350          return 0;
 351      }
 352  
 353      /**

 354       * Returns true if there is an item with the given ID

 355       *

 356       * @static

 357       */              
 358  	function exists($id,$future,$draft) {
 359          global $manager;
 360  
 361          $id = intval($id);
 362  
 363          $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
 364          if (!$future) {
 365              $bid = getBlogIDFromItemID($id);
 366              if (!$bid) return 0;
 367              $b =& $manager->getBlog($bid);
 368              $r .= ' and itime<='.mysqldate($b->getCorrectTime());
 369          }
 370          if (!$draft) {
 371              $r .= ' and idraft=0';
 372          }
 373          $r = sql_query($r);
 374  
 375          return (sql_num_rows($r) != 0);
 376      }
 377  
 378      /**

 379       * Tries to create an draft from the data in the current request (comes from

 380       * bookmarklet or admin area

 381       *

 382       * Returns an array with status info:

 383       * status = 'added', 'error', 'newcategory'

 384       *

 385       * @static

 386       *

 387       * Used by xmlHTTPRequest AutoDraft

 388       */
 389  	function createDraftFromRequest() {
 390          global $member, $manager;
 391  
 392          $i_author = $member->getID();
 393          $i_body = postVar('body');
 394          $i_title = postVar('title');
 395          $i_more = postVar('more');
 396          //$i_actiontype = postVar('actiontype');

 397          $i_closed = intPostVar('closed');
 398          //$i_hour = intPostVar('hour');

 399          //$i_minutes = intPostVar('minutes');

 400          //$i_month = intPostVar('month');

 401          //$i_day = intPostVar('day');

 402          //$i_year = intPostVar('year');

 403          $i_catid = postVar('catid');
 404          $i_draft = 1;
 405          $type = postVar('type');
 406          if ($type == 'edit') {
 407              $i_blogid = getBlogIDFromItemID(intPostVar('itemid'));
 408          }
 409          else {
 410              $i_blogid = intPostVar('blogid');
 411          }
 412          $i_draftid = intPostVar('draftid');
 413  
 414          if (!$member->canAddItem($i_catid)) {
 415              return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
 416          }
 417  
 418          if (!trim($i_body)) {
 419              return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
 420          }
 421  
 422          // create new category if needed

 423          if (strstr($i_catid, 'newcat')) {
 424              // Set in default category

 425              $blog =& $manager->getBlog($i_blogid);
 426              $i_catid = $blog->getDefaultCategory();
 427          }
 428          else {
 429              // force blogid (must be same as category id)

 430              $i_blogid = getBlogIDFromCatID($i_catid);
 431              $blog =& $manager->getBlog($i_blogid);
 432          }
 433  
 434          $posttime = 0;
 435  
 436          if ($i_draftid > 0) {
 437              ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
 438              $itemid = $i_draftid;
 439          }
 440          else {
 441              $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
 442          }
 443  
 444          // No plugin support in AutoSaveDraft yet

 445          //Setting the itemOptions

 446          //$aOptions = requestArray('plugoption');

 447          //NucleusPlugin::_applyPluginOptions($aOptions, $itemid);

 448          //$manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $i_title, 'body' => $i_body, 'more' => $i_more, 'closed' => $i_closed, 'catid' => $i_catid)));

 449  
 450          // success

 451          return array('status' => 'added', 'draftid' => $itemid);
 452      }
 453  
 454  }
 455  
 456  ?>


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