source: extensions/rv_autocomplete/functions.inc.php @ 27153

Last change on this file since 27153 was 22246, checked in by rvelices, 11 years ago

rv_autocomplete multi tags (need refinement)

File size: 5.2 KB
Line 
1<?php
2
3function rvac_get_conf()
4{
5  global $conf;
6  if (is_array($conf['rvac_opts']))
7    return $conf['rvac_opts'];
8  $conf['rvac_opts'] = unserialize($conf['rvac_opts']);
9  return $conf['rvac_opts'];
10}
11
12function rvac_save_conf($cnf)
13{
14  global $conf;
15  $conf['rvac_opts'] = $cnf;
16  conf_update_param('rvac_opts', addslashes(serialize($conf['rvac_opts'])) );
17}
18
19function rvac_get_url_roots()
20{
21  $roots = array(
22    'r' => get_root_url()
23  );
24
25  $url = make_index_url( array('tags'=>array( array('id'=>123,'name'=>123,'url_name'=>123))) );
26  $url = substr($url, 0, strpos($url, '123'));
27  $roots['t'] = $url;
28
29  $url = make_index_url( array('category'=>array('id'=>123,'name'=>123,'permalink'=>123)) );
30  $url = substr($url, 0, strpos($url, '123'));
31  $roots['a'] = $url;
32 
33  return $roots;
34}
35
36
37function rvac_normalize($q)
38{
39  $q = str_replace('&', ' ', $q);
40  $q = str_replace('.', ' ', $q);
41  $q = str_replace(',', ' ', $q);
42  $q = str_replace('-', ' ', $q);
43  $q = transliterate($q);
44  for($i=0; $i<3; $i++)
45    $q = str_replace('  ', ' ', $q);
46  return $q;
47}
48
49function rvac_get_elem($item, $key)
50{
51  $l = $item['names'][$key];
52  $q = $item['q_names'][$key];
53  $ret = array(
54    'label' => $l,
55    'value' => $item['url'],
56    );
57  if ($q != strtolower($l))
58    $ret['q'] = $q;
59        if ($item['counter'] > 0)
60                $ret['w'] = $item['counter'];
61  return $ret;
62}
63
64function rvac_get_index()
65{
66  global $user;
67
68  $rvac_conf = rvac_get_conf();
69  $root_url = get_root_url();
70  $items = array();
71
72  $roots = rvac_get_url_roots();
73
74
75  $query = 'SELECT id,name FROM '.TAGS_TABLE;
76  if ( !empty($rvac_conf['excluded_tags']) )
77    $query .= ' WHERE id NOT IN ('.implode(',',$rvac_conf['excluded_tags']).')';
78  $tag_names = simple_hash_from_query($query, 'id','name');
79
80  $url_len = strlen( $roots['t'] );
81  foreach( get_available_tags() as $row)
82  {
83    if (!isset($tag_names[$row['id']])) continue;
84    $row['type'] = 't';
85    $row['url'] = '$t/' . substr( make_index_url( array('tags'=>array($row)) ), $url_len);
86    $row['name'] = $tag_names[ $row['id'] ];
87    $items[] = $row;
88  }
89
90
91  $query = 'SELECT id,name,permalink,nb_images AS counter
92  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
93  ON id = cat_id AND user_id = '.$user['id'];
94  if ( !empty($rvac_conf['excluded_albums']) )
95    $query .= ' WHERE id NOT IN ('.implode(',',$rvac_conf['excluded_albums']).')';
96
97  $url_len = strlen( $roots['a'] );
98
99  $result = pwg_query($query);
100  while ($row = pwg_db_fetch_assoc($result))
101  {
102    $row['type'] = 'a';
103    $row['url'] = '$a/' . substr( make_index_url( array('category'=>$row) ), $url_len);
104    $items[] = $row;
105  }
106
107  $query = 'SELECT name,counter,url FROM '.RVAC_SUGGESTIONS;
108  $result = pwg_query($query);
109  while ($row = pwg_db_fetch_assoc($result))
110  {
111    $row['type'] = 's';
112    if (empty($row['url']))
113      $row['url'] = -1; // special use in js to submit form (cannot put 0 because jquery ui tests often item.value)
114    $items[] = $row;
115  }
116
117  array_walk($items, function(&$obj) {
118    $alt_names = trigger_event('get_tag_alt_names', array(), $obj['name']);
119
120    if (empty($alt_names))
121      $alt_names['default'] = $obj['name'];
122    else
123    {
124      if (!isset($alt_names['default']) && isset($alt_names[0]))
125      {
126        $alt_names['default'] = $alt_names[0];
127        unset($alt_names[0]);
128      }
129    }
130
131    $obj['names'] = $alt_names;
132    if ('s' !== $obj['type'])
133    {
134      $obj['q_names'] = $alt_names;
135      foreach ($obj['q_names'] as &$q)
136      {
137        $q = rvac_normalize($q);
138      }
139    }
140    else
141    {
142      foreach ($obj['names'] as $l => &$name)
143      {
144        $pos = strpos($name, '\\');
145        if ($pos === false)
146        {
147          $q = rvac_normalize($name);
148        }
149        else
150        {
151          $q = substr($name, 0, $pos);
152          if (-1 === $obj['url'] && 'default'==$l)
153          {
154            $obj['url'] = 'q='.$q;
155          }
156          $q = rvac_normalize($q);
157          $name = substr_replace($name, '', $pos, 1);
158        }
159        $obj['q_names'][$l] = $q;
160      }
161    }
162
163  });
164
165  $type_order = array_flip( array('t','a','s') );
166  usort($items, function($a, $b) use($type_order) {
167    $d = $a['counter']-$b['counter'];
168    if ($d) return -$d;
169    $d = $type_order[$a['type']] - $type_order[$b['type']];
170    if ($d) return $d;
171    return strcmp($a['q_names']['default'],$b['q_names']['default']);
172  });
173
174  //var_export($items);
175  $res = array();
176  $res_alt = array();
177  $lang = substr($user['language'],0,2);
178  array_walk($items, function(&$obj) use($lang, &$res, &$res_alt) {
179    $key = $lang;
180    if (empty($obj['q_names'][$key]))
181      $key = 'default';
182    if (!empty($obj['q_names'][$key]))
183      $res[] = rvac_get_elem($obj, $key);
184    else
185      $key = null;
186    if ($key !== null) {
187      foreach( array_keys($obj['q_names']) as $k) {
188        if ($k==$key) continue;
189        if (strncmp($obj['q_names'][$key],$obj['q_names'][$k],4)==0) continue;
190        $res_alt[] = rvac_get_elem($obj, $k);
191      }
192    }
193    else {
194      foreach( $obj['q_names'] as $k => $q)
195      {
196        if (!empty($q))
197          $res_alt[] = rvac_get_elem($obj, $k);
198      }
199    }
200  });
201
202  return array(
203      'total' => count($res) + count($res_alt),
204      'altLangIndex' => count($res),
205      'src' => array_merge($res, $res_alt),
206      'roots' => $roots
207    );
208}
209?>
Note: See TracBrowser for help on using the repository browser.