OSDN Git Service

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