source: extensions/UserCollections/include/UserCollection.class.php @ 16625

Last change on this file since 16625 was 16625, checked in by mistic100, 12 years ago

-add download link if BatchDownloader is installed
-merge two prefilters
-better support of Stripped

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