00001 <?php 00002 00003 /* 00004 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) 00005 * Copyright (C) 2002-2007 The Nucleus Group 00006 * 00007 * This program is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU General Public License 00009 * as published by the Free Software Foundation; either version 2 00010 * of the License, or (at your option) any later version. 00011 * (see nucleus/documentation/index.html#license for more info) 00012 */ 00021 class ITEM { 00022 00023 var $itemid; 00024 00025 function ITEM($itemid) { 00026 $this->itemid = $itemid; 00027 } 00028 00033 function getitem($itemid, $allowdraft, $allowfuture) { 00034 global $manager; 00035 00036 $itemid = intval($itemid); 00037 00038 $query = 'SELECT i.idraft as draft, i.inumber as itemid, i.iclosed as closed, ' 00039 . ' i.ititle as title, i.ibody as body, m.mname as author, ' 00040 . ' i.iauthor as authorid, i.itime, i.imore as more, i.ikarmapos as karmapos, ' 00041 . ' i.ikarmaneg as karmaneg, i.icat as catid, i.iblog as blogid ' 00042 . ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, ' . sql_table('blog') . ' as b ' 00043 . ' WHERE i.inumber=' . $itemid 00044 . ' and i.iauthor=m.mnumber ' 00045 . ' and i.iblog=b.bnumber'; 00046 00047 if (!$allowdraft) 00048 $query .= ' and i.idraft=0'; 00049 00050 if (!$allowfuture) { 00051 $blog =& $manager->getBlog(getBlogIDFromItemID($itemid)); 00052 $query .= ' and i.itime <=' . mysqldate($blog->getCorrectTime()); 00053 } 00054 00055 $query .= ' LIMIT 1'; 00056 00057 $res = sql_query($query); 00058 00059 if (mysql_num_rows($res) == 1) 00060 { 00061 $aItemInfo = mysql_fetch_assoc($res); 00062 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']); 00063 return $aItemInfo; 00064 } else { 00065 return 0; 00066 } 00067 00068 } 00069 00078 function createFromRequest() { 00079 global $member, $manager; 00080 00081 $i_author = $member->getID(); 00082 $i_body = postVar('body'); 00083 $i_title = postVar('title'); 00084 $i_more = postVar('more'); 00085 $i_actiontype = postVar('actiontype'); 00086 $i_closed = intPostVar('closed'); 00087 $i_hour = intPostVar('hour'); 00088 $i_minutes = intPostVar('minutes'); 00089 $i_month = intPostVar('month'); 00090 $i_day = intPostVar('day'); 00091 $i_year = intPostVar('year'); 00092 00093 $i_catid = postVar('catid'); 00094 00095 $i_draftid = intPostVar('draftid'); 00096 00097 if (!$member->canAddItem($i_catid)) 00098 return array('status' => 'error', 'message' => _ERROR_DISALLOWED); 00099 00100 if (!$i_actiontype) $i_actiontype = 'addnow'; 00101 00102 switch ($i_actiontype) { 00103 case 'adddraft': 00104 $i_draft = 1; 00105 break; 00106 case 'addfuture': 00107 case 'addnow': 00108 default: 00109 $i_draft = 0; 00110 } 00111 00112 if (!trim($i_body)) 00113 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS); 00114 00115 // create new category if needed 00116 if (strstr($i_catid,'newcat')) { 00117 // get blogid 00118 list($i_blogid) = sscanf($i_catid,"newcat-%d"); 00119 00120 // create 00121 $blog =& $manager->getBlog($i_blogid); 00122 $i_catid = $blog->createNewCategory(); 00123 00124 // show error when sth goes wrong 00125 if (!$i_catid) 00126 return array('status' => 'error','message' => 'Could not create new category'); 00127 } else { 00128 // force blogid (must be same as category id) 00129 $i_blogid = getBlogIDFromCatID($i_catid); 00130 $blog =& $manager->getBlog($i_blogid); 00131 } 00132 00133 if ($i_actiontype == 'addfuture') { 00134 $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year); 00135 00136 // make sure the date is in the future, unless we allow past dates 00137 if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime())) 00138 $posttime = $blog->getCorrectTime(); 00139 } else { 00140 // time with offset, or 0 for drafts 00141 $posttime = $i_draft ? 0 : $blog->getCorrectTime(); 00142 } 00143 00144 if ($posttime > $blog->getCorrectTime()) { 00145 $posted = 0; 00146 $blog->setFuturePost(); 00147 } 00148 else { 00149 $posted = 1; 00150 } 00151 00152 $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft,$posted); 00153 00154 //Setting the itemOptions 00155 $aOptions = requestArray('plugoption'); 00156 NucleusPlugin::_applyPluginOptions($aOptions, $itemid); 00157 $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))); 00158 00159 if ($i_draftid > 0) { 00160 ITEM::delete($i_draftid); 00161 } 00162 00163 // success 00164 if ($i_catid != intRequestVar('catid')) 00165 return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid); 00166 else 00167 return array('status' => 'added', 'itemid' => $itemid); 00168 } 00169 00170 00174 function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) { 00175 global $manager; 00176 00177 $itemid = intval($itemid); 00178 00179 // make sure value is 1 or 0 00180 if ($closed != 1) $closed = 0; 00181 00182 // get destination blogid 00183 $new_blogid = getBlogIDFromCatID($catid); 00184 $old_blogid = getBlogIDFromItemID($itemid); 00185 00186 // move will be done on end of method 00187 if ($new_blogid != $old_blogid) 00188 $moveNeeded = 1; 00189 00190 // add <br /> before newlines 00191 $blog =& $manager->getBlog($new_blogid); 00192 if ($blog->convertBreaks()) { 00193 $body = addBreaks($body); 00194 $more = addBreaks($more); 00195 } 00196 00197 // call plugins 00198 $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid)); 00199 00200 // update item itsself 00201 $query = 'UPDATE '.sql_table('item') 00202 . ' SET' 00203 . " ibody='". addslashes($body) ."'," 00204 . " ititle='" . addslashes($title) . "'," 00205 . " imore='" . addslashes($more) . "'," 00206 . " iclosed=" . intval($closed) . "," 00207 . " icat=" . intval($catid); 00208 00209 // if we received an updated timestamp in the past, but past posting is not allowed, 00210 // reject that date change (timestamp = 0 will make sure the current date is kept) 00211 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime())) 00212 $timestamp = 0; 00213 00214 if ($timestamp > $blog->getCorrectTime(time())) { 00215 $isFuture = 1; 00216 $query .= ', iposted=0'; 00217 } 00218 else { 00219 $isFuture = 0; 00220 $query .= ', iposted=1'; 00221 } 00222 00223 if ($wasdraft && $publish) { 00224 // set timestamp to current date only if it's not a future item 00225 // draft items have timestamp == 0 00226 // don't allow timestamps in the past (unless otherwise defined in blogsettings) 00227 $query .= ', idraft=0'; 00228 00229 if ($timestamp == 0) 00230 $timestamp = $blog->getCorrectTime(); 00231 00232 // send new item notification 00233 if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem()) 00234 $blog->sendNewItemNotification($itemid, $title, $body); 00235 } 00236 00237 // update timestamp when needed 00238 if ($timestamp != 0) 00239 $query .= ", itime=" . mysqldate($timestamp); 00240 00241 // make sure the correct item is updated 00242 $query .= ' WHERE inumber=' . $itemid; 00243 00244 // off we go! 00245 sql_query($query); 00246 00247 $manager->notify('PostUpdateItem',array('itemid' => $itemid)); 00248 00249 // when needed, move item and comments to new blog 00250 if ($moveNeeded) 00251 ITEM::move($itemid, $catid); 00252 00253 //update the itemOptions 00254 $aOptions = requestArray('plugoption'); 00255 NucleusPlugin::_applyPluginOptions($aOptions); 00256 $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $title, 'body' => $body, 'more' => $more, 'closed' => $closed, 'catid' => $catid))); 00257 00258 } 00259 00260 // move an item to another blog (no checks, static) 00261 function move($itemid, $new_catid) { 00262 global $manager; 00263 00264 $itemid = intval($itemid); 00265 $new_catid = intval($new_catid); 00266 00267 $new_blogid = getBlogIDFromCatID($new_catid); 00268 00269 $manager->notify( 00270 'PreMoveItem', 00271 array( 00272 'itemid' => $itemid, 00273 'destblogid' => $new_blogid, 00274 'destcatid' => $new_catid 00275 ) 00276 ); 00277 00278 00279 // update item table 00280 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid"; 00281 sql_query($query); 00282 00283 // update comments 00284 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid; 00285 sql_query($query); 00286 00287 $manager->notify( 00288 'PostMoveItem', 00289 array( 00290 'itemid' => $itemid, 00291 'destblogid' => $new_blogid, 00292 'destcatid' => $new_catid 00293 ) 00294 ); 00295 } 00296 00300 function delete($itemid) { 00301 global $manager; 00302 00303 $itemid = intval($itemid); 00304 00305 $manager->notify('PreDeleteItem', array('itemid' => $itemid)); 00306 00307 // delete item 00308 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid; 00309 sql_query($query); 00310 00311 // delete the comments associated with the item 00312 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid; 00313 sql_query($query); 00314 00315 // delete all associated plugin options 00316 NucleusPlugin::_deleteOptionValues('item', $itemid); 00317 00318 $manager->notify('PostDeleteItem', array('itemid' => $itemid)); 00319 } 00320 00321 // returns true if there is an item with the given ID (static) 00322 function exists($id,$future,$draft) { 00323 global $manager; 00324 00325 $id = intval($id); 00326 00327 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id; 00328 if (!$future) { 00329 $bid = getBlogIDFromItemID($id); 00330 if (!$bid) return 0; 00331 $b =& $manager->getBlog($bid); 00332 $r .= ' and itime<='.mysqldate($b->getCorrectTime()); 00333 } 00334 if (!$draft) { 00335 $r .= ' and idraft=0'; 00336 } 00337 $r = sql_query($r); 00338 00339 return (mysql_num_rows($r) != 0); 00340 } 00341 00352 function createDraftFromRequest() { 00353 global $member, $manager; 00354 00355 $i_author = $member->getID(); 00356 $i_body = postVar('body'); 00357 $i_title = postVar('title'); 00358 $i_more = postVar('more'); 00359 00360 if(_CHERSET != 'UTF-8'){ 00361 $i_body = mb_convert_encoding($i_body, _CHARSET, "UTF-8"); 00362 $i_title = mb_convert_encoding($i_title, _CHARSET, "UTF-8"); 00363 $i_more = mb_convert_encoding($i_more, _CHARSET, "UTF-8"); 00364 } 00365 //$i_actiontype = postVar('actiontype'); 00366 $i_closed = intPostVar('closed'); 00367 //$i_hour = intPostVar('hour'); 00368 //$i_minutes = intPostVar('minutes'); 00369 //$i_month = intPostVar('month'); 00370 //$i_day = intPostVar('day'); 00371 //$i_year = intPostVar('year'); 00372 $i_catid = postVar('catid'); 00373 $i_draft = 1; 00374 $type = postVar('type'); 00375 if ($type == 'edit') { 00376 $i_blogid = getBlogIDFromItemID(intPostVar('itemid')); 00377 } 00378 else { 00379 $i_blogid = intPostVar('blogid'); 00380 } 00381 $i_draftid = intPostVar('draftid'); 00382 00383 if (!$member->canAddItem($i_catid)) { 00384 return array('status' => 'error', 'message' => _ERROR_DISALLOWED); 00385 } 00386 00387 if (!trim($i_body)) { 00388 return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS); 00389 } 00390 00391 // create new category if needed 00392 if (strstr($i_catid, 'newcat')) { 00393 // Set in default category 00394 $blog =& $manager->getBlog($i_blogid); 00395 $i_catid = $blog->getDefaultCategory(); 00396 } 00397 else { 00398 // force blogid (must be same as category id) 00399 $i_blogid = getBlogIDFromCatID($i_catid); 00400 $blog =& $manager->getBlog($i_blogid); 00401 } 00402 00403 $posttime = 0; 00404 00405 if ($i_draftid > 0) { 00406 ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0); 00407 $itemid = $i_draftid; 00408 } 00409 else { 00410 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft); 00411 } 00412 00413 // No plugin support in AutoSaveDraft yet 00414 //Setting the itemOptions 00415 //$aOptions = requestArray('plugoption'); 00416 //NucleusPlugin::_applyPluginOptions($aOptions, $itemid); 00417 //$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))); 00418 00419 // success 00420 return array('status' => 'added', 'draftid' => $itemid); 00421 } 00422 00423 } 00424 00425 ?>