4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2007 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 * A class representing an item
16 * @license http://nucleuscms.org/license.txt GNU General Public License
17 * @copyright Copyright (C) 2002-2007 The Nucleus Group
18 * @version $Id: ITEM.php,v 1.8 2008-02-08 09:31:22 kimitake Exp $
19 * $NucleusJP: ITEM.php,v 1.7.2.3 2008/02/07 06:13:30 kimitake Exp $
25 function ITEM($itemid) {
26 $this->itemid = $itemid;
30 * Returns one item with the specific itemid
33 function getitem($itemid, $allowdraft, $allowfuture) {
36 $itemid = intval($itemid);
38 $query = 'SELECT i.idraft as draft, i.inumber as itemid, i.iclosed as closed, '
39 . ' i.ititle as title, i.ibody as body, m.mname as author, '
40 . ' i.iauthor as authorid, i.itime, i.imore as more, i.ikarmapos as karmapos, '
41 . ' i.ikarmaneg as karmaneg, i.icat as catid, i.iblog as blogid '
42 . ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, ' . sql_table('blog') . ' as b '
43 . ' WHERE i.inumber=' . $itemid
44 . ' and i.iauthor=m.mnumber '
45 . ' and i.iblog=b.bnumber';
48 $query .= ' and i.idraft=0';
51 $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
52 $query .= ' and i.itime <=' . mysqldate($blog->getCorrectTime());
57 $res = sql_query($query);
59 if (mysql_num_rows($res) == 1)
61 $aItemInfo = mysql_fetch_assoc($res);
62 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
71 * Tries to create an item from the data in the current request (comes from
72 * bookmarklet or admin area
74 * Returns an array with status info (status = 'added', 'error', 'newcategory')
78 function createFromRequest() {
79 global $member, $manager;
81 $i_author = $member->getID();
82 $i_body = postVar('body');
83 $i_title = postVar('title');
84 $i_more = postVar('more');
85 $i_actiontype = postVar('actiontype');
86 $i_closed = intPostVar('closed');
87 $i_hour = intPostVar('hour');
88 $i_minutes = intPostVar('minutes');
89 $i_month = intPostVar('month');
90 $i_day = intPostVar('day');
91 $i_year = intPostVar('year');
93 $i_catid = postVar('catid');
95 $i_draftid = intPostVar('draftid');
97 if (!$member->canAddItem($i_catid))
98 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
100 if (!$i_actiontype) $i_actiontype = 'addnow';
102 switch ($i_actiontype) {
113 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
115 // create new category if needed
116 if (strstr($i_catid,'newcat')) {
118 list($i_blogid) = sscanf($i_catid,"newcat-%d");
121 $blog =& $manager->getBlog($i_blogid);
122 $i_catid = $blog->createNewCategory();
124 // show error when sth goes wrong
126 return array('status' => 'error','message' => 'Could not create new category');
128 // force blogid (must be same as category id)
129 $i_blogid = getBlogIDFromCatID($i_catid);
130 $blog =& $manager->getBlog($i_blogid);
133 if ($i_actiontype == 'addfuture') {
134 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
136 // make sure the date is in the future, unless we allow past dates
137 if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()))
138 $posttime = $blog->getCorrectTime();
140 // time with offset, or 0 for drafts
141 $posttime = $i_draft ? 0 : $blog->getCorrectTime();
144 if ($posttime > $blog->getCorrectTime()) {
146 $blog->setFuturePost();
152 $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft,$posted);
154 //Setting the itemOptions
155 $aOptions = requestArray('plugoption');
156 NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
157 $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $i_title, 'body' => $i_body, 'more' => $i_more, 'closed' => $i_closed, 'catid' => $i_catid)));
159 if ($i_draftid > 0) {
160 ITEM::delete($i_draftid);
164 if ($i_catid != intRequestVar('catid'))
165 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
167 return array('status' => 'added', 'itemid' => $itemid);
172 * Updates an item (static)
174 function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
177 $itemid = intval($itemid);
179 // make sure value is 1 or 0
180 if ($closed != 1) $closed = 0;
182 // get destination blogid
183 $new_blogid = getBlogIDFromCatID($catid);
184 $old_blogid = getBlogIDFromItemID($itemid);
186 // move will be done on end of method
187 if ($new_blogid != $old_blogid)
190 // add <br /> before newlines
191 $blog =& $manager->getBlog($new_blogid);
192 if ($blog->convertBreaks()) {
193 $body = addBreaks($body);
194 $more = addBreaks($more);
198 $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid));
200 // update item itsself
201 $query = 'UPDATE '.sql_table('item')
203 . " ibody='". addslashes($body) ."',"
204 . " ititle='" . addslashes($title) . "',"
205 . " imore='" . addslashes($more) . "',"
206 . " iclosed=" . intval($closed) . ","
207 . " icat=" . intval($catid);
209 // if we received an updated timestamp in the past, but past posting is not allowed,
210 // reject that date change (timestamp = 0 will make sure the current date is kept)
211 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
214 if ($timestamp > $blog->getCorrectTime(time())) {
216 $query .= ', iposted=0';
220 $query .= ', iposted=1';
223 if ($wasdraft && $publish) {
224 // set timestamp to current date only if it's not a future item
225 // draft items have timestamp == 0
226 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
227 $query .= ', idraft=0';
230 $timestamp = $blog->getCorrectTime();
232 // send new item notification
233 if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
234 $blog->sendNewItemNotification($itemid, $title, $body);
237 // update timestamp when needed
239 $query .= ", itime=" . mysqldate($timestamp);
241 // make sure the correct item is updated
242 $query .= ' WHERE inumber=' . $itemid;
247 $manager->notify('PostUpdateItem',array('itemid' => $itemid));
249 // when needed, move item and comments to new blog
251 ITEM::move($itemid, $catid);
253 //update the itemOptions
254 $aOptions = requestArray('plugoption');
255 NucleusPlugin::_applyPluginOptions($aOptions);
256 $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $title, 'body' => $body, 'more' => $more, 'closed' => $closed, 'catid' => $catid)));
260 // move an item to another blog (no checks, static)
261 function move($itemid, $new_catid) {
264 $itemid = intval($itemid);
265 $new_catid = intval($new_catid);
267 $new_blogid = getBlogIDFromCatID($new_catid);
273 'destblogid' => $new_blogid,
274 'destcatid' => $new_catid
280 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
284 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
291 'destblogid' => $new_blogid,
292 'destcatid' => $new_catid
300 function delete($itemid) {
303 $itemid = intval($itemid);
305 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
308 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
311 // delete the comments associated with the item
312 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
315 // delete all associated plugin options
316 NucleusPlugin::_deleteOptionValues('item', $itemid);
318 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
321 // returns true if there is an item with the given ID (static)
322 function exists($id,$future,$draft) {
327 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
329 $bid = getBlogIDFromItemID($id);
331 $b =& $manager->getBlog($bid);
332 $r .= ' and itime<='.mysqldate($b->getCorrectTime());
335 $r .= ' and idraft=0';
339 return (mysql_num_rows($r) != 0);
343 * Tries to create an draft from the data in the current request (comes from
344 * bookmarklet or admin area
346 * Returns an array with status info (status = 'added', 'error', 'newcategory')
350 * Used by xmlHTTPRequest AutoDraft
352 function createDraftFromRequest() {
353 global $member, $manager;
355 $i_author = $member->getID();
356 $i_body = postVar('body');
357 $i_title = postVar('title');
358 $i_more = postVar('more');
360 if(_CHARSET != 'UTF-8'){
361 $i_body = mb_convert_encoding($i_body, _CHARSET, "UTF-8");
362 $i_title = mb_convert_encoding($i_title, _CHARSET, "UTF-8");
363 $i_more = mb_convert_encoding($i_more, _CHARSET, "UTF-8");
365 //$i_actiontype = postVar('actiontype');
366 $i_closed = intPostVar('closed');
367 //$i_hour = intPostVar('hour');
368 //$i_minutes = intPostVar('minutes');
369 //$i_month = intPostVar('month');
370 //$i_day = intPostVar('day');
371 //$i_year = intPostVar('year');
372 $i_catid = postVar('catid');
374 $type = postVar('type');
375 if ($type == 'edit') {
376 $i_blogid = getBlogIDFromItemID(intPostVar('itemid'));
379 $i_blogid = intPostVar('blogid');
381 $i_draftid = intPostVar('draftid');
383 if (!$member->canAddItem($i_catid)) {
384 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
387 if (!trim($i_body)) {
388 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
391 // create new category if needed
392 if (strstr($i_catid, 'newcat')) {
393 // Set in default category
394 $blog =& $manager->getBlog($i_blogid);
395 $i_catid = $blog->getDefaultCategory();
398 // force blogid (must be same as category id)
399 $i_blogid = getBlogIDFromCatID($i_catid);
400 $blog =& $manager->getBlog($i_blogid);
405 if ($i_draftid > 0) {
406 ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
407 $itemid = $i_draftid;
410 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
413 // No plugin support in AutoSaveDraft yet
414 //Setting the itemOptions
415 //$aOptions = requestArray('plugoption');
416 //NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
417 //$manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $i_title, 'body' => $i_body, 'more' => $i_more, 'closed' => $i_closed, 'catid' => $i_catid)));
420 return array('status' => 'added', 'draftid' => $itemid);