OSDN Git Service

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