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.6 2006-07-20 08:01:52 kimitake Exp $
19 * $NucleusJP: ITEM.php,v 1.5 2006/07/17 20:03:44 kimitake Exp $
26 function ITEM($itemid) {
27 $this->itemid = $itemid;
31 * Returns one item with the specific itemid
34 function getitem($itemid, $allowdraft, $allowfuture) {
37 $itemid = intval($itemid);
39 $query = 'SELECT i.idraft as draft, i.inumber as itemid, i.iclosed as closed, '
40 . ' i.ititle as title, i.ibody as body, m.mname as author, '
41 . ' i.iauthor as authorid, i.itime, i.imore as more, i.ikarmapos as karmapos, '
42 . ' i.ikarmaneg as karmaneg, i.icat as catid, i.iblog as blogid '
43 . ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, ' . sql_table('blog') . ' as b '
44 . ' WHERE i.inumber=' . $itemid
45 . ' and i.iauthor=m.mnumber '
46 . ' and i.iblog=b.bnumber';
49 $query .= ' and i.idraft=0';
52 $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
53 $query .= ' and i.itime <=' . mysqldate($blog->getCorrectTime());
58 $res = sql_query($query);
60 if (mysql_num_rows($res) == 1)
62 $aItemInfo = mysql_fetch_assoc($res);
63 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
72 * Tries to create an item from the data in the current request (comes from
73 * bookmarklet or admin area
75 * Returns an array with status info (status = 'added', 'error', 'newcategory')
79 function createFromRequest() {
80 global $member, $manager;
82 $i_author = $member->getID();
83 $i_body = postVar('body');
84 $i_title = postVar('title');
85 $i_more = postVar('more');
86 $i_actiontype = postVar('actiontype');
87 $i_closed = intPostVar('closed');
88 $i_hour = intPostVar('hour');
89 $i_minutes = intPostVar('minutes');
90 $i_month = intPostVar('month');
91 $i_day = intPostVar('day');
92 $i_year = intPostVar('year');
94 $i_catid = postVar('catid');
96 $i_draftid = intPostVar('draftid');
98 if (!$member->canAddItem($i_catid))
99 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
101 if (!$i_actiontype) $i_actiontype = 'addnow';
103 switch ($i_actiontype) {
114 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
116 // create new category if needed
117 if (strstr($i_catid,'newcat')) {
119 list($i_blogid) = sscanf($i_catid,"newcat-%d");
122 $blog =& $manager->getBlog($i_blogid);
123 $i_catid = $blog->createNewCategory();
125 // show error when sth goes wrong
127 return array('status' => 'error','message' => 'Could not create new category');
129 // force blogid (must be same as category id)
130 $i_blogid = getBlogIDFromCatID($i_catid);
131 $blog =& $manager->getBlog($i_blogid);
134 if ($i_actiontype == 'addfuture') {
135 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
137 // make sure the date is in the future, unless we allow past dates
138 if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()))
139 $posttime = $blog->getCorrectTime();
141 // time with offset, or 0 for drafts
142 $posttime = $i_draft ? 0 : $blog->getCorrectTime();
145 $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft);
147 //Setting the itemOptions
148 $aOptions = requestArray('plugoption');
149 NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
150 $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)));
152 if ($i_draftid > 0) {
153 ITEM::delete($i_draftid);
157 if ($i_catid != intRequestVar('catid'))
158 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
160 return array('status' => 'added', 'itemid' => $itemid);
165 * Updates an item (static)
167 function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
170 $itemid = intval($itemid);
172 // make sure value is 1 or 0
173 if ($closed != 1) $closed = 0;
175 // get destination blogid
176 $new_blogid = getBlogIDFromCatID($catid);
177 $old_blogid = getBlogIDFromItemID($itemid);
179 // move will be done on end of method
180 if ($new_blogid != $old_blogid)
183 // add <br /> before newlines
184 $blog =& $manager->getBlog($new_blogid);
185 if ($blog->convertBreaks()) {
186 $body = addBreaks($body);
187 $more = addBreaks($more);
191 $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid));
193 // update item itsself
194 $query = 'UPDATE '.sql_table('item')
196 . " ibody='". addslashes($body) ."',"
197 . " ititle='" . addslashes($title) . "',"
198 . " imore='" . addslashes($more) . "',"
199 . " iclosed=" . intval($closed) . ","
200 . " icat=" . intval($catid);
202 // if we received an updated timestamp in the past, but past posting is not allowed,
203 // reject that date change (timestamp = 0 will make sure the current date is kept)
204 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
207 if ($wasdraft && $publish) {
208 $query .= ', idraft=0';
210 // set timestamp to current date only if it's not a future item
211 // draft items have timestamp == 0
212 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
213 if ($timestamp > $blog->getCorrectTime())
217 $timestamp = $blog->getCorrectTime();
219 // send new item notification
220 if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
221 $blog->sendNewItemNotification($itemid, $title, $body);
224 // update timestamp when needed
226 $query .= ", itime=" . mysqldate($timestamp);
228 // make sure the correct item is updated
229 $query .= ' WHERE inumber=' . $itemid;
234 $manager->notify('PostUpdateItem',array('itemid' => $itemid));
236 // when needed, move item and comments to new blog
238 ITEM::move($itemid, $catid);
240 //update the itemOptions
241 $aOptions = requestArray('plugoption');
242 NucleusPlugin::_applyPluginOptions($aOptions);
243 $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $title, 'body' => $body, 'more' => $more, 'closed' => $closed, 'catid' => $catid)));
247 // move an item to another blog (no checks, static)
248 function move($itemid, $new_catid) {
251 $itemid = intval($itemid);
252 $new_catid = intval($new_catid);
254 $new_blogid = getBlogIDFromCatID($new_catid);
260 'destblogid' => $new_blogid,
261 'destcatid' => $new_catid
267 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
271 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
278 'destblogid' => $new_blogid,
279 'destcatid' => $new_catid
287 function delete($itemid) {
290 $itemid = intval($itemid);
292 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
295 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
298 // delete the comments associated with the item
299 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
302 // delete all associated plugin options
303 NucleusPlugin::_deleteOptionValues('item', $itemid);
305 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
308 // returns true if there is an item with the given ID (static)
309 function exists($id,$future,$draft) {
314 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
316 $bid = getBlogIDFromItemID($id);
318 $b =& $manager->getBlog($bid);
319 $r .= ' and itime<='.mysqldate($b->getCorrectTime());
322 $r .= ' and idraft=0';
326 return (mysql_num_rows($r) != 0);
330 * Tries to create an draft from the data in the current request (comes from
331 * bookmarklet or admin area
333 * Returns an array with status info (status = 'added', 'error', 'newcategory')
337 * Used by xmlHTTPRequest AutoDraft
339 function createDraftFromRequest() {
340 global $member, $manager;
342 $i_author = $member->getID();
343 $i_body = postVar('body');
344 $i_title = postVar('title');
345 $i_more = postVar('more');
346 //$i_actiontype = postVar('actiontype');
347 $i_closed = intPostVar('closed');
348 //$i_hour = intPostVar('hour');
349 //$i_minutes = intPostVar('minutes');
350 //$i_month = intPostVar('month');
351 //$i_day = intPostVar('day');
352 //$i_year = intPostVar('year');
353 $i_catid = postVar('catid');
355 $type = postVar('type');
356 if ($type == 'edit') {
357 $i_blogid = getBlogIDFromItemID(intPostVar('itemid'));
360 $i_blogid = intPostVar('blogid');
362 $i_draftid = intPostVar('draftid');
364 if (!$member->canAddItem($i_catid)) {
365 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
368 if (!trim($i_body)) {
369 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
372 // create new category if needed
373 if (strstr($i_catid, 'newcat')) {
374 // Set in default category
375 $blog =& $manager->getBlog($i_blogid);
376 $i_catid = $blog->getDefaultCategory();
379 // force blogid (must be same as category id)
380 $i_blogid = getBlogIDFromCatID($i_catid);
381 $blog =& $manager->getBlog($i_blogid);
386 if ($i_draftid > 0) {
387 ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
388 $itemid = $i_draftid;
391 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
394 // No plugin support in AutoSaveDraft yet
395 //Setting the itemOptions
396 //$aOptions = requestArray('plugoption');
397 //NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
398 //$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)));
401 return array('status' => 'added', 'draftid' => $itemid);