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

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

repository for SmartAlbums

File size: 5.8 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 SmartAlbums_make_associations($cat_id)
10{
11  pwg_query("DELETE FROM ".IMAGE_CATEGORY_TABLE." WHERE category_id = ".$cat_id." AND smart = true;");
12 
13  $images = SmartAlbums_get_pictures($cat_id);
14  $dbfields = array('image_id', 'category_id', 'smart');
15 
16  if (count($images) != 0)
17  {
18    foreach ($images as $img)
19    {
20      $datas[] = array(
21        'image_id' => $img,
22        'category_id' => $cat_id,
23        'smart' => true,
24      );
25    }
26    mass_inserts_ignore(IMAGE_CATEGORY_TABLE, $dbfields, $datas);
27  }
28 
29  return $images;
30}
31
32
33/*
34 * Generates the list of images, according to the filters of the category
35 * @param int category_id
36 * @param array filters, if null => catch from db
37 * @return array
38 */
39function SmartAlbums_get_pictures($cat_id, $filters = null)
40{
41  global $conf;
42
43  /* get filters */
44  if ($filters == null)
45  {
46    $filters = pwg_query("SELECT * FROM ".CATEGORY_FILTERS_TABLE." WHERE category_id = ".$cat_id." ORDER BY type ASC, cond ASC;");
47    if (!pwg_db_num_rows($filters)) return array();
48   
49    while ($filter = pwg_db_fetch_assoc($filters))
50    {
51      $temp[] = array(
52        'type' => $filter['type'],
53        'cond' => $filter['cond'],
54        'value' => $filter['value'],
55      );
56    }
57     
58    $filters = $temp;
59  }
60   
61  /* build constrains */
62  ## generate 'join', 'where' arrays and 'limit' string to create the SQL query
63  ## inspired by PicsEngine by Michael Villar
64  $i_tags = 1;
65  foreach ($filters as $filter)
66  {
67    // tags
68    if ($filter['type'] == 'tags')
69    {
70        if($filter['cond'] == "all")
71        {
72          $tags_arr = explode(',', $filter['value']);
73         
74          foreach($tags_arr as $value)
75          {
76            $join[] = "".IMAGE_TAG_TABLE." AS it_$i_tags ON i.id = it_$i_tags.image_id";
77            $where[] = "it_$i_tags.tag_id = ".$value."";
78            $i_tags++;
79          }
80        }
81        else if ($filter['cond'] == 'one') 
82        {
83          $join[] = "".IMAGE_TAG_TABLE." AS it_$i_tags ON i.id = it_$i_tags.image_id";
84          $where[] = "it_$i_tags.tag_id IN (".$filter['value'].")";
85          $i_tags++;
86        }
87        else if ($filter['cond'] == 'none') 
88        {
89          $sub_query = "SELECT it_$i_tags.image_id
90            FROM ".IMAGE_TAG_TABLE." AS it_$i_tags
91            WHERE it_$i_tags.image_id = i.id
92            AND it_$i_tags.tag_id IN (".$filter['value'].")
93            GROUP BY it_$i_tags.image_id";
94          $where[] = "NOT EXISTS (".$sub_query.")";
95          $i_tags++;
96        }
97        else if ($filter['cond'] == 'only') 
98        {
99          $sub_query = "SELECT it_$i_tags.image_id
100            FROM ".IMAGE_TAG_TABLE." AS it_$i_tags
101            WHERE it_$i_tags.image_id = i.id
102            AND it_$i_tags.tag_id NOT IN (".$filter['value'].")
103            GROUP BY it_$i_tags.image_id";
104          $where[] = "NOT EXISTS (".$sub_query.")";
105       
106          $i_tags++;
107          $tags_arr = explode(',', $filter['value']);
108         
109          foreach($tags_arr as $value)
110          {
111            $join[] = "".IMAGE_TAG_TABLE." AS it_$i_tags ON i.id = it_$i_tags.image_id";
112            $where[] = "it_$i_tags.tag_id = ".$value."";
113            $i_tags++;
114          }
115        }       
116    }
117    // date
118    else if ($filter['type'] == 'date')
119    {
120      if ($filter['cond'] == 'the')         $where[] = "date_available BETWEEN '".$filter['value']." 00:00:00' AND '".$filter['value']." 23:59:59'";
121      else if ($filter['cond'] == 'before') $where[] = "date_available < '".$filter['value']." 00:00:00'";
122      else if ($filter['cond'] == 'after')  $where[] = "date_available > '".$filter['value']." 23:59:59'";
123    }
124    // limit
125    else if ($filter['type'] == 'limit')
126    {
127      $limit = "0, ".$filter['value'];
128    }
129  }
130 
131  /* bluid query */
132  $MainQuery = "SELECT i.id
133    FROM (
134      SELECT i.id
135      FROM ".IMAGES_TABLE." AS i"."\n";
136     
137      if (isset($join))
138      {
139        foreach ($join as $query)
140        {
141          $MainQuery .= "LEFT JOIN ".$query."\n";
142        }
143      }
144      if (isset($where))
145      {
146        $MainQuery .= "WHERE"."\n";
147        $i = 0;
148        foreach ($where as $query)
149        {
150          if ($i != 0) $MainQuery .= "AND ";
151          $MainQuery .= $query."\n";
152          $i++;
153        }
154      }
155 
156      $MainQuery .= "GROUP BY i.id
157      ".$conf['order_by']."
158      ".(isset($limit) ? "LIMIT ".$limit : null)."
159    ) AS i
160  ";
161 
162  return array_from_query($MainQuery, 'id');
163}
164
165
166/**
167 * inserts multiple lines in a table, ignore duplicate entries
168 *
169 * @param string table_name
170 * @param array dbfields
171 * @param array inserts
172 * @return void
173 */
174function mass_inserts_ignore($table_name, $dbfields, $datas)
175{
176  if (count($datas) != 0)
177  {
178    $first = true;
179
180    $query = 'SHOW VARIABLES LIKE \'max_allowed_packet\'';
181    list(, $packet_size) = pwg_db_fetch_row(pwg_query($query));
182    $packet_size = $packet_size - 2000; // The last list of values MUST not exceed 2000 character*/
183    $query = '';
184
185    foreach ($datas as $insert)
186    {
187      if (strlen($query) >= $packet_size)
188      {
189        pwg_query($query);
190        $first = true;
191      }
192
193      if ($first)
194      {
195        $query = '
196INSERT IGNORE INTO '.$table_name.'
197  ('.implode(',', $dbfields).')
198  VALUES';
199        $first = false;
200      }
201      else
202      {
203        $query .= '
204  , ';
205      }
206
207      $query .= '(';
208      foreach ($dbfields as $field_id => $dbfield)
209      {
210        if ($field_id > 0)
211        {
212          $query .= ',';
213        }
214
215        if (!isset($insert[$dbfield]) or $insert[$dbfield] === '')
216        {
217          $query .= 'NULL';
218        }
219        else
220        {
221          $query .= "'".$insert[$dbfield]."'";
222        }
223      }
224      $query .= ')';
225    }
226    pwg_query($query);
227  }
228}
229?>
Note: See TracBrowser for help on using the repository browser.