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()
49 * Returns one item with the specific itemid
52 * @param bool $allow_draft
53 * @param bool $allow_future
57 static public function getitem($item_id, $allow_draft, $allow_future)
61 $item_id = intval($item_id);
64 'i.idraft AS draft, ' .
65 'i.inumber AS itemid, ' .
66 'i.iclosed AS closed, ' .
67 'i.ititle AS title, ' .
69 'm.mname AS author, ' .
70 'i.iauthor AS authorid, ' .
73 'i.ikarmapos AS karmapos, ' .
74 'i.ikarmaneg AS karmaneg, ' .
76 'i.iblog AS blogid ' .
77 'FROM %s AS i, %s AS m, %s AS b ' .
78 'WHERE i.inumber = %d ' .
79 'AND i.iauthor = m.mnumber ' .
80 'AND i.iblog = b.bnumber ';
82 $query = sprintf($query, sql_table('item'), sql_table('member'), sql_table('blog'), $item_id);
86 $query .= "AND i.idraft = 0 ";
91 $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));
92 $query .= "AND `i`.`itime` <= '" . i18n::formatted_datetime('mysql', $blog->getCorrectTime()) ."'";
96 $result = sql_query($query);
98 if ( sql_num_rows($result) == 1 )
100 $aItemInfo = sql_fetch_assoc($result);
101 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
111 * ITEM::createFromRequest()
112 * Tries to create an item from the data in the current request (comes from
113 * bookmarklet or admin area
117 * @return array (status = added/error/newcategory, message)
120 static public function createFromRequest()
122 global $member, $manager;
125 * TODO: these values from user agent should be validated but not implemented yet
127 $i_author = $member->getID();
128 $i_body = postVar('body');
129 $i_title = postVar('title');
130 $i_more = postVar('more');
131 $i_actiontype = postVar('actiontype');
132 $i_closed = intPostVar('closed');
133 $i_hour = intPostVar('hour');
134 $i_minutes = intPostVar('minutes');
135 $i_month = intPostVar('month');
136 $i_day = intPostVar('day');
137 $i_year = intPostVar('year');
138 $i_catid = postVar('catid');
139 $i_draftid = intPostVar('draftid');
141 if ( !$member->canAddItem($i_catid) )
143 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
146 if ( !in_array($i_actiontype, self::$actiontypes) )
148 $i_actiontype = 'addnow';
151 $i_draft = (integer) ( $i_actiontype == 'adddraft' );
153 if ( !trim($i_body) )
155 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
158 // create new category if needed
159 if ( i18n::strpos($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');
176 // force blogid (must be same as category id)
177 $i_blogid = getBlogIDFromCatID($i_catid);
178 $blog =& $manager->getBlog($i_blogid);
181 if ( $i_actiontype == 'addfuture' )
183 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
185 // make sure the date is in the future, unless we allow past dates
186 if ( (!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()) )
188 $posttime = $blog->getCorrectTime();
193 // time with offset, or 0 for drafts
194 $posttime = $i_draft ? 0 : $blog->getCorrectTime();
197 if ( $posttime > $blog->getCorrectTime() )
200 $blog->setFuturePost();
207 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft, $posted);
209 //Setting the itemOptions
210 $aOptions = requestArray('plugoption');
211 NucleusPlugin::apply_plugin_options($aOptions, $itemid);
212 $manager->notify('PostPluginOptionsUpdate', array(
219 'closed' => $i_closed,
225 if ( $i_draftid > 0 )
227 // delete permission is checked inside ITEM::delete()
228 self::delete($i_draftid);
232 if ( $i_catid != intRequestVar('catid') )
234 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
238 return array('status' => 'added', 'itemid' => $itemid);
247 * @param integer $itemid item id
248 * @param integer $catid category id
249 * @param string $title title
250 * @param string $body body text
251 * @param string $more more text
252 * @param boolean $closed closed or not
253 * @param boolean $wasdraft previously draft or not
254 * @param boolean $publish published or not
255 * @param timestamp $timestamp timestamp
259 static public function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)
263 $itemid = (integer) $itemid;
264 $closed = (boolean) $closed;
266 // get destination blogid
267 $new_blogid = getBlogIDFromCatID($catid);
268 $old_blogid = getBlogIDFromItemID($itemid);
270 // move will be done on end of method
272 if ( $new_blogid != $old_blogid )
277 $blog =& $manager->getBlog($new_blogid);
279 // begin if: convert line breaks to <br/>
280 if ( $blog->convertBreaks() )
282 $body = addBreaks($body);
283 $more = addBreaks($more);
287 $manager->notify('PreUpdateItem', array(
293 'closed' => &$closed,
298 // update item itself
299 $query = 'UPDATE ' . sql_table('item')
301 . " ibody = '" . sql_real_escape_string($body) . "',"
302 . " ititle = '" . sql_real_escape_string($title) . "',"
303 . " imore = '" . sql_real_escape_string($more) . "',"
304 . " iclosed = " . intval($closed) . ","
305 . " icat = " . intval($catid);
307 // 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)
308 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()) )
313 // begin if: post is in the future
314 if ( $timestamp > $blog->getCorrectTime(time()) )
317 $query .= ', iposted = 0';
322 $query .= ', iposted = 1';
325 if ( $wasdraft && $publish )
327 // set timestamp to current date only if it's not a future item
328 // draft items have timestamp == 0
329 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
330 $query .= ', idraft = 0';
332 if ( $timestamp == 0 )
334 $timestamp = $blog->getCorrectTime();
337 // send new item notification
338 if ( !$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem() )
340 $blog->sendNewItemNotification($itemid, $title, $body);
344 // save back to drafts
345 if ( !$wasdraft && !$publish )
347 $query .= ', idraft = 1';
348 // set timestamp back to zero for a draft
349 $query .= ", itime = '" . i18n::formatted_datetime('mysql', $timestamp) ."'";
352 // update timestamp when needed
353 if ( $timestamp != 0 )
355 $query .= ", itime = '" . i18n::formatted_datetime('mysql', $timestamp) ."'";
358 // make sure the correct item is updated
359 $query .= ' WHERE inumber = ' . $itemid;
364 $manager->notify('PostUpdateItem', array('itemid' => $itemid));
366 // when needed, move item and comments to new blog
369 self::move($itemid, $catid);
372 //update the itemOptions
373 $aOptions = requestArray('plugoption');
374 NucleusPlugin::apply_plugin_options($aOptions);
375 $manager->notify('PostPluginOptionsUpdate', array(
392 * Move an item to another blog (no checks)
395 * @param integer $itemid
396 * @param integer $new_catid
399 static public function move($itemid, $new_catid)
403 $itemid = (integer) $itemid;
404 $new_catid = (integer) $new_catid;
405 $new_blogid = getBlogIDFromCatID($new_catid);
411 'destblogid' => $new_blogid,
412 'destcatid' => $new_catid
417 $query = "UPDATE %s SET iblog=%d, icat=%d WHERE inumber=%d";
418 $query = sprintf($query, sql_table('item'), $new_blogid, $new_catid, $itemid);
422 $query = "UPDATE %s SET cblog=%d WHERE citem=%d";
423 $query = sprintf($query, sql_table('comment'), $new_blogid, $itemid);
430 'destblogid' => $new_blogid,
431 'destcatid' => $new_catid
441 * @param integer $itemid
444 static public function delete($itemid)
446 global $manager, $member;
448 $itemid = (integer) $itemid;
451 if ( !$member->canAlterItem($itemid) )
456 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
459 $query = "DELETE FROM %s WHERE inumber=%d";
460 $query = sprintf($query, sql_table('item'), $itemid);
463 // delete the comments associated with the item
464 $query = "DELETE FROM %s WHERE citem=%d";
465 $query = sprintf($query, sql_table('comment'), $itemid);
468 // delete all associated plugin options
469 NucleusPlugin::delete_option_values('item', $itemid);
471 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
478 * Returns true if there is an item with the given ID
481 * @param integer $itemid
482 * @param boolean $future
483 * @param boolean $draft
484 * @return boolean exists or not
487 static public function exists($itemid, $future, $draft)
491 $itemid = (integer) $itemid;
492 $query = 'select * FROM '.sql_table('item').' WHERE inumber='.$itemid;
496 $blogid = getBlogIDFromItemID($itemid);
501 $blog =& $manager->getBlog($blogid);
502 $query .= " and itime<='" . i18n::formatted_datetime('mysql', $blog->getCorrectTime()) ."'";
506 $query .= ' and idraft=0';
508 $result = sql_query($query);
509 return ( sql_num_rows($result) != 0 );
513 * ITEM::createDraftFromRequest()
514 * Tries to create an draft from the data
515 * in the current request (comes from bookmarklet or admin area)
516 * Used by xmlHTTPRequest AutoDraft
518 * Returns an array with status info:
519 * status = 'added', 'error', 'newcategory'
523 * @return array (status = added/error/newcategory, message)
526 static public function createDraftFromRequest()
528 global $member, $manager;
531 * TODO: these values from user agent should be validated but not implemented yet
533 $i_author = $member->getID();
534 $i_body = postVar('body');
535 $i_title = postVar('title');
536 $i_more = postVar('more');
537 $i_closed = intPostVar('closed');
538 $i_catid = postVar('catid');
540 $type = postVar('type');
541 $i_draftid = intPostVar('draftid');
543 if ( $type == 'edit' )
545 $itemid = intPostVar('itemid');
546 $i_blogid = getBlogIDFromItemID($itemid);
550 $i_blogid = intPostVar('blogid');
553 if ( !$member->canAddItem($i_catid) )
555 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
558 if ( !trim($i_body) )
560 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
563 // create new category if needed
564 if ( strstr($i_catid, 'newcat') )
566 // Set in default category
567 $blog =& $manager->getBlog($i_blogid);
568 $i_catid = $blog->getDefaultCategory();
572 // force blogid (must be same as category id)
573 $i_blogid = getBlogIDFromCatID($i_catid);
574 $blog =& $manager->getBlog($i_blogid);
579 if ( $i_draftid > 0 )
581 self::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
582 $itemid = $i_draftid;
586 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
589 return array('status' => 'added', 'draftid' => $itemid);