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

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

first commit

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