4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2005 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 * A class representing an item
\r
16 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
17 * @copyright Copyright (C) 2002-2005 The Nucleus Group
\r
18 * @version $Id: ITEM.php,v 1.4 2005-08-13 07:33:02 kimitake Exp $
\r
19 * $NucleusJP: ITEM.php,v 1.3 2005/03/12 06:19:05 kimitake Exp $
\r
25 function ITEM($itemid) {
\r
26 $this->itemid = $itemid;
\r
30 * Returns one item with the specific itemid
\r
33 function getitem($itemid, $allowdraft, $allowfuture) {
\r
36 $itemid = intval($itemid);
\r
38 $query = 'SELECT i.idraft as draft, i.inumber as itemid, i.iclosed as closed, '
\r
39 . ' i.ititle as title, i.ibody as body, m.mname as author, '
\r
40 . ' i.iauthor as authorid, i.itime, i.imore as more, i.ikarmapos as karmapos, '
\r
41 . ' i.ikarmaneg as karmaneg, i.icat as catid, i.iblog as blogid '
\r
42 . ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, ' . sql_table('blog') . ' as b '
\r
43 . ' WHERE i.inumber=' . $itemid
\r
44 . ' and i.iauthor=m.mnumber '
\r
45 . ' and i.iblog=b.bnumber';
\r
48 $query .= ' and i.idraft=0';
\r
50 if (!$allowfuture) {
\r
51 $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
\r
52 $query .= ' and i.itime <=' . mysqldate($blog->getCorrectTime());
\r
55 $query .= ' LIMIT 1';
\r
57 $res = sql_query($query);
\r
59 if (mysql_num_rows($res) == 1)
\r
61 $aItemInfo = mysql_fetch_assoc($res);
\r
62 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
\r
71 * Tries to create an item from the data in the current request (comes from
\r
72 * bookmarklet or admin area
\r
74 * Returns an array with status info (status = 'added', 'error', 'newcategory')
\r
78 function createFromRequest() {
\r
79 global $member, $manager;
\r
81 $i_author = $member->getID();
\r
82 $i_body = postVar('body');
\r
83 $i_title = postVar('title');
\r
84 $i_more = postVar('more');
\r
85 $i_actiontype = postVar('actiontype');
\r
86 $i_closed = intPostVar('closed');
\r
87 $i_hour = intPostVar('hour');
\r
88 $i_minutes = intPostVar('minutes');
\r
89 $i_month = intPostVar('month');
\r
90 $i_day = intPostVar('day');
\r
91 $i_year = intPostVar('year');
\r
93 $i_catid = postVar('catid');
\r
95 if (!$member->canAddItem($i_catid))
\r
96 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
98 if (!$i_actiontype) $i_actiontype = 'addnow';
\r
100 switch ($i_actiontype) {
\r
110 if (!trim($i_body))
\r
111 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
113 // create new category if needed
\r
114 if (strstr($i_catid,'newcat')) {
\r
116 list($i_blogid) = sscanf($i_catid,"newcat-%d");
\r
119 $blog =& $manager->getBlog($i_blogid);
\r
120 $i_catid = $blog->createNewCategory();
\r
122 // show error when sth goes wrong
\r
124 return array('status' => 'error','message' => 'Could not create new category');
\r
126 // force blogid (must be same as category id)
\r
127 $i_blogid = getBlogIDFromCatID($i_catid);
\r
128 $blog =& $manager->getBlog($i_blogid);
\r
131 if ($i_actiontype == 'addfuture') {
\r
132 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
\r
134 // make sure the date is in the future, unless we allow past dates
\r
135 if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()))
\r
136 $posttime = $blog->getCorrectTime();
\r
138 // time with offset, or 0 for drafts
\r
139 $posttime = $i_draft ? 0 : $blog->getCorrectTime();
\r
142 $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft);
\r
144 //Setting the itemOptions
\r
145 $aOptions = requestArray('plugoption');
\r
146 NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
\r
147 $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)));
\r
150 if ($i_catid != intRequestVar('catid'))
\r
151 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
\r
153 return array('status' => 'added', 'itemid' => $itemid);
\r
158 * Updates an item (static)
\r
160 function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
\r
163 $itemid = intval($itemid);
\r
165 // make sure value is 1 or 0
\r
166 if ($closed != 1) $closed = 0;
\r
168 // get destination blogid
\r
169 $new_blogid = getBlogIDFromCatID($catid);
\r
170 $old_blogid = getBlogIDFromItemID($itemid);
\r
172 // move will be done on end of method
\r
173 if ($new_blogid != $old_blogid)
\r
176 // add <br /> before newlines
\r
177 $blog =& $manager->getBlog($new_blogid);
\r
178 if ($blog->convertBreaks()) {
\r
179 $body = addBreaks($body);
\r
180 $more = addBreaks($more);
\r
184 $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid));
\r
186 // update item itsself
\r
187 $query = 'UPDATE '.sql_table('item')
\r
189 . " ibody='". addslashes($body) ."',"
\r
190 . " ititle='" . addslashes($title) . "',"
\r
191 . " imore='" . addslashes($more) . "',"
\r
192 . " iclosed=" . intval($closed) . ","
\r
193 . " icat=" . intval($catid);
\r
195 // if we received an updated timestamp in the past, but past posting is not allowed,
\r
196 // reject that date change (timestamp = 0 will make sure the current date is kept)
\r
197 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
\r
200 if ($wasdraft && $publish) {
\r
201 $query .= ', idraft=0';
\r
203 // set timestamp to current date only if it's not a future item
\r
204 // draft items have timestamp == 0
\r
205 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
\r
206 if ($timestamp > $blog->getCorrectTime())
\r
209 if ($timestamp == 0)
\r
210 $timestamp = $blog->getCorrectTime();
\r
212 // send new item notification
\r
213 if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
\r
214 $blog->sendNewItemNotification($itemid, $title, $body);
\r
217 // update timestamp when needed
\r
218 if ($timestamp != 0)
\r
219 $query .= ", itime=" . mysqldate($timestamp);
\r
221 // make sure the correct item is updated
\r
222 $query .= ' WHERE inumber=' . $itemid;
\r
225 sql_query($query);
\r
227 // when needed, move item and comments to new blog
\r
229 ITEM::move($itemid, $catid);
\r
231 //update the itemOptions
\r
232 $aOptions = requestArray('plugoption');
\r
233 NucleusPlugin::_applyPluginOptions($aOptions);
\r
234 $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $title, 'body' => $body, 'more' => $more, 'closed' => $closed, 'catid' => $catid)));
\r
238 // move an item to another blog (no checks, static)
\r
239 function move($itemid, $new_catid) {
\r
242 $itemid = intval($itemid);
\r
243 $new_catid = intval($new_catid);
\r
245 $new_blogid = getBlogIDFromCatID($new_catid);
\r
250 'itemid' => $itemid,
\r
251 'destblogid' => $new_blogid,
\r
252 'destcatid' => $new_catid
\r
257 // update item table
\r
258 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
\r
259 sql_query($query);
\r
262 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
\r
263 sql_query($query);
\r
268 'itemid' => $itemid,
\r
269 'destblogid' => $new_blogid,
\r
270 'destcatid' => $new_catid
\r
278 function delete($itemid) {
\r
281 $itemid = intval($itemid);
\r
283 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
\r
286 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
\r
289 // delete the comments associated with the item
\r
290 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
\r
291 sql_query($query);
\r
293 // delete all associated plugin options
\r
294 NucleusPlugin::_deleteOptionValues('item', $itemid);
\r
296 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
\r
299 // returns true if there is an item with the given ID (static)
\r
300 function exists($id,$future,$draft) {
\r
305 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
\r
307 $bid = getBlogIDFromItemID($id);
\r
308 if (!$bid) return 0;
\r
309 $b =& $manager->getBlog($bid);
\r
310 $r .= ' and itime<='.mysqldate($b->getCorrectTime());
\r
313 $r .= ' and idraft=0';
\r
315 $r = sql_query($r);
\r
317 return (mysql_num_rows($r) != 0);
\r