OSDN Git Service

Merge branch 'skinnable-master'
[nucleus-jp/nucleus-next.git] / nucleus / libs / ITEM.php
1 <<<<<<< HEAD
2 <?php\r
3 \r
4 /*\r
5  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
6  * Copyright (C) 2002-2012 The Nucleus Group\r
7  *\r
8  * This program is free software; you can redistribute it and/or\r
9  * modify it under the terms of the GNU General Public License\r
10  * as published by the Free Software Foundation; either version 2\r
11  * of the License, or (at your option) any later version.\r
12  * (see nucleus/documentation/index.html#license for more info)\r
13  */\r
14 /**\r
15  * @license http://nucleuscms.org/license.txt GNU General Public License\r
16  * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
17  * @version $Id: ITEM.php 1668 2012-02-19 14:36:44Z sakamocchi $\r
18  */\r
19 \r
20 /**\r
21  * A class representing an item\r
22  *\r
23  */\r
24 class Item\r
25 {\r
26         /**\r
27          * Item::$actiontypes\r
28          * actiontype list for handling items\r
29          * \r
30          * @static\r
31          */\r
32         static private $actiontypes = array(\r
33                 'addnow', 'adddraft', 'addfuture', 'edit',\r
34                 'changedate', 'backtodrafts', 'delete'\r
35         );\r
36         \r
37         /**\r
38          * Item::$itemid\r
39          * item id\r
40          * @deprecated\r
41          */\r
42         public $itemid;\r
43         \r
44         /**\r
45          * Item::__construct()\r
46          * Creates a new ITEM object\r
47          * \r
48          * @deprecated\r
49          * @param integer       $item_id        id for item\r
50          * @return void\r
51          */\r
52         public function __construct($item_id)\r
53         {\r
54                 $this->itemid = $item_id;\r
55                 return;\r
56         }\r
57         \r
58         /**\r
59          * Item::getitem()\r
60          * Returns one item with the specific itemid\r
61          * \r
62          * @static\r
63          * @param int $item_id\r
64          * @param bool $allow_draft\r
65          * @param bool $allow_future\r
66          * @return mixed\r
67          */\r
68         static public function getitem($item_id, $allow_draft, $allow_future)\r
69         {\r
70                 global $manager;\r
71                 \r
72                 $item_id = (integer) $item_id;\r
73                 \r
74                 $query = 'SELECT ' .\r
75                         'i.idraft AS draft, ' .\r
76                         'i.inumber AS itemid, ' .\r
77                         'i.iclosed AS closed, ' .\r
78                         'i.ititle AS title, ' .\r
79                         'i.ibody AS body, ' .\r
80                         'm.mname AS author, ' .\r
81                         'i.iauthor AS authorid, ' .\r
82                         'i.itime, ' .\r
83                         'i.imore AS more, ' .\r
84                         'i.ikarmapos AS karmapos, ' .\r
85                         'i.ikarmaneg AS karmaneg, ' .\r
86                         'i.icat AS catid, ' .\r
87                         'i.iblog AS blogid ' .\r
88                         'FROM %s AS i, %s AS m, %s AS b ' .\r
89                         'WHERE i.inumber = %d ' .\r
90                         'AND i.iauthor = m.mnumber ' .\r
91                         'AND i.iblog = b.bnumber ';\r
92                 \r
93                 $query = sprintf($query, sql_table('item'), sql_table('member'), sql_table('blog'), $item_id);\r
94                 \r
95                 if ( !$allow_draft )\r
96                 {\r
97                         $query .= "AND i.idraft = 0 ";\r
98                 }\r
99                 \r
100                 if ( !$allow_future )\r
101                 {\r
102                         $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));\r
103                         $query .= 'AND i.itime <= ' . DB::formatDateTime($blog->getCorrectTime());\r
104                 }\r
105                 \r
106                 $query .= ' LIMIT 1';\r
107                 $result = DB::getResult($query);\r
108                 \r
109                 if ( $result->rowCount() == 1 )\r
110                 {\r
111                         $aItemInfo = $result->fetch(PDO::FETCH_ASSOC);\r
112                         $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);\r
113                         return $aItemInfo;\r
114                 }\r
115                 return 0;\r
116         }\r
117         \r
118         /**\r
119          * Item::createFromRequest()\r
120          * Tries to create an item from the data in the current request (comes from\r
121          * bookmarklet or admin area\r
122          *\r
123          * @static\r
124          * @param       void\r
125          * @return      array   (status = added/error/newcategory, message)\r
126          * \r
127          */\r
128         static public function createFromRequest()\r
129         {\r
130                 global $member, $manager;\r
131                 \r
132                 /*\r
133                  * TODO: these values from user agent should be validated but not implemented yet\r
134                  */\r
135                 $i_author               = $member->getID();\r
136                 $i_body                 = postVar('body');\r
137                 $i_title                = postVar('title');\r
138                 $i_more                 = postVar('more');\r
139                 $i_actiontype   = postVar('actiontype');\r
140                 $i_closed               = intPostVar('closed');\r
141                 $i_hour                 = intPostVar('hour');\r
142                 $i_minutes              = intPostVar('minutes');\r
143                 $i_month                = intPostVar('month');\r
144                 $i_day                  = intPostVar('day');\r
145                 $i_year                 = intPostVar('year');\r
146                 $i_catid                = postVar('catid');\r
147                 $i_draftid              = intPostVar('draftid');\r
148                 \r
149                 if ( !$member->canAddItem($i_catid) )\r
150                 {\r
151                         return array('status' => 'error', 'message' => _ERROR_DISALLOWED);\r
152                 }\r
153                 \r
154                 if ( !in_array($i_actiontype, self::$actiontypes) )\r
155                 {\r
156                         $i_actiontype = 'addnow';\r
157                 }\r
158                 \r
159                 $i_draft = (integer) ( $i_actiontype == 'adddraft' );\r
160                 \r
161                 if ( !trim($i_body) )\r
162                 {\r
163                         return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);\r
164                 }\r
165                 \r
166                 // create new category if needed\r
167                 if ( i18n::strpos($i_catid, 'newcat') === 0 )\r
168                 {\r
169                         // get blogid\r
170                         list($i_blogid) = sscanf($i_catid, "newcat-%d");\r
171                         \r
172                         // create\r
173                         $blog =& $manager->getBlog($i_blogid);\r
174                         $i_catid = $blog->createNewCategory();\r
175                         \r
176                         // show error when sth goes wrong\r
177                         if ( !$i_catid )\r
178                         {\r
179                                 return array('status' => 'error','message' => 'Could not create new category');\r
180                         }\r
181                 }\r
182                 else\r
183                 {\r
184                         // force blogid (must be same as category id)\r
185                         $i_blogid = getBlogIDFromCatID($i_catid);\r
186                         $blog =& $manager->getBlog($i_blogid);\r
187                 }\r
188                 \r
189                 if ( $i_actiontype == 'addfuture' )\r
190                 {\r
191                         $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);\r
192                         \r
193                         // make sure the date is in the future, unless we allow past dates\r
194                         if ( (!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()) )\r
195                         {\r
196                                 $posttime = $blog->getCorrectTime();\r
197                         }\r
198                 }\r
199                 else\r
200                 {\r
201                         if ( !$i_draft )\r
202                         {\r
203                                 $posttime = $blog->getCorrectTime();\r
204                         }\r
205                         else\r
206                         {\r
207                                 $posttime = 0;\r
208                         }\r
209                 }\r
210                 \r
211                 if ( $posttime > $blog->getCorrectTime() )\r
212                 {\r
213                         $posted = 0;\r
214                         $blog->setFuturePost();\r
215                 }\r
216                 else\r
217                 {\r
218                         $posted = 1;\r
219                 }\r
220                 \r
221                 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft, $posted);\r
222                 \r
223                 //Setting the itemOptions\r
224                 $aOptions = requestArray('plugoption');\r
225                 NucleusPlugin::apply_plugin_options($aOptions, $itemid);\r
226                 $data = array(\r
227                         'context'       => 'item',\r
228                         'itemid'        => $itemid,\r
229                         'item'          => array(\r
230                                 'title'         => $i_title,\r
231                                 'body'          => $i_body,\r
232                                 'more'          => $i_more,\r
233                                 'closed'        => $i_closed,\r
234                                 'catid'         => $i_catid\r
235                         )\r
236                 );\r
237                 \r
238                 $manager->notify('PostPluginOptionsUpdate', $data);\r
239                 \r
240                 if ( $i_draftid > 0 )\r
241                 {\r
242                         // delete permission is checked inside Item::delete()\r
243                         self::delete($i_draftid);\r
244                 }\r
245                 \r
246                 // success\r
247                 if ( $i_catid != intRequestVar('catid') )\r
248                 {\r
249                         return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);\r
250                 }\r
251                 \r
252                 return array('status' => 'added', 'itemid' => $itemid);\r
253         }\r
254         \r
255         /**\r
256          * Item::update()\r
257          * Updates an item\r
258          *\r
259          * @static\r
260          * @param       integer $itemid item id\r
261          * @param       integer $catid  category id\r
262          * @param       string  $title  title\r
263          * @param       string  $body   body text\r
264          * @param       string  $more   more text\r
265          * @param       boolean $closed closed or not\r
266          * @param       boolean $wasdraft       previously draft or not\r
267          * @param       boolean $publish        published or not\r
268          * @param       timestamp       $timestamp      timestamp\r
269          * @return      void\r
270          */\r
271         static public function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)\r
272         {\r
273                 global $manager;\r
274                 \r
275                 $itemid = (integer) $itemid;\r
276                 $closed = (boolean) $closed;\r
277                 \r
278                 // get destination blogid\r
279                 $new_blogid = getBlogIDFromCatID($catid);\r
280                 $old_blogid = getBlogIDFromItemID($itemid);\r
281                 \r
282                 // move will be done on end of method\r
283                 $moveNeeded = 0;\r
284                 if ( $new_blogid != $old_blogid )\r
285                 {\r
286                         $moveNeeded = 1;\r
287                 }\r
288                 \r
289                 $blog =& $manager->getBlog($new_blogid);\r
290                 \r
291                 // begin if: convert line breaks to <br/>\r
292                 if ( $blog->convertBreaks() )\r
293                 {\r
294                         $body = addBreaks($body);\r
295                         $more = addBreaks($more);\r
296                 }\r
297                 \r
298                 // call plugins\r
299                 $data = array(\r
300                         'itemid'        => $itemid,\r
301                         'title'         => &$title,\r
302                         'body'          => &$body,\r
303                         'more'          => &$more,\r
304                         'blog'          => &$blog,\r
305                         'closed'        => &$closed,\r
306                         'catid'         => &$catid\r
307                 );\r
308                 $manager->notify('PreUpdateItem', $data);\r
309                 \r
310                 // update item itself\r
311                 $query =  'UPDATE ' . sql_table('item')\r
312                                 . ' SET'\r
313                                 . ' ibody = ' . DB::quoteValue($body) . ','\r
314                                 . ' ititle = ' . DB::quoteValue($title) . ','\r
315                                 . ' imore = ' . DB::quoteValue($more) . ','\r
316                                 . ' iclosed = ' . intval($closed) . ','\r
317                                 . ' icat = ' . intval($catid);\r
318                 \r
319                 // if we received an updated timestamp that is in the past, but past posting is not allowed,\r
320                 // reject that date change (timestamp = 0 will make sure the current date is kept)\r
321                 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()) )\r
322                 {\r
323                         $timestamp = 0;\r
324                 }\r
325                 \r
326                 // begin if: post is in the future\r
327                 if ( $timestamp > $blog->getCorrectTime(time()) )\r
328                 {\r
329                         $isFuture = 1;\r
330                         $query .= ', iposted = 0';\r
331                 }\r
332                 else\r
333                 {\r
334                         $isFuture = 0;\r
335                         $query .= ', iposted = 1';\r
336                 }\r
337                 \r
338                 if ( $wasdraft && $publish )\r
339                 {\r
340                         // set timestamp to current date only if it's not a future item\r
341                         // draft items have timestamp == 0\r
342                         // don't allow timestamps in the past (unless otherwise defined in blogsettings)\r
343                         $query .= ', idraft = 0';\r
344                         \r
345                         if ( $timestamp == 0 )\r
346                         {\r
347                                 $timestamp = $blog->getCorrectTime();\r
348                         }\r
349                         \r
350                         // send new item notification\r
351                         if ( !$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem() )\r
352                         {\r
353                                 $blog->sendNewItemNotification($itemid, $title, $body);\r
354                         }\r
355                 }\r
356                 \r
357                 // save back to drafts\r
358                 if ( !$wasdraft && !$publish )\r
359                 {\r
360                         $query .= ', idraft = 1';\r
361                         // set timestamp back to zero for a draft\r
362                         $query .= ', itime = ' . DB::formatDateTime($timestamp);\r
363                 }\r
364                 \r
365                 // update timestamp when needed\r
366                 if ( $timestamp != 0 )\r
367                 {\r
368                         $query .= ', itime = ' . DB::formatDateTime($timestamp);\r
369                 }\r
370                 \r
371                 // make sure the correct item is updated\r
372                 $query .= ' WHERE inumber = ' . $itemid;\r
373                 \r
374                 // off we go!\r
375                 DB::execute($query);\r
376                 \r
377                 $manager->notify('PostUpdateItem', array('itemid' => $itemid));\r
378                 \r
379                 // when needed, move item and comments to new blog\r
380                 if ( $moveNeeded )\r
381                 {\r
382                         self::move($itemid, $catid);\r
383                 }\r
384                 \r
385                 //update the itemOptions\r
386                 $aOptions = requestArray('plugoption');\r
387                 NucleusPlugin::apply_plugin_options($aOptions);\r
388                 $data = array(\r
389                         'context'       => 'item',\r
390                         'itemid'        => $itemid,\r
391                         'item'          => array(\r
392                                 'title'         => $title,\r
393                                 'body'          => $body,\r
394                                 'more'          => $more,\r
395                                 'closed'        => $closed,\r
396                                 'catid'         => $catid\r
397                         )\r
398                 );\r
399                 $manager->notify('PostPluginOptionsUpdate', $data);\r
400                 return;\r
401         }\r
402         \r
403         /**\r
404          * Item::move()\r
405          * Move an item to another blog (no checks)\r
406          *\r
407          * @static\r
408          * @param       integer $itemid\r
409          * @param       integer $new_catid\r
410          * @return      void\r
411          */\r
412         static public function move($itemid, $new_catid)\r
413         {\r
414                 global $manager;\r
415                 \r
416                 $itemid = (integer) $itemid;\r
417                 $new_catid = (integer) $new_catid;\r
418                 $new_blogid = getBlogIDFromCatID($new_catid);\r
419                 \r
420                 $data = array(\r
421                         'itemid'                => $itemid,\r
422                         'destblogid'    => $new_blogid,\r
423                         'destcatid'             => $new_catid\r
424                 );\r
425                 $manager->notify('PreMoveItem', $data);\r
426                 \r
427                 // update item table\r
428                 $query = "UPDATE %s SET iblog=%d, icat=%d WHERE inumber=%d";\r
429                 $query = sprintf($query, sql_table('item'), $new_blogid, $new_catid, $itemid);\r
430                 DB::execute($query);\r
431                 \r
432                 // update comments\r
433                 $query = "UPDATE %s SET cblog=%d WHERE citem=%d";\r
434                 $query = sprintf($query, sql_table('comment'), $new_blogid, $itemid);\r
435                 DB::execute($query);\r
436                 \r
437                 $data = array(\r
438                         'itemid'                => $itemid,\r
439                         'destblogid'    => $new_blogid,\r
440                         'destcatid'             => $new_catid\r
441                 );\r
442                 $manager->notify('PostMoveItem', $data);\r
443                 return;\r
444         }\r
445         \r
446         /**\r
447          * Item::delete()\r
448          * Deletes an item\r
449          * \r
450          * @param       integer $itemid\r
451          * @return      void\r
452          */\r
453         static public function delete($itemid)\r
454         {\r
455                 global $manager, $member;\r
456                 \r
457                 $itemid = (integer) $itemid;\r
458                 \r
459                 // check permission\r
460                 if ( !$member->canAlterItem($itemid) )\r
461                 {\r
462                         return 1;\r
463                 }\r
464                 \r
465                 $manager->notify('PreDeleteItem', array('itemid' => $itemid));\r
466                 \r
467                 // delete item\r
468                 $query = "DELETE FROM %s WHERE inumber=%d;";\r
469                 $query = sprintf($query, sql_table('item'), $itemid);\r
470                 DB::execute($query);\r
471                 \r
472                 // delete the comments associated with the item\r
473                 $query = "DELETE FROM %s WHERE citem=%d;";\r
474                 $query = sprintf($query, sql_table('comment'), $itemid);\r
475                 DB::execute($query);\r
476                 \r
477                 // delete all associated plugin options\r
478                 NucleusPlugin::delete_option_values('item', $itemid);\r
479                 \r
480                 $manager->notify('PostDeleteItem', array('itemid' => $itemid));\r
481                 \r
482                 return 0;\r
483         }\r
484         \r
485         /**\r
486          * Item::exists()\r
487          * Returns true if there is an item with the given ID\r
488          *\r
489          * @static\r
490          * @param       integer $itemid\r
491          * @param       boolean $future\r
492          * @param       boolean $draft\r
493          * @return      boolean exists or not\r
494          */\r
495         static public function exists($itemid, $future, $draft)\r
496         {\r
497                 global $manager;\r
498                 \r
499                 $itemid = (integer) $itemid;\r
500                 \r
501                 $query = 'SELECT * FROM %s WHERE inumber=%d';\r
502                 $query = sprintf($query, sql_table('item'), $itemid);\r
503                 \r
504                 if ( !$future )\r
505                 {\r
506                         $blogid = getBlogIDFromItemID($itemid);\r
507                         if ( !$blogid )\r
508                         {\r
509                                 return 0;\r
510                         }\r
511                         $blog =& $manager->getBlog($blogid);\r
512                         $query .= ' AND itime<=' . DB::formatDateTime($blog->getCorrectTime());\r
513                 }\r
514                 \r
515                 if ( !$draft )\r
516                 {\r
517                         $query .= ' AND idraft=0';\r
518                 }\r
519                 \r
520                 $result = DB::getResult($query);\r
521                 return ( $result->rowCount() != 0 );\r
522         }\r
523         \r
524         /**\r
525          * Item::createDraftFromRequest()\r
526          * Tries to create an draft from the data\r
527          *  in the current request (comes from bookmarklet or admin area)\r
528          *   Used by xmlHTTPRequest AutoDraft\r
529          *\r
530          * Returns an array with status info:\r
531          * status = 'added', 'error', 'newcategory'\r
532          *\r
533          * @static\r
534          * @param       void\r
535          * @return      array   (status = added/error/newcategory, message)\r
536          */\r
537         static public function createDraftFromRequest()\r
538         {\r
539                 global $member, $manager;\r
540                 \r
541                 /*\r
542                  * TODO: these values from user agent should be validated but not implemented yet\r
543                  */\r
544                 $i_author       = $member->getID();\r
545                 $i_body         = postVar('body');\r
546                 $i_title        = postVar('title');\r
547                 $i_more         = postVar('more');\r
548                 $i_closed       = intPostVar('closed');\r
549                 $i_catid        = postVar('catid');\r
550                 $i_draft        = 1;\r
551                 $type           = postVar('type');\r
552                 $i_draftid      = intPostVar('draftid');\r
553                 \r
554                 if ( $type == 'edit' )\r
555                 {\r
556                         $itemid = intPostVar('itemid');\r
557                         $i_blogid = getBlogIDFromItemID($itemid);\r
558                 }\r
559                 else\r
560                 {\r
561                         $i_blogid = intPostVar('blogid');\r
562                 }\r
563                 \r
564                 if ( !$member->canAddItem($i_catid) )\r
565                 {\r
566                         return array('status' => 'error', 'message' => _ERROR_DISALLOWED);\r
567                 }\r
568                 \r
569                 if ( !trim($i_body) )\r
570                 {\r
571                         return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);\r
572                 }\r
573                 \r
574                 // create new category if needed\r
575                 if ( i18n::strpos($i_catid,'newcat') === 0 )\r
576                 {\r
577                         // Set in default category\r
578                         $blog =& $manager->getBlog($i_blogid);\r
579                         $i_catid = $blog->getDefaultCategory();\r
580                 }\r
581                 else\r
582                 {\r
583                         // force blogid (must be same as category id)\r
584                         $i_blogid = getBlogIDFromCatID($i_catid);\r
585                         $blog =& $manager->getBlog($i_blogid);\r
586                 }\r
587                 \r
588                 $posttime = 0;\r
589                 \r
590                 if ( $i_draftid > 0 )\r
591                 {\r
592                         self::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);\r
593                         $itemid = $i_draftid;\r
594                 }\r
595                 else\r
596                 {\r
597                         $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);\r
598                 }\r
599                 \r
600                 return array('status' => 'added', 'draftid' => $itemid);\r
601         }\r
602 }\r
603 =======
604 <?php
605
606 /*
607  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
608  * Copyright (C) 2002-2009 The Nucleus Group
609  *
610  * This program is free software; you can redistribute it and/or
611  * modify it under the terms of the GNU General Public License
612  * as published by the Free Software Foundation; either version 2
613  * of the License, or (at your option) any later version.
614  * (see nucleus/documentation/index.html#license for more info)
615  */
616 /**
617  * @license http://nucleuscms.org/license.txt GNU General Public License
618  * @copyright Copyright (C) 2002-2009 The Nucleus Group
619  * @version $Id: ITEM.php 1877 2012-06-17 07:40:11Z sakamocchi $
620  */
621
622 /**
623  * A class representing an item
624  *
625  */
626 class Item
627 {
628         /**
629          * Item::$actiontypes
630          * actiontype list for handling items
631          * 
632          * @static
633          */
634         static private $actiontypes = array(
635                 'addnow', 'adddraft', 'addfuture', 'edit',
636                 'changedate', 'backtodrafts', 'delete'
637         );
638         
639         /**
640          * Item::$itemid
641          * item id
642          * @deprecated
643          */
644         public $itemid;
645         
646         /**
647          * Item::__construct()
648          * Creates a new ITEM object
649          * 
650          * @deprecated
651          * @param integer       $item_id        id for item
652          * @return void
653          */
654         public function __construct($item_id)
655         {
656                 $this->itemid = $item_id;
657                 return;
658         }
659         
660         /**
661          * Item::getitem()
662          * Returns one item with the specific itemid
663          * 
664          * @static
665          * @param int $item_id
666          * @param bool $allow_draft
667          * @param bool $allow_future
668          * @return mixed
669          */
670         static public function getitem($item_id, $allow_draft, $allow_future)
671         {
672                 global $manager;
673                 
674                 $item_id = (integer) $item_id;
675                 
676                 $query = 'SELECT ' .
677                         'i.idraft AS draft, ' .
678                         'i.inumber AS itemid, ' .
679                         'i.iclosed AS closed, ' .
680                         'i.ititle AS title, ' .
681                         'i.ibody AS body, ' .
682                         'm.mname AS author, ' .
683                         'i.iauthor AS authorid, ' .
684                         'i.itime, ' .
685                         'i.imore AS more, ' .
686                         'i.ikarmapos AS karmapos, ' .
687                         'i.ikarmaneg AS karmaneg, ' .
688                         'i.icat AS catid, ' .
689                         'i.iblog AS blogid ' .
690                         'FROM %s AS i, %s AS m, %s AS b ' .
691                         'WHERE i.inumber = %d ' .
692                         'AND i.iauthor = m.mnumber ' .
693                         'AND i.iblog = b.bnumber ';
694                 
695                 $query = sprintf($query, sql_table('item'), sql_table('member'), sql_table('blog'), $item_id);
696                 
697                 if ( !$allow_draft )
698                 {
699                         $query .= "AND i.idraft = 0 ";
700                 }
701                 
702                 if ( !$allow_future )
703                 {
704                         $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));
705                         $query .= 'AND i.itime <= ' . DB::formatDateTime($blog->getCorrectTime());
706                 }
707                 
708                 $query .= ' LIMIT 1';
709                 $result = DB::getResult($query);
710                 
711                 if ( $result->rowCount() != 1 )
712                 {
713                         return 0;
714                 }
715                 
716                 $aItemInfo = $result->fetch(PDO::FETCH_ASSOC);
717                 $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
718                 return $aItemInfo;
719         }
720         
721         /**
722          * Item::createFromRequest()
723          * Tries to create an item from the data in the current request (comes from
724          * bookmarklet or admin area
725          *
726          * @static
727          * @param       void
728          * @return      array   (status = added/error/newcategory, message)
729          * 
730          */
731         static public function createFromRequest()
732         {
733                 global $member, $manager;
734                 
735                 /*
736                  * TODO: these values from user agent should be validated but not implemented yet
737                  */
738                 $i_author               = $member->getID();
739                 $i_body                 = postVar('body');
740                 $i_title                = postVar('title');
741                 $i_more                 = postVar('more');
742                 $i_actiontype   = postVar('actiontype');
743                 $i_closed               = intPostVar('closed');
744                 $i_hour                 = intPostVar('hour');
745                 $i_minutes              = intPostVar('minutes');
746                 $i_month                = intPostVar('month');
747                 $i_day                  = intPostVar('day');
748                 $i_year                 = intPostVar('year');
749                 $i_catid                = postVar('catid');
750                 $i_draftid              = intPostVar('draftid');
751                 
752                 if ( !$member->canAddItem($i_catid) )
753                 {
754                         return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
755                 }
756                 
757                 if ( !in_array($i_actiontype, self::$actiontypes) )
758                 {
759                         $i_actiontype = 'addnow';
760                 }
761                 
762                 $i_draft = (integer) ( $i_actiontype == 'adddraft' );
763                 
764                 if ( !trim($i_body) )
765                 {
766                         return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
767                 }
768                 
769                 // create new category if needed
770                 if ( i18n::strpos($i_catid, 'newcat') === 0 )
771                 {
772                         // get blogid
773                         list($i_blogid) = sscanf($i_catid, "newcat-%d");
774                         
775                         // create
776                         $blog =& $manager->getBlog($i_blogid);
777                         $i_catid = $blog->createNewCategory();
778                         
779                         // show error when sth goes wrong
780                         if ( !$i_catid )
781                         {
782                                 return array('status' => 'error','message' => 'Could not create new category');
783                         }
784                 }
785                 else
786                 {
787                         // force blogid (must be same as category id)
788                         $i_blogid = getBlogIDFromCatID($i_catid);
789                         $blog =& $manager->getBlog($i_blogid);
790                 }
791                 
792                 if ( $i_actiontype == 'addfuture' )
793                 {
794                         $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
795                         
796                         // make sure the date is in the future, unless we allow past dates
797                         if ( (!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()) )
798                         {
799                                 $posttime = $blog->getCorrectTime();
800                         }
801                 }
802                 else
803                 {
804                         if ( !$i_draft )
805                         {
806                                 $posttime = $blog->getCorrectTime();
807                         }
808                         else
809                         {
810                                 $posttime = 0;
811                         }
812                 }
813                 
814                 if ( $posttime > $blog->getCorrectTime() )
815                 {
816                         $posted = 0;
817                         $blog->setFuturePost();
818                 }
819                 else
820                 {
821                         $posted = 1;
822                 }
823                 
824                 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft, $posted);
825                 
826                 //Setting the itemOptions
827                 $aOptions = requestArray('plugoption');
828                 NucleusPlugin::apply_plugin_options($aOptions, $itemid);
829                 $data = array(
830                         'context'       => 'item',
831                         'itemid'        => $itemid,
832                         'item'          => array(
833                                 'title'         => $i_title,
834                                 'body'          => $i_body,
835                                 'more'          => $i_more,
836                                 'closed'        => $i_closed,
837                                 'catid'         => $i_catid
838                         )
839                 );
840                 
841                 $manager->notify('PostPluginOptionsUpdate', $data);
842                 
843                 if ( $i_draftid > 0 )
844                 {
845                         // delete permission is checked inside Item::delete()
846                         self::delete($i_draftid);
847                 }
848                 
849                 // success
850                 if ( $i_catid != intRequestVar('catid') )
851                 {
852                         return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
853                 }
854                 
855                 return array('status' => 'added', 'itemid' => $itemid);
856         }
857         
858         /**
859          * Item::update()
860          * Updates an item
861          *
862          * @static
863          * @param       integer $itemid item id
864          * @param       integer $catid  category id
865          * @param       string  $title  title
866          * @param       string  $body   body text
867          * @param       string  $more   more text
868          * @param       boolean $closed closed or not
869          * @param       boolean $wasdraft       previously draft or not
870          * @param       boolean $publish        published or not
871          * @param       timestamp       $timestamp      timestamp
872          * @return      void
873          */
874         static public function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)
875         {
876                 global $manager;
877                 
878                 $itemid = (integer) $itemid;
879                 $closed = (boolean) $closed;
880                 
881                 // get destination blogid
882                 $new_blogid = getBlogIDFromCatID($catid);
883                 $old_blogid = getBlogIDFromItemID($itemid);
884                 
885                 // move will be done on end of method
886                 $moveNeeded = 0;
887                 if ( $new_blogid != $old_blogid )
888                 {
889                         $moveNeeded = 1;
890                 }
891                 
892                 $blog =& $manager->getBlog($new_blogid);
893                 
894                 // begin if: convert line breaks to <br/>
895                 if ( $blog->convertBreaks() )
896                 {
897                         $body = addBreaks($body);
898                         $more = addBreaks($more);
899                 }
900                 
901                 // call plugins
902                 $data = array(
903                         'itemid'        => $itemid,
904                         'title'         => &$title,
905                         'body'          => &$body,
906                         'more'          => &$more,
907                         'blog'          => &$blog,
908                         'closed'        => &$closed,
909                         'catid'         => &$catid
910                 );
911                 $manager->notify('PreUpdateItem', $data);
912                 
913                 // update item itself
914                 $query =  'UPDATE ' . sql_table('item')
915                                 . ' SET'
916                                 . ' ibody = ' . DB::quoteValue($body) . ','
917                                 . ' ititle = ' . DB::quoteValue($title) . ','
918                                 . ' imore = ' . DB::quoteValue($more) . ','
919                                 . ' iclosed = ' . intval($closed) . ','
920                                 . ' icat = ' . intval($catid);
921                 
922                 // if we received an updated timestamp that is in the past, but past posting is not allowed,
923                 // reject that date change (timestamp = 0 will make sure the current date is kept)
924                 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()) )
925                 {
926                         $timestamp = 0;
927                 }
928                 
929                 // begin if: post is in the future
930                 if ( $timestamp > $blog->getCorrectTime(time()) )
931                 {
932                         $isFuture = 1;
933                         $query .= ', iposted = 0';
934                 }
935                 else
936                 {
937                         $isFuture = 0;
938                         $query .= ', iposted = 1';
939                 }
940                 
941                 if ( $wasdraft && $publish )
942                 {
943                         // set timestamp to current date only if it's not a future item
944                         // draft items have timestamp == 0
945                         // don't allow timestamps in the past (unless otherwise defined in blogsettings)
946                         $query .= ', idraft = 0';
947                         
948                         if ( $timestamp == 0 )
949                         {
950                                 $timestamp = $blog->getCorrectTime();
951                         }
952                         
953                         // send new item notification
954                         if ( !$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem() )
955                         {
956                                 $blog->sendNewItemNotification($itemid, $title, $body);
957                         }
958                 }
959                 
960                 // save back to drafts
961                 if ( !$wasdraft && !$publish )
962                 {
963                         $query .= ', idraft = 1';
964                         // set timestamp back to zero for a draft
965                         $query .= ', itime = ' . DB::formatDateTime($timestamp);
966                 }
967                 
968                 // update timestamp when needed
969                 if ( $timestamp != 0 )
970                 {
971                         $query .= ', itime = ' . DB::formatDateTime($timestamp);
972                 }
973                 
974                 // make sure the correct item is updated
975                 $query .= ' WHERE inumber = ' . $itemid;
976                 
977                 // off we go!
978                 DB::execute($query);
979
980                 $data = array('itemid' => $itemid);
981                 $manager->notify('PostUpdateItem', $data);
982                 
983                 // when needed, move item and comments to new blog
984                 if ( $moveNeeded )
985                 {
986                         self::move($itemid, $catid);
987                 }
988                 
989                 //update the itemOptions
990                 $aOptions = requestArray('plugoption');
991                 NucleusPlugin::apply_plugin_options($aOptions);
992                 $data = array(
993                         'context'       => 'item',
994                         'itemid'        => $itemid,
995                         'item'          => array(
996                                 'title'         => $title,
997                                 'body'          => $body,
998                                 'more'          => $more,
999                                 'closed'        => $closed,
1000                                 'catid'         => $catid
1001                         )
1002                 );
1003                 $manager->notify('PostPluginOptionsUpdate', $data);
1004                 return;
1005         }
1006         
1007         /**
1008          * Item::move()
1009          * Move an item to another blog (no checks)
1010          *
1011          * @static
1012          * @param       integer $itemid
1013          * @param       integer $new_catid
1014          * @return      void
1015          */
1016         static public function move($itemid, $new_catid)
1017         {
1018                 global $manager;
1019                 
1020                 $itemid         = (integer) $itemid;
1021                 $new_catid      = (integer) $new_catid;
1022                 $new_blogid     = getBlogIDFromCatID($new_catid);
1023                 
1024                 $data = array(
1025                         'itemid'                => $itemid,
1026                         'destblogid'    => $new_blogid,
1027                         'destcatid'             => $new_catid
1028                 );
1029                 $manager->notify('PreMoveItem', $data);
1030                 
1031                 // update item table
1032                 $query = "UPDATE %s SET iblog=%d, icat=%d WHERE inumber=%d";
1033                 $query = sprintf($query, sql_table('item'), $new_blogid, $new_catid, $itemid);
1034                 DB::execute($query);
1035                 
1036                 // update comments
1037                 $query = "UPDATE %s SET cblog=%d WHERE citem=%d";
1038                 $query = sprintf($query, sql_table('comment'), $new_blogid, $itemid);
1039                 DB::execute($query);
1040                 
1041                 $data = array(
1042                         'itemid'                => $itemid,
1043                         'destblogid'    => $new_blogid,
1044                         'destcatid'             => $new_catid
1045                 );
1046                 $manager->notify('PostMoveItem', $data);
1047                 return;
1048         }
1049         
1050         /**
1051          * Item::delete()
1052          * Deletes an item
1053          * 
1054          * @param       integer $itemid
1055          * @return      void
1056          */
1057         static public function delete($itemid)
1058         {
1059                 global $manager, $member;
1060                 
1061                 $itemid = (integer) $itemid;
1062                 
1063                 // check permission
1064                 if ( !$member->canAlterItem($itemid) )
1065                 {
1066                         return 1;
1067                 }
1068
1069                 $data = array('itemid' => $itemid);
1070                 $manager->notify('PreDeleteItem', $data);
1071                 
1072                 // delete item
1073                 $query = "DELETE FROM %s WHERE inumber=%d;";
1074                 $query = sprintf($query, sql_table('item'), $itemid);
1075                 DB::execute($query);
1076                 
1077                 // delete the comments associated with the item
1078                 $query = "DELETE FROM %s WHERE citem=%d;";
1079                 $query = sprintf($query, sql_table('comment'), $itemid);
1080                 DB::execute($query);
1081                 
1082                 // delete all associated plugin options
1083                 NucleusPlugin::delete_option_values('item', $itemid);
1084                 
1085                 $manager->notify('PostDeleteItem', $data);
1086                 
1087                 return 0;
1088         }
1089         
1090         /**
1091          * Item::exists()
1092          * Returns true if there is an item with the given ID
1093          *
1094          * @static
1095          * @param       integer $itemid
1096          * @param       boolean $future
1097          * @param       boolean $draft
1098          * @return      boolean exists or not
1099          */
1100         static public function exists($itemid, $future, $draft)
1101         {
1102                 global $manager;
1103                 
1104                 $itemid = (integer) $itemid;
1105                 
1106                 $query = 'SELECT * FROM %s WHERE inumber=%d';
1107                 $query = sprintf($query, sql_table('item'), $itemid);
1108                 
1109                 if ( !$future )
1110                 {
1111                         $blogid = getBlogIDFromItemID($itemid);
1112                         if ( !$blogid )
1113                         {
1114                                 return 0;
1115                         }
1116                         $blog =& $manager->getBlog($blogid);
1117                         $query .= ' AND itime<=' . DB::formatDateTime($blog->getCorrectTime());
1118                 }
1119                 
1120                 if ( !$draft )
1121                 {
1122                         $query .= ' AND idraft=0';
1123                 }
1124                 
1125                 $result = DB::getResult($query);
1126                 return ( $result->rowCount() != 0 );
1127         }
1128         
1129         /**
1130          * Item::createDraftFromRequest()
1131          * Tries to create an draft from the data
1132          *  in the current request (comes from bookmarklet or admin area)
1133          *   Used by xmlHTTPRequest AutoDraft
1134          *
1135          * Returns an array with status info:
1136          * status = 'added', 'error', 'newcategory'
1137          *
1138          * @static
1139          * @param       void
1140          * @return      array   (status = added/error/newcategory, message)
1141          */
1142         static public function createDraftFromRequest()
1143         {
1144                 global $member, $manager;
1145                 
1146                 /*
1147                  * TODO: these values from user agent should be validated but not implemented yet
1148                  */
1149                 $i_author       = $member->getID();
1150                 $i_body         = postVar('body');
1151                 $i_title        = postVar('title');
1152                 $i_more         = postVar('more');
1153                 $i_closed       = intPostVar('closed');
1154                 $i_catid        = postVar('catid');
1155                 $i_draft        = 1;
1156                 $type           = postVar('type');
1157                 $i_draftid      = intPostVar('draftid');
1158                 
1159                 if ( $type == 'edit' )
1160                 {
1161                         $itemid = intPostVar('itemid');
1162                         $i_blogid = getBlogIDFromItemID($itemid);
1163                 }
1164                 else
1165                 {
1166                         $i_blogid = intPostVar('blogid');
1167                 }
1168                 
1169                 if ( !$member->canAddItem($i_catid) )
1170                 {
1171                         return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
1172                 }
1173                 
1174                 if ( !trim($i_body) )
1175                 {
1176                         return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
1177                 }
1178                 
1179                 // create new category if needed
1180                 if ( i18n::strpos($i_catid,'newcat') === 0 )
1181                 {
1182                         // Set in default category
1183                         $blog =& $manager->getBlog($i_blogid);
1184                         $i_catid = $blog->getDefaultCategory();
1185                 }
1186                 else
1187                 {
1188                         // force blogid (must be same as category id)
1189                         $i_blogid = getBlogIDFromCatID($i_catid);
1190                         $blog =& $manager->getBlog($i_blogid);
1191                 }
1192                 
1193                 $posttime = 0;
1194                 
1195                 if ( $i_draftid > 0 )
1196                 {
1197                         self::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
1198                         $itemid = $i_draftid;
1199                 }
1200                 else
1201                 {
1202                         $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
1203                 }
1204                 
1205                 return array('status' => 'added', 'draftid' => $itemid);
1206         }
1207 }
1208 >>>>>>> skinnable-master