OSDN Git Service

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