4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2012 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
20 * Constructor of an ITEM object
\r
22 * @param integer $itemid id of the item
\r
24 function ITEM($itemid) {
\r
25 $this->itemid = $itemid;
\r
29 * Returns one item with the specific itemid
\r
31 * @param integer $itemid id of the item
\r
32 * @param boolean $allowdraft
\r
33 * @param boolean $allowfuture
\r
36 function getitem($itemid, $allowdraft, $allowfuture) {
\r
39 $itemid = intval($itemid);
\r
41 $query = 'SELECT i.idraft as draft, i.inumber as itemid, i.iclosed as closed, '
\r
42 . ' i.ititle as title, i.ibody as body, m.mname as author, '
\r
43 . ' i.iauthor as authorid, i.itime, i.imore as more, i.ikarmapos as karmapos, '
\r
44 . ' i.ikarmaneg as karmaneg, i.icat as catid, i.iblog as blogid '
\r
45 . ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, ' . sql_table('blog') . ' as b '
\r
46 . ' WHERE i.inumber=' . $itemid
\r
47 . ' and i.iauthor=m.mnumber '
\r
48 . ' and i.iblog=b.bnumber';
\r
51 $query .= ' and i.idraft=0';
\r
53 if (!$allowfuture) {
\r
54 $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
\r
55 $query .= ' and i.itime <=' . mysqldate($blog->getCorrectTime());
\r
58 $query .= ' LIMIT 1';
\r
60 $res = sql_query($query);
\r
62 if (sql_num_rows($res) == 1)
\r
64 $aItemInfo = sql_fetch_assoc($res);
\r
65 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
\r
74 * Tries to create an item from the data in the current request (comes from
\r
75 * bookmarklet or admin area
\r
77 * Returns an array with status info:
\r
78 * status = 'added', 'error', 'newcategory'
\r
82 function createFromRequest() {
\r
83 global $member, $manager;
\r
85 $i_author = $member->getID();
\r
86 $i_body = postVar('body');
\r
87 $i_title = postVar('title');
\r
88 $i_more = postVar('more');
\r
89 $i_actiontype = postVar('actiontype');
\r
90 $i_closed = intPostVar('closed');
\r
91 $i_hour = intPostVar('hour');
\r
92 $i_minutes = intPostVar('minutes');
\r
93 $i_month = intPostVar('month');
\r
94 $i_day = intPostVar('day');
\r
95 $i_year = intPostVar('year');
\r
97 $i_catid = postVar('catid');
\r
99 $i_draftid = intPostVar('draftid');
\r
101 if (!$member->canAddItem($i_catid))
\r
102 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
104 if (!$i_actiontype) $i_actiontype = 'addnow';
\r
106 switch ($i_actiontype) {
\r
116 if (!trim($i_body))
\r
117 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
119 // create new category if needed
\r
120 if (strstr($i_catid,'newcat')) {
\r
122 list($i_blogid) = sscanf($i_catid,"newcat-%d");
\r
125 $blog =& $manager->getBlog($i_blogid);
\r
126 $i_catid = $blog->createNewCategory();
\r
128 // show error when sth goes wrong
\r
130 return array('status' => 'error','message' => 'Could not create new category');
\r
132 // force blogid (must be same as category id)
\r
133 $i_blogid = getBlogIDFromCatID($i_catid);
\r
134 $blog =& $manager->getBlog($i_blogid);
\r
137 if ($i_actiontype == 'addfuture') {
\r
138 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
\r
140 // make sure the date is in the future, unless we allow past dates
\r
141 if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()))
\r
142 $posttime = $blog->getCorrectTime();
\r
144 // time with offset, or 0 for drafts
\r
145 $posttime = $i_draft ? 0 : $blog->getCorrectTime();
\r
148 if ($posttime > $blog->getCorrectTime()) {
\r
150 $blog->setFuturePost();
\r
156 $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft,$posted);
\r
158 //Setting the itemOptions
\r
159 $aOptions = requestArray('plugoption');
\r
160 NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
\r
162 'context' => 'item',
\r
163 'itemid' => $itemid,
\r
165 'title' => $i_title,
\r
168 'closed' => $i_closed,
\r
169 'catid' => $i_catid)
\r
171 $manager->notify('PostPluginOptionsUpdate', $param);
\r
173 if ($i_draftid > 0) {
\r
174 // delete permission is checked inside ITEM::delete()
\r
175 ITEM::delete($i_draftid);
\r
179 if ($i_catid != intRequestVar('catid'))
\r
180 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
\r
182 return array('status' => 'added', 'itemid' => $itemid);
\r
191 function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
\r
194 $itemid = intval($itemid);
\r
196 // make sure value is 1 or 0
\r
197 if ($closed != 1) $closed = 0;
\r
199 // get destination blogid
\r
200 $new_blogid = getBlogIDFromCatID($catid);
\r
201 $old_blogid = getBlogIDFromItemID($itemid);
\r
203 // move will be done on end of method
\r
204 if ($new_blogid != $old_blogid)
\r
207 // add <br /> before newlines
\r
208 $blog =& $manager->getBlog($new_blogid);
\r
209 if ($blog->convertBreaks()) {
\r
210 $body = addBreaks($body);
\r
211 $more = addBreaks($more);
\r
216 ' itemid' => $itemid,
\r
217 'title' => &$title,
\r
221 'closed' => &$closed,
\r
224 $manager->notify('PreUpdateItem', $param);
\r
226 // update item itsself
\r
227 $query = 'UPDATE '.sql_table('item')
\r
229 . " ibody='". sql_real_escape_string($body) ."',"
\r
230 . " ititle='" . sql_real_escape_string($title) . "',"
\r
231 . " imore='" . sql_real_escape_string($more) . "',"
\r
232 . " iclosed=" . intval($closed) . ","
\r
233 . " icat=" . intval($catid);
\r
235 // if we received an updated timestamp in the past, but past posting is not allowed,
\r
236 // reject that date change (timestamp = 0 will make sure the current date is kept)
\r
237 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
\r
240 if ($timestamp > $blog->getCorrectTime(time())) {
\r
242 $query .= ', iposted=0';
\r
246 $query .= ', iposted=1';
\r
249 if ($wasdraft && $publish) {
\r
250 // set timestamp to current date only if it's not a future item
\r
251 // draft items have timestamp == 0
\r
252 // don't allow timestamps in the past (unless otherwise defined in blogsettings)
\r
253 $query .= ', idraft=0';
\r
255 if ($timestamp == 0)
\r
256 $timestamp = $blog->getCorrectTime();
\r
258 // send new item notification
\r
259 if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
\r
260 $blog->sendNewItemNotification($itemid, $title, $body);
\r
263 // save back to drafts
\r
264 if (!$wasdraft && !$publish) {
\r
265 $query .= ', idraft=1';
\r
266 // set timestamp back to zero for a draft
\r
267 $query .= ", itime=" . mysqldate($timestamp);
\r
270 // update timestamp when needed
\r
271 if ($timestamp != 0)
\r
272 $query .= ", itime=" . mysqldate($timestamp);
\r
274 // make sure the correct item is updated
\r
275 $query .= ' WHERE inumber=' . $itemid;
\r
280 $param = array('itemid' => $itemid);
\r
281 $manager->notify('PostUpdateItem', $param);
\r
283 // when needed, move item and comments to new blog
\r
285 ITEM::move($itemid, $catid);
\r
287 //update the itemOptions
\r
288 $aOptions = requestArray('plugoption');
\r
289 NucleusPlugin::_applyPluginOptions($aOptions);
\r
291 'context' => 'item',
\r
292 'itemid' => $itemid,
\r
297 'closed' => $closed,
\r
301 $manager->notify('PostPluginOptionsUpdate', $param);
\r
306 * Move an item to another blog (no checks)
\r
310 function move($itemid, $new_catid) {
\r
313 $itemid = intval($itemid);
\r
314 $new_catid = intval($new_catid);
\r
316 $new_blogid = getBlogIDFromCatID($new_catid);
\r
319 'itemid' => $itemid,
\r
320 'destblogid' => $new_blogid,
\r
321 'destcatid' => $new_catid
\r
323 $manager->notify('PreMoveItem', $param);
\r
326 // update item table
\r
327 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
\r
331 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
\r
335 'itemid' => $itemid,
\r
336 'destblogid' => $new_blogid,
\r
337 'destcatid' => $new_catid
\r
339 $manager->notify('PostMoveItem', $param);
\r
345 function delete($itemid) {
\r
346 global $manager, $member;
\r
348 $itemid = intval($itemid);
\r
350 // check to ensure only those allow to alter the item can
\r
352 if (!$member->canAlterItem($itemid)) {
\r
356 $param = array('itemid' => $itemid);
\r
357 $manager->notify('PreDeleteItem', $param);
\r
360 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
\r
363 // delete the comments associated with the item
\r
364 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
\r
367 // delete all associated plugin options
\r
368 NucleusPlugin::_deleteOptionValues('item', $itemid);
\r
370 $param = array('itemid' => $itemid);
\r
371 $manager->notify('PostDeleteItem', $param);
\r
377 * Returns true if there is an item with the given ID
\r
381 function exists($id,$future,$draft) {
\r
386 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
\r
388 $bid = getBlogIDFromItemID($id);
\r
389 if (!$bid) return 0;
\r
390 $b =& $manager->getBlog($bid);
\r
391 $r .= ' and itime<='.mysqldate($b->getCorrectTime());
\r
394 $r .= ' and idraft=0';
\r
396 $r = sql_query($r);
\r
398 return (sql_num_rows($r) != 0);
\r
402 * Tries to create an draft from the data in the current request (comes from
\r
403 * bookmarklet or admin area
\r
405 * Returns an array with status info:
\r
406 * status = 'added', 'error', 'newcategory'
\r
410 * Used by xmlHTTPRequest AutoDraft
\r
412 function createDraftFromRequest() {
\r
413 global $member, $manager;
\r
415 $i_author = $member->getID();
\r
416 $i_body = postVar('body');
\r
417 $i_title = postVar('title');
\r
418 $i_more = postVar('more');
\r
420 if(strtoupper(_CHARSET) != 'UTF-8'){
\r
421 $i_body = mb_convert_encoding($i_body, _CHARSET, "UTF-8");
\r
422 $i_title = mb_convert_encoding($i_title, _CHARSET, "UTF-8");
\r
423 $i_more = mb_convert_encoding($i_more, _CHARSET, "UTF-8");
\r
425 //$i_actiontype = postVar('actiontype');
\r
426 $i_closed = intPostVar('closed');
\r
427 //$i_hour = intPostVar('hour');
\r
428 //$i_minutes = intPostVar('minutes');
\r
429 //$i_month = intPostVar('month');
\r
430 //$i_day = intPostVar('day');
\r
431 //$i_year = intPostVar('year');
\r
432 $i_catid = postVar('catid');
\r
434 $type = postVar('type');
\r
435 if ($type == 'edit') {
\r
436 $i_blogid = getBlogIDFromItemID(intPostVar('itemid'));
\r
439 $i_blogid = intPostVar('blogid');
\r
441 $i_draftid = intPostVar('draftid');
\r
443 if (!$member->canAddItem($i_catid)) {
\r
444 return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
\r
447 if (!trim($i_body)) {
\r
448 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
\r
451 // create new category if needed
\r
452 if (strstr($i_catid, 'newcat')) {
\r
453 // Set in default category
\r
454 $blog =& $manager->getBlog($i_blogid);
\r
455 $i_catid = $blog->getDefaultCategory();
\r
458 // force blogid (must be same as category id)
\r
459 $i_blogid = getBlogIDFromCatID($i_catid);
\r
460 $blog =& $manager->getBlog($i_blogid);
\r
465 if ($i_draftid > 0) {
\r
466 ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
\r
467 $itemid = $i_draftid;
\r
470 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
\r
474 // No plugin support in AutoSaveDraft yet
\r
475 //Setting the itemOptions
\r
476 $aOptions = requestArray('plugoption');
\r
477 NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
\r
479 'context' => 'item',
\r
480 'itemid' => $itemid,
\r
482 'title' => $i_title,
\r
485 'closed' => $i_closed,
\r
486 'catid' => $i_catid
\r
489 $manager->notify('PostPluginOptionsUpdate', $param);
\r
493 return array('status' => 'added', 'draftid' => $itemid);
\r