OSDN Git Service

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