[ Index ]

PHP Cross Reference of Nucleus CMS 3.32

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-2007 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-2007 The Nucleus Group
  18   * @version $Id: ITEM.php 1195 2007-09-05 06:19:47Z kimitake $
  19   */
  20  class ITEM {
  21  
  22      var $itemid;
  23  
  24  	function ITEM($itemid) {
  25          $this->itemid = $itemid;
  26      }
  27  
  28      /**
  29        * Returns one item with the specific itemid
  30        * (static)
  31        */
  32  	function getitem($itemid, $allowdraft, $allowfuture) {
  33          global $manager;
  34  
  35          $itemid = intval($itemid);
  36  
  37          $query =  'SELECT i.idraft as draft, i.inumber as itemid, i.iclosed as closed, '
  38                 . ' i.ititle as title, i.ibody as body, m.mname as author, '
  39                 . ' i.iauthor as authorid, i.itime, i.imore as more, i.ikarmapos as karmapos, '
  40                 . ' i.ikarmaneg as karmaneg, i.icat as catid, i.iblog as blogid '
  41                 . ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, ' . sql_table('blog') . ' as b '
  42                 . ' WHERE i.inumber=' . $itemid
  43                 . ' and i.iauthor=m.mnumber '
  44                 . ' and i.iblog=b.bnumber';
  45  
  46          if (!$allowdraft)
  47              $query .= ' and i.idraft=0';
  48  
  49          if (!$allowfuture) {
  50              $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
  51              $query .= ' and i.itime <=' . mysqldate($blog->getCorrectTime());
  52          }
  53  
  54          $query .= ' LIMIT 1';
  55  
  56          $res = sql_query($query);
  57  
  58          if (mysql_num_rows($res) == 1)
  59          {
  60              $aItemInfo = mysql_fetch_assoc($res);
  61              $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
  62              return $aItemInfo;
  63          } else {
  64              return 0;
  65          }
  66  
  67      }
  68  
  69      /**
  70       * Tries to create an item from the data in the current request (comes from
  71       * bookmarklet or admin area
  72       *
  73       * Returns an array with status info (status = 'added', 'error', 'newcategory')
  74       *
  75       * (static)
  76       */
  77  	function createFromRequest() {
  78           global $member, $manager;
  79  
  80           $i_author =         $member->getID();
  81           $i_body =             postVar('body');
  82           $i_title =            postVar('title');
  83           $i_more =             postVar('more');
  84           $i_actiontype =     postVar('actiontype');
  85           $i_closed =         intPostVar('closed');
  86           $i_hour =             intPostVar('hour');
  87           $i_minutes =         intPostVar('minutes');
  88           $i_month =         intPostVar('month');
  89           $i_day =             intPostVar('day');
  90           $i_year =             intPostVar('year');
  91  
  92           $i_catid =         postVar('catid');
  93  
  94           $i_draftid =         intPostVar('draftid');
  95  
  96           if (!$member->canAddItem($i_catid))
  97              return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
  98  
  99           if (!$i_actiontype) $i_actiontype = 'addnow';
 100  
 101           switch ($i_actiontype) {
 102              case 'adddraft':
 103                  $i_draft = 1;
 104                  break;
 105              case 'addfuture':
 106              case 'addnow':
 107              default:
 108                  $i_draft = 0;
 109           }
 110  
 111           if (!trim($i_body))
 112              return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
 113  
 114          // create new category if needed
 115          if (strstr($i_catid,'newcat')) {
 116              // get blogid
 117              list($i_blogid) = sscanf($i_catid,"newcat-%d");
 118  
 119              // create
 120              $blog =& $manager->getBlog($i_blogid);
 121              $i_catid = $blog->createNewCategory();
 122  
 123              // show error when sth goes wrong
 124              if (!$i_catid)
 125                  return array('status' => 'error','message' => 'Could not create new category');
 126          } else {
 127              // force blogid (must be same as category id)
 128              $i_blogid = getBlogIDFromCatID($i_catid);
 129              $blog =& $manager->getBlog($i_blogid);
 130          }
 131  
 132          if ($i_actiontype == 'addfuture') {
 133              $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
 134  
 135              // make sure the date is in the future, unless we allow past dates
 136              if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()))
 137                  $posttime = $blog->getCorrectTime();
 138          } else {
 139              // time with offset, or 0 for drafts
 140              $posttime = $i_draft ? 0 : $blog->getCorrectTime();
 141          }
 142  
 143          if ($posttime > $blog->getCorrectTime()) {
 144              $posted = 0;
 145              $blog->setFuturePost();
 146          }
 147          else {
 148              $posted = 1;
 149          }
 150  
 151          $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft,$posted);
 152  
 153          //Setting the itemOptions
 154          $aOptions = requestArray('plugoption');
 155          NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
 156          $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)));
 157  
 158          if ($i_draftid > 0) {
 159              ITEM::delete($i_draftid);
 160          }
 161  
 162          // success
 163          if ($i_catid != intRequestVar('catid'))
 164              return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
 165          else
 166              return array('status' => 'added', 'itemid' => $itemid);
 167      }
 168  
 169  
 170      /**
 171        * Updates an item (static)
 172        */
 173  	function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
 174          global $manager;
 175  
 176          $itemid = intval($itemid);
 177  
 178          // make sure value is 1 or 0
 179          if ($closed != 1) $closed = 0;
 180  
 181          // get destination blogid
 182          $new_blogid = getBlogIDFromCatID($catid);
 183          $old_blogid = getBlogIDFromItemID($itemid);
 184  
 185          // move will be done on end of method
 186          if ($new_blogid != $old_blogid)
 187              $moveNeeded = 1;
 188  
 189          // add <br /> before newlines
 190          $blog =& $manager->getBlog($new_blogid);
 191          if ($blog->convertBreaks()) {
 192              $body = addBreaks($body);
 193              $more = addBreaks($more);
 194          }
 195  
 196          // call plugins
 197          $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid));
 198  
 199          // update item itsself
 200          $query =  'UPDATE '.sql_table('item')
 201                 . ' SET'
 202                 . " ibody='". addslashes($body) ."',"
 203                 . " ititle='" . addslashes($title) . "',"
 204                 . " imore='" . addslashes($more) . "',"
 205                 . " iclosed=" . intval($closed) . ","
 206                 . " icat=" . intval($catid);
 207  
 208          // if we received an updated timestamp in the past, but past posting is not allowed,
 209          // reject that date change (timestamp = 0 will make sure the current date is kept)
 210          if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
 211                  $timestamp = 0;
 212  
 213          if ($timestamp > $blog->getCorrectTime(time())) {
 214              $isFuture = 1;
 215              $query .= ', iposted=0';
 216          }
 217          else {
 218              $isFuture = 0;
 219              $query .= ', iposted=1';
 220          }
 221  
 222          if ($wasdraft && $publish) {
 223              // set timestamp to current date only if it's not a future item
 224              // draft items have timestamp == 0
 225              // don't allow timestamps in the past (unless otherwise defined in blogsettings)
 226              $query .= ', idraft=0';
 227  
 228              if ($timestamp == 0)
 229                  $timestamp = $blog->getCorrectTime();
 230  
 231              // send new item notification
 232              if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
 233                  $blog->sendNewItemNotification($itemid, $title, $body);
 234          }
 235  
 236          // update timestamp when needed
 237          if ($timestamp != 0)
 238              $query .= ", itime=" . mysqldate($timestamp);
 239  
 240          // make sure the correct item is updated
 241          $query .= ' WHERE inumber=' . $itemid;
 242  
 243          // off we go!
 244          sql_query($query);
 245  
 246          $manager->notify('PostUpdateItem',array('itemid' => $itemid));
 247  
 248          // when needed, move item and comments to new blog
 249          if ($moveNeeded)
 250              ITEM::move($itemid, $catid);
 251  
 252          //update the itemOptions
 253          $aOptions = requestArray('plugoption');
 254          NucleusPlugin::_applyPluginOptions($aOptions);
 255          $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $title, 'body' => $body, 'more' => $more, 'closed' => $closed, 'catid' => $catid)));
 256  
 257      }
 258  
 259      // move an item to another blog (no checks, static)
 260  	function move($itemid, $new_catid) {
 261          global $manager;
 262  
 263          $itemid = intval($itemid);
 264          $new_catid = intval($new_catid);
 265  
 266          $new_blogid = getBlogIDFromCatID($new_catid);
 267  
 268          $manager->notify(
 269              'PreMoveItem',
 270              array(
 271                  'itemid' => $itemid,
 272                  'destblogid' => $new_blogid,
 273                  'destcatid' => $new_catid
 274              )
 275          );
 276  
 277  
 278          // update item table
 279          $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
 280          sql_query($query);
 281  
 282          // update comments
 283          $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
 284          sql_query($query);
 285  
 286          $manager->notify(
 287              'PostMoveItem',
 288              array(
 289                  'itemid' => $itemid,
 290                  'destblogid' => $new_blogid,
 291                  'destcatid' => $new_catid
 292              )
 293          );
 294      }
 295  
 296      /**
 297        * Deletes an item
 298        */
 299  	function delete($itemid) {
 300          global $manager;
 301  
 302          $itemid = intval($itemid);
 303  
 304          $manager->notify('PreDeleteItem', array('itemid' => $itemid));
 305  
 306          // delete item
 307          $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
 308          sql_query($query);
 309  
 310          // delete the comments associated with the item
 311          $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
 312          sql_query($query);
 313  
 314          // delete all associated plugin options
 315          NucleusPlugin::_deleteOptionValues('item', $itemid);
 316  
 317          $manager->notify('PostDeleteItem', array('itemid' => $itemid));
 318      }
 319  
 320      // returns true if there is an item with the given ID (static)
 321  	function exists($id,$future,$draft) {
 322          global $manager;
 323  
 324          $id = intval($id);
 325  
 326          $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
 327          if (!$future) {
 328              $bid = getBlogIDFromItemID($id);
 329              if (!$bid) return 0;
 330              $b =& $manager->getBlog($bid);
 331              $r .= ' and itime<='.mysqldate($b->getCorrectTime());
 332          }
 333          if (!$draft) {
 334              $r .= ' and idraft=0';
 335          }
 336          $r = sql_query($r);
 337  
 338          return (mysql_num_rows($r) != 0);
 339      }
 340  
 341      /**
 342       * Tries to create an draft from the data in the current request (comes from
 343       * bookmarklet or admin area
 344       *
 345       * Returns an array with status info (status = 'added', 'error', 'newcategory')
 346       *
 347       * (static)
 348       *
 349       * Used by xmlHTTPRequest AutoDraft
 350       */
 351  	function createDraftFromRequest() {
 352          global $member, $manager;
 353  
 354          $i_author = $member->getID();
 355          $i_body = postVar('body');
 356          $i_title = postVar('title');
 357          $i_more = postVar('more');
 358          //$i_actiontype = postVar('actiontype');
 359          $i_closed = intPostVar('closed');
 360          //$i_hour = intPostVar('hour');
 361          //$i_minutes = intPostVar('minutes');
 362          //$i_month = intPostVar('month');
 363          //$i_day = intPostVar('day');
 364          //$i_year = intPostVar('year');
 365          $i_catid = postVar('catid');
 366          $i_draft = 1;
 367          $type = postVar('type');
 368          if ($type == 'edit') {
 369              $i_blogid = getBlogIDFromItemID(intPostVar('itemid'));
 370          }
 371          else {
 372              $i_blogid = intPostVar('blogid');
 373          }
 374          $i_draftid = intPostVar('draftid');
 375  
 376          if (!$member->canAddItem($i_catid)) {
 377              return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
 378          }
 379  
 380          if (!trim($i_body)) {
 381              return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
 382          }
 383  
 384          // create new category if needed
 385          if (strstr($i_catid, 'newcat')) {
 386              // Set in default category
 387              $blog =& $manager->getBlog($i_blogid);
 388              $i_catid = $blog->getDefaultCategory();
 389          }
 390          else {
 391              // force blogid (must be same as category id)
 392              $i_blogid = getBlogIDFromCatID($i_catid);
 393              $blog =& $manager->getBlog($i_blogid);
 394          }
 395  
 396          $posttime = 0;
 397  
 398          if ($i_draftid > 0) {
 399              ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
 400              $itemid = $i_draftid;
 401          }
 402          else {
 403              $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
 404          }
 405  
 406          // No plugin support in AutoSaveDraft yet
 407          //Setting the itemOptions
 408          //$aOptions = requestArray('plugoption');
 409          //NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
 410          //$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)));
 411  
 412          // success
 413          return array('status' => 'added', 'draftid' => $itemid);
 414      }
 415  
 416  }
 417  
 418  ?>


Generated: Tue Feb 12 15:34:36 2008 Cross-referenced by PHPXref 0.7