1 | <?php |
---|
2 | defined('USER_COLLEC_PATH') or die('Hacking attempt!'); |
---|
3 | |
---|
4 | class UserCollection |
---|
5 | { |
---|
6 | private $data; |
---|
7 | private $images; |
---|
8 | |
---|
9 | /** |
---|
10 | * __construct |
---|
11 | * @param: mixed col id (##|'new'|'active') |
---|
12 | * @param: array images |
---|
13 | */ |
---|
14 | function __construct($col_id, $images=array(), $name=null, $active=false, $public=false) |
---|
15 | { |
---|
16 | global $user; |
---|
17 | |
---|
18 | $this->data = array( |
---|
19 | 'id' => 0, |
---|
20 | 'user_id' => $user['id'], |
---|
21 | 'name' => null, |
---|
22 | 'date_creation' => '0000-00-00 00:00:00', |
---|
23 | 'nb_images' => 0, |
---|
24 | 'active' => false, |
---|
25 | 'public' => false, |
---|
26 | 'public_id' => null, |
---|
27 | ); |
---|
28 | $this->images = array(); |
---|
29 | |
---|
30 | // access from public id |
---|
31 | if ( strlen($col_id) == 10 and strpos($col_id, 'uc') === 0 ) |
---|
32 | { |
---|
33 | $query = ' |
---|
34 | SELECT id |
---|
35 | FROM '.COLLECTIONS_TABLE.' |
---|
36 | WHERE public_id = "'.$col_id.'" |
---|
37 | ;'; |
---|
38 | $result = pwg_query($query); |
---|
39 | |
---|
40 | if (!pwg_db_num_rows($result)) |
---|
41 | { |
---|
42 | $col_id = 0; |
---|
43 | } |
---|
44 | else |
---|
45 | { |
---|
46 | list($col_id) = pwg_db_fetch_row($result); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | // load specific collection |
---|
51 | if (preg_match('#^[0-9]+$#', $col_id)) |
---|
52 | { |
---|
53 | $query = ' |
---|
54 | SELECT |
---|
55 | id, |
---|
56 | user_id, |
---|
57 | name, |
---|
58 | date_creation, |
---|
59 | nb_images, |
---|
60 | active, |
---|
61 | public, |
---|
62 | public_id |
---|
63 | FROM '.COLLECTIONS_TABLE.' |
---|
64 | WHERE |
---|
65 | id = '.$col_id.' |
---|
66 | '.(!is_admin() ? 'AND (user_id = '.$this->data['user_id'].' OR public = 1)' : null).' |
---|
67 | ;'; |
---|
68 | $result = pwg_query($query); |
---|
69 | |
---|
70 | if (pwg_db_num_rows($result)) |
---|
71 | { |
---|
72 | $this->data = array_merge( |
---|
73 | $this->data, |
---|
74 | pwg_db_fetch_assoc($result) |
---|
75 | ); |
---|
76 | |
---|
77 | // make sur all pictures of the collection exist |
---|
78 | $query = ' |
---|
79 | DELETE FROM '.COLLECTION_IMAGES_TABLE.' |
---|
80 | WHERE image_id NOT IN ( |
---|
81 | SELECT id FROM '.IMAGES_TABLE.' |
---|
82 | ) |
---|
83 | ;'; |
---|
84 | pwg_query($query); |
---|
85 | |
---|
86 | $query = ' |
---|
87 | SELECT image_id |
---|
88 | FROM '.COLLECTION_IMAGES_TABLE.' |
---|
89 | WHERE col_id = '.$this->data['id'].' |
---|
90 | ;'; |
---|
91 | $this->images = array_from_query($query, 'image_id'); |
---|
92 | |
---|
93 | if ($this->data['nb_images'] != count($this->images)) |
---|
94 | { |
---|
95 | $this->updateParam('nb_images', count($this->images)); |
---|
96 | } |
---|
97 | } |
---|
98 | else |
---|
99 | { |
---|
100 | throw new Exception(l10n('Invalid collection')); |
---|
101 | } |
---|
102 | } |
---|
103 | // create a new collection |
---|
104 | else if ($col_id == 'new') |
---|
105 | { |
---|
106 | $this->data['name'] = $name; |
---|
107 | $this->data['active'] = $active; |
---|
108 | $this->data['public'] = $public; |
---|
109 | $this->data['public_id'] = 'uc'.hash('crc32', uniqid(serialize($this->data), true)); |
---|
110 | |
---|
111 | $query = ' |
---|
112 | INSERT INTO '.COLLECTIONS_TABLE.'( |
---|
113 | user_id, |
---|
114 | name, |
---|
115 | date_creation, |
---|
116 | active, |
---|
117 | public, |
---|
118 | public_id |
---|
119 | ) |
---|
120 | VALUES( |
---|
121 | '.$this->data['user_id'].', |
---|
122 | "'.$this->data['name'].'", |
---|
123 | NOW(), |
---|
124 | '.(int)$this->data['active'].', |
---|
125 | '.(int)$this->data['public'].', |
---|
126 | "'.$this->data['public_id'].'" |
---|
127 | ) |
---|
128 | ;'; |
---|
129 | pwg_query($query); |
---|
130 | $this->data['id'] = pwg_db_insert_id(); |
---|
131 | |
---|
132 | $date = pwg_query('SELECT FROM_UNIXTIME(NOW());'); |
---|
133 | list($this->data['date_creation']) = pwg_db_fetch_row($date); |
---|
134 | |
---|
135 | if (!empty($images)) |
---|
136 | { |
---|
137 | $this->addImages($images); |
---|
138 | } |
---|
139 | |
---|
140 | // only one active collection allowed |
---|
141 | if ($this->data['active']) |
---|
142 | { |
---|
143 | $query = ' |
---|
144 | UPDATE '.COLLECTIONS_TABLE.' |
---|
145 | SET active = 0 |
---|
146 | WHERE |
---|
147 | user_id = '.$this->data['user_id'].' |
---|
148 | AND id != '.$this->data['id'].' |
---|
149 | ;'; |
---|
150 | pwg_query($query); |
---|
151 | } |
---|
152 | } |
---|
153 | else |
---|
154 | { |
---|
155 | trigger_error('UserCollection::__construct, invalid input parameter', E_USER_ERROR); |
---|
156 | } |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * updateParam |
---|
161 | * @param: string param name |
---|
162 | * @param: mixed param value |
---|
163 | */ |
---|
164 | function updateParam($name, $value) |
---|
165 | { |
---|
166 | $this->data[$name] = $value; |
---|
167 | pwg_query('UPDATE '.COLLECTIONS_TABLE.' SET '.$name.' = "'.$value.'" WHERE id = '.$this->data['id'].';'); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * getParam |
---|
172 | * @param: string param name |
---|
173 | * @return: mixed param value |
---|
174 | */ |
---|
175 | function getParam($name) |
---|
176 | { |
---|
177 | return $this->data[$name]; |
---|
178 | } |
---|
179 | |
---|
180 | /** |
---|
181 | * getImages |
---|
182 | * @return: array |
---|
183 | */ |
---|
184 | function getImages() |
---|
185 | { |
---|
186 | return $this->images; |
---|
187 | } |
---|
188 | |
---|
189 | /** |
---|
190 | * isInSet |
---|
191 | * @param: int image id |
---|
192 | * @return: bool |
---|
193 | */ |
---|
194 | function isInSet($image_id) |
---|
195 | { |
---|
196 | return in_array($image_id, $this->images); |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * removeImages |
---|
201 | * @param: array image ids |
---|
202 | */ |
---|
203 | function removeImages($image_ids) |
---|
204 | { |
---|
205 | if (empty($image_ids) or !is_array($image_ids)) return; |
---|
206 | |
---|
207 | foreach ($image_ids as $image_id) |
---|
208 | { |
---|
209 | unset($this->images[ array_search($image_id, $this->images) ]); |
---|
210 | } |
---|
211 | |
---|
212 | $query = ' |
---|
213 | DELETE FROM '.COLLECTION_IMAGES_TABLE.' |
---|
214 | WHERE |
---|
215 | col_id = '.$this->data['id'].' |
---|
216 | AND image_id IN('.implode(',', $image_ids).') |
---|
217 | ;'; |
---|
218 | pwg_query($query); |
---|
219 | |
---|
220 | $this->updateParam('nb_images', count($this->images)); |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | * addImages |
---|
225 | * @param: array image ids |
---|
226 | */ |
---|
227 | function addImages($image_ids) |
---|
228 | { |
---|
229 | if (empty($image_ids) or !is_array($image_ids)) return; |
---|
230 | |
---|
231 | $image_ids = array_unique($image_ids); |
---|
232 | $inserts = array(); |
---|
233 | |
---|
234 | foreach ($image_ids as $image_id) |
---|
235 | { |
---|
236 | if ($this->isInSet($image_id)) continue; |
---|
237 | |
---|
238 | array_push($this->images, $image_id); |
---|
239 | array_push($inserts, array('col_id'=>$this->data['id'], 'image_id'=>$image_id)); |
---|
240 | } |
---|
241 | |
---|
242 | mass_inserts( |
---|
243 | COLLECTION_IMAGES_TABLE, |
---|
244 | array('col_id', 'image_id'), |
---|
245 | $inserts |
---|
246 | ); |
---|
247 | |
---|
248 | $this->updateParam('nb_images', count($this->images)); |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * toggleImage |
---|
253 | * @param: int image id |
---|
254 | */ |
---|
255 | function toggleImage($image_id) |
---|
256 | { |
---|
257 | if ($this->isInSet($image_id)) |
---|
258 | { |
---|
259 | $this->removeImages(array($image_id)); |
---|
260 | } |
---|
261 | else |
---|
262 | { |
---|
263 | $this->addImages(array($image_id)); |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | /** |
---|
268 | * clearImages |
---|
269 | */ |
---|
270 | function clearImages() |
---|
271 | { |
---|
272 | $this->images = array(); |
---|
273 | $this->updateParam('nb_images', 0); |
---|
274 | |
---|
275 | $query = ' |
---|
276 | DELETE FROM '.COLLECTION_IMAGES_TABLE.' |
---|
277 | WHERE col_id = '.$this->data['id'].' |
---|
278 | ;'; |
---|
279 | pwg_query($query); |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * getCollectionInfo |
---|
284 | * @return: array |
---|
285 | */ |
---|
286 | function getCollectionInfo() |
---|
287 | { |
---|
288 | $set = array( |
---|
289 | 'NAME' => $this->data['name'], |
---|
290 | 'NB_IMAGES' => $this->data['nb_images'], |
---|
291 | 'ACTIVE' => (bool)$this->data['active'], |
---|
292 | 'PUBLIC' => (bool)$this->data['public'], |
---|
293 | 'DATE_CREATION' => format_date($this->data['date_creation'], true), |
---|
294 | 'U_PUBLIC' => USER_COLLEC_PUBLIC . 'view/'.$this->data['public_id'], |
---|
295 | 'IS_TEMP' => $this->data['name'] == 'temp', |
---|
296 | ); |
---|
297 | |
---|
298 | return $set; |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | ?> |
---|