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
13 * A class representing an item
\r
15 * $Id: ITEM.php,v 1.3 2005-03-12 06:19:05 kimitake Exp $
\r
22 function ITEM($itemid) {
\r
23 $this->itemid = $itemid;
\r
27 * Returns one item with the specific itemid
\r
30 function getitem($itemid, $allowdraft, $allowfuture) {
\r
33 $itemid = intval($itemid);
\r
35 $query = 'SELECT i.idraft as draft, i.inumber as itemid, i.iclosed as closed, '
\r
36 . ' i.ititle as title, i.ibody as body, m.mname as author, '
\r
37 . ' i.iauthor as authorid, i.itime, i.imore as more, i.ikarmapos as karmapos, '
\r
38 . ' i.ikarmaneg as karmaneg, i.icat as catid, i.iblog as blogid '
\r
39 . ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, ' . sql_table('blog') . ' as b '
\r
40 . ' WHERE i.inumber=' . $itemid
\r
41 . ' and i.iauthor=m.mnumber '
\r
42 . ' and i.iblog=b.bnumber';
\r
45 $query .= ' and i.idraft=0';
\r
47 if (!$allowfuture) {
\r
48 $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
\r
49 $query .= ' and i.itime <=' . mysqldate($blog->getCorrectTime());
\r
52 $query .= ' LIMIT 1';
\r
54 $res = sql_query($query);
\r
56 if (mysql_num_rows($res) == 1)
\r
58 $aItemInfo = mysql_fetch_assoc($res);
\r
59 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
\r
68 * Tries to create an item from the data in the current request (comes from
\r
69 * bookmarklet or admin area
\r
71 * Returns an array with status info (status = 'added', 'error', 'newcategory')
\r
75 function createFromRequest() {
\r
76 global $member, $manager;
\r
78 $i_author = $member->getID();
\r
79 $i_body = postVar('body');
\r
80 $i_title = postVar('title');
\r
81 $i_more = postVar('more');
\r
82 $i_actiontype = postVar('actiontype');
\r
83 $i_closed = intPostVar('closed');
\r
84 $i_hour = intPostVar('hour');
\r
85 $i_minutes = intPostVar('minutes');
\r
86 $i_month = intPostVar('month');
\r
87 $i_day = intPostVar('day');
\r
88 $i_year = intPostVar('year');
\r
90 $i_catid = postVar('catid');
\r
92 if (!$member->canAddItem($i_catid))
\r
93 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
95 if (!$i_actiontype) $i_actiontype = 'addnow';
\r
97 switch ($i_actiontype) {
\r
107 if (!trim($i_body))
\r
108 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
110 // create new category if needed
\r
111 if (strstr($i_catid,'newcat')) {
\r
113 list($i_blogid) = sscanf($i_catid,"newcat-%d");
\r
116 $blog =& $manager->getBlog($i_blogid);
\r
117 $i_catid = $blog->createNewCategory();
\r
119 // show error when sth goes wrong
\r
121 return array('status' => 'error','message' => 'Could not create new category');
\r
123 // force blogid (must be same as category id)
\r
124 $i_blogid = getBlogIDFromCatID($i_catid);
\r
125 $blog =& $manager->getBlog($i_blogid);
\r
128 if ($i_actiontype == 'addfuture') {
\r
129 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
\r
131 // make sure the date is in the future, unless we allow past dates
\r
132 if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()))
\r
133 $posttime = $blog->getCorrectTime();
\r
135 // time with offset, or 0 for drafts
\r
136 $posttime = $i_draft ? 0 : $blog->getCorrectTime();
\r
139 $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft);
\r
141 //Setting the itemOptions
\r
142 $aOptions = requestArray('plugoption');
\r
143 NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
\r
144 $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
147 if ($i_catid != intRequestVar('catid'))
\r
148 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
\r
150 return array('status' => 'added', 'itemid' => $itemid);
\r
155 * Updates an item (static)
\r
157 function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
\r
160 $itemid = intval($itemid);
\r
162 // make sure value is 1 or 0
\r
163 if ($closed != 1) $closed = 0;
\r
165 // get destination blogid
\r
166 $new_blogid = getBlogIDFromCatID($catid);
\r
167 $old_blogid = getBlogIDFromItemID($itemid);
\r
169 // move will be done on end of method
\r
170 if ($new_blogid != $old_blogid)
\r
173 // add <br /> before newlines
\r
174 $blog =& $manager->getBlog($new_blogid);
\r
175 if ($blog->convertBreaks()) {
\r
176 $body = addBreaks($body);
\r
177 $more = addBreaks($more);
\r
181 $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid));
\r
183 // update item itsself
\r
184 $query = 'UPDATE '.sql_table('item')
\r
186 . " ibody='". addslashes($body) ."',"
\r
187 . " ititle='" . addslashes($title) . "',"
\r
188 . " imore='" . addslashes($more) . "',"
\r
189 . " iclosed=" . intval($closed) . ","
\r
190 . " icat=" . intval($catid);
\r
192 // if we received an updated timestamp in the past, but past posting is not allowed,
\r
193 // reject that date change (timestamp = 0 will make sure the current date is kept)
\r
194 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
\r
197 if ($wasdraft && $publish) {
\r
198 $query .= ', idraft=0';
\r
200 // set timestamp to current date only if it's not a future item
\r
201 // draft items have timestamp == 0
\r
202 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
\r
203 if ($timestamp > $blog->getCorrectTime())
\r
206 if ($timestamp == 0)
\r
207 $timestamp = $blog->getCorrectTime();
\r
209 // send new item notification
\r
210 if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
\r
211 $blog->sendNewItemNotification($itemid, $title, $body);
\r
214 // update timestamp when needed
\r
215 if ($timestamp != 0)
\r
216 $query .= ", itime=" . mysqldate($timestamp);
\r
218 // make sure the correct item is updated
\r
219 $query .= ' WHERE inumber=' . $itemid;
\r
222 sql_query($query);
\r
224 // when needed, move item and comments to new blog
\r
226 ITEM::move($itemid, $catid);
\r
228 //update the itemOptions
\r
229 $aOptions = requestArray('plugoption');
\r
230 NucleusPlugin::_applyPluginOptions($aOptions);
\r
231 $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $title, 'body' => $body, 'more' => $more, 'closed' => $closed, 'catid' => $catid)));
\r
235 // move an item to another blog (no checks, static)
\r
236 function move($itemid, $new_catid) {
\r
239 $itemid = intval($itemid);
\r
240 $new_catid = intval($new_catid);
\r
242 $new_blogid = getBlogIDFromCatID($new_catid);
\r
247 'itemid' => $itemid,
\r
248 'destblogid' => $new_blogid,
\r
249 'destcatid' => $new_catid
\r
254 // update item table
\r
255 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
\r
256 sql_query($query);
\r
259 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
\r
260 sql_query($query);
\r
265 'itemid' => $itemid,
\r
266 'destblogid' => $new_blogid,
\r
267 'destcatid' => $new_catid
\r
275 function delete($itemid) {
\r
278 $itemid = intval($itemid);
\r
280 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
\r
283 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
\r
286 // delete the comments associated with the item
\r
287 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
\r
288 sql_query($query);
\r
290 // delete all associated plugin options
\r
291 NucleusPlugin::_deleteOptionValues('item', $itemid);
\r
293 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
\r
296 // returns true if there is an item with the given ID (static)
\r
297 function exists($id,$future,$draft) {
\r
302 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
\r
304 $bid = getBlogIDFromItemID($id);
\r
305 if (!$bid) return 0;
\r
306 $b =& $manager->getBlog($bid);
\r
307 $r .= ' and itime<='.mysqldate($b->getCorrectTime());
\r
310 $r .= ' and idraft=0';
\r
312 $r = sql_query($r);
\r
314 return (mysql_num_rows($r) != 0);
\r