4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2004 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)
13 * A class representing an item
15 * $Id: ITEM.php,v 1.1.1.1 2005-02-28 07:14:50 kimitake Exp $
21 function ITEM($itemid) {
22 $this->itemid = $itemid;
26 * Returns one item with the specific itemid
29 function getitem($itemid, $allowdraft, $allowfuture) {
32 $itemid = intval($itemid);
34 $query = 'SELECT i.idraft as draft, i.inumber as itemid, i.iclosed as closed, '
35 . ' i.ititle as title, i.ibody as body, m.mname as author, '
36 . ' i.iauthor as authorid, i.itime, i.imore as more, i.ikarmapos as karmapos, '
37 . ' i.ikarmaneg as karmaneg, i.icat as catid, i.iblog as blogid '
38 . ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, ' . sql_table('blog') . ' as b '
39 . ' WHERE i.inumber=' . $itemid
40 . ' and i.iauthor=m.mnumber '
41 . ' and i.iblog=b.bnumber';
44 $query .= ' and i.idraft=0';
47 $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
48 $query .= ' and i.itime <=' . mysqldate($blog->getCorrectTime());
53 $res = sql_query($query);
55 if (mysql_num_rows($res) == 1)
57 $aItemInfo = mysql_fetch_assoc($res);
58 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
67 * Tries to create an item from the data in the current request (comes from
68 * bookmarklet or admin area
70 * Returns an array with status info (status = 'added', 'error', 'newcategory')
74 function createFromRequest() {
75 global $member, $manager;
77 $i_author = $member->getID();
78 $i_body = postVar('body');
79 $i_title = postVar('title');
80 $i_more = postVar('more');
81 $i_actiontype = postVar('actiontype');
82 $i_closed = intPostVar('closed');
83 $i_hour = intPostVar('hour');
84 $i_minutes = intPostVar('minutes');
85 $i_month = intPostVar('month');
86 $i_day = intPostVar('day');
87 $i_year = intPostVar('year');
89 $i_catid = postVar('catid');
91 if (!$member->canAddItem($i_catid))
92 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
94 if (!$i_actiontype) $i_actiontype = 'addnow';
96 switch ($i_actiontype) {
107 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
109 // create new category if needed
110 if (strstr($i_catid,'newcat')) {
112 list($i_blogid) = sscanf($i_catid,"newcat-%d");
115 $blog =& $manager->getBlog($i_blogid);
116 $i_catid = $blog->createNewCategory();
118 // show error when sth goes wrong
120 return array('status' => 'error','message' => 'Could not create new category');
122 // force blogid (must be same as category id)
123 $i_blogid = getBlogIDFromCatID($i_catid);
124 $blog =& $manager->getBlog($i_blogid);
127 if ($i_actiontype == 'addfuture') {
128 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
130 // make sure the date is in the future, unless we allow past dates
131 if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()))
132 $posttime = $blog->getCorrectTime();
134 // time with offset, or 0 for drafts
135 $posttime = $i_draft ? 0 : $blog->getCorrectTime();
138 $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft);
140 //Setting the itemOptions
141 $aOptions = requestArray('plugoption');
142 NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
143 $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)));
146 if ($i_catid != intRequestVar('catid'))
147 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
149 return array('status' => 'added', 'itemid' => $itemid);
154 * Updates an item (static)
156 function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
159 $itemid = intval($itemid);
161 // make sure value is 1 or 0
162 if ($closed != 1) $closed = 0;
164 // get destination blogid
165 $new_blogid = getBlogIDFromCatID($catid);
166 $old_blogid = getBlogIDFromItemID($itemid);
168 // move will be done on end of method
169 if ($new_blogid != $old_blogid)
172 // add <br /> before newlines
173 $blog =& $manager->getBlog($new_blogid);
174 if ($blog->convertBreaks()) {
175 $body = addBreaks($body);
176 $more = addBreaks($more);
180 $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid));
182 // update item itsself
183 $query = 'UPDATE '.sql_table('item')
185 . " ibody='". addslashes($body) ."',"
186 . " ititle='" . addslashes($title) . "',"
187 . " imore='" . addslashes($more) . "',"
188 . " iclosed=" . intval($closed) . ","
189 . " icat=" . intval($catid);
191 // if we received an updated timestamp in the past, but past posting is not allowed,
192 // reject that date change (timestamp = 0 will make sure the current date is kept)
193 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
196 if ($wasdraft && $publish) {
197 $query .= ', idraft=0';
199 // set timestamp to current date only if it's not a future item
200 // draft items have timestamp == 0
201 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
202 if ($timestamp > $blog->getCorrectTime())
206 $timestamp = $blog->getCorrectTime();
208 // send new item notification
209 if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
210 $blog->sendNewItemNotification($itemid, $title, $body);
213 // update timestamp when needed
215 $query .= ", itime=" . mysqldate($timestamp);
217 // make sure the correct item is updated
218 $query .= ' WHERE inumber=' . $itemid;
223 // when needed, move item and comments to new blog
225 ITEM::move($itemid, $catid);
227 //update the itemOptions
228 $aOptions = requestArray('plugoption');
229 NucleusPlugin::_applyPluginOptions($aOptions);
230 $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $title, 'body' => $body, 'more' => $more, 'closed' => $closed, 'catid' => $catid)));
234 // move an item to another blog (no checks, static)
235 function move($itemid, $new_catid) {
238 $itemid = intval($itemid);
239 $new_catid = intval($new_catid);
241 $new_blogid = getBlogIDFromCatID($new_catid);
247 'destblogid' => $new_blogid,
248 'destcatid' => $new_catid
254 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
258 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
265 'destblogid' => $new_blogid,
266 'destcatid' => $new_catid
274 function delete($itemid) {
277 $itemid = intval($itemid);
279 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
282 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
285 // delete the comments associated with the item
286 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
289 // delete all associated plugin options
290 NucleusPlugin::_deleteOptionValues('item', $itemid);
292 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
295 // returns true if there is an item with the given ID (static)
296 function exists($id,$future,$draft) {
301 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
303 $bid = getBlogIDFromItemID($id);
305 $b =& $manager->getBlog($bid);
306 $r .= ' and itime<='.mysqldate($b->getCorrectTime());
309 $r .= ' and idraft=0';
313 return (mysql_num_rows($r) != 0);