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 1668 2012-02-19 14:36:44Z sakamocchi $
20 * A class representing an item
27 * actiontype list for handling items
31 static private $actiontypes
32 = array('addnow', 'adddraft', 'addfuture', 'edit', 'changedate', 'backtodrafts', 'delete');
36 * Creates a new ITEM object
42 public function __construct()
48 * Returns one item with the specific itemid
51 * @param bool $allow_draft
52 * @param bool $allow_future
55 static public function getitem($item_id, $allow_draft, $allow_future)
59 $item_id = intval($item_id);
62 '`i`.`idraft` AS `draft`, ' .
63 '`i`.`inumber` AS `itemid`, ' .
64 '`i`.`iclosed` AS `closed`, ' .
65 '`i`.`ititle` AS `title`, ' .
66 '`i`.`ibody` AS `body`, ' .
67 '`m`.`mname` AS `author`, ' .
68 '`i`.`iauthor` AS `authorid`, ' .
70 '`i`.`imore` AS `more`, ' .
71 '`i`.`ikarmapos` AS `karmapos`, ' .
72 '`i`.`ikarmaneg` AS `karmaneg`, ' .
73 '`i`.`icat` AS `catid`, ' .
74 '`i`.`iblog` AS `blogid` ' .
75 'FROM `%s` AS `i`, `%s` AS `m`, `%s` AS `b` ' .
76 'WHERE `i`.`inumber` = %d ' .
77 'AND `i`.`iauthor` = `m`.`mnumber` ' .
78 'AND `i`.`iblog` = `b`.`bnumber`';
80 $query = sprintf($query, sql_table('item'), sql_table('member'), sql_table('blog'), $item_id);
84 $query .= 'AND `i`.`idraft` = 0 ';
89 $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));
90 $query .= 'AND `i`.`itime` <= ' . mysqldate($blog->getCorrectTime());
94 $result = sql_query($query);
96 if ( sql_num_rows($result) == 1 )
98 $aItemInfo = sql_fetch_assoc($result);
99 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
109 * ITEM::createFromRequest()
110 * Tries to create an item from the data in the current request (comes from
111 * bookmarklet or admin area
115 * @return array (status = added/error/newcategory, message)
117 static public function createFromRequest()
119 global $member, $manager;
122 * TODO: these values from user agent should be validated but not implemented yet
124 $i_author = $member->getID();
125 $i_body = postVar('body');
126 $i_title = postVar('title');
127 $i_more = postVar('more');
128 $i_actiontype = postVar('actiontype');
129 $i_closed = intPostVar('closed');
130 $i_hour = intPostVar('hour');
131 $i_minutes = intPostVar('minutes');
132 $i_month = intPostVar('month');
133 $i_day = intPostVar('day');
134 $i_year = intPostVar('year');
135 $i_catid = postVar('catid');
136 $i_draftid = intPostVar('draftid');
138 if ( !$member->canAddItem($i_catid) )
140 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
143 if ( !in_array($i_actiontype, self::$actiontypes) )
145 $i_actiontype = 'addnow';
148 $i_draft = (integer) ( $i_actiontype == 'adddraft' );
150 if ( !trim($i_body) )
152 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
155 // create new category if needed
156 if ( i18n::strpos($i_catid, 'newcat') )
159 list($i_blogid) = sscanf($i_catid, "newcat-%d");
162 $blog =& $manager->getBlog($i_blogid);
163 $i_catid = $blog->createNewCategory();
165 // show error when sth goes wrong
168 return array('status' => 'error','message' => 'Could not create new category');
173 // force blogid (must be same as category id)
174 $i_blogid = getBlogIDFromCatID($i_catid);
175 $blog =& $manager->getBlog($i_blogid);
178 if ( $i_actiontype == 'addfuture' )
180 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
182 // make sure the date is in the future, unless we allow past dates
183 if ( (!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()) )
185 $posttime = $blog->getCorrectTime();
190 // time with offset, or 0 for drafts
191 $posttime = $i_draft ? 0 : $blog->getCorrectTime();
194 if ( $posttime > $blog->getCorrectTime() )
197 $blog->setFuturePost();
204 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft, $posted);
206 //Setting the itemOptions
207 $aOptions = requestArray('plugoption');
208 NucleusPlugin::apply_plugin_options($aOptions, $itemid);
209 $manager->notify('PostPluginOptionsUpdate', array(
216 'closed' => $i_closed,
222 if ( $i_draftid > 0 )
224 // delete permission is checked inside ITEM::delete()
225 self::delete($i_draftid);
229 if ( $i_catid != intRequestVar('catid') )
231 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
235 return array('status' => 'added', 'itemid' => $itemid);
244 * @param integer $itemid item id
245 * @param integer $catid category id
246 * @param string $title title
247 * @param string $body body text
248 * @param string $more more text
249 * @param boolean $closed closed or not
250 * @param boolean $wasdraft previously draft or not
251 * @param boolean $publish published or not
252 * @param timestamp $timestamp timestamp
256 static public function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)
260 $itemid = (integer) $itemid;
261 $closed = (boolean) $closed;
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);
340 // save back to drafts
341 if ( !$wasdraft && !$publish )
343 $query .= ', idraft = 1';
344 // set timestamp back to zero for a draft
345 $query .= ', itime = ' . mysqldate($timestamp);
348 // update timestamp when needed
349 if ( $timestamp != 0 )
351 $query .= ', itime = ' . mysqldate($timestamp);
354 // make sure the correct item is updated
355 $query .= ' WHERE inumber = ' . $itemid;
360 $manager->notify('PostUpdateItem', array('itemid' => $itemid));
362 // when needed, move item and comments to new blog
365 self::move($itemid, $catid);
368 //update the itemOptions
369 $aOptions = requestArray('plugoption');
370 NucleusPlugin::apply_plugin_options($aOptions);
371 $manager->notify('PostPluginOptionsUpdate', array(
388 * Move an item to another blog (no checks)
391 * @param integer $itemid
392 * @param integer $new_catid
395 static public function move($itemid, $new_catid)
399 $itemid = (integer) $itemid;
400 $new_catid = (integer) $new_catid;
401 $new_blogid = getBlogIDFromCatID($new_catid);
407 'destblogid' => $new_blogid,
408 'destcatid' => $new_catid
413 $query = "UPDATE %s SET iblog=%d, icat=%d WHERE inumber=%d";
414 $query = sprintf($query, sql_table('item'), $new_blogid, $new_catid, $itemid);
418 $query = "UPDATE %s SET cblog=%d WHERE citem=%d";
419 $query = sprintf($query, sql_table('comment'), $new_blogid, $itemid);
426 'destblogid' => $new_blogid,
427 'destcatid' => $new_catid
437 * @param integer $itemid
440 static public function delete($itemid)
442 global $manager, $member;
444 $itemid = (integer) $itemid;
447 if ( !$member->canAlterItem($itemid) )
452 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
455 $query = "DELETE FROM %s WHERE inumber=%d";
456 $query = sprintf($query, sql_table('item'), $itemid);
459 // delete the comments associated with the item
460 $query = "DELETE FROM %s WHERE citem=%d";
461 $query = sprintf($query, sql_table('comment'), $itemid);
464 // delete all associated plugin options
465 NucleusPlugin::delete_option_values('item', $itemid);
467 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
474 * Returns true if there is an item with the given ID
477 * @param integer $itemid
478 * @param boolean $future
479 * @param boolean $draft
480 * @return boolean exists or not
482 static public function exists($itemid, $future, $draft)
486 $itemid = (integer) $itemid;
487 $query = 'select * FROM '.sql_table('item').' WHERE inumber='.$itemid;
491 $blogid = getBlogIDFromItemID($itemid);
496 $blog =& $manager->getBlog($blogid);
497 $query .= ' and itime<=' . mysqldate($blog->getCorrectTime());
501 $query .= ' and idraft=0';
503 $result = sql_query($query);
504 return ( sql_num_rows($result) != 0 );
508 * ITEM::createDraftFromRequest()
509 * Tries to create an draft from the data
510 * in the current request (comes from bookmarklet or admin area)
511 * Used by xmlHTTPRequest AutoDraft
513 * Returns an array with status info:
514 * status = 'added', 'error', 'newcategory'
518 * @return array (status = added/error/newcategory, message)
521 static public function createDraftFromRequest()
523 global $member, $manager;
526 * TODO: these values from user agent should be validated but not implemented yet
528 $i_author = $member->getID();
529 $i_body = postVar('body');
530 $i_title = postVar('title');
531 $i_more = postVar('more');
532 $i_closed = intPostVar('closed');
533 $i_catid = postVar('catid');
535 $type = postVar('type');
536 $i_draftid = intPostVar('draftid');
538 if ( $type == 'edit' )
540 $itemid = intPostVar('itemid');
541 $i_blogid = getBlogIDFromItemID($itemid);
545 $i_blogid = intPostVar('blogid');
548 if ( !$member->canAddItem($i_catid) )
550 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
553 if ( !trim($i_body) )
555 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
558 // create new category if needed
559 if ( strstr($i_catid, 'newcat') )
561 // Set in default category
562 $blog =& $manager->getBlog($i_blogid);
563 $i_catid = $blog->getDefaultCategory();
567 // force blogid (must be same as category id)
568 $i_blogid = getBlogIDFromCatID($i_catid);
569 $blog =& $manager->getBlog($i_blogid);
574 if ( $i_draftid > 0 )
576 self::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
577 $itemid = $i_draftid;
581 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
584 return array('status' => 'added', 'draftid' => $itemid);