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

Last change on this file since 26461 was 26461, checked in by mistic100, 10 years ago

Update headers to 2014. Happy new year!!

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  $image_ids = get_image_ids_for_tags(
114    $tag_ids,
115    $params['tag_mode_and'] ? 'AND' : 'OR',
116    $where_clauses,
117    ws_std_image_sql_order($params)
118    );
119
120  $count_set = count($image_ids);
121  $image_ids = array_slice($image_ids, $params['per_page']*$params['page'], $params['per_page'] );
122
123  $image_tag_map = array();
124  // build list of image ids with associated tags per image
125  if (!empty($image_ids) and !$params['tag_mode_and'])
126  {
127    $query = '
128SELECT image_id, GROUP_CONCAT(tag_id) AS tag_ids
129  FROM '. IMAGE_TAG_TABLE .'
130  WHERE tag_id IN ('. implode(',', $tag_ids) .')
131    AND image_id IN ('. implode(',', $image_ids) .')
132  GROUP BY image_id
133;';
134    $result = pwg_query($query);
135
136    while ($row = pwg_db_fetch_assoc($result))
137    {
138      $row['image_id'] = (int)$row['image_id'];
139      $image_ids[] = $row['image_id'];
140      $image_tag_map[ $row['image_id'] ] = explode(',', $row['tag_ids']);
141    }
142  }
143
144  $images = array();
145  if (!empty($image_ids))
146  {
147    $rank_of = array_flip($image_ids);
148
149    $query = '
150SELECT *
151  FROM '. IMAGES_TABLE .'
152  WHERE id IN ('. implode(',',$image_ids) .')
153;';
154    $result = pwg_query($query);
155
156    while ($row = pwg_db_fetch_assoc($result))
157    {
158      $image = array();
159      $image['rank'] = $rank_of[ $row['id'] ];
160
161      foreach (array('id', 'width', 'height', 'hit') as $k)
162      {
163        if (isset($row[$k]))
164        {
165          $image[$k] = (int)$row[$k];
166        }
167      }
168      foreach (array('file', 'name', 'comment', 'date_creation', 'date_available') as $k)
169      {
170        $image[$k] = $row[$k];
171      }
172      $image = array_merge( $image, ws_std_get_urls($row) );
173
174      $image_tag_ids = ($params['tag_mode_and']) ? $tag_ids : $image_tag_map[$image['id']];
175      $image_tags = array();
176      foreach ($image_tag_ids as $tag_id)
177      {
178        $url = make_index_url(
179          array(
180            'section'=>'tags',
181            'tags'=> array($tags_by_id[$tag_id])
182            )
183          );
184        $page_url = make_picture_url(
185          array(
186            'section'=>'tags',
187            'tags'=> array($tags_by_id[$tag_id]),
188            'image_id' => $row['id'],
189            'image_file' => $row['file'],
190            )
191          );
192        $image_tags[] = array(
193          'id' => (int)$tag_id,
194          'url' => $url,
195          'page_url' => $page_url,
196          );
197      }
198
199      $image['tags'] = new PwgNamedArray($image_tags, 'tag', ws_std_get_tag_xml_attributes() );
200      $images[] = $image;
201    }
202
203    usort($images, 'rank_compare');
204    unset($rank_of);
205  }
206
207  return array(
208    'paging' => new PwgNamedStruct(
209      array(
210        'page' => $params['page'],
211        'per_page' => $params['per_page'],
212        'count' => count($images),
213        'total_count' => $count_set,
214        )
215      ),
216    'images' => new PwgNamedArray(
217      $images,
218      'image',
219      ws_std_get_image_xml_attributes()
220      )
221    );
222}
223
224/**
225 * API method
226 * Adds a tag
227 * @param mixed[] $params
228 *    @option string name
229 */
230function ws_tags_add($params, &$service)
231{
232  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
233
234  $creation_output = create_tag($params['name']);
235
236  if (isset($creation_output['error']))
237  {
238    return new PwgError(500, $creation_output['error']);
239  }
240
241  return $creation_output;
242}
243
244?>
Note: See TracBrowser for help on using the repository browser.