OSDN Git Service

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