4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2004 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
19 function ITEM($itemid) {
\r
20 $this->itemid = $itemid;
\r
24 * Returns one item with the specific itemid
\r
27 function getitem($itemid, $allowdraft, $allowfuture) {
\r
30 $itemid = intval($itemid);
\r
32 $query = 'SELECT i.idraft as draft, i.inumber as itemid, i.iclosed as closed, '
\r
33 . ' i.ititle as title, i.ibody as body, m.mname as author, '
\r
34 . ' i.iauthor as authorid, i.itime, i.imore as more, i.ikarmapos as karmapos, '
\r
35 . ' i.ikarmaneg as karmaneg, i.icat as catid, i.iblog as blogid '
\r
36 . ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, ' . sql_table('blog') . ' as b '
\r
37 . ' WHERE i.inumber=' . $itemid
\r
38 . ' and i.iauthor=m.mnumber '
\r
39 . ' and i.iblog=b.bnumber';
\r
42 $query .= ' and i.idraft=0';
\r
44 if (!$allowfuture) {
\r
45 $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
\r
46 $query .= ' and i.itime <=' . mysqldate($blog->getCorrectTime());
\r
49 $query .= ' LIMIT 1';
\r
51 $res = sql_query($query);
\r
53 if (mysql_num_rows($res) == 1)
\r
55 $aItemInfo = mysql_fetch_assoc($res);
\r
56 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
\r
65 * Tries to create an item from the data in the current request (comes from
\r
66 * bookmarklet or admin area
\r
68 * Returns an array with status info (status = 'added', 'error', 'newcategory')
\r
72 function createFromRequest() {
\r
73 global $member, $manager;
\r
75 $i_author = $member->getID();
\r
76 $i_body = postVar('body');
\r
77 $i_title = postVar('title');
\r
78 $i_more = postVar('more');
\r
79 $i_actiontype = postVar('actiontype');
\r
80 $i_closed = intPostVar('closed');
\r
81 $i_hour = intPostVar('hour');
\r
82 $i_minutes = intPostVar('minutes');
\r
83 $i_month = intPostVar('month');
\r
84 $i_day = intPostVar('day');
\r
85 $i_year = intPostVar('year');
\r
87 $i_catid = postVar('catid');
\r
89 if (!$member->canAddItem($i_catid))
\r
90 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
92 if (!$i_actiontype) $i_actiontype = 'addnow';
\r
94 switch ($i_actiontype) {
\r
104 if (!trim($i_body))
\r
105 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
107 // create new category if needed
\r
108 if (strstr($i_catid,'newcat')) {
\r
110 list($i_blogid) = sscanf($i_catid,"newcat-%d");
\r
113 $blog =& $manager->getBlog($i_blogid);
\r
114 $i_catid = $blog->createNewCategory();
\r
116 // show error when sth goes wrong
\r
118 return array('status' => 'error','message' => 'Could not create new category');
\r
120 // force blogid (must be same as category id)
\r
121 $i_blogid = getBlogIDFromCatID($i_catid);
\r
122 $blog =& $manager->getBlog($i_blogid);
\r
125 if ($i_actiontype == 'addfuture') {
\r
126 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
\r
128 // make sure the date is in the future, unless we allow past dates
\r
129 if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()))
\r
130 $posttime = $blog->getCorrectTime();
\r
132 // time with offset, or 0 for drafts
\r
133 $posttime = $i_draft ? 0 : $blog->getCorrectTime();
\r
136 $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft);
\r
139 if ($i_catid != intRequestVar('catid'))
\r
140 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
\r
142 return array('status' => 'added', 'itemid' => $itemid);
\r
147 * Updates an item (static)
\r
149 function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
\r
152 $itemid = intval($itemid);
\r
154 // make sure value is 1 or 0
\r
155 if ($closed != 1) $closed = 0;
\r
157 // get destination blogid
\r
158 $new_blogid = getBlogIDFromCatID($catid);
\r
159 $old_blogid = getBlogIDFromItemID($itemid);
\r
161 // move will be done on end of method
\r
162 if ($new_blogid != $old_blogid)
\r
165 // add <br /> before newlines
\r
166 $blog =& $manager->getBlog($new_blogid);
\r
167 if ($blog->convertBreaks()) {
\r
168 $body = addBreaks($body);
\r
169 $more = addBreaks($more);
\r
173 $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid));
\r
175 // update item itsself
\r
176 $query = 'UPDATE '.sql_table('item')
\r
178 . " ibody='". addslashes($body) ."',"
\r
179 . " ititle='" . addslashes($title) . "',"
\r
180 . " imore='" . addslashes($more) . "',"
\r
181 . " iclosed=" . intval($closed) . ","
\r
182 . " icat=" . intval($catid);
\r
184 // if we received an updated timestamp in the past, but past posting is not allowed,
\r
185 // reject that date change (timestamp = 0 will make sure the current date is kept)
\r
186 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
\r
189 if ($wasdraft && $publish) {
\r
190 $query .= ', idraft=0';
\r
192 // set timestamp to current date only if it's not a future item
\r
193 // draft items have timestamp == 0
\r
194 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
\r
195 if ($timestamp > $blog->getCorrectTime())
\r
198 if ($timestamp == 0)
\r
199 $timestamp = $blog->getCorrectTime();
\r
201 // send new item notification
\r
202 if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
\r
203 $blog->sendNewItemNotification($itemid, $title, $body);
\r
206 // update timestamp when needed
\r
207 if ($timestamp != 0)
\r
208 $query .= ", itime=" . mysqldate($timestamp);
\r
210 // make sure the correct item is updated
\r
211 $query .= ' WHERE inumber=' . $itemid;
\r
214 sql_query($query);
\r
216 // when needed, move item and comments to new blog
\r
218 ITEM::move($itemid, $catid);
\r
222 // move an item to another blog (no checks, static)
\r
223 function move($itemid, $new_catid) {
\r
226 $itemid = intval($itemid);
\r
227 $new_catid = intval($new_catid);
\r
229 $new_blogid = getBlogIDFromCatID($new_catid);
\r
234 'itemid' => $itemid,
\r
235 'destblogid' => $new_blogid,
\r
236 'destcatid' => $new_catid
\r
241 // update item table
\r
242 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
\r
243 sql_query($query);
\r
246 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
\r
247 sql_query($query);
\r
252 'itemid' => $itemid,
\r
253 'destblogid' => $new_blogid,
\r
254 'destcatid' => $new_catid
\r
262 function delete($itemid) {
\r
265 $itemid = intval($itemid);
\r
267 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
\r
270 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
\r
273 // delete the comments associated with the item
\r
274 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
\r
275 sql_query($query);
\r
277 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
\r
280 // returns true if there is an item with the given ID (static)
\r
281 function exists($id,$future,$draft) {
\r
286 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
\r
288 $bid = getBlogIDFromItemID($id);
\r
289 if (!$bid) return 0;
\r
290 $b =& $manager->getBlog($bid);
\r
291 $r .= ' and itime<='.mysqldate($b->getCorrectTime());
\r
294 $r .= ' and idraft=0';
\r
296 $r = sql_query($r);
\r
298 return (mysql_num_rows($r) != 0);
\r