OSDN Git Service

MERGE:リビジョン1668/1669をマージ。
[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 1593 2011-11-01 18:42:03Z gregorlove $
17  */
18
19 /**
20  * A class representing an item
21  *
22  */
23 class ITEM
24 {
25
26         /**
27          * Item ID (int)
28          */
29         public $itemid;
30
31
32         /**
33          * Creates a new ITEM object
34          * @param int $item_id
35          */
36         public function __construct($item_id)
37         {
38                 $this->itemid = $item_id;
39         }
40
41
42         /**
43          * Returns one item with the specific itemid
44          *
45          * @param int $item_id
46          * @param bool $allow_draft
47          * @param bool $allow_future
48          * @return mixed
49          */
50         public function getitem($item_id, $allow_draft, $allow_future)
51         {
52                 global $manager;
53
54                 $item_id = intval($item_id);
55
56                 $query = 'SELECT ' .
57                         '`i`.`idraft` AS `draft`, ' .
58                         '`i`.`inumber` AS `itemid`, ' .
59                         '`i`.`iclosed` AS `closed`, ' .
60                         '`i`.`ititle` AS `title`, ' .
61                         '`i`.`ibody` AS `body`, ' .
62                         '`m`.`mname` AS `author`, ' .
63                         '`i`.`iauthor` AS `authorid`, ' .
64                         '`i`.`itime`, ' .
65                         '`i`.`imore` AS `more`, ' .
66                         '`i`.`ikarmapos` AS `karmapos`, ' .
67                         '`i`.`ikarmaneg` AS `karmaneg`, ' .
68                         '`i`.`icat` AS `catid`, ' .
69                         '`i`.`iblog` AS `blogid` ' .
70                         'FROM `%s` AS `i`, `%s` AS `m`, `%s` AS `b` ' .
71                         'WHERE `i`.`inumber` = %d ' .
72                         'AND `i`.`iauthor` = `m`.`mnumber` ' .
73                         'AND `i`.`iblog` = `b`.`bnumber` ';
74
75                 $query = sprintf($query, sql_table('item'), sql_table('member'), sql_table('blog'), $item_id);
76
77                 if ( !$allow_draft )
78                 {
79                         $query .= 'AND `i`.`idraft` = 0 ';
80                 }
81
82                 if ( !$allow_future )
83                 {
84                         $blog =& $manager->getBlog(getBlogIDFromItemID($item_id));
85                         $query .= 'AND `i`.`itime` <= ' . mysqldate($blog->getCorrectTime());
86                 }
87
88                 $query .= ' LIMIT 1';
89                 $result = sql_query($query);
90
91                 if ( sql_num_rows($result) == 1 )
92                 {
93                         $aItemInfo = sql_fetch_assoc($result);
94                         $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
95                         return $aItemInfo;
96                 }
97                 else
98                 {
99                         return 0;
100                 }
101
102         }
103
104
105         /**
106          * Tries to create an item from the data in the current request (comes from
107          * bookmarklet or admin area
108          *
109          * Returns an array with status info:
110          * status = 'added', 'error', 'newcategory'
111          *
112          * @static
113          */
114         function createFromRequest()
115         {
116                 global $member, $manager;
117
118                 $i_author = $member->getID();
119                 $i_body = postVar('body');
120                 $i_title = postVar('title');
121                 $i_more = postVar('more');
122                 $i_actiontype = postVar('actiontype');
123                 $i_closed = intPostVar('closed');
124                 $i_hour = intPostVar('hour');
125                 $i_minutes = intPostVar('minutes');
126                 $i_month = intPostVar('month');
127                 $i_day = intPostVar('day');
128                 $i_year = intPostVar('year');
129                 $i_catid = postVar('catid');
130                 $i_draftid = intPostVar('draftid');
131
132                 if ( !$member->canAddItem($i_catid) )
133                 {
134                         return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
135                 }
136
137                 if (!$i_actiontype)
138                         $i_actiontype = 'addnow';
139
140                 switch ( $i_actiontype )
141                 {
142                         case 'adddraft':
143                                 $i_draft = 1;
144                         break;
145
146                         case 'addfuture':
147                         case 'addnow':
148                         default:
149                                 $i_draft = 0;
150                         break;
151                 }
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 ( strstr($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                 }
175                 else
176                 {
177                         // force blogid (must be same as category id)
178                         $i_blogid = getBlogIDFromCatID($i_catid);
179                         $blog =& $manager->getBlog($i_blogid);
180                 }
181
182                 if ( $i_actiontype == 'addfuture' )
183                 {
184                         $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
185
186                         // make sure the date is in the future, unless we allow past dates
187                         if ( (!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()) )
188                         {
189                                 $posttime = $blog->getCorrectTime();
190                         }
191
192                 }
193                 else
194                 {
195                         // time with offset, or 0 for drafts
196                         $posttime = $i_draft ? 0 : $blog->getCorrectTime();
197                 }
198
199                 if ( $posttime > $blog->getCorrectTime() )
200                 {
201                         $posted = 0;
202                         $blog->setFuturePost();
203                 }
204                 else
205                 {
206                         $posted = 1;
207                 }
208
209                 $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft, $posted);
210
211                 //Setting the itemOptions
212                 $aOptions = requestArray('plugoption');
213                 NucleusPlugin::apply_plugin_options($aOptions, $itemid);
214                 $manager->notify('PostPluginOptionsUpdate', array(
215                         'context' => 'item',
216                         'itemid' => $itemid,
217                         'item' => array(
218                                 'title' => $i_title,
219                                 'body' => $i_body,
220                                 'more' => $i_more,
221                                 'closed' => $i_closed,
222                                 'catid' => $i_catid
223                                 )
224                         )
225                 );
226
227                 if ( $i_draftid > 0 )
228                 {
229                         // delete permission is checked inside ITEM::delete()
230                         ITEM::delete($i_draftid);
231                 }
232
233                 // success
234                 if ( $i_catid != intRequestVar('catid') )
235                 {
236                         return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
237                 }
238                 else
239                 {
240                         return array('status' => 'added', 'itemid' => $itemid);
241                 }
242
243         }
244
245
246         /**
247           * Updates an item
248           *
249           * @static
250           */
251         function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0)
252         {
253                 global $manager;
254
255                 $itemid = intval($itemid);
256
257                 // make sure value is 1 or 0
258                 if ( $closed != 1 )
259                 {
260                         $closed = 0;
261                 }
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                 } // end if
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                 } // end if
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                 } // end if
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                 } // end if
340
341                 // save back to drafts
342                 if ( !$wasdraft && !$publish )
343                 {
344                         $query .= ', idraft = 1';
345                         // set timestamp back to zero for a draft
346                         $query .= ', itime = ' . mysqldate($timestamp);
347                 }
348
349                 // update timestamp when needed
350                 if ( $timestamp != 0 )
351                 {
352                         $query .= ', itime = ' . mysqldate($timestamp);
353                 }
354
355                 // make sure the correct item is updated
356                 $query .= ' WHERE inumber = ' . $itemid;
357
358                 // off we go!
359                 sql_query($query);
360
361                 $manager->notify('PostUpdateItem', array('itemid' => $itemid));
362
363                 // when needed, move item and comments to new blog
364                 if ( $moveNeeded )
365                 {
366                         ITEM::move($itemid, $catid);
367                 }
368
369                 //update the itemOptions
370                 $aOptions = requestArray('plugoption');
371                 NucleusPlugin::apply_plugin_options($aOptions);
372                 $manager->notify('PostPluginOptionsUpdate', array(
373                         'context' => 'item',
374                         'itemid' => $itemid,
375                         'item' => array(
376                                 'title' => $title,
377                                 'body' => $body,
378                                 'more' => $more,
379                                 'closed' => $closed,
380                                 'catid' => $catid
381                                 )
382                         )
383                 );
384         }
385
386
387         /**
388          * Move an item to another blog (no checks)
389          *
390          * @static
391          */
392         function move($itemid, $new_catid) {
393                 global $manager;
394
395                 $itemid = intval($itemid);
396                 $new_catid = intval($new_catid);
397
398                 $new_blogid = getBlogIDFromCatID($new_catid);
399
400                 $manager->notify(
401                         'PreMoveItem',
402                         array(
403                                 'itemid' => $itemid,
404                                 'destblogid' => $new_blogid,
405                                 'destcatid' => $new_catid
406                         )
407                 );
408
409
410                 // update item table
411                 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
412                 sql_query($query);
413
414                 // update comments
415                 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
416                 sql_query($query);
417
418                 $manager->notify(
419                         'PostMoveItem',
420                         array(
421                                 'itemid' => $itemid,
422                                 'destblogid' => $new_blogid,
423                                 'destcatid' => $new_catid
424                         )
425                 );
426         }
427         
428         /**
429          * ITEM::delete()
430          * Deletes an item
431          * 
432          * @param       Void
433          * @return      Void
434          */
435         function delete($itemid)
436         {
437                 global $manager, $member;
438                 
439                 $itemid = (integer) $itemid;
440                 
441                 // check to ensure only those allow to alter the item can
442                 // proceed
443                 if ( !$member->canAlterItem($itemid) )
444                 {
445                         return 1;
446                 }
447                 
448                 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
449                 
450                 // delete item
451                 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
452                 sql_query($query);
453                 
454                 // delete the comments associated with the item
455                 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
456                 sql_query($query);
457                 
458                 // delete all associated plugin options
459                 NucleusPlugin::delete_option_values('item', $itemid);
460                 
461                 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
462                 
463                 return 0;
464         }
465         
466         /**
467          * Returns true if there is an item with the given ID
468          *
469          * @static
470          */
471         function exists($id,$future,$draft) {
472                 global $manager;
473
474                 $id = intval($id);
475
476                 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
477                 if (!$future) {
478                         $bid = getBlogIDFromItemID($id);
479                         if (!$bid) return 0;
480                         $b =& $manager->getBlog($bid);
481                         $r .= ' and itime<='.mysqldate($b->getCorrectTime());
482                 }
483                 if (!$draft) {
484                         $r .= ' and idraft=0';
485                 }
486                 $r = sql_query($r);
487
488                 return (sql_num_rows($r) != 0);
489         }
490
491         /**
492          * Tries to create an draft from the data in the current request (comes from
493          * bookmarklet or admin area
494          *
495          * Returns an array with status info:
496          * status = 'added', 'error', 'newcategory'
497          *
498          * @static
499          *
500          * Used by xmlHTTPRequest AutoDraft
501          */
502         function createDraftFromRequest() {
503                 global $member, $manager;
504
505                 $i_author = $member->getID();
506                 $i_body = postVar('body');
507                 $i_title = postVar('title');
508                 $i_more = postVar('more');
509                 //$i_actiontype = postVar('actiontype');
510                 $i_closed = intPostVar('closed');
511                 //$i_hour = intPostVar('hour');
512                 //$i_minutes = intPostVar('minutes');
513                 //$i_month = intPostVar('month');
514                 //$i_day = intPostVar('day');
515                 //$i_year = intPostVar('year');
516                 $i_catid = postVar('catid');
517                 $i_draft = 1;
518                 $type = postVar('type');
519                 if ($type == 'edit') {
520                         $i_blogid = getBlogIDFromItemID(intPostVar('itemid'));
521                 }
522                 else {
523                         $i_blogid = intPostVar('blogid');
524                 }
525                 $i_draftid = intPostVar('draftid');
526
527                 if (!$member->canAddItem($i_catid)) {
528                         return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
529                 }
530
531                 if (!trim($i_body)) {
532                         return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
533                 }
534
535                 // create new category if needed
536                 if (strstr($i_catid, 'newcat')) {
537                         // Set in default category
538                         $blog =& $manager->getBlog($i_blogid);
539                         $i_catid = $blog->getDefaultCategory();
540                 }
541                 else {
542                         // force blogid (must be same as category id)
543                         $i_blogid = getBlogIDFromCatID($i_catid);
544                         $blog =& $manager->getBlog($i_blogid);
545                 }
546
547                 $posttime = 0;
548
549                 if ($i_draftid > 0) {
550                         ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
551                         $itemid = $i_draftid;
552                 }
553                 else {
554                         $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
555                 }
556
557                 // No plugin support in AutoSaveDraft yet
558                 //Setting the itemOptions
559                 //$aOptions = requestArray('plugoption');
560                 //NucleusPlugin::apply_plugin_options($aOptions, $itemid);
561                 //$manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $i_title, 'body' => $i_body, 'more' => $i_more, 'closed' => $i_closed, 'catid' => $i_catid)));
562
563                 // success
564                 return array('status' => 'added', 'draftid' => $itemid);
565         }
566
567 }
568
569 ?>