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

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

small autocomplete fix

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