OSDN Git Service

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