4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2006 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-2006 The Nucleus Group
18 * @version $Id: ITEM.php,v 1.5 2006-07-17 20:03:44 kimitake Exp $
19 * $NucleusJP: ITEM.php,v 1.4 2005/08/13 07:33:02 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 $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft);
146 //Setting the itemOptions
147 $aOptions = requestArray('plugoption');
148 NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
149 $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)));
151 if ($i_draftid > 0) {
152 ITEM::delete($i_draftid);
156 if ($i_catid != intRequestVar('catid'))
157 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
159 return array('status' => 'added', 'itemid' => $itemid);
164 * Updates an item (static)
166 function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
169 $itemid = intval($itemid);
171 // make sure value is 1 or 0
172 if ($closed != 1) $closed = 0;
174 // get destination blogid
175 $new_blogid = getBlogIDFromCatID($catid);
176 $old_blogid = getBlogIDFromItemID($itemid);
178 // move will be done on end of method
179 if ($new_blogid != $old_blogid)
182 // add <br /> before newlines
183 $blog =& $manager->getBlog($new_blogid);
184 if ($blog->convertBreaks()) {
185 $body = addBreaks($body);
186 $more = addBreaks($more);
190 $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid));
192 // update item itsself
193 $query = 'UPDATE '.sql_table('item')
195 . " ibody='". addslashes($body) ."',"
196 . " ititle='" . addslashes($title) . "',"
197 . " imore='" . addslashes($more) . "',"
198 . " iclosed=" . intval($closed) . ","
199 . " icat=" . intval($catid);
201 // if we received an updated timestamp in the past, but past posting is not allowed,
202 // reject that date change (timestamp = 0 will make sure the current date is kept)
203 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
206 if ($wasdraft && $publish) {
207 $query .= ', idraft=0';
209 // set timestamp to current date only if it's not a future item
210 // draft items have timestamp == 0
211 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
212 if ($timestamp > $blog->getCorrectTime())
216 $timestamp = $blog->getCorrectTime();
218 // send new item notification
219 if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
220 $blog->sendNewItemNotification($itemid, $title, $body);
223 // update timestamp when needed
225 $query .= ", itime=" . mysqldate($timestamp);
227 // make sure the correct item is updated
228 $query .= ' WHERE inumber=' . $itemid;
233 $manager->notify('PostUpdateItem',array('itemid' => $itemid));
235 // when needed, move item and comments to new blog
237 ITEM::move($itemid, $catid);
239 //update the itemOptions
240 $aOptions = requestArray('plugoption');
241 NucleusPlugin::_applyPluginOptions($aOptions);
242 $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $title, 'body' => $body, 'more' => $more, 'closed' => $closed, 'catid' => $catid)));
246 // move an item to another blog (no checks, static)
247 function move($itemid, $new_catid) {
250 $itemid = intval($itemid);
251 $new_catid = intval($new_catid);
253 $new_blogid = getBlogIDFromCatID($new_catid);
259 'destblogid' => $new_blogid,
260 'destcatid' => $new_catid
266 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
270 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
277 'destblogid' => $new_blogid,
278 'destcatid' => $new_catid
286 function delete($itemid) {
289 $itemid = intval($itemid);
291 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
294 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
297 // delete the comments associated with the item
298 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
301 // delete all associated plugin options
302 NucleusPlugin::_deleteOptionValues('item', $itemid);
304 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
307 // returns true if there is an item with the given ID (static)
308 function exists($id,$future,$draft) {
313 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
315 $bid = getBlogIDFromItemID($id);
317 $b =& $manager->getBlog($bid);
318 $r .= ' and itime<='.mysqldate($b->getCorrectTime());
321 $r .= ' and idraft=0';
325 return (mysql_num_rows($r) != 0);
329 * Tries to create an draft from the data in the current request (comes from
330 * bookmarklet or admin area
332 * Returns an array with status info (status = 'added', 'error', 'newcategory')
336 * Used by xmlHTTPRequest AutoDraft
338 function createDraftFromRequest() {
339 global $member, $manager;
341 $i_author = $member->getID();
342 $i_body = postVar('body');
343 $i_title = postVar('title');
344 $i_more = postVar('more');
345 //$i_actiontype = postVar('actiontype');
346 $i_closed = intPostVar('closed');
347 //$i_hour = intPostVar('hour');
348 //$i_minutes = intPostVar('minutes');
349 //$i_month = intPostVar('month');
350 //$i_day = intPostVar('day');
351 //$i_year = intPostVar('year');
352 $i_catid = postVar('catid');
354 $type = postVar('type');
355 if ($type == 'edit') {
356 $i_blogid = getBlogIDFromItemID(intPostVar('itemid'));
359 $i_blogid = intPostVar('blogid');
361 $i_draftid = intPostVar('draftid');
363 if (!$member->canAddItem($i_catid)) {
364 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
367 if (!trim($i_body)) {
368 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
371 // create new category if needed
372 if (strstr($i_catid, 'newcat')) {
373 // Set in default category
374 $blog =& $manager->getBlog($i_blogid);
375 $i_catid = $blog->getDefaultCategory();
378 // force blogid (must be same as category id)
379 $i_blogid = getBlogIDFromCatID($i_catid);
380 $blog =& $manager->getBlog($i_blogid);
385 if ($i_draftid > 0) {
386 ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
387 $itemid = $i_draftid;
390 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
393 // No plugin support in AutoSaveDraft yet
394 //Setting the itemOptions
395 //$aOptions = requestArray('plugoption');
396 //NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
397 //$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)));
400 return array('status' => 'added', 'draftid' => $itemid);