4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2012 The Nucleus Group
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation; either version 2
\r
10 * of the License, or (at your option) any later version.
\r
11 * (see nucleus/documentation/index.html#license for more info)
\r
14 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
15 * @copyright Copyright (C) 2002-2012 The Nucleus Group
\r
16 * @version $Id: ITEM.php 1668 2012-02-19 14:36:44Z sakamocchi $
\r
20 * A class representing an item
\r
26 * Item::$actiontypes
\r
27 * actiontype list for handling items
\r
31 static private $actiontypes = array(
\r
32 'addnow', 'adddraft', 'addfuture', 'edit',
\r
33 'changedate', 'backtodrafts', 'delete'
\r
44 * Item::__construct()
\r
45 * Creates a new ITEM object
\r
48 * @param integer $item_id id for item
\r
51 public function __construct($item_id)
\r
53 $this->itemid = $item_id;
\r
59 * Returns one item with the specific itemid
\r
62 * @param int $item_id
\r
63 * @param bool $allow_draft
\r
64 * @param bool $allow_future
\r
67 static public function getitem($item_id, $allow_draft, $allow_future)
\r
71 $item_id = (integer) $item_id;
\r
73 $query = 'SELECT ' .
\r
74 'i.idraft AS draft, ' .
\r
75 'i.inumber AS itemid, ' .
\r
76 'i.iclosed AS closed, ' .
\r
77 'i.ititle AS title, ' .
\r
78 'i.ibody AS body, ' .
\r
79 'm.mname AS author, ' .
\r
80 'i.iauthor AS authorid, ' .
\r
82 'i.imore AS more, ' .
\r
83 'i.ikarmapos AS karmapos, ' .
\r
84 'i.ikarmaneg AS karmaneg, ' .
\r
85 'i.icat AS catid, ' .
\r
86 'i.iblog AS blogid ' .
\r
87 'FROM %s AS i, %s AS m, %s AS b ' .
\r
88 'WHERE i.inumber = %d ' .
\r
89 'AND i.iauthor = m.mnumber ' .
\r
90 'AND i.iblog = b.bnumber ';
\r
92 $query = sprintf($query, sql_table('item'), sql_table('member'), sql_table('blog'), $item_id);
\r
94 if ( !$allow_draft )
\r
96 $query .= "AND i.idraft = 0 ";
\r
99 if ( !$allow_future )
\r
101 $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));
\r
102 $query .= 'AND i.itime <= ' . DB::formatDateTime($blog->getCorrectTime());
\r
105 $query .= ' LIMIT 1';
\r
106 $result = DB::getResult($query);
\r
108 if ( $result->rowCount() == 1 )
\r
110 $aItemInfo = $result->fetch(PDO::FETCH_ASSOC);
\r
111 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
\r
118 * Item::createFromRequest()
\r
119 * Tries to create an item from the data in the current request (comes from
\r
120 * bookmarklet or admin area
\r
124 * @return array (status = added/error/newcategory, message)
\r
127 static public function createFromRequest()
\r
129 global $member, $manager;
\r
132 * TODO: these values from user agent should be validated but not implemented yet
\r
134 $i_author = $member->getID();
\r
135 $i_body = postVar('body');
\r
136 $i_title = postVar('title');
\r
137 $i_more = postVar('more');
\r
138 $i_actiontype = postVar('actiontype');
\r
139 $i_closed = intPostVar('closed');
\r
140 $i_hour = intPostVar('hour');
\r
141 $i_minutes = intPostVar('minutes');
\r
142 $i_month = intPostVar('month');
\r
143 $i_day = intPostVar('day');
\r
144 $i_year = intPostVar('year');
\r
145 $i_catid = postVar('catid');
\r
146 $i_draftid = intPostVar('draftid');
\r
148 if ( !$member->canAddItem($i_catid) )
\r
150 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
153 if ( !in_array($i_actiontype, self::$actiontypes) )
\r
155 $i_actiontype = 'addnow';
\r
158 $i_draft = (integer) ( $i_actiontype == 'adddraft' );
\r
160 if ( !trim($i_body) )
\r
162 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
165 // create new category if needed
\r
166 if ( i18n::strpos($i_catid, 'newcat') === 0 )
\r
169 list($i_blogid) = sscanf($i_catid, "newcat-%d");
\r
172 $blog =& $manager->getBlog($i_blogid);
\r
173 $i_catid = $blog->createNewCategory();
\r
175 // show error when sth goes wrong
\r
178 return array('status' => 'error','message' => 'Could not create new category');
\r
183 // force blogid (must be same as category id)
\r
184 $i_blogid = getBlogIDFromCatID($i_catid);
\r
185 $blog =& $manager->getBlog($i_blogid);
\r
188 if ( $i_actiontype == 'addfuture' )
\r
190 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
\r
192 // make sure the date is in the future, unless we allow past dates
\r
193 if ( (!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()) )
\r
195 $posttime = $blog->getCorrectTime();
\r
202 $posttime = $blog->getCorrectTime();
\r
210 if ( $posttime > $blog->getCorrectTime() )
\r
213 $blog->setFuturePost();
\r
220 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft, $posted);
\r
222 //Setting the itemOptions
\r
223 $aOptions = requestArray('plugoption');
\r
224 NucleusPlugin::apply_plugin_options($aOptions, $itemid);
\r
226 'context' => 'item',
\r
227 'itemid' => $itemid,
\r
229 'title' => $i_title,
\r
232 'closed' => $i_closed,
\r
233 'catid' => $i_catid
\r
237 $manager->notify('PostPluginOptionsUpdate', $data);
\r
239 if ( $i_draftid > 0 )
\r
241 // delete permission is checked inside Item::delete()
\r
242 self::delete($i_draftid);
\r
246 if ( $i_catid != intRequestVar('catid') )
\r
248 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
\r
251 return array('status' => 'added', 'itemid' => $itemid);
\r
259 * @param integer $itemid item id
\r
260 * @param integer $catid category id
\r
261 * @param string $title title
\r
262 * @param string $body body text
\r
263 * @param string $more more text
\r
264 * @param boolean $closed closed or not
\r
265 * @param boolean $wasdraft previously draft or not
\r
266 * @param boolean $publish published or not
\r
267 * @param timestamp $timestamp timestamp
\r
270 static public function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)
\r
274 $itemid = (integer) $itemid;
\r
275 $closed = (boolean) $closed;
\r
277 // get destination blogid
\r
278 $new_blogid = getBlogIDFromCatID($catid);
\r
279 $old_blogid = getBlogIDFromItemID($itemid);
\r
281 // move will be done on end of method
\r
283 if ( $new_blogid != $old_blogid )
\r
288 $blog =& $manager->getBlog($new_blogid);
\r
290 // begin if: convert line breaks to <br/>
\r
291 if ( $blog->convertBreaks() )
\r
293 $body = addBreaks($body);
\r
294 $more = addBreaks($more);
\r
299 'itemid' => $itemid,
\r
300 'title' => &$title,
\r
304 'closed' => &$closed,
\r
307 $manager->notify('PreUpdateItem', $data);
\r
309 // update item itself
\r
310 $query = 'UPDATE ' . sql_table('item')
\r
312 . ' ibody = ' . DB::quoteValue($body) . ','
\r
313 . ' ititle = ' . DB::quoteValue($title) . ','
\r
314 . ' imore = ' . DB::quoteValue($more) . ','
\r
315 . ' iclosed = ' . intval($closed) . ','
\r
316 . ' icat = ' . intval($catid);
\r
318 // if we received an updated timestamp that is in the past, but past posting is not allowed,
\r
319 // reject that date change (timestamp = 0 will make sure the current date is kept)
\r
320 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()) )
\r
325 // begin if: post is in the future
\r
326 if ( $timestamp > $blog->getCorrectTime(time()) )
\r
329 $query .= ', iposted = 0';
\r
334 $query .= ', iposted = 1';
\r
337 if ( $wasdraft && $publish )
\r
339 // set timestamp to current date only if it's not a future item
\r
340 // draft items have timestamp == 0
\r
341 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
\r
342 $query .= ', idraft = 0';
\r
344 if ( $timestamp == 0 )
\r
346 $timestamp = $blog->getCorrectTime();
\r
349 // send new item notification
\r
350 if ( !$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem() )
\r
352 $blog->sendNewItemNotification($itemid, $title, $body);
\r
356 // save back to drafts
\r
357 if ( !$wasdraft && !$publish )
\r
359 $query .= ', idraft = 1';
\r
360 // set timestamp back to zero for a draft
\r
361 $query .= ', itime = ' . DB::formatDateTime($timestamp);
\r
364 // update timestamp when needed
\r
365 if ( $timestamp != 0 )
\r
367 $query .= ', itime = ' . DB::formatDateTime($timestamp);
\r
370 // make sure the correct item is updated
\r
371 $query .= ' WHERE inumber = ' . $itemid;
\r
374 DB::execute($query);
\r
376 $manager->notify('PostUpdateItem', array('itemid' => $itemid));
\r
378 // when needed, move item and comments to new blog
\r
381 self::move($itemid, $catid);
\r
384 //update the itemOptions
\r
385 $aOptions = requestArray('plugoption');
\r
386 NucleusPlugin::apply_plugin_options($aOptions);
\r
388 'context' => 'item',
\r
389 'itemid' => $itemid,
\r
394 'closed' => $closed,
\r
398 $manager->notify('PostPluginOptionsUpdate', $data);
\r
404 * Move an item to another blog (no checks)
\r
407 * @param integer $itemid
\r
408 * @param integer $new_catid
\r
411 static public function move($itemid, $new_catid)
\r
415 $itemid = (integer) $itemid;
\r
416 $new_catid = (integer) $new_catid;
\r
417 $new_blogid = getBlogIDFromCatID($new_catid);
\r
420 'itemid' => $itemid,
\r
421 'destblogid' => $new_blogid,
\r
422 'destcatid' => $new_catid
\r
424 $manager->notify('PreMoveItem', $data);
\r
426 // update item table
\r
427 $query = "UPDATE %s SET iblog=%d, icat=%d WHERE inumber=%d";
\r
428 $query = sprintf($query, sql_table('item'), $new_blogid, $new_catid, $itemid);
\r
429 DB::execute($query);
\r
432 $query = "UPDATE %s SET cblog=%d WHERE citem=%d";
\r
433 $query = sprintf($query, sql_table('comment'), $new_blogid, $itemid);
\r
434 DB::execute($query);
\r
437 'itemid' => $itemid,
\r
438 'destblogid' => $new_blogid,
\r
439 'destcatid' => $new_catid
\r
441 $manager->notify('PostMoveItem', $data);
\r
449 * @param integer $itemid
\r
452 static public function delete($itemid)
\r
454 global $manager, $member;
\r
456 $itemid = (integer) $itemid;
\r
458 // check permission
\r
459 if ( !$member->canAlterItem($itemid) )
\r
464 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
\r
467 $query = "DELETE FROM %s WHERE inumber=%d;";
\r
468 $query = sprintf($query, sql_table('item'), $itemid);
\r
469 DB::execute($query);
\r
471 // delete the comments associated with the item
\r
472 $query = "DELETE FROM %s WHERE citem=%d;";
\r
473 $query = sprintf($query, sql_table('comment'), $itemid);
\r
474 DB::execute($query);
\r
476 // delete all associated plugin options
\r
477 NucleusPlugin::delete_option_values('item', $itemid);
\r
479 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
\r
486 * Returns true if there is an item with the given ID
\r
489 * @param integer $itemid
\r
490 * @param boolean $future
\r
491 * @param boolean $draft
\r
492 * @return boolean exists or not
\r
494 static public function exists($itemid, $future, $draft)
\r
498 $itemid = (integer) $itemid;
\r
500 $query = 'SELECT * FROM %s WHERE inumber=%d';
\r
501 $query = sprintf($query, sql_table('item'), $itemid);
\r
505 $blogid = getBlogIDFromItemID($itemid);
\r
510 $blog =& $manager->getBlog($blogid);
\r
511 $query .= ' AND itime<=' . DB::formatDateTime($blog->getCorrectTime());
\r
516 $query .= ' AND idraft=0';
\r
519 $result = DB::getResult($query);
\r
520 return ( $result->rowCount() != 0 );
\r
524 * Item::createDraftFromRequest()
\r
525 * Tries to create an draft from the data
\r
526 * in the current request (comes from bookmarklet or admin area)
\r
527 * Used by xmlHTTPRequest AutoDraft
\r
529 * Returns an array with status info:
\r
530 * status = 'added', 'error', 'newcategory'
\r
534 * @return array (status = added/error/newcategory, message)
\r
536 static public function createDraftFromRequest()
\r
538 global $member, $manager;
\r
541 * TODO: these values from user agent should be validated but not implemented yet
\r
543 $i_author = $member->getID();
\r
544 $i_body = postVar('body');
\r
545 $i_title = postVar('title');
\r
546 $i_more = postVar('more');
\r
547 $i_closed = intPostVar('closed');
\r
548 $i_catid = postVar('catid');
\r
550 $type = postVar('type');
\r
551 $i_draftid = intPostVar('draftid');
\r
553 if ( $type == 'edit' )
\r
555 $itemid = intPostVar('itemid');
\r
556 $i_blogid = getBlogIDFromItemID($itemid);
\r
560 $i_blogid = intPostVar('blogid');
\r
563 if ( !$member->canAddItem($i_catid) )
\r
565 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
568 if ( !trim($i_body) )
\r
570 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
573 // create new category if needed
\r
574 if ( i18n::strpos($i_catid,'newcat') === 0 )
\r
576 // Set in default category
\r
577 $blog =& $manager->getBlog($i_blogid);
\r
578 $i_catid = $blog->getDefaultCategory();
\r
582 // force blogid (must be same as category id)
\r
583 $i_blogid = getBlogIDFromCatID($i_catid);
\r
584 $blog =& $manager->getBlog($i_blogid);
\r
589 if ( $i_draftid > 0 )
\r
591 self::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
\r
592 $itemid = $i_draftid;
\r
596 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
\r
599 return array('status' => 'added', 'draftid' => $itemid);
\r