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
\r
32 = array('addnow', 'adddraft', 'addfuture', 'edit', 'changedate', 'backtodrafts', 'delete');
\r
43 * Item::__construct()
\r
44 * Creates a new ITEM object
\r
47 * @param integer $item_id id for item
\r
50 public function __construct($item_id)
\r
52 $this->itemid = $item_id;
\r
58 * Returns one item with the specific itemid
\r
60 * @param int $item_id
\r
61 * @param bool $allow_draft
\r
62 * @param bool $allow_future
\r
66 static public function getitem($item_id, $allow_draft, $allow_future)
\r
70 $item_id = (integer) $item_id;
\r
72 $query = 'SELECT ' .
\r
73 'i.idraft AS draft, ' .
\r
74 'i.inumber AS itemid, ' .
\r
75 'i.iclosed AS closed, ' .
\r
76 'i.ititle AS title, ' .
\r
77 'i.ibody AS body, ' .
\r
78 'm.mname AS author, ' .
\r
79 'i.iauthor AS authorid, ' .
\r
81 'i.imore AS more, ' .
\r
82 'i.ikarmapos AS karmapos, ' .
\r
83 'i.ikarmaneg AS karmaneg, ' .
\r
84 'i.icat AS catid, ' .
\r
85 'i.iblog AS blogid ' .
\r
86 'FROM %s AS i, %s AS m, %s AS b ' .
\r
87 'WHERE i.inumber = %d ' .
\r
88 'AND i.iauthor = m.mnumber ' .
\r
89 'AND i.iblog = b.bnumber ';
\r
91 $query = sprintf($query, sql_table('item'), sql_table('member'), sql_table('blog'), $item_id);
\r
93 if ( !$allow_draft )
\r
95 $query .= "AND i.idraft = 0 ";
\r
98 if ( !$allow_future )
\r
100 $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));
\r
101 $query .= "AND i.itime <= '" . i18n::formatted_datetime('mysql', $blog->getCorrectTime()) ."'";
\r
104 $query .= ' LIMIT 1';
\r
105 $result = DB::getResult($query);
\r
107 if ( $result->rowCount() == 1 )
\r
109 $aItemInfo = $result->fetch(PDO::FETCH_ASSOC);
\r
110 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
\r
120 * Item::createFromRequest()
\r
121 * Tries to create an item from the data in the current request (comes from
\r
122 * bookmarklet or admin area
\r
126 * @return array (status = added/error/newcategory, message)
\r
129 static public function createFromRequest()
\r
131 global $member, $manager;
\r
134 * TODO: these values from user agent should be validated but not implemented yet
\r
136 $i_author = $member->getID();
\r
137 $i_body = postVar('body');
\r
138 $i_title = postVar('title');
\r
139 $i_more = postVar('more');
\r
140 $i_actiontype = postVar('actiontype');
\r
141 $i_closed = intPostVar('closed');
\r
142 $i_hour = intPostVar('hour');
\r
143 $i_minutes = intPostVar('minutes');
\r
144 $i_month = intPostVar('month');
\r
145 $i_day = intPostVar('day');
\r
146 $i_year = intPostVar('year');
\r
147 $i_catid = postVar('catid');
\r
148 $i_draftid = intPostVar('draftid');
\r
150 if ( !$member->canAddItem($i_catid) )
\r
152 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
155 if ( !in_array($i_actiontype, self::$actiontypes) )
\r
157 $i_actiontype = 'addnow';
\r
160 $i_draft = (integer) ( $i_actiontype == 'adddraft' );
\r
162 if ( !trim($i_body) )
\r
164 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
167 // create new category if needed
\r
168 if ( i18n::strpos($i_catid, 'newcat') )
\r
171 list($i_blogid) = sscanf($i_catid, "newcat-%d");
\r
174 $blog =& $manager->getBlog($i_blogid);
\r
175 $i_catid = $blog->createNewCategory();
\r
177 // show error when sth goes wrong
\r
180 return array('status' => 'error','message' => 'Could not create new category');
\r
185 // force blogid (must be same as category id)
\r
186 $i_blogid = getBlogIDFromCatID($i_catid);
\r
187 $blog =& $manager->getBlog($i_blogid);
\r
190 if ( $i_actiontype == 'addfuture' )
\r
192 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
\r
194 // make sure the date is in the future, unless we allow past dates
\r
195 if ( (!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()) )
\r
197 $posttime = $blog->getCorrectTime();
\r
204 $posttime = $blog->getCorrectTime();
\r
212 if ( $posttime > $blog->getCorrectTime() )
\r
215 $blog->setFuturePost();
\r
222 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft, $posted);
\r
224 //Setting the itemOptions
\r
225 $aOptions = requestArray('plugoption');
\r
226 NucleusPlugin::apply_plugin_options($aOptions, $itemid);
\r
227 $manager->notify('PostPluginOptionsUpdate', array(
\r
228 'context' => 'item',
\r
229 'itemid' => $itemid,
\r
231 'title' => $i_title,
\r
234 'closed' => $i_closed,
\r
235 'catid' => $i_catid
\r
240 if ( $i_draftid > 0 )
\r
242 // delete permission is checked inside Item::delete()
\r
243 self::delete($i_draftid);
\r
247 if ( $i_catid != intRequestVar('catid') )
\r
249 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
\r
253 return array('status' => 'added', 'itemid' => $itemid);
\r
262 * @param integer $itemid item id
\r
263 * @param integer $catid category id
\r
264 * @param string $title title
\r
265 * @param string $body body text
\r
266 * @param string $more more text
\r
267 * @param boolean $closed closed or not
\r
268 * @param boolean $wasdraft previously draft or not
\r
269 * @param boolean $publish published or not
\r
270 * @param timestamp $timestamp timestamp
\r
274 static public function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)
\r
278 $itemid = (integer) $itemid;
\r
279 $closed = (boolean) $closed;
\r
281 // get destination blogid
\r
282 $new_blogid = getBlogIDFromCatID($catid);
\r
283 $old_blogid = getBlogIDFromItemID($itemid);
\r
285 // move will be done on end of method
\r
287 if ( $new_blogid != $old_blogid )
\r
292 $blog =& $manager->getBlog($new_blogid);
\r
294 // begin if: convert line breaks to <br/>
\r
295 if ( $blog->convertBreaks() )
\r
297 $body = addBreaks($body);
\r
298 $more = addBreaks($more);
\r
302 $manager->notify('PreUpdateItem', array(
\r
303 'itemid' => $itemid,
\r
304 'title' => &$title,
\r
308 'closed' => &$closed,
\r
313 // update item itself
\r
314 $query = 'UPDATE ' . sql_table('item')
\r
316 . ' ibody = ' . DB::quoteValue($body) . ','
\r
317 . ' ititle = ' . DB::quoteValue($title) . ','
\r
318 . ' imore = ' . DB::quoteValue($more) . ','
\r
319 . ' iclosed = ' . intval($closed) . ','
\r
320 . ' icat = ' . intval($catid);
\r
322 // 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)
\r
323 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()) )
\r
328 // begin if: post is in the future
\r
329 if ( $timestamp > $blog->getCorrectTime(time()) )
\r
332 $query .= ', iposted = 0';
\r
337 $query .= ', iposted = 1';
\r
340 if ( $wasdraft && $publish )
\r
342 // set timestamp to current date only if it's not a future item
\r
343 // draft items have timestamp == 0
\r
344 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
\r
345 $query .= ', idraft = 0';
\r
347 if ( $timestamp == 0 )
\r
349 $timestamp = $blog->getCorrectTime();
\r
352 // send new item notification
\r
353 if ( !$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem() )
\r
355 $blog->sendNewItemNotification($itemid, $title, $body);
\r
359 // save back to drafts
\r
360 if ( !$wasdraft && !$publish )
\r
362 $query .= ', idraft = 1';
\r
363 // set timestamp back to zero for a draft
\r
364 $query .= ", itime = '" . i18n::formatted_datetime('mysql', $timestamp) ."'";
\r
367 // update timestamp when needed
\r
368 if ( $timestamp != 0 )
\r
370 $query .= ", itime = '" . i18n::formatted_datetime('mysql', $timestamp) ."'";
\r
373 // make sure the correct item is updated
\r
374 $query .= ' WHERE inumber = ' . $itemid;
\r
377 DB::execute($query);
\r
379 $manager->notify('PostUpdateItem', array('itemid' => $itemid));
\r
381 // when needed, move item and comments to new blog
\r
384 self::move($itemid, $catid);
\r
387 //update the itemOptions
\r
388 $aOptions = requestArray('plugoption');
\r
389 NucleusPlugin::apply_plugin_options($aOptions);
\r
390 $manager->notify('PostPluginOptionsUpdate', array(
\r
391 'context' => 'item',
\r
392 'itemid' => $itemid,
\r
397 'closed' => $closed,
\r
407 * Move an item to another blog (no checks)
\r
410 * @param integer $itemid
\r
411 * @param integer $new_catid
\r
414 static public function move($itemid, $new_catid)
\r
418 $itemid = (integer) $itemid;
\r
419 $new_catid = (integer) $new_catid;
\r
420 $new_blogid = getBlogIDFromCatID($new_catid);
\r
425 'itemid' => $itemid,
\r
426 'destblogid' => $new_blogid,
\r
427 'destcatid' => $new_catid
\r
431 // update item table
\r
432 $query = "UPDATE %s SET iblog=%d, icat=%d WHERE inumber=%d";
\r
433 $query = sprintf($query, sql_table('item'), $new_blogid, $new_catid, $itemid);
\r
434 DB::execute($query);
\r
437 $query = "UPDATE %s SET cblog=%d WHERE citem=%d";
\r
438 $query = sprintf($query, sql_table('comment'), $new_blogid, $itemid);
\r
439 DB::execute($query);
\r
444 'itemid' => $itemid,
\r
445 'destblogid' => $new_blogid,
\r
446 'destcatid' => $new_catid
\r
456 * @param integer $itemid
\r
459 static public function delete($itemid)
\r
461 global $manager, $member;
\r
463 $itemid = (integer) $itemid;
\r
465 // check permission
\r
466 if ( !$member->canAlterItem($itemid) )
\r
471 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
\r
474 $query = "DELETE FROM %s WHERE inumber=%d";
\r
475 $query = sprintf($query, sql_table('item'), $itemid);
\r
476 DB::execute($query);
\r
478 // delete the comments associated with the item
\r
479 $query = "DELETE FROM %s WHERE citem=%d";
\r
480 $query = sprintf($query, sql_table('comment'), $itemid);
\r
481 DB::execute($query);
\r
483 // delete all associated plugin options
\r
484 NucleusPlugin::delete_option_values('item', $itemid);
\r
486 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
\r
493 * Returns true if there is an item with the given ID
\r
496 * @param integer $itemid
\r
497 * @param boolean $future
\r
498 * @param boolean $draft
\r
499 * @return boolean exists or not
\r
502 static public function exists($itemid, $future, $draft)
\r
506 $itemid = (integer) $itemid;
\r
507 $query = 'select * FROM '.sql_table('item').' WHERE inumber='.$itemid;
\r
511 $blogid = getBlogIDFromItemID($itemid);
\r
516 $blog =& $manager->getBlog($blogid);
\r
517 $query .= " and itime<='" . i18n::formatted_datetime('mysql', $blog->getCorrectTime()) ."'";
\r
521 $query .= ' and idraft=0';
\r
523 $result = DB::getResult($query);
\r
524 return ( $result->rowCount() != 0 );
\r
528 * Item::createDraftFromRequest()
\r
529 * Tries to create an draft from the data
\r
530 * in the current request (comes from bookmarklet or admin area)
\r
531 * Used by xmlHTTPRequest AutoDraft
\r
533 * Returns an array with status info:
\r
534 * status = 'added', 'error', 'newcategory'
\r
538 * @return array (status = added/error/newcategory, message)
\r
541 static public function createDraftFromRequest()
\r
543 global $member, $manager;
\r
546 * TODO: these values from user agent should be validated but not implemented yet
\r
548 $i_author = $member->getID();
\r
549 $i_body = postVar('body');
\r
550 $i_title = postVar('title');
\r
551 $i_more = postVar('more');
\r
552 $i_closed = intPostVar('closed');
\r
553 $i_catid = postVar('catid');
\r
555 $type = postVar('type');
\r
556 $i_draftid = intPostVar('draftid');
\r
558 if ( $type == 'edit' )
\r
560 $itemid = intPostVar('itemid');
\r
561 $i_blogid = getBlogIDFromItemID($itemid);
\r
565 $i_blogid = intPostVar('blogid');
\r
568 if ( !$member->canAddItem($i_catid) )
\r
570 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
573 if ( !trim($i_body) )
\r
575 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
578 // create new category if needed
\r
579 if ( strstr($i_catid, 'newcat') )
\r
581 // Set in default category
\r
582 $blog =& $manager->getBlog($i_blogid);
\r
583 $i_catid = $blog->getDefaultCategory();
\r
587 // force blogid (must be same as category id)
\r
588 $i_blogid = getBlogIDFromCatID($i_catid);
\r
589 $blog =& $manager->getBlog($i_blogid);
\r
594 if ( $i_draftid > 0 )
\r
596 self::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
\r
597 $itemid = $i_draftid;
\r
601 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
\r
604 return array('status' => 'added', 'draftid' => $itemid);
\r