source: extensions/SmartAlbums/include/functions.inc.php @ 11333

Last change on this file since 11333 was 11333, checked in by mistic100, 13 years ago

clean code, use TokenInput

File size: 7.1 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4/*
5 * Associates images to the category according to the filters
6 * @param int category_id
7 * @return array
8 */
9function smart_make_associations($cat_id)
10{
11  pwg_query('DELETE FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$cat_id.' AND smart = true;');
12 
13  $images = smart_get_pictures($cat_id);
14 
15  if (count($images) != 0)
16  {
17    foreach ($images as $img)
18    {
19      $datas[] = array(
20        'image_id' => $img,
21        'category_id' => $cat_id,
22        'smart' => true,
23        );
24    }
25    mass_inserts_ignore(
26      IMAGE_CATEGORY_TABLE, 
27      array_keys($datas[0]), 
28      $datas
29      );
30    set_random_representant(array($cat_id));
31  }
32 
33  return $images;
34}
35
36
37/*
38 * Generates the list of images, according to the filters of the category
39 * @param int category_id
40 * @param array filters, if null => catch from db
41 * @return array
42 */
43function smart_get_pictures($cat_id, $filters = null)
44{
45  global $conf;
46
47  /* get filters */
48  if ($filters == null)
49  {
50    $query = '
51SELECT *
52  FROM '.CATEGORY_FILTERS_TABLE.'
53  WHERE category_id = '.$cat_id.'
54  ORDER BY type ASC, cond ASC
55;';
56    $filters = pwg_query($query);
57   
58    if (!pwg_db_num_rows($filters)) return array();
59   
60    while ($filter = pwg_db_fetch_assoc($filters))
61    {
62      $temp[] = array(
63        'type' => $filter['type'],
64        'cond' => $filter['cond'],
65        'value' => $filter['value'],
66        );
67    }
68     
69    $filters = $temp;
70  }
71   
72  /* build constrains */
73  ## generate 'join', 'where' arrays and 'limit' string to create the SQL query
74  ## inspired by PicsEngine by Michael Villar
75  $i_tags = 1;
76  foreach ($filters as $filter)
77  {
78    // tags
79    if ($filter['type'] == 'tags')
80    {
81        if($filter['cond'] == "all")
82        {
83          $tags_arr = explode(',', $filter['value']);
84         
85          foreach($tags_arr as $value)
86          {
87            $join[] = IMAGE_TAG_TABLE.' AS it_'.$i_tags.' ON i.id = it_'.$i_tags.'.image_id';
88            $where[] = 'it_'.$i_tags.'.tag_id = '.$value;
89            $i_tags++;
90          }
91        }
92        else if ($filter['cond'] == 'one') 
93        {
94          $join[] = IMAGE_TAG_TABLE.' AS it_'.$i_tags.' ON i.id = it_'.$i_tags.'.image_id';
95          $where[] = 'it_'.$i_tags.'.tag_id IN ('.$filter['value'].')';
96          $i_tags++;
97        }
98        else if ($filter['cond'] == 'none') 
99        {
100          $sub_query = '
101      SELECT it_'.$i_tags.'.image_id
102        FROM '.IMAGE_TAG_TABLE.' AS it_'.$i_tags.'
103        WHERE
104          it_'.$i_tags.'.image_id = i.id AND
105          it_'.$i_tags.'.tag_id IN ('.$filter['value'].')
106        GROUP BY it_'.$i_tags.'.image_id
107    ';
108          $where[] = 'NOT EXISTS ('.$sub_query.')';
109          $i_tags++;
110        }
111        else if ($filter['cond'] == 'only') 
112        {
113          $sub_query = '
114      SELECT it_'.$i_tags.'.image_id
115        FROM '.IMAGE_TAG_TABLE.' AS it_'.$i_tags.'
116        WHERE
117          it_'.$i_tags.'.image_id = i.id AND
118          it_'.$i_tags.'.tag_id NOT IN ('.$filter['value'].')
119        GROUP BY it_'.$i_tags.'.image_id
120    ';
121          $where[] = 'NOT EXISTS ('.$sub_query.')';
122       
123          $i_tags++;
124          $tags_arr = explode(',', $filter['value']);
125         
126          foreach($tags_arr as $value)
127          {
128            $join[] = IMAGE_TAG_TABLE.' AS it_'.$i_tags.' ON i.id = it_'.$i_tags.'.image_id';
129            $where[] = 'it_'.$i_tags.'.tag_id = '.$value;
130            $i_tags++;
131          }
132        }       
133    }
134    // date
135    else if ($filter['type'] == 'date')
136    {
137      if      ($filter['cond'] == 'the')    $where[] = 'date_available BETWEEN "'.$filter['value'].' 00:00:00" AND "'.$filter['value'].' 23:59:59"';
138      else if ($filter['cond'] == 'before') $where[] = 'date_available < "'.$filter['value'].' 00:00:00"';
139      else if ($filter['cond'] == 'after')  $where[] = 'date_available > "'.$filter['value'].' 23:59:59"';
140    }
141    // limit
142    else if ($filter['type'] == 'limit')
143    {
144      $limit = '0, '.$filter['value'];
145    }
146  }
147 
148  /* bluid query */
149  $MainQuery = '
150SELECT i.id
151  FROM '.IMAGES_TABLE.' AS i';
152   
153    if (isset($join))
154    {
155      foreach ($join as $query)
156      {
157        $MainQuery .= '
158    LEFT JOIN '.$query;
159      }
160    }
161    if (isset($where))
162    {
163      $MainQuery .= '
164  WHERE';
165      $i = 0;
166      foreach ($where as $query)
167      {
168        if ($i != 0) $MainQuery .= ' AND';
169        $MainQuery .= '
170    '.$query;
171        $i++;
172      }
173    }
174
175    $MainQuery .= '
176  GROUP BY i.id
177 '.$conf['order_by_inside_category'].'
178  '.(isset($limit) ? "LIMIT ".$limit : null).'
179;';
180
181  return array_from_query($MainQuery, 'id');
182}
183
184
185/**
186 * Check if the filter is proper
187 *
188 * @param array filter
189 * @return array or false
190 */
191function smart_check_filter($filter)
192{
193  global $limit_is_set, $page;
194  $error = false;
195 
196  # tags
197  if ($filter['type'] == 'tags')
198  {
199    if ($filter['value'] == null) // tags fields musn't be null
200    {
201      $error = true;
202      array_push($page['errors'], l10n('No tag selected'));
203    }
204    else
205    {
206      $filter['value'] = implode(',', get_tag_ids($filter['value']));
207    }
208  }
209  # date
210  else if ($filter['type'] == 'date')
211  {
212    if (!preg_match('#([0-9]{4})-([0-9]{2})-([0-9]{2})#', $filter['value'])) // dates must be proper
213    {
214      $error = true;
215      array_push($page['errors'], l10n('Date string is malformed'));
216    }
217  }
218  # limit
219  else if ($filter['type'] == 'limit')
220  {
221    if (!preg_match('#([0-9]{1,})#', $filter['value'])) // limit must be an integer
222    {
223      $error = true;
224      array_push($page['errors'], l10n('Limit must be an integer'));
225    }
226    else if ($limit_is_set == true) // only one limit is allowed, first is saved
227    {
228      $error = true;
229      array_push($page['errors'], l10n('You can\'t use more than one limit'));
230    }
231    else
232    {
233      $limit_is_set = true;
234    }
235  }
236 
237  # out
238  if ($error == false)
239  {
240    return $filter;
241  }
242  else
243  {
244    return false;
245  }
246}
247
248
249/**
250 * inserts multiple lines in a table, ignore duplicate entries
251 *
252 * @param string table_name
253 * @param array dbfields
254 * @param array inserts
255 * @return void
256 */
257function mass_inserts_ignore($table_name, $dbfields, $datas)
258{
259  if (count($datas) != 0)
260  {
261    $first = true;
262
263    $query = 'SHOW VARIABLES LIKE \'max_allowed_packet\'';
264    list(, $packet_size) = pwg_db_fetch_row(pwg_query($query));
265    $packet_size = $packet_size - 2000; // The last list of values MUST not exceed 2000 character*/
266    $query = '';
267
268    foreach ($datas as $insert)
269    {
270      if (strlen($query) >= $packet_size)
271      {
272        pwg_query($query);
273        $first = true;
274      }
275
276      if ($first)
277      {
278        $query = '
279INSERT IGNORE INTO '.$table_name.'
280  ('.implode(',', $dbfields).')
281  VALUES';
282        $first = false;
283      }
284      else
285      {
286        $query .= '
287  , ';
288      }
289
290      $query .= '(';
291      foreach ($dbfields as $field_id => $dbfield)
292      {
293        if ($field_id > 0)
294        {
295          $query .= ',';
296        }
297
298        if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
299        {
300          $query .= 'NULL';
301        }
302        else
303        {
304          $query .= "'".$insert[$dbfield]."'";
305        }
306      }
307      $query .= ')';
308    }
309    pwg_query($query);
310  }
311}
312?>
Note: See TracBrowser for help on using the repository browser.