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 1877 2012-06-17 07:40:11Z sakamocchi $
20 * A class representing an item
27 * actiontype list for handling items
31 static private $actiontypes = array(
32 'addnow', 'adddraft', 'addfuture', 'edit',
33 'changedate', 'backtodrafts', 'delete'
45 * Creates a new ITEM object
48 * @param integer $item_id id for item
51 public function __construct($item_id)
53 $this->itemid = $item_id;
59 * Returns one item with the specific itemid
63 * @param bool $allow_draft
64 * @param bool $allow_future
67 static public function getitem($item_id, $allow_draft, $allow_future)
71 $item_id = (integer) $item_id;
74 'i.idraft AS draft, ' .
75 'i.inumber AS itemid, ' .
76 'i.iclosed AS closed, ' .
77 'i.ititle AS title, ' .
79 'm.mname AS author, ' .
80 'i.iauthor AS authorid, ' .
83 'i.ikarmapos AS karmapos, ' .
84 'i.ikarmaneg AS karmaneg, ' .
86 'i.iblog AS blogid ' .
87 'FROM %s AS i, %s AS m, %s AS b ' .
88 'WHERE i.inumber = %d ' .
89 'AND i.iauthor = m.mnumber ' .
90 'AND i.iblog = b.bnumber ';
92 $query = sprintf($query, sql_table('item'), sql_table('member'), sql_table('blog'), $item_id);
96 $query .= "AND i.idraft = 0 ";
101 $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));
102 $query .= 'AND i.itime <= ' . DB::formatDateTime($blog->getCorrectTime());
105 $query .= ' LIMIT 1';
106 $result = DB::getResult($query);
108 if ( $result->rowCount() != 1 )
113 $aItemInfo = $result->fetch(PDO::FETCH_ASSOC);
114 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
119 * Item::createFromRequest()
120 * Tries to create an item from the data in the current request (comes from
121 * bookmarklet or admin area
125 * @return array (status = added/error/newcategory, message)
128 static public function createFromRequest()
130 global $member, $manager;
133 * TODO: these values from user agent should be validated but not implemented yet
135 $i_author = $member->getID();
136 $i_body = postVar('body');
137 $i_title = postVar('title');
138 $i_more = postVar('more');
139 $i_actiontype = postVar('actiontype');
140 $i_closed = intPostVar('closed');
141 $i_hour = intPostVar('hour');
142 $i_minutes = intPostVar('minutes');
143 $i_month = intPostVar('month');
144 $i_day = intPostVar('day');
145 $i_year = intPostVar('year');
146 $i_catid = postVar('catid');
147 $i_draftid = intPostVar('draftid');
149 if ( !$member->canAddItem($i_catid) )
151 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
154 if ( !in_array($i_actiontype, self::$actiontypes) )
156 $i_actiontype = 'addnow';
159 $i_draft = (integer) ( $i_actiontype == 'adddraft' );
161 if ( !trim($i_body) )
163 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
166 // create new category if needed
167 if ( i18n::strpos($i_catid, 'newcat') === 0 )
170 list($i_blogid) = sscanf($i_catid, "newcat-%d");
173 $blog =& $manager->getBlog($i_blogid);
174 $i_catid = $blog->createNewCategory();
176 // show error when sth goes wrong
179 return array('status' => 'error','message' => 'Could not create new category');
184 // force blogid (must be same as category id)
185 $i_blogid = getBlogIDFromCatID($i_catid);
186 $blog =& $manager->getBlog($i_blogid);
189 if ( $i_actiontype == 'addfuture' )
191 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
193 // make sure the date is in the future, unless we allow past dates
194 if ( (!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()) )
196 $posttime = $blog->getCorrectTime();
203 $posttime = $blog->getCorrectTime();
211 if ( $posttime > $blog->getCorrectTime() )
214 $blog->setFuturePost();
221 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft, $posted);
223 //Setting the itemOptions
224 $aOptions = requestArray('plugoption');
225 NucleusPlugin::apply_plugin_options($aOptions, $itemid);
233 'closed' => $i_closed,
238 $manager->notify('PostPluginOptionsUpdate', $data);
240 if ( $i_draftid > 0 )
242 // delete permission is checked inside Item::delete()
243 self::delete($i_draftid);
247 if ( $i_catid != intRequestVar('catid') )
249 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
252 return array('status' => 'added', 'itemid' => $itemid);
260 * @param integer $itemid item id
261 * @param integer $catid category id
262 * @param string $title title
263 * @param string $body body text
264 * @param string $more more text
265 * @param boolean $closed closed or not
266 * @param boolean $wasdraft previously draft or not
267 * @param boolean $publish published or not
268 * @param timestamp $timestamp timestamp
271 static public function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)
275 $itemid = (integer) $itemid;
276 $closed = (boolean) $closed;
278 // get destination blogid
279 $new_blogid = getBlogIDFromCatID($catid);
280 $old_blogid = getBlogIDFromItemID($itemid);
282 // move will be done on end of method
284 if ( $new_blogid != $old_blogid )
289 $blog =& $manager->getBlog($new_blogid);
291 // begin if: convert line breaks to <br/>
292 if ( $blog->convertBreaks() )
294 $body = addBreaks($body);
295 $more = addBreaks($more);
305 'closed' => &$closed,
308 $manager->notify('PreUpdateItem', $data);
310 // update item itself
311 $query = 'UPDATE ' . sql_table('item')
313 . ' ibody = ' . DB::quoteValue($body) . ','
314 . ' ititle = ' . DB::quoteValue($title) . ','
315 . ' imore = ' . DB::quoteValue($more) . ','
316 . ' iclosed = ' . intval($closed) . ','
317 . ' icat = ' . intval($catid);
319 // if we received an updated timestamp that is in the past, but past posting is not allowed,
320 // reject that date change (timestamp = 0 will make sure the current date is kept)
321 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()) )
326 // begin if: post is in the future
327 if ( $timestamp > $blog->getCorrectTime(time()) )
330 $query .= ', iposted = 0';
335 $query .= ', iposted = 1';
338 if ( $wasdraft && $publish )
340 // set timestamp to current date only if it's not a future item
341 // draft items have timestamp == 0
342 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
343 $query .= ', idraft = 0';
345 if ( $timestamp == 0 )
347 $timestamp = $blog->getCorrectTime();
350 // send new item notification
351 if ( !$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem() )
353 $blog->sendNewItemNotification($itemid, $title, $body);
357 // save back to drafts
358 if ( !$wasdraft && !$publish )
360 $query .= ', idraft = 1';
361 // set timestamp back to zero for a draft
362 $query .= ', itime = ' . DB::formatDateTime($timestamp);
365 // update timestamp when needed
366 if ( $timestamp != 0 )
368 $query .= ', itime = ' . DB::formatDateTime($timestamp);
371 // make sure the correct item is updated
372 $query .= ' WHERE inumber = ' . $itemid;
377 $manager->notify('PostUpdateItem', array('itemid' => $itemid));
379 // when needed, move item and comments to new blog
382 self::move($itemid, $catid);
385 //update the itemOptions
386 $aOptions = requestArray('plugoption');
387 NucleusPlugin::apply_plugin_options($aOptions);
399 $manager->notify('PostPluginOptionsUpdate', $data);
405 * Move an item to another blog (no checks)
408 * @param integer $itemid
409 * @param integer $new_catid
412 static public function move($itemid, $new_catid)
416 $itemid = (integer) $itemid;
417 $new_catid = (integer) $new_catid;
418 $new_blogid = getBlogIDFromCatID($new_catid);
422 'destblogid' => $new_blogid,
423 'destcatid' => $new_catid
425 $manager->notify('PreMoveItem', $data);
428 $query = "UPDATE %s SET iblog=%d, icat=%d WHERE inumber=%d";
429 $query = sprintf($query, sql_table('item'), $new_blogid, $new_catid, $itemid);
433 $query = "UPDATE %s SET cblog=%d WHERE citem=%d";
434 $query = sprintf($query, sql_table('comment'), $new_blogid, $itemid);
439 'destblogid' => $new_blogid,
440 'destcatid' => $new_catid
442 $manager->notify('PostMoveItem', $data);
450 * @param integer $itemid
453 static public function delete($itemid)
455 global $manager, $member;
457 $itemid = (integer) $itemid;
460 if ( !$member->canAlterItem($itemid) )
465 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
468 $query = "DELETE FROM %s WHERE inumber=%d;";
469 $query = sprintf($query, sql_table('item'), $itemid);
472 // delete the comments associated with the item
473 $query = "DELETE FROM %s WHERE citem=%d;";
474 $query = sprintf($query, sql_table('comment'), $itemid);
477 // delete all associated plugin options
478 NucleusPlugin::delete_option_values('item', $itemid);
480 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
487 * Returns true if there is an item with the given ID
490 * @param integer $itemid
491 * @param boolean $future
492 * @param boolean $draft
493 * @return boolean exists or not
495 static public function exists($itemid, $future, $draft)
499 $itemid = (integer) $itemid;
501 $query = 'SELECT * FROM %s WHERE inumber=%d';
502 $query = sprintf($query, sql_table('item'), $itemid);
506 $blogid = getBlogIDFromItemID($itemid);
511 $blog =& $manager->getBlog($blogid);
512 $query .= ' AND itime<=' . DB::formatDateTime($blog->getCorrectTime());
517 $query .= ' AND idraft=0';
520 $result = DB::getResult($query);
521 return ( $result->rowCount() != 0 );
525 * Item::createDraftFromRequest()
526 * Tries to create an draft from the data
527 * in the current request (comes from bookmarklet or admin area)
528 * Used by xmlHTTPRequest AutoDraft
530 * Returns an array with status info:
531 * status = 'added', 'error', 'newcategory'
535 * @return array (status = added/error/newcategory, message)
537 static public function createDraftFromRequest()
539 global $member, $manager;
542 * TODO: these values from user agent should be validated but not implemented yet
544 $i_author = $member->getID();
545 $i_body = postVar('body');
546 $i_title = postVar('title');
547 $i_more = postVar('more');
548 $i_closed = intPostVar('closed');
549 $i_catid = postVar('catid');
551 $type = postVar('type');
552 $i_draftid = intPostVar('draftid');
554 if ( $type == 'edit' )
556 $itemid = intPostVar('itemid');
557 $i_blogid = getBlogIDFromItemID($itemid);
561 $i_blogid = intPostVar('blogid');
564 if ( !$member->canAddItem($i_catid) )
566 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
569 if ( !trim($i_body) )
571 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
574 // create new category if needed
575 if ( i18n::strpos($i_catid,'newcat') === 0 )
577 // Set in default category
578 $blog =& $manager->getBlog($i_blogid);
579 $i_catid = $blog->getDefaultCategory();
583 // force blogid (must be same as category id)
584 $i_blogid = getBlogIDFromCatID($i_catid);
585 $blog =& $manager->getBlog($i_blogid);
590 if ( $i_draftid > 0 )
592 self::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
593 $itemid = $i_draftid;
597 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
600 return array('status' => 'added', 'draftid' => $itemid);