OSDN Git Service

Avoid fatal error in parse_bloglist() when the blog is not defined.
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / ITEM.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2007 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  * A class representing an item
15  *
16  * @license http://nucleuscms.org/license.txt GNU General Public License
17  * @copyright Copyright (C) 2002-2007 The Nucleus Group
18  * @version $Id: ITEM.php,v 1.7 2007-02-04 06:28:46 kimitake Exp $
19  * $NucleusJP: ITEM.php,v 1.6 2006/07/20 08:01:52 kimitake Exp $
20  */
21 class ITEM {
22
23         var $itemid;
24
25         function ITEM($itemid) {
26                 $this->itemid = $itemid;
27         }
28
29         /**
30           * Returns one item with the specific itemid
31           * (static)
32           */
33         function getitem($itemid, $allowdraft, $allowfuture) {
34                 global $manager;
35
36                 $itemid = intval($itemid);
37
38                 $query =  'SELECT i.idraft as draft, i.inumber as itemid, i.iclosed as closed, '
39                            . ' i.ititle as title, i.ibody as body, m.mname as author, '
40                            . ' i.iauthor as authorid, i.itime, i.imore as more, i.ikarmapos as karmapos, '
41                            . ' i.ikarmaneg as karmaneg, i.icat as catid, i.iblog as blogid '
42                            . ' FROM '.sql_table('item').' as i, '.sql_table('member').' as m, ' . sql_table('blog') . ' as b '
43                            . ' WHERE i.inumber=' . $itemid
44                            . ' and i.iauthor=m.mnumber '
45                            . ' and i.iblog=b.bnumber';
46
47                 if (!$allowdraft)
48                         $query .= ' and i.idraft=0';
49
50                 if (!$allowfuture) {
51                         $blog =& $manager->getBlog(getBlogIDFromItemID($itemid));
52                         $query .= ' and i.itime <=' . mysqldate($blog->getCorrectTime());
53                 }
54
55                 $query .= ' LIMIT 1';
56
57                 $res = sql_query($query);
58
59                 if (mysql_num_rows($res) == 1)
60                 {
61                         $aItemInfo = mysql_fetch_assoc($res);
62                         $aItemInfo['timestamp'] = strtotime($aItemInfo['itime']);
63                         return $aItemInfo;
64                 } else {
65                         return 0;
66                 }
67
68         }
69
70         /**
71          * Tries to create an item from the data in the current request (comes from
72          * bookmarklet or admin area
73          *
74          * Returns an array with status info (status = 'added', 'error', 'newcategory')
75          *
76          * (static)
77          */
78         function createFromRequest() {
79                  global $member, $manager;
80
81                  $i_author =            $member->getID();
82                  $i_body =                      postVar('body');
83                  $i_title =                     postVar('title');
84                  $i_more =                      postVar('more');
85                  $i_actiontype =        postVar('actiontype');
86                  $i_closed =            intPostVar('closed');
87                  $i_hour =                      intPostVar('hour');
88                  $i_minutes =           intPostVar('minutes');
89                  $i_month =             intPostVar('month');
90                  $i_day =                       intPostVar('day');
91                  $i_year =                      intPostVar('year');
92
93                  $i_catid =             postVar('catid');
94
95                  $i_draftid =           intPostVar('draftid');
96
97                  if (!$member->canAddItem($i_catid))
98                         return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
99
100                  if (!$i_actiontype) $i_actiontype = 'addnow';
101
102                  switch ($i_actiontype) {
103                         case 'adddraft':
104                                 $i_draft = 1;
105                                 break;
106                         case 'addfuture':
107                         case 'addnow':
108                         default:
109                                 $i_draft = 0;
110                  }
111
112                  if (!trim($i_body))
113                         return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
114
115                 // create new category if needed
116                 if (strstr($i_catid,'newcat')) {
117                         // get blogid
118                         list($i_blogid) = sscanf($i_catid,"newcat-%d");
119
120                         // create
121                         $blog =& $manager->getBlog($i_blogid);
122                         $i_catid = $blog->createNewCategory();
123
124                         // show error when sth goes wrong
125                         if (!$i_catid)
126                                 return array('status' => 'error','message' => 'Could not create new category');
127                 } else {
128                         // force blogid (must be same as category id)
129                         $i_blogid = getBlogIDFromCatID($i_catid);
130                         $blog =& $manager->getBlog($i_blogid);
131                 }
132
133                 if ($i_actiontype == 'addfuture') {
134                         $posttime = mktime($i_hour, $i_minutes, 0, $i_month, $i_day, $i_year);
135
136                         // make sure the date is in the future, unless we allow past dates
137                         if ((!$blog->allowPastPosting()) && ($posttime < $blog->getCorrectTime()))
138                                 $posttime = $blog->getCorrectTime();
139                 } else {
140                         // time with offset, or 0 for drafts
141                         $posttime = $i_draft ? 0 : $blog->getCorrectTime();
142                 }
143
144                 $itemid = $blog->additem($i_catid, $i_title,$i_body,$i_more,$i_blogid,$i_author,$posttime,$i_closed,$i_draft);
145
146                 //Setting the itemOptions
147                 $aOptions = requestArray('plugoption');
148                 NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
149                 $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)));
150
151                 if ($i_draftid > 0) {
152                         ITEM::delete($i_draftid);
153                 }
154
155                 // success
156                 if ($i_catid != intRequestVar('catid'))
157                         return array('status' => 'newcategory', 'itemid' => $itemid, 'catid' => $i_catid);
158                 else
159                         return array('status' => 'added', 'itemid' => $itemid);
160         }
161
162
163         /**
164           * Updates an item (static)
165           */
166         function update($itemid, $catid, $title, $body, $more, $closed, $wasdraft, $publish, $timestamp = 0) {
167                 global $manager;
168
169                 $itemid = intval($itemid);
170
171                 // make sure value is 1 or 0
172                 if ($closed != 1) $closed = 0;
173
174                 // get destination blogid
175                 $new_blogid = getBlogIDFromCatID($catid);
176                 $old_blogid = getBlogIDFromItemID($itemid);
177
178                 // move will be done on end of method
179                 if ($new_blogid != $old_blogid)
180                         $moveNeeded = 1;
181
182                 // add <br /> before newlines
183                 $blog =& $manager->getBlog($new_blogid);
184                 if ($blog->convertBreaks()) {
185                         $body = addBreaks($body);
186                         $more = addBreaks($more);
187                 }
188
189                 // call plugins
190                 $manager->notify('PreUpdateItem',array('itemid' => $itemid, 'title' => &$title, 'body' => &$body, 'more' => &$more, 'blog' => &$blog, 'closed' => &$closed, 'catid' => &$catid));
191
192                 // update item itsself
193                 $query =  'UPDATE '.sql_table('item')
194                            . ' SET'
195                            . " ibody='". addslashes($body) ."',"
196                            . " ititle='" . addslashes($title) . "',"
197                            . " imore='" . addslashes($more) . "',"
198                            . " iclosed=" . intval($closed) . ","
199                            . " icat=" . intval($catid);
200
201                 // if we received an updated timestamp in the past, but past posting is not allowed,
202                 // reject that date change (timestamp = 0 will make sure the current date is kept)
203                 if ( (!$blog->allowPastPosting()) && ($timestamp < $blog->getCorrectTime()))
204                                 $timestamp = 0;
205
206                 if ($wasdraft && $publish) {
207                         $query .= ', idraft=0';
208
209                         // set timestamp to current date only if it's not a future item
210                         // draft items have timestamp == 0
211                         // don't allow timestamps in the past (unless otherwise defined in blogsettings)
212                         if ($timestamp > $blog->getCorrectTime())
213                                 $isFuture = 1;
214
215                         if ($timestamp == 0)
216                                 $timestamp = $blog->getCorrectTime();
217
218                         // send new item notification
219                         if (!$isFuture && $blog->getNotifyAddress() && $blog->notifyOnNewItem())
220                                 $blog->sendNewItemNotification($itemid, $title, $body);
221                 }
222
223                 // update timestamp when needed
224                 if ($timestamp != 0)
225                         $query .= ", itime=" . mysqldate($timestamp);
226
227                 // make sure the correct item is updated
228                 $query .= ' WHERE inumber=' . $itemid;
229
230                 // off we go!
231                 sql_query($query);
232
233                 $manager->notify('PostUpdateItem',array('itemid' => $itemid));
234
235                 // when needed, move item and comments to new blog
236                 if ($moveNeeded)
237                         ITEM::move($itemid, $catid);
238
239                 //update the itemOptions
240                 $aOptions = requestArray('plugoption');
241                 NucleusPlugin::_applyPluginOptions($aOptions);
242                 $manager->notify('PostPluginOptionsUpdate',array('context' => 'item', 'itemid' => $itemid, 'item' => array('title' => $title, 'body' => $body, 'more' => $more, 'closed' => $closed, 'catid' => $catid)));
243
244         }
245
246         // move an item to another blog (no checks, static)
247         function move($itemid, $new_catid) {
248                 global $manager;
249
250                 $itemid = intval($itemid);
251                 $new_catid = intval($new_catid);
252
253                 $new_blogid = getBlogIDFromCatID($new_catid);
254
255                 $manager->notify(
256                         'PreMoveItem',
257                         array(
258                                 'itemid' => $itemid,
259                                 'destblogid' => $new_blogid,
260                                 'destcatid' => $new_catid
261                         )
262                 );
263
264
265                 // update item table
266                 $query = 'UPDATE '.sql_table('item')." SET iblog=$new_blogid, icat=$new_catid WHERE inumber=$itemid";
267                 sql_query($query);
268
269                 // update comments
270                 $query = 'UPDATE '.sql_table('comment')." SET cblog=" . $new_blogid." WHERE citem=" . $itemid;
271                 sql_query($query);
272
273                 $manager->notify(
274                         'PostMoveItem',
275                         array(
276                                 'itemid' => $itemid,
277                                 'destblogid' => $new_blogid,
278                                 'destcatid' => $new_catid
279                         )
280                 );
281         }
282
283         /**
284           * Deletes an item
285           */
286         function delete($itemid) {
287                 global $manager;
288
289                 $itemid = intval($itemid);
290
291                 $manager->notify('PreDeleteItem', array('itemid' => $itemid));
292
293                 // delete item
294                 $query = 'DELETE FROM '.sql_table('item').' WHERE inumber=' . $itemid;
295                 sql_query($query);
296
297                 // delete the comments associated with the item
298                 $query = 'DELETE FROM '.sql_table('comment').' WHERE citem=' . $itemid;
299                 sql_query($query);
300
301                 // delete all associated plugin options
302                 NucleusPlugin::_deleteOptionValues('item', $itemid);
303
304                 $manager->notify('PostDeleteItem', array('itemid' => $itemid));
305         }
306
307         // returns true if there is an item with the given ID (static)
308         function exists($id,$future,$draft) {
309                 global $manager;
310
311                 $id = intval($id);
312
313                 $r = 'select * FROM '.sql_table('item').' WHERE inumber='.$id;
314                 if (!$future) {
315                         $bid = getBlogIDFromItemID($id);
316                         if (!$bid) return 0;
317                         $b =& $manager->getBlog($bid);
318                         $r .= ' and itime<='.mysqldate($b->getCorrectTime());
319                 }
320                 if (!$draft) {
321                         $r .= ' and idraft=0';
322                 }
323                 $r = sql_query($r);
324
325                 return (mysql_num_rows($r) != 0);
326         }
327
328         /**
329          * Tries to create an draft from the data in the current request (comes from
330          * bookmarklet or admin area
331          *
332          * Returns an array with status info (status = 'added', 'error', 'newcategory')
333          *
334          * (static)
335          *
336          * Used by xmlHTTPRequest AutoDraft
337          */
338         function createDraftFromRequest() {
339                 global $member, $manager;
340
341                 $i_author = $member->getID();
342                 $i_body = postVar('body');
343                 $i_title = postVar('title');
344                 $i_more = postVar('more');
345                 //$i_actiontype = postVar('actiontype');
346                 $i_closed = intPostVar('closed');
347                 //$i_hour = intPostVar('hour');
348                 //$i_minutes = intPostVar('minutes');
349                 //$i_month = intPostVar('month');
350                 //$i_day = intPostVar('day');
351                 //$i_year = intPostVar('year');
352                 $i_catid = postVar('catid');
353                 $i_draft = 1;
354                 $type = postVar('type');
355                 if ($type == 'edit') {
356                         $i_blogid = getBlogIDFromItemID(intPostVar('itemid'));
357                 }
358                 else {
359                         $i_blogid = intPostVar('blogid');
360                 }
361                 $i_draftid = intPostVar('draftid');
362
363                 if (!$member->canAddItem($i_catid)) {
364                         return array('status' => 'error', 'message' => _ERROR_DISALLOWED);
365                 }
366
367                 if (!trim($i_body)) {
368                         return array('status' => 'error', 'message' => _ERROR_NOEMPTYITEMS);
369                 }
370
371                 // create new category if needed
372                 if (strstr($i_catid, 'newcat')) {
373                         // Set in default category
374                         $blog =& $manager->getBlog($i_blogid);
375                         $i_catid = $blog->getDefaultCategory();
376                 }
377                 else {
378                         // force blogid (must be same as category id)
379                         $i_blogid = getBlogIDFromCatID($i_catid);
380                         $blog =& $manager->getBlog($i_blogid);
381                 }
382
383                 $posttime = 0;
384
385                 if ($i_draftid > 0) {
386                         ITEM::update($i_draftid, $i_catid, $i_title, $i_body, $i_more, $i_closed, 1, 0, 0);
387                         $itemid = $i_draftid;
388                 }
389                 else {
390                         $itemid = $blog->additem($i_catid, $i_title, $i_body, $i_more, $i_blogid, $i_author, $posttime, $i_closed, $i_draft);
391                 }
392
393                 // No plugin support in AutoSaveDraft yet
394                 //Setting the itemOptions
395                 //$aOptions = requestArray('plugoption');
396                 //NucleusPlugin::_applyPluginOptions($aOptions, $itemid);
397                 //$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)));
398
399                 // success
400                 return array('status' => 'added', 'draftid' => $itemid);
401         }
402
403 }
404
405 ?>