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