4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2010 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-2010 The Nucleus Group
\r
19 * $NucleusJP: ITEM.php,v 1.7.2.3 2008/02/07 06:13:30 kimitake Exp $
\r
26 * Constructor of an ITEM object
\r
28 * @param integer $itemid id of the item
\r
30 function ITEM($itemid) {
\r
31 $this->itemid = $itemid;
\r
35 * Returns one item with the specific itemid
\r
37 * @param integer $itemid id of the item
\r
38 * @param boolean $allowdraft
\r
39 * @param boolean $allowfuture
\r
42 function getitem($itemid, $allowdraft, $allowfuture) {
\r
45 $itemid = intval($itemid);
\r
47 $query = 'SELECT i.idraft as draft, i.inumber as itemid, i.iclosed as closed, '
\r
48 . ' i.ititle as title, i.ibody as body, m.mname as author, '
\r
49 . ' i.iauthor as authorid, i.itime, i.imore as more, i.ikarmapos as karmapos, '
\r
50 . ' i.ikarmaneg as karmaneg, i.icat as catid, i.iblog as blogid '
\r
51 . ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, ' . sql_table('blog') . ' as b '
\r
52 . ' WHERE i.inumber=' . $itemid
\r
53 . ' and i.iauthor=m.mnumber '
\r
54 . ' and i.iblog=b.bnumber';
\r
57 $query .= ' and i.idraft=0';
\r
59 if (!$allowfuture) {
\r
60 $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
\r
61 $query .= ' and i.itime <=' . mysqldate($blog->getCorrectTime());
\r
64 $query .= ' LIMIT 1';
\r
66 $res = sql_query($query);
\r
68 if (sql_num_rows($res) == 1)
\r
70 $aItemInfo = sql_fetch_assoc($res);
\r
71 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
\r
80 * Tries to create an item from the data in the current request (comes from
\r
81 * bookmarklet or admin area
\r
83 * Returns an array with status info:
\r
84 * status = 'added', 'error', 'newcategory'
\r
88 function createFromRequest() {
\r
89 global $member, $manager;
\r
91 $i_author = $member->getID();
\r
92 $i_body = postVar('body');
\r
93 $i_title = postVar('title');
\r
94 $i_more = postVar('more');
\r
95 $i_actiontype = postVar('actiontype');
\r
96 $i_closed = intPostVar('closed');
\r
97 $i_hour = intPostVar('hour');
\r
98 $i_minutes = intPostVar('minutes');
\r
99 $i_month = intPostVar('month');
\r
100 $i_day = intPostVar('day');
\r
101 $i_year = intPostVar('year');
\r
103 $i_catid = postVar('catid');
\r
105 $i_draftid = intPostVar('draftid');
\r
107 if (!$member->canAddItem($i_catid))
\r
108 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
110 if (!$i_actiontype) $i_actiontype = 'addnow';
\r
112 switch ($i_actiontype) {
\r
122 if (!trim($i_body))
\r
123 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
125 // create new category if needed
\r
126 if (strstr($i_catid,'newcat')) {
\r
128 list($i_blogid) = sscanf($i_catid,"newcat-%d");
\r
131 $blog =& $manager->getBlog($i_blogid);
\r
132 $i_catid = $blog->createNewCategory();
\r
134 // show error when sth goes wrong
\r
136 return array('status' => 'error','message' => 'Could not create new category');
\r
138 // force blogid (must be same as category id)
\r
139 $i_blogid = getBlogIDFromCatID($i_catid);
\r
140 $blog =& $manager->getBlog($i_blogid);
\r
143 if ($i_actiontype == 'addfuture') {
\r
144 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
\r
146 // make sure the date is in the future, unless we allow past dates
\r
147 if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()))
\r
148 $posttime = $blog->getCorrectTime();
\r
150 // time with offset, or 0 for drafts
\r
151 $posttime = $i_draft ? 0 : $blog->getCorrectTime();
\r
154 if ($posttime > $blog->getCorrectTime()) {
\r
156 $blog->setFuturePost();
\r
162 $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft,$posted);
\r
164 //Setting the itemOptions
\r
165 $aOptions = requestArray('plugoption');
\r
166 NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
\r
167 $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
169 if ($i_draftid > 0) {
\r
170 // delete permission is checked inside ITEM::delete()
\r
171 ITEM::delete($i_draftid);
\r
175 if ($i_catid != intRequestVar('catid'))
\r
176 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
\r
178 return array('status' => 'added', 'itemid' => $itemid);
\r
187 function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
\r
190 $itemid = intval($itemid);
\r
192 // make sure value is 1 or 0
\r
193 if ($closed != 1) $closed = 0;
\r
195 // get destination blogid
\r
196 $new_blogid = getBlogIDFromCatID($catid);
\r
197 $old_blogid = getBlogIDFromItemID($itemid);
\r
199 // move will be done on end of method
\r
200 if ($new_blogid != $old_blogid)
\r
203 // add <br /> before newlines
\r
204 $blog =& $manager->getBlog($new_blogid);
\r
205 if ($blog->convertBreaks()) {
\r
206 $body = addBreaks($body);
\r
207 $more = addBreaks($more);
\r
211 $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid));
\r
213 // update item itsself
\r
214 $query = 'UPDATE '.sql_table('item')
\r
216 . " ibody='". sql_real_escape_string($body) ."',"
\r
217 . " ititle='" . sql_real_escape_string($title) . "',"
\r
218 . " imore='" . sql_real_escape_string($more) . "',"
\r
219 . " iclosed=" . intval($closed) . ","
\r
220 . " icat=" . intval($catid);
\r
222 // if we received an updated timestamp in the past, but past posting is not allowed,
\r
223 // reject that date change (timestamp = 0 will make sure the current date is kept)
\r
224 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
\r
227 if ($timestamp > $blog->getCorrectTime(time())) {
\r
229 $query .= ', iposted=0';
\r
233 $query .= ', iposted=1';
\r
236 if ($wasdraft && $publish) {
\r
237 // set timestamp to current date only if it's not a future item
\r
238 // draft items have timestamp == 0
\r
239 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
\r
240 $query .= ', idraft=0';
\r
242 if ($timestamp == 0)
\r
243 $timestamp = $blog->getCorrectTime();
\r
245 // send new item notification
\r
246 if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
\r
247 $blog->sendNewItemNotification($itemid, $title, $body);
\r
250 // save back to drafts
\r
251 if (!$wasdraft && !$publish) {
\r
252 $query .= ', idraft=1';
\r
253 // set timestamp back to zero for a draft
\r
254 $query .= ", itime=" . mysqldate($timestamp);
\r
257 // update timestamp when needed
\r
258 if ($timestamp != 0)
\r
259 $query .= ", itime=" . mysqldate($timestamp);
\r
261 // make sure the correct item is updated
\r
262 $query .= ' WHERE inumber=' . $itemid;
\r
267 $manager->notify('PostUpdateItem',array('itemid' => $itemid));
\r
269 // when needed, move item and comments to new blog
\r
271 ITEM::move($itemid, $catid);
\r
273 //update the itemOptions
\r
274 $aOptions = requestArray('plugoption');
\r
275 NucleusPlugin::_applyPluginOptions($aOptions);
\r
276 $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $title, 'body' => $body, 'more' => $more, 'closed' => $closed, 'catid' => $catid)));
\r
281 * Move an item to another blog (no checks)
\r
285 function move($itemid, $new_catid) {
\r
288 $itemid = intval($itemid);
\r
289 $new_catid = intval($new_catid);
\r
291 $new_blogid = getBlogIDFromCatID($new_catid);
\r
296 'itemid' => $itemid,
\r
297 'destblogid' => $new_blogid,
\r
298 'destcatid' => $new_catid
\r
303 // update item table
\r
304 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
\r
308 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
\r
314 'itemid' => $itemid,
\r
315 'destblogid' => $new_blogid,
\r
316 'destcatid' => $new_catid
\r
324 function delete($itemid) {
\r
325 global $manager, $member;
\r
327 $itemid = intval($itemid);
\r
329 // check to ensure only those allow to alter the item can
\r
331 if (!$member->canAlterItem($itemid)) {
\r
336 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
\r
339 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
\r
342 // delete the comments associated with the item
\r
343 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
\r
346 // delete all associated plugin options
\r
347 NucleusPlugin::_deleteOptionValues('item', $itemid);
\r
349 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
\r
355 * Returns true if there is an item with the given ID
\r
359 function exists($id,$future,$draft) {
\r
364 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
\r
366 $bid = getBlogIDFromItemID($id);
\r
367 if (!$bid) return 0;
\r
368 $b =& $manager->getBlog($bid);
\r
369 $r .= ' and itime<='.mysqldate($b->getCorrectTime());
\r
372 $r .= ' and idraft=0';
\r
374 $r = sql_query($r);
\r
376 return (sql_num_rows($r) != 0);
\r
380 * Tries to create an draft from the data in the current request (comes from
\r
381 * bookmarklet or admin area
\r
383 * Returns an array with status info:
\r
384 * status = 'added', 'error', 'newcategory'
\r
388 * Used by xmlHTTPRequest AutoDraft
\r
390 function createDraftFromRequest() {
\r
391 global $member, $manager;
\r
393 $i_author = $member->getID();
\r
394 $i_body = postVar('body');
\r
395 $i_title = postVar('title');
\r
396 $i_more = postVar('more');
\r
398 if(strtoupper(_CHARSET) != 'UTF-8'){
\r
399 $i_body = mb_convert_encoding($i_body, _CHARSET, "UTF-8");
\r
400 $i_title = mb_convert_encoding($i_title, _CHARSET, "UTF-8");
\r
401 $i_more = mb_convert_encoding($i_more, _CHARSET, "UTF-8");
\r
403 //$i_actiontype = postVar('actiontype');
\r
404 $i_closed = intPostVar('closed');
\r
405 //$i_hour = intPostVar('hour');
\r
406 //$i_minutes = intPostVar('minutes');
\r
407 //$i_month = intPostVar('month');
\r
408 //$i_day = intPostVar('day');
\r
409 //$i_year = intPostVar('year');
\r
410 $i_catid = postVar('catid');
\r
412 $type = postVar('type');
\r
413 if ($type == 'edit') {
\r
414 $i_blogid = getBlogIDFromItemID(intPostVar('itemid'));
\r
417 $i_blogid = intPostVar('blogid');
\r
419 $i_draftid = intPostVar('draftid');
\r
421 if (!$member->canAddItem($i_catid)) {
\r
422 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
425 if (!trim($i_body)) {
\r
426 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
429 // create new category if needed
\r
430 if (strstr($i_catid, 'newcat')) {
\r
431 // Set in default category
\r
432 $blog =& $manager->getBlog($i_blogid);
\r
433 $i_catid = $blog->getDefaultCategory();
\r
436 // force blogid (must be same as category id)
\r
437 $i_blogid = getBlogIDFromCatID($i_catid);
\r
438 $blog =& $manager->getBlog($i_blogid);
\r
443 if ($i_draftid > 0) {
\r
444 ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
\r
445 $itemid = $i_draftid;
\r
448 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
\r
451 // No plugin support in AutoSaveDraft yet
\r
452 //Setting the itemOptions
\r
453 //$aOptions = requestArray('plugoption');
\r
454 //NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
\r
455 //$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
458 return array('status' => 'added', 'draftid' => $itemid);
\r