source: trunk/include/ws_functions/pwg.tags.php @ 31102

Last change on this file since 31102 was 30359, checked in by rvelices, 10 years ago

bug 3163: pwg.tags.getImages mysql error if order parameter is provided

File size: 6.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/**
25 * API method
26 * Returns a list of tags
27 * @param mixed[] $params
28 *    @option bool sort_by_counter
29 */
30function ws_tags_getList($params, &$service)
31{
32  $tags = get_available_tags();
33  if ($params['sort_by_counter'])
34  {
35    usort($tags, create_function('$a,$b', 'return -$a["counter"]+$b["counter"];') );
36  }
37  else
38  {
39    usort($tags, 'tag_alpha_compare');
40  }
41
42  for ($i=0; $i<count($tags); $i++)
43  {
44    $tags[$i]['id'] = (int)$tags[$i]['id'];
45    $tags[$i]['counter'] = (int)$tags[$i]['counter'];
46    $tags[$i]['url'] = make_index_url(
47      array(
48        'section'=>'tags',
49        'tags'=>array($tags[$i])
50        )
51      );
52  }
53
54  return array(
55    'tags' => new PwgNamedArray(
56      $tags,
57      'tag',
58      ws_std_get_tag_xml_attributes()
59      )
60    );
61}
62
63/**
64 * API method
65 * Returns the list of tags as you can see them in administration
66 * @param mixed[] $params
67 *
68 * Only admin can run this method and permissions are not taken into
69 * account.
70 */
71function ws_tags_getAdminList($params, &$service)
72{
73  return array(
74    'tags' => new PwgNamedArray(
75      get_all_tags(),
76      'tag',
77      ws_std_get_tag_xml_attributes()
78      )
79    );
80}
81
82/**
83 * API method
84 * Returns a list of images for tags
85 * @param mixed[] $params
86 *    @option int[] tag_id (optional)
87 *    @option string[] tag_url_name (optional)
88 *    @option string[] tag_name (optional)
89 *    @option bool tag_mode_and
90 *    @option int per_page
91 *    @option int page
92 *    @option string order
93 */
94function ws_tags_getImages($params, &$service)
95{
96  // first build all the tag_ids we are interested in
97  $tags = find_tags($params['tag_id'], $params['tag_url_name'], $params['tag_name']);
98  $tags_by_id = array();
99  foreach ($tags as $tag)
100  {
101    $tags['id'] = (int)$tag['id'];
102    $tags_by_id[ $tag['id'] ] = $tag;
103  }
104  unset($tags);
105  $tag_ids = array_keys($tags_by_id);
106
107  $where_clauses = ws_std_image_sql_filter($params);
108  if (!empty($where_clauses))
109  {
110    $where_clauses = implode(' AND ', $where_clauses);
111  }
112
113  $order_by = ws_std_image_sql_order($params, 'i.');
114  if (!empty($order_by))
115  {
116    $order_by = 'ORDER BY '.$order_by;
117  }
118  $image_ids = get_image_ids_for_tags(
119    $tag_ids,
120    $params['tag_mode_and'] ? 'AND' : 'OR',
121    $where_clauses,
122    $order_by
123    );
124
125  $count_set = count($image_ids);
126  $image_ids = array_slice($image_ids, $params['per_page']*$params['page'], $params['per_page'] );
127
128  $image_tag_map = array();
129  // build list of image ids with associated tags per image
130  if (!empty($image_ids) and !$params['tag_mode_and'])
131  {
132    $query = '
133SELECT image_id, GROUP_CONCAT(tag_id) AS tag_ids
134  FROM '. IMAGE_TAG_TABLE .'
135  WHERE tag_id IN ('. implode(',', $tag_ids) .')
136    AND image_id IN ('. implode(',', $image_ids) .')
137  GROUP BY image_id
138;';
139    $result = pwg_query($query);
140
141    while ($row = pwg_db_fetch_assoc($result))
142    {
143      $row['image_id'] = (int)$row['image_id'];
144      $image_tag_map[ $row['image_id'] ] = explode(',', $row['tag_ids']);
145    }
146  }
147
148  $images = array();
149  if (!empty($image_ids))
150  {
151    $rank_of = array_flip($image_ids);
152
153    $query = '
154SELECT *
155  FROM '. IMAGES_TABLE .'
156  WHERE id IN ('. implode(',',$image_ids) .')
157;';
158    $result = pwg_query($query);
159
160    while ($row = pwg_db_fetch_assoc($result))
161    {
162      $image = array();
163      $image['rank'] = $rank_of[ $row['id'] ];
164
165      foreach (array('id', 'width', 'height', 'hit') as $k)
166      {
167        if (isset($row[$k]))
168        {
169          $image[$k] = (int)$row[$k];
170        }
171      }
172      foreach (array('file', 'name', 'comment', 'date_creation', 'date_available') as $k)
173      {
174        $image[$k] = $row[$k];
175      }
176      $image = array_merge( $image, ws_std_get_urls($row) );
177
178      $image_tag_ids = ($params['tag_mode_and']) ? $tag_ids : $image_tag_map[$image['id']];
179      $image_tags = array();
180      foreach ($image_tag_ids as $tag_id)
181      {
182        $url = make_index_url(
183          array(
184            'section'=>'tags',
185            'tags'=> array($tags_by_id[$tag_id])
186            )
187          );
188        $page_url = make_picture_url(
189          array(
190            'section'=>'tags',
191            'tags'=> array($tags_by_id[$tag_id]),
192            'image_id' => $row['id'],
193            'image_file' => $row['file'],
194            )
195          );
196        $image_tags[] = array(
197          'id' => (int)$tag_id,
198          'url' => $url,
199          'page_url' => $page_url,
200          );
201      }
202
203      $image['tags'] = new PwgNamedArray($image_tags, 'tag', ws_std_get_tag_xml_attributes() );
204      $images[] = $image;
205    }
206
207    usort($images, 'rank_compare');
208    unset($rank_of);
209  }
210
211  return array(
212    'paging' => new PwgNamedStruct(
213      array(
214        'page' => $params['page'],
215        'per_page' => $params['per_page'],
216        'count' => count($images),
217        'total_count' => $count_set,
218        )
219      ),
220    'images' => new PwgNamedArray(
221      $images,
222      'image',
223      ws_std_get_image_xml_attributes()
224      )
225    );
226}
227
228/**
229 * API method
230 * Adds a tag
231 * @param mixed[] $params
232 *    @option string name
233 */
234function ws_tags_add($params, &$service)
235{
236  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
237
238  $creation_output = create_tag($params['name']);
239
240  if (isset($creation_output['error']))
241  {
242    return new PwgError(500, $creation_output['error']);
243  }
244
245  return $creation_output;
246}
247
248?>
Note: See TracBrowser for help on using the repository browser.