4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2009 The Nucleus Group
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)
14 * @license http://nucleuscms.org/license.txt GNU General Public License
15 * @copyright Copyright (C) 2002-2009 The Nucleus Group
16 * @version $Id: ITEM.php 1593 2011-11-01 18:42:03Z gregorlove $
20 * A class representing an item
33 * Creates a new ITEM object
36 public function __construct($item_id)
38 $this->itemid = $item_id;
43 * Returns one item with the specific itemid
46 * @param bool $allow_draft
47 * @param bool $allow_future
50 public function getitem($item_id, $allow_draft, $allow_future)
54 $item_id = intval($item_id);
57 '`i`.`idraft` AS `draft`, ' .
58 '`i`.`inumber` AS `itemid`, ' .
59 '`i`.`iclosed` AS `closed`, ' .
60 '`i`.`ititle` AS `title`, ' .
61 '`i`.`ibody` AS `body`, ' .
62 '`m`.`mname` AS `author`, ' .
63 '`i`.`iauthor` AS `authorid`, ' .
65 '`i`.`imore` AS `more`, ' .
66 '`i`.`ikarmapos` AS `karmapos`, ' .
67 '`i`.`ikarmaneg` AS `karmaneg`, ' .
68 '`i`.`icat` AS `catid`, ' .
69 '`i`.`iblog` AS `blogid` ' .
70 'FROM `%s` AS `i`, `%s` AS `m`, `%s` AS `b` ' .
71 'WHERE `i`.`inumber` = %d ' .
72 'AND `i`.`iauthor` = `m`.`mnumber` ' .
73 'AND `i`.`iblog` = `b`.`bnumber` ';
75 $query = sprintf($query, sql_table('item'), sql_table('member'), sql_table('blog'), $item_id);
79 $query .= 'AND `i`.`idraft` = 0 ';
84 $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));
85 $query .= 'AND `i`.`itime` <= ' . mysqldate($blog->getCorrectTime());
89 $result = sql_query($query);
91 if ( sql_num_rows($result) == 1 )
93 $aItemInfo = sql_fetch_assoc($result);
94 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
106 * Tries to create an item from the data in the current request (comes from
107 * bookmarklet or admin area
109 * Returns an array with status info:
110 * status = 'added', 'error', 'newcategory'
114 function createFromRequest()
116 global $member, $manager;
118 $i_author = $member->getID();
119 $i_body = postVar('body');
120 $i_title = postVar('title');
121 $i_more = postVar('more');
122 $i_actiontype = postVar('actiontype');
123 $i_closed = intPostVar('closed');
124 $i_hour = intPostVar('hour');
125 $i_minutes = intPostVar('minutes');
126 $i_month = intPostVar('month');
127 $i_day = intPostVar('day');
128 $i_year = intPostVar('year');
129 $i_catid = postVar('catid');
130 $i_draftid = intPostVar('draftid');
132 if ( !$member->canAddItem($i_catid) )
134 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
138 $i_actiontype = 'addnow';
140 switch ( $i_actiontype )
153 if ( !trim($i_body) )
155 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
158 // create new category if needed
159 if ( strstr($i_catid,'newcat') )
162 list($i_blogid) = sscanf($i_catid, "newcat-%d");
165 $blog =& $manager->getBlog($i_blogid);
166 $i_catid = $blog->createNewCategory();
168 // show error when sth goes wrong
171 return array('status' => 'error','message' => 'Could not create new category');
177 // force blogid (must be same as category id)
178 $i_blogid = getBlogIDFromCatID($i_catid);
179 $blog =& $manager->getBlog($i_blogid);
182 if ( $i_actiontype == 'addfuture' )
184 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
186 // make sure the date is in the future, unless we allow past dates
187 if ( (!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()) )
189 $posttime = $blog->getCorrectTime();
195 // time with offset, or 0 for drafts
196 $posttime = $i_draft ? 0 : $blog->getCorrectTime();
199 if ( $posttime > $blog->getCorrectTime() )
202 $blog->setFuturePost();
209 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft, $posted);
211 //Setting the itemOptions
212 $aOptions = requestArray('plugoption');
213 NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
214 $manager->notify('PostPluginOptionsUpdate', array(
221 'closed' => $i_closed,
227 if ( $i_draftid > 0 )
229 // delete permission is checked inside ITEM::delete()
230 ITEM::delete($i_draftid);
234 if ( $i_catid != intRequestVar('catid') )
236 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
240 return array('status' => 'added', 'itemid' => $itemid);
251 function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)
255 $itemid = intval($itemid);
257 // make sure value is 1 or 0
263 // get destination blogid
264 $new_blogid = getBlogIDFromCatID($catid);
265 $old_blogid = getBlogIDFromItemID($itemid);
267 // move will be done on end of method
268 if ( $new_blogid != $old_blogid )
273 $blog =& $manager->getBlog($new_blogid);
275 // begin if: convert line breaks to <br/>
276 if ( $blog->convertBreaks() )
278 $body = addBreaks($body);
279 $more = addBreaks($more);
283 $manager->notify('PreUpdateItem', array(
289 'closed' => &$closed,
294 // update item itself
295 $query = 'UPDATE ' . sql_table('item')
297 . " ibody = '" . sql_real_escape_string($body) . "',"
298 . " ititle = '" . sql_real_escape_string($title) . "',"
299 . " imore = '" . sql_real_escape_string($more) . "',"
300 . " iclosed = " . intval($closed) . ","
301 . " icat = " . intval($catid);
303 // if we received an updated timestamp that is in the past, but past posting is not allowed, reject that date change (timestamp = 0 will make sure the current date is kept)
304 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()) )
309 // begin if: post is in the future
310 if ( $timestamp > $blog->getCorrectTime(time()) )
313 $query .= ', iposted = 0';
318 $query .= ', iposted = 1';
321 if ( $wasdraft && $publish )
323 // set timestamp to current date only if it's not a future item
324 // draft items have timestamp == 0
325 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
326 $query .= ', idraft = 0';
328 if ( $timestamp == 0 )
330 $timestamp = $blog->getCorrectTime();
333 // send new item notification
334 if ( !$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem() )
336 $blog->sendNewItemNotification($itemid, $title, $body);
341 // save back to drafts
342 if ( !$wasdraft && !$publish )
344 $query .= ', idraft = 1';
345 // set timestamp back to zero for a draft
346 $query .= ', itime = ' . mysqldate($timestamp);
349 // update timestamp when needed
350 if ( $timestamp != 0 )
352 $query .= ', itime = ' . mysqldate($timestamp);
355 // make sure the correct item is updated
356 $query .= ' WHERE inumber = ' . $itemid;
361 $manager->notify('PostUpdateItem', array('itemid' => $itemid));
363 // when needed, move item and comments to new blog
366 ITEM::move($itemid, $catid);
369 //update the itemOptions
370 $aOptions = requestArray('plugoption');
371 NucleusPlugin::_applyPluginOptions($aOptions);
372 $manager->notify('PostPluginOptionsUpdate', array(
388 * Move an item to another blog (no checks)
392 function move($itemid, $new_catid) {
395 $itemid = intval($itemid);
396 $new_catid = intval($new_catid);
398 $new_blogid = getBlogIDFromCatID($new_catid);
404 'destblogid' => $new_blogid,
405 'destcatid' => $new_catid
411 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
415 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
422 'destblogid' => $new_blogid,
423 'destcatid' => $new_catid
431 function delete($itemid) {
432 global $manager, $member;
434 $itemid = intval($itemid);
436 // check to ensure only those allow to alter the item can
438 if (!$member->canAlterItem($itemid)) {
443 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
446 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
449 // delete the comments associated with the item
450 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
453 // delete all associated plugin options
454 NucleusPlugin::_deleteOptionValues('item', $itemid);
456 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
462 * Returns true if there is an item with the given ID
466 function exists($id,$future,$draft) {
471 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
473 $bid = getBlogIDFromItemID($id);
475 $b =& $manager->getBlog($bid);
476 $r .= ' and itime<='.mysqldate($b->getCorrectTime());
479 $r .= ' and idraft=0';
483 return (sql_num_rows($r) != 0);
487 * Tries to create an draft from the data in the current request (comes from
488 * bookmarklet or admin area
490 * Returns an array with status info:
491 * status = 'added', 'error', 'newcategory'
495 * Used by xmlHTTPRequest AutoDraft
497 function createDraftFromRequest() {
498 global $member, $manager;
500 $i_author = $member->getID();
501 $i_body = postVar('body');
502 $i_title = postVar('title');
503 $i_more = postVar('more');
504 //$i_actiontype = postVar('actiontype');
505 $i_closed = intPostVar('closed');
506 //$i_hour = intPostVar('hour');
507 //$i_minutes = intPostVar('minutes');
508 //$i_month = intPostVar('month');
509 //$i_day = intPostVar('day');
510 //$i_year = intPostVar('year');
511 $i_catid = postVar('catid');
513 $type = postVar('type');
514 if ($type == 'edit') {
515 $i_blogid = getBlogIDFromItemID(intPostVar('itemid'));
518 $i_blogid = intPostVar('blogid');
520 $i_draftid = intPostVar('draftid');
522 if (!$member->canAddItem($i_catid)) {
523 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
526 if (!trim($i_body)) {
527 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
530 // create new category if needed
531 if (strstr($i_catid, 'newcat')) {
532 // Set in default category
533 $blog =& $manager->getBlog($i_blogid);
534 $i_catid = $blog->getDefaultCategory();
537 // force blogid (must be same as category id)
538 $i_blogid = getBlogIDFromCatID($i_catid);
539 $blog =& $manager->getBlog($i_blogid);
544 if ($i_draftid > 0) {
545 ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
546 $itemid = $i_draftid;
549 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
552 // No plugin support in AutoSaveDraft yet
553 //Setting the itemOptions
554 //$aOptions = requestArray('plugoption');
555 //NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
556 //$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)));
559 return array('status' => 'added', 'draftid' => $itemid);