4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2007 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-2007 The Nucleus Group
\r
18 * @version $Id: ITEM.php,v 1.8 2008-02-08 09:31:22 kimitake Exp $
\r
19 * $NucleusJP: ITEM.php,v 1.7.2.3 2008/02/07 06:13:30 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 $i_draftid = intPostVar('draftid');
\r
97 if (!$member->canAddItem($i_catid))
\r
98 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
100 if (!$i_actiontype) $i_actiontype = 'addnow';
\r
102 switch ($i_actiontype) {
\r
112 if (!trim($i_body))
\r
113 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
115 // create new category if needed
\r
116 if (strstr($i_catid,'newcat')) {
\r
118 list($i_blogid) = sscanf($i_catid,"newcat-%d");
\r
121 $blog =& $manager->getBlog($i_blogid);
\r
122 $i_catid = $blog->createNewCategory();
\r
124 // show error when sth goes wrong
\r
126 return array('status' => 'error','message' => 'Could not create new category');
\r
128 // force blogid (must be same as category id)
\r
129 $i_blogid = getBlogIDFromCatID($i_catid);
\r
130 $blog =& $manager->getBlog($i_blogid);
\r
133 if ($i_actiontype == 'addfuture') {
\r
134 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
\r
136 // make sure the date is in the future, unless we allow past dates
\r
137 if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()))
\r
138 $posttime = $blog->getCorrectTime();
\r
140 // time with offset, or 0 for drafts
\r
141 $posttime = $i_draft ? 0 : $blog->getCorrectTime();
\r
144 if ($posttime > $blog->getCorrectTime()) {
\r
146 $blog->setFuturePost();
\r
152 $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft,$posted);
\r
154 //Setting the itemOptions
\r
155 $aOptions = requestArray('plugoption');
\r
156 NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
\r
157 $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
159 if ($i_draftid > 0) {
\r
160 // delete permission is checked inside ITEM::delete()
\r
161 ITEM::delete($i_draftid);
\r
165 if ($i_catid != intRequestVar('catid'))
\r
166 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
\r
168 return array('status' => 'added', 'itemid' => $itemid);
\r
173 * Updates an item (static)
\r
175 function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
\r
178 $itemid = intval($itemid);
\r
180 // make sure value is 1 or 0
\r
181 if ($closed != 1) $closed = 0;
\r
183 // get destination blogid
\r
184 $new_blogid = getBlogIDFromCatID($catid);
\r
185 $old_blogid = getBlogIDFromItemID($itemid);
\r
187 // move will be done on end of method
\r
188 if ($new_blogid != $old_blogid)
\r
191 // add <br /> before newlines
\r
192 $blog =& $manager->getBlog($new_blogid);
\r
193 if ($blog->convertBreaks()) {
\r
194 $body = addBreaks($body);
\r
195 $more = addBreaks($more);
\r
199 $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid));
\r
201 // update item itsself
\r
202 $query = 'UPDATE '.sql_table('item')
\r
204 . " ibody='". addslashes($body) ."',"
\r
205 . " ititle='" . addslashes($title) . "',"
\r
206 . " imore='" . addslashes($more) . "',"
\r
207 . " iclosed=" . intval($closed) . ","
\r
208 . " icat=" . intval($catid);
\r
210 // if we received an updated timestamp in the past, but past posting is not allowed,
\r
211 // reject that date change (timestamp = 0 will make sure the current date is kept)
\r
212 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
\r
215 if ($timestamp > $blog->getCorrectTime(time())) {
\r
217 $query .= ', iposted=0';
\r
221 $query .= ', iposted=1';
\r
224 if ($wasdraft && $publish) {
\r
225 // set timestamp to current date only if it's not a future item
\r
226 // draft items have timestamp == 0
\r
227 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
\r
228 $query .= ', idraft=0';
\r
230 if ($timestamp == 0)
\r
231 $timestamp = $blog->getCorrectTime();
\r
233 // send new item notification
\r
234 if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
\r
235 $blog->sendNewItemNotification($itemid, $title, $body);
\r
238 // update timestamp when needed
\r
239 if ($timestamp != 0)
\r
240 $query .= ", itime=" . mysqldate($timestamp);
\r
242 // make sure the correct item is updated
\r
243 $query .= ' WHERE inumber=' . $itemid;
\r
248 $manager->notify('PostUpdateItem',array('itemid' => $itemid));
\r
250 // when needed, move item and comments to new blog
\r
252 ITEM::move($itemid, $catid);
\r
254 //update the itemOptions
\r
255 $aOptions = requestArray('plugoption');
\r
256 NucleusPlugin::_applyPluginOptions($aOptions);
\r
257 $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $title, 'body' => $body, 'more' => $more, 'closed' => $closed, 'catid' => $catid)));
\r
261 // move an item to another blog (no checks, static)
\r
262 function move($itemid, $new_catid) {
\r
265 $itemid = intval($itemid);
\r
266 $new_catid = intval($new_catid);
\r
268 $new_blogid = getBlogIDFromCatID($new_catid);
\r
273 'itemid' => $itemid,
\r
274 'destblogid' => $new_blogid,
\r
275 'destcatid' => $new_catid
\r
280 // update item table
\r
281 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
\r
285 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
\r
291 'itemid' => $itemid,
\r
292 'destblogid' => $new_blogid,
\r
293 'destcatid' => $new_catid
\r
301 function delete($itemid) {
\r
302 global $manager, $member;
\r
304 $itemid = intval($itemid);
\r
305 // check to ensure only those allow to alter the item can
\r
307 if (!$member->canAlterItem($itemid)) {
\r
311 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
\r
314 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
\r
317 // delete the comments associated with the item
\r
318 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
\r
321 // delete all associated plugin options
\r
322 NucleusPlugin::_deleteOptionValues('item', $itemid);
\r
324 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
\r
327 // returns true if there is an item with the given ID (static)
\r
328 function exists($id,$future,$draft) {
\r
333 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
\r
335 $bid = getBlogIDFromItemID($id);
\r
336 if (!$bid) return 0;
\r
337 $b =& $manager->getBlog($bid);
\r
338 $r .= ' and itime<='.mysqldate($b->getCorrectTime());
\r
341 $r .= ' and idraft=0';
\r
343 $r = sql_query($r);
\r
345 return (mysql_num_rows($r) != 0);
\r
349 * Tries to create an draft from the data in the current request (comes from
\r
350 * bookmarklet or admin area
\r
352 * Returns an array with status info (status = 'added', 'error', 'newcategory')
\r
356 * Used by xmlHTTPRequest AutoDraft
\r
358 function createDraftFromRequest() {
\r
359 global $member, $manager;
\r
361 $i_author = $member->getID();
\r
362 $i_body = postVar('body');
\r
363 $i_title = postVar('title');
\r
364 $i_more = postVar('more');
\r
366 if(_CHARSET != 'UTF-8'){
\r
367 $i_body = mb_convert_encoding($i_body, _CHARSET, "UTF-8");
\r
368 $i_title = mb_convert_encoding($i_title, _CHARSET, "UTF-8");
\r
369 $i_more = mb_convert_encoding($i_more, _CHARSET, "UTF-8");
\r
371 //$i_actiontype = postVar('actiontype');
\r
372 $i_closed = intPostVar('closed');
\r
373 //$i_hour = intPostVar('hour');
\r
374 //$i_minutes = intPostVar('minutes');
\r
375 //$i_month = intPostVar('month');
\r
376 //$i_day = intPostVar('day');
\r
377 //$i_year = intPostVar('year');
\r
378 $i_catid = postVar('catid');
\r
380 $type = postVar('type');
\r
381 if ($type == 'edit') {
\r
382 $i_blogid = getBlogIDFromItemID(intPostVar('itemid'));
\r
385 $i_blogid = intPostVar('blogid');
\r
387 $i_draftid = intPostVar('draftid');
\r
389 if (!$member->canAddItem($i_catid)) {
\r
390 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
393 if (!trim($i_body)) {
\r
394 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
397 // create new category if needed
\r
398 if (strstr($i_catid, 'newcat')) {
\r
399 // Set in default category
\r
400 $blog =& $manager->getBlog($i_blogid);
\r
401 $i_catid = $blog->getDefaultCategory();
\r
404 // force blogid (must be same as category id)
\r
405 $i_blogid = getBlogIDFromCatID($i_catid);
\r
406 $blog =& $manager->getBlog($i_blogid);
\r
411 if ($i_draftid > 0) {
\r
412 ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
\r
413 $itemid = $i_draftid;
\r
416 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
\r
419 // No plugin support in AutoSaveDraft yet
\r
420 //Setting the itemOptions
\r
421 //$aOptions = requestArray('plugoption');
\r
422 //NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
\r
423 //$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
426 return array('status' => 'added', 'draftid' => $itemid);
\r