OSDN Git Service

FIX:アイテム追加時、カテゴリを「新しいカテゴリーの追加」とするとカテゴリー追加画面が表示されずにアイテムの登録が失敗する問題を修正
[nucleus-jp/nucleus-next.git] / nucleus / libs / ITEM.php
1 <?php\r
2 \r
3 /*\r
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
5  * Copyright (C) 2002-2012 The Nucleus Group\r
6  *\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
12  */\r
13 /**\r
14  * @license http://nucleuscms.org/license.txt GNU General Public License\r
15  * @copyright Copyright (C) 2002-2012 The Nucleus Group\r
16  * @version $Id: ITEM.php 1668 2012-02-19 14:36:44Z sakamocchi $\r
17  */\r
18 \r
19 /**\r
20  * A class representing an item\r
21  *\r
22  */\r
23 class Item\r
24 {\r
25         /**\r
26          * Item::$actiontypes\r
27          * actiontype list for handling items\r
28          * \r
29          * @static\r
30          */\r
31         static private $actiontypes\r
32                 = array('addnow', 'adddraft', 'addfuture', 'edit', 'changedate', 'backtodrafts', 'delete');\r
33         \r
34         /**\r
35          * Item::$itemid\r
36          * item id\r
37          * @deprecated\r
38          * \r
39          */\r
40         public $itemid;\r
41         \r
42         /**\r
43          * Item::__construct()\r
44          * Creates a new ITEM object\r
45          * \r
46          * @deprecated\r
47          * @param integer       $item_id        id for item\r
48          * @return void\r
49          */\r
50         public function __construct($item_id)\r
51         {\r
52                 $this->itemid = $item_id;\r
53                 return;\r
54         }\r
55         \r
56         /**\r
57          * Item::getitem()\r
58          * Returns one item with the specific itemid\r
59          *\r
60          * @param int $item_id\r
61          * @param bool $allow_draft\r
62          * @param bool $allow_future\r
63          * @return mixed\r
64          * \r
65          */\r
66         static public function getitem($item_id, $allow_draft, $allow_future)\r
67         {\r
68                 global $manager;\r
69                 \r
70                 $item_id = (integer) $item_id;\r
71                 \r
72                 $query = 'SELECT ' .\r
73                         'i.idraft AS draft, ' .\r
74                         'i.inumber AS itemid, ' .\r
75                         'i.iclosed AS closed, ' .\r
76                         'i.ititle AS title, ' .\r
77                         'i.ibody AS body, ' .\r
78                         'm.mname AS author, ' .\r
79                         'i.iauthor AS authorid, ' .\r
80                         'i.itime, ' .\r
81                         'i.imore AS more, ' .\r
82                         'i.ikarmapos AS karmapos, ' .\r
83                         'i.ikarmaneg AS karmaneg, ' .\r
84                         'i.icat AS catid, ' .\r
85                         'i.iblog AS blogid ' .\r
86                         'FROM %s AS i, %s AS m, %s AS b ' .\r
87                         'WHERE i.inumber = %d ' .\r
88                         'AND i.iauthor = m.mnumber ' .\r
89                         'AND i.iblog = b.bnumber ';\r
90                 \r
91                 $query = sprintf($query, sql_table('item'), sql_table('member'), sql_table('blog'), $item_id);\r
92                 \r
93                 if ( !$allow_draft )\r
94                 {\r
95                         $query .= "AND i.idraft = 0 ";\r
96                 }\r
97                 \r
98                 if ( !$allow_future )\r
99                 {\r
100                         $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));\r
101                         $query .= "AND i.itime <= '" . i18n::formatted_datetime('mysql', $blog->getCorrectTime()) ."'";\r
102                 }\r
103                 \r
104                 $query .= ' LIMIT 1';\r
105                 $result = DB::getResult($query);\r
106                 \r
107                 if ( $result->rowCount() == 1 )\r
108                 {\r
109                         $aItemInfo = $result->fetch(PDO::FETCH_ASSOC);\r
110                         $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);\r
111                         return $aItemInfo;\r
112                 }\r
113                 else\r
114                 {\r
115                         return 0;\r
116                 }\r
117         }\r
118         \r
119         /**\r
120          * Item::createFromRequest()\r
121          * Tries to create an item from the data in the current request (comes from\r
122          * bookmarklet or admin area\r
123          *\r
124          * @static\r
125          * @param       void\r
126          * @return      array   (status = added/error/newcategory, message)\r
127          * \r
128          */\r
129         static public function createFromRequest()\r
130         {\r
131                 global $member, $manager;\r
132                 \r
133                 /*\r
134                  * TODO: these values from user agent should be validated but not implemented yet\r
135                  */\r
136                 $i_author               = $member->getID();\r
137                 $i_body                 = postVar('body');\r
138                 $i_title                = postVar('title');\r
139                 $i_more                 = postVar('more');\r
140                 $i_actiontype   = postVar('actiontype');\r
141                 $i_closed               = intPostVar('closed');\r
142                 $i_hour                 = intPostVar('hour');\r
143                 $i_minutes              = intPostVar('minutes');\r
144                 $i_month                = intPostVar('month');\r
145                 $i_day                  = intPostVar('day');\r
146                 $i_year                 = intPostVar('year');\r
147                 $i_catid                = postVar('catid');\r
148                 $i_draftid              = intPostVar('draftid');\r
149                 \r
150                 if ( !$member->canAddItem($i_catid) )\r
151                 {\r
152                         return array('status' => 'error', 'message' => _ERROR_DISALLOWED);\r
153                 }\r
154                 \r
155                 if ( !in_array($i_actiontype, self::$actiontypes) )\r
156                 {\r
157                         $i_actiontype = 'addnow';\r
158                 }\r
159                 \r
160                 $i_draft = (integer) ( $i_actiontype == 'adddraft' );\r
161                 \r
162                 if ( !trim($i_body) )\r
163                 {\r
164                         return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);\r
165                 }\r
166                 \r
167                 // create new category if needed\r
168                 if ( !i18n::strpos($i_catid, 'newcat') )\r
169                 {\r
170                         // get blogid\r
171                         list($i_blogid) = sscanf($i_catid, "newcat-%d");\r
172                         \r
173                         // create\r
174                         $blog =& $manager->getBlog($i_blogid);\r
175                         $i_catid = $blog->createNewCategory();\r
176                         \r
177                         // show error when sth goes wrong\r
178                         if ( !$i_catid )\r
179                         {\r
180                                 return array('status' => 'error','message' => 'Could not create new category');\r
181                         }\r
182                 }\r
183                 else\r
184                 {\r
185                         // force blogid (must be same as category id)\r
186                         $i_blogid = getBlogIDFromCatID($i_catid);\r
187                         $blog =& $manager->getBlog($i_blogid);\r
188                 }\r
189                 \r
190                 if ( $i_actiontype == 'addfuture' )\r
191                 {\r
192                         $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);\r
193                         \r
194                         // make sure the date is in the future, unless we allow past dates\r
195                         if ( (!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()) )\r
196                         {\r
197                                 $posttime = $blog->getCorrectTime();\r
198                         }\r
199                 }\r
200                 else\r
201                 {\r
202                         if ( !$i_draft )\r
203                         {\r
204                                 $posttime = $blog->getCorrectTime();\r
205                         }\r
206                         else\r
207                         {\r
208                                 $posttime = 0;\r
209                         }\r
210                 }\r
211                 \r
212                 if ( $posttime > $blog->getCorrectTime() )\r
213                 {\r
214                         $posted = 0;\r
215                         $blog->setFuturePost();\r
216                 }\r
217                 else\r
218                 {\r
219                         $posted = 1;\r
220                 }\r
221                 \r
222                 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft, $posted);\r
223                 \r
224                 //Setting the itemOptions\r
225                 $aOptions = requestArray('plugoption');\r
226                 NucleusPlugin::apply_plugin_options($aOptions, $itemid);\r
227                 $manager->notify('PostPluginOptionsUpdate', array(\r
228                         'context' => 'item',\r
229                         'itemid' => $itemid,\r
230                         'item' => array(\r
231                                 'title' => $i_title,\r
232                                 'body' => $i_body,\r
233                                 'more' => $i_more,\r
234                                 'closed' => $i_closed,\r
235                                 'catid' => $i_catid\r
236                                 )\r
237                         )\r
238                 );\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                 else\r
252                 {\r
253                         return array('status' => 'added', 'itemid' => $itemid);\r
254                 }\r
255         }\r
256         \r
257         /**\r
258          * Item::update()\r
259          * Updates an item\r
260          *\r
261          * @static\r
262          * @param       integer $itemid item id\r
263          * @param       integer $catid  category id\r
264          * @param       string  $title  title\r
265          * @param       string  $body   body text\r
266          * @param       string  $more   more text\r
267          * @param       boolean $closed closed or not\r
268          * @param       boolean $wasdraft       previously draft or not\r
269          * @param       boolean $publish        published or not\r
270          * @param       timestamp       $timestamp      timestamp\r
271          * @return      void\r
272          * \r
273          */\r
274         static public function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)\r
275         {\r
276                 global $manager;\r
277                 \r
278                 $itemid = (integer) $itemid;\r
279                 $closed = (boolean) $closed;\r
280                 \r
281                 // get destination blogid\r
282                 $new_blogid = getBlogIDFromCatID($catid);\r
283                 $old_blogid = getBlogIDFromItemID($itemid);\r
284                 \r
285                 // move will be done on end of method\r
286                 $moveNeeded = 0;\r
287                 if ( $new_blogid != $old_blogid )\r
288                 {\r
289                         $moveNeeded = 1;\r
290                 }\r
291                 \r
292                 $blog =& $manager->getBlog($new_blogid);\r
293                 \r
294                 // begin if: convert line breaks to <br/>\r
295                 if ( $blog->convertBreaks() )\r
296                 {\r
297                         $body = addBreaks($body);\r
298                         $more = addBreaks($more);\r
299                 }\r
300                 \r
301                 // call plugins\r
302                 $manager->notify('PreUpdateItem', array(\r
303                         'itemid'        => $itemid,\r
304                         'title'         => &$title,\r
305                         'body'          => &$body,\r
306                         'more'          => &$more,\r
307                         'blog'          => &$blog,\r
308                         'closed'        => &$closed,\r
309                         'catid'         => &$catid\r
310                         )\r
311                 );\r
312                 \r
313                 // update item itself\r
314                 $query =  'UPDATE ' . sql_table('item')\r
315                                 . ' SET'\r
316                                 . ' ibody = ' . DB::quoteValue($body) . ','\r
317                                 . ' ititle = ' . DB::quoteValue($title) . ','\r
318                                 . ' imore = ' . DB::quoteValue($more) . ','\r
319                                 . ' iclosed = ' . intval($closed) . ','\r
320                                 . ' icat = ' . intval($catid);\r
321                 \r
322                 // if we received an updated timestamp that is in the past, but past posting is not allowed, reject that date change (timestamp = 0 will make sure the current date is kept)\r
323                 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()) )\r
324                 {\r
325                         $timestamp = 0;\r
326                 }\r
327                 \r
328                 // begin if: post is in the future\r
329                 if ( $timestamp > $blog->getCorrectTime(time()) )\r
330                 {\r
331                         $isFuture = 1;\r
332                         $query .= ', iposted = 0';\r
333                 }\r
334                 else\r
335                 {\r
336                         $isFuture = 0;\r
337                         $query .= ', iposted = 1';\r
338                 }\r
339                 \r
340                 if ( $wasdraft && $publish )\r
341                 {\r
342                         // set timestamp to current date only if it's not a future item\r
343                         // draft items have timestamp == 0\r
344                         // don't allow timestamps in the past (unless otherwise defined in blogsettings)\r
345                         $query .= ', idraft = 0';\r
346                         \r
347                         if ( $timestamp == 0 )\r
348                         {\r
349                                 $timestamp = $blog->getCorrectTime();\r
350                         }\r
351                         \r
352                         // send new item notification\r
353                         if ( !$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem() )\r
354                         {\r
355                                 $blog->sendNewItemNotification($itemid, $title, $body);\r
356                         }\r
357                 }\r
358                 \r
359                 // save back to drafts\r
360                 if ( !$wasdraft && !$publish )\r
361                 {\r
362                         $query .= ', idraft = 1';\r
363                         // set timestamp back to zero for a draft\r
364                         $query .= ", itime = '" . i18n::formatted_datetime('mysql', $timestamp) ."'";\r
365                 }\r
366                 \r
367                 // update timestamp when needed\r
368                 if ( $timestamp != 0 )\r
369                 {\r
370                         $query .= ", itime = '" . i18n::formatted_datetime('mysql', $timestamp) ."'";\r
371                 }\r
372                 \r
373                 // make sure the correct item is updated\r
374                 $query .= ' WHERE inumber = ' . $itemid;\r
375                 \r
376                 // off we go!\r
377                 DB::execute($query);\r
378                 \r
379                 $manager->notify('PostUpdateItem', array('itemid' => $itemid));\r
380                 \r
381                 // when needed, move item and comments to new blog\r
382                 if ( $moveNeeded )\r
383                 {\r
384                         self::move($itemid, $catid);\r
385                 }\r
386                 \r
387                 //update the itemOptions\r
388                 $aOptions = requestArray('plugoption');\r
389                 NucleusPlugin::apply_plugin_options($aOptions);\r
390                 $manager->notify('PostPluginOptionsUpdate', array(\r
391                         'context' => 'item',\r
392                         'itemid' => $itemid,\r
393                         'item' => array(\r
394                                 'title' => $title,\r
395                                 'body' => $body,\r
396                                 'more' => $more,\r
397                                 'closed' => $closed,\r
398                                 'catid' => $catid\r
399                                 )\r
400                         )\r
401                 );\r
402                 return;\r
403         }\r
404         \r
405         /**\r
406          * Item::move()\r
407          * Move an item to another blog (no checks)\r
408          *\r
409          * @static\r
410          * @param       integer $itemid\r
411          * @param       integer $new_catid\r
412          * @return      void\r
413          */\r
414         static public function move($itemid, $new_catid)\r
415         {\r
416                 global $manager;\r
417                 \r
418                 $itemid                 = (integer) $itemid;\r
419                 $new_catid      = (integer) $new_catid;\r
420                 $new_blogid     = getBlogIDFromCatID($new_catid);\r
421                 \r
422                 $manager->notify(\r
423                         'PreMoveItem',\r
424                         array(\r
425                                 'itemid' => $itemid,\r
426                                 'destblogid' => $new_blogid,\r
427                                 'destcatid' => $new_catid\r
428                         )\r
429                 );\r
430                 \r
431                 // update item table\r
432                 $query = "UPDATE %s SET iblog=%d, icat=%d WHERE inumber=%d";\r
433                 $query = sprintf($query, sql_table('item'), $new_blogid, $new_catid, $itemid);\r
434                 DB::execute($query);\r
435                 \r
436                 // update comments\r
437                 $query = "UPDATE %s SET cblog=%d WHERE citem=%d";\r
438                 $query = sprintf($query, sql_table('comment'), $new_blogid, $itemid);\r
439                 DB::execute($query);\r
440                 \r
441                 $manager->notify(\r
442                         'PostMoveItem',\r
443                         array(\r
444                                 'itemid' => $itemid,\r
445                                 'destblogid' => $new_blogid,\r
446                                 'destcatid' => $new_catid\r
447                         )\r
448                 );\r
449                 return;\r
450         }\r
451         \r
452         /**\r
453          * Item::delete()\r
454          * Deletes an item\r
455          * \r
456          * @param       integer $itemid\r
457          * @return      void\r
458          */\r
459         static public function delete($itemid)\r
460         {\r
461                 global $manager, $member;\r
462                 \r
463                 $itemid = (integer) $itemid;\r
464                 \r
465                 // check permission\r
466                 if ( !$member->canAlterItem($itemid) )\r
467                 {\r
468                         return 1;\r
469                 }\r
470                 \r
471                 $manager->notify('PreDeleteItem', array('itemid' => $itemid));\r
472                 \r
473                 // delete item\r
474                 $query = "DELETE FROM %s WHERE inumber=%d";\r
475                 $query = sprintf($query, sql_table('item'), $itemid);\r
476                 DB::execute($query);\r
477                 \r
478                 // delete the comments associated with the item\r
479                 $query = "DELETE FROM %s WHERE citem=%d";\r
480                 $query = sprintf($query, sql_table('comment'), $itemid);\r
481                 DB::execute($query);\r
482                 \r
483                 // delete all associated plugin options\r
484                 NucleusPlugin::delete_option_values('item', $itemid);\r
485                 \r
486                 $manager->notify('PostDeleteItem', array('itemid' => $itemid));\r
487                 \r
488                 return 0;\r
489         }\r
490         \r
491         /**\r
492          * Item::exists()\r
493          * Returns true if there is an item with the given ID\r
494          *\r
495          * @static\r
496          * @param       integer $itemid\r
497          * @param       boolean $future\r
498          * @param       boolean $draft\r
499          * @return      boolean exists or not\r
500          * \r
501          */\r
502         static public function exists($itemid, $future, $draft)\r
503         {\r
504                 global $manager;\r
505                 \r
506                 $itemid = (integer) $itemid;\r
507                 $query = 'select * FROM '.sql_table('item').' WHERE inumber='.$itemid;\r
508                 \r
509                 if ( !$future )\r
510                 {\r
511                         $blogid = getBlogIDFromItemID($itemid);\r
512                         if ( !$blogid )\r
513                         {\r
514                                 return 0;\r
515                         }\r
516                         $blog =& $manager->getBlog($blogid);\r
517                         $query .= " and itime<='" . i18n::formatted_datetime('mysql', $blog->getCorrectTime()) ."'";\r
518                 }\r
519                 if ( !$draft )\r
520                 {\r
521                         $query .= ' and idraft=0';\r
522                 }\r
523                 $result = DB::getResult($query);\r
524                 return ( $result->rowCount() != 0 );\r
525         }\r
526         \r
527         /**\r
528          * Item::createDraftFromRequest()\r
529          * Tries to create an draft from the data\r
530          *  in the current request (comes from bookmarklet or admin area)\r
531          *   Used by xmlHTTPRequest AutoDraft\r
532          *\r
533          * Returns an array with status info:\r
534          * status = 'added', 'error', 'newcategory'\r
535          *\r
536          * @static\r
537          * @param       void\r
538          * @return      array   (status = added/error/newcategory, message)\r
539          *\r
540          */\r
541         static public function createDraftFromRequest()\r
542         {\r
543                 global $member, $manager;\r
544                 \r
545                 /*\r
546                  * TODO: these values from user agent should be validated but not implemented yet\r
547                  */\r
548                 $i_author               = $member->getID();\r
549                 $i_body                 = postVar('body');\r
550                 $i_title                = postVar('title');\r
551                 $i_more                 = postVar('more');\r
552                 $i_closed               = intPostVar('closed');\r
553                 $i_catid                = postVar('catid');\r
554                 $i_draft                = 1;\r
555                 $type                           = postVar('type');\r
556                 $i_draftid      = intPostVar('draftid');\r
557                 \r
558                 if ( $type == 'edit' )\r
559                 {\r
560                         $itemid = intPostVar('itemid');\r
561                         $i_blogid = getBlogIDFromItemID($itemid);\r
562                 }\r
563                 else\r
564                 {\r
565                         $i_blogid = intPostVar('blogid');\r
566                 }\r
567                 \r
568                 if ( !$member->canAddItem($i_catid) )\r
569                 {\r
570                         return array('status' => 'error', 'message' => _ERROR_DISALLOWED);\r
571                 }\r
572                 \r
573                 if ( !trim($i_body) )\r
574                 {\r
575                         return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);\r
576                 }\r
577                 \r
578                 // create new category if needed\r
579                 if ( strstr($i_catid, 'newcat') )\r
580                 {\r
581                         // Set in default category\r
582                         $blog =& $manager->getBlog($i_blogid);\r
583                         $i_catid = $blog->getDefaultCategory();\r
584                 }\r
585                 else\r
586                 {\r
587                         // force blogid (must be same as category id)\r
588                         $i_blogid = getBlogIDFromCatID($i_catid);\r
589                         $blog =& $manager->getBlog($i_blogid);\r
590                 }\r
591                 \r
592                 $posttime = 0;\r
593                 \r
594                 if ( $i_draftid > 0 )\r
595                 {\r
596                         self::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);\r
597                         $itemid = $i_draftid;\r
598                 }\r
599                 else\r
600                 {\r
601                         $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);\r
602                 }\r
603                 \r
604                 return array('status' => 'added', 'draftid' => $itemid);\r
605         }\r
606 }\r