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

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

test version

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