[22201] | 1 | <?php |
---|
| 2 | |
---|
| 3 | function rvac_get_conf() |
---|
| 4 | { |
---|
[28153] | 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']; |
---|
[22201] | 10 | } |
---|
| 11 | |
---|
| 12 | function rvac_save_conf($cnf) |
---|
| 13 | { |
---|
[28153] | 14 | global $conf; |
---|
| 15 | $conf['rvac_opts'] = $cnf; |
---|
| 16 | conf_update_param('rvac_opts', addslashes(serialize($conf['rvac_opts'])) ); |
---|
[22201] | 17 | } |
---|
| 18 | |
---|
| 19 | function rvac_get_url_roots() |
---|
| 20 | { |
---|
[28153] | 21 | $roots = array( |
---|
| 22 | 'r' => get_root_url() |
---|
| 23 | ); |
---|
[22201] | 24 | |
---|
[28153] | 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; |
---|
[22201] | 28 | |
---|
[28153] | 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; |
---|
[22201] | 34 | } |
---|
| 35 | |
---|
| 36 | |
---|
[28088] | 37 | function rvac_normalize($name) |
---|
[22201] | 38 | { |
---|
[28161] | 39 | $invalid = '&()".,:;-'; |
---|
[28153] | 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; |
---|
[22201] | 45 | } |
---|
| 46 | |
---|
| 47 | function rvac_get_elem($item, $key) |
---|
| 48 | { |
---|
[28153] | 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; |
---|
[22246] | 57 | if ($item['counter'] > 0) |
---|
[28088] | 58 | $ret['w'] = intval($item['counter']); |
---|
[28153] | 59 | return $ret; |
---|
[22201] | 60 | } |
---|
| 61 | |
---|
| 62 | function rvac_get_index() |
---|
| 63 | { |
---|
[28153] | 64 | global $user; |
---|
[22201] | 65 | |
---|
[28153] | 66 | $rvac_conf = rvac_get_conf(); |
---|
| 67 | $root_url = get_root_url(); |
---|
| 68 | $items = array(); |
---|
[22201] | 69 | |
---|
[28153] | 70 | $roots = rvac_get_url_roots(); |
---|
[22201] | 71 | |
---|
| 72 | |
---|
[28153] | 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'); |
---|
[22201] | 77 | |
---|
[28153] | 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 | } |
---|
[22201] | 87 | |
---|
| 88 | |
---|
[28153] | 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']).')'; |
---|
[22201] | 94 | |
---|
[28153] | 95 | $url_len = strlen( $roots['a'] ); |
---|
[22201] | 96 | |
---|
[28153] | 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 | } |
---|
[22201] | 104 | |
---|
[28153] | 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 | } |
---|
[22201] | 114 | |
---|
[28153] | 115 | array_walk($items, function(&$obj) { |
---|
[28597] | 116 | $alt_names = trigger_change('get_tag_alt_names', array(), $obj['name']); |
---|
[22201] | 117 | |
---|
[28153] | 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 | } |
---|
[22201] | 128 | |
---|
[28153] | 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 | $q = rvac_normalize($q); |
---|
| 135 | } |
---|
| 136 | else |
---|
| 137 | { |
---|
| 138 | foreach ($obj['names'] as $l => &$name) |
---|
| 139 | { |
---|
| 140 | $pos = strpos($name, '\\'); |
---|
| 141 | if ($pos === false) |
---|
| 142 | { |
---|
| 143 | $q = rvac_normalize($name); |
---|
| 144 | } |
---|
| 145 | else |
---|
| 146 | { |
---|
| 147 | $q = substr($name, 0, $pos); |
---|
| 148 | if (-1 === $obj['url'] && 'default'==$l) |
---|
| 149 | $obj['url'] = 'q='.$q; |
---|
[28597] | 150 | |
---|
[28153] | 151 | $q = rvac_normalize($q); |
---|
| 152 | $name = substr_replace($name, '', $pos, 1); |
---|
| 153 | } |
---|
| 154 | $obj['q_names'][$l] = $q; |
---|
| 155 | } |
---|
| 156 | } |
---|
[22201] | 157 | |
---|
[28153] | 158 | }); |
---|
[22201] | 159 | |
---|
[28153] | 160 | $type_order = array_flip( array('t','a','s') ); |
---|
| 161 | usort($items, function($a, $b) use($type_order) { |
---|
| 162 | $d = $a['counter']-$b['counter']; |
---|
| 163 | if ($d) return -$d; |
---|
| 164 | $d = $type_order[$a['type']] - $type_order[$b['type']]; |
---|
| 165 | if ($d) return $d; |
---|
| 166 | return strcmp($a['q_names']['default'],$b['q_names']['default']); |
---|
| 167 | }); |
---|
[22201] | 168 | |
---|
[28153] | 169 | //var_export($items); |
---|
| 170 | $res = array(); |
---|
| 171 | $res_alt = array(); |
---|
| 172 | $lang = substr($user['language'],0,2); |
---|
| 173 | array_walk($items, function(&$obj) use($lang, &$res, &$res_alt) { |
---|
| 174 | $key = $lang; |
---|
| 175 | if (empty($obj['q_names'][$key])) |
---|
| 176 | $key = 'default'; |
---|
| 177 | if (!empty($obj['q_names'][$key])) |
---|
| 178 | $res[] = rvac_get_elem($obj, $key); |
---|
| 179 | else |
---|
| 180 | $key = null; |
---|
| 181 | if ($key !== null) { |
---|
| 182 | foreach( array_keys($obj['q_names']) as $k) { |
---|
| 183 | if ($k==$key) continue; |
---|
| 184 | if (strncmp($obj['q_names'][$key],$obj['q_names'][$k],4)==0) continue; |
---|
| 185 | $res_alt[] = rvac_get_elem($obj, $k); |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | else { |
---|
| 189 | foreach( $obj['q_names'] as $k => $q) |
---|
| 190 | { |
---|
| 191 | if (!empty($q)) |
---|
| 192 | $res_alt[] = rvac_get_elem($obj, $k); |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | }); |
---|
[22201] | 196 | |
---|
[28153] | 197 | return array( |
---|
| 198 | 'total' => count($res) + count($res_alt), |
---|
| 199 | 'altLangIndex' => count($res), |
---|
| 200 | 'src' => array_merge($res, $res_alt), |
---|
| 201 | 'roots' => $roots |
---|
| 202 | ); |
---|
[22201] | 203 | } |
---|
[28153] | 204 | |
---|
| 205 | function rvac_on_qsearch_expression_parsed($expr) |
---|
| 206 | { |
---|
| 207 | global $conf; |
---|
| 208 | $file=PHPWG_ROOT_PATH.$conf['data_location'].'tmp/autocomplete_variants.dat'; |
---|
| 209 | $data = @unserialize( file_get_contents($file)); |
---|
| 210 | if ($data === false) |
---|
| 211 | return; |
---|
| 212 | |
---|
| 213 | $rmap = $data['replace']; |
---|
| 214 | $amap = $data['add']; |
---|
| 215 | foreach ($expr->stokens as $token) |
---|
| 216 | { |
---|
| 217 | if (isset($token->scope) && !$token->scope->is_text) |
---|
| 218 | continue; |
---|
| 219 | if ($token->modifier & (QST_QUOTED|QST_WILDCARD)) |
---|
| 220 | continue; |
---|
| 221 | |
---|
| 222 | $all = array_merge( array($token->term), $token->variants); |
---|
| 223 | |
---|
| 224 | $in = $all[0]; |
---|
| 225 | $in_t = transliterate($in); |
---|
| 226 | if (isset($rmap[$in_t])) |
---|
| 227 | { |
---|
| 228 | $all = $rmap[$in_t]; |
---|
| 229 | foreach ($all as &$one) |
---|
| 230 | { |
---|
| 231 | if ('$i' == $one) |
---|
| 232 | $one = $in; |
---|
| 233 | } |
---|
| 234 | unset($one); |
---|
| 235 | } |
---|
| 236 | |
---|
[28161] | 237 | $processed = array(); |
---|
[28153] | 238 | for ($i=0; $i<count($all); $i++) |
---|
| 239 | { |
---|
| 240 | $in = $all[$i]; |
---|
| 241 | $in_t = transliterate($in); |
---|
| 242 | |
---|
[28161] | 243 | if (isset($processed[$in_t])) |
---|
[28153] | 244 | { |
---|
| 245 | array_splice($all, $i, 1); |
---|
| 246 | $i--; |
---|
| 247 | continue; |
---|
| 248 | } |
---|
| 249 | $processed[$in_t] = 1; |
---|
| 250 | |
---|
| 251 | if (isset($amap[$in_t])) |
---|
[28161] | 252 | $all = array_merge($all, $amap[$in_t]); |
---|
[28153] | 253 | } |
---|
| 254 | |
---|
| 255 | $token->term = array_shift($all); |
---|
| 256 | $token->variants = $all; |
---|
| 257 | } |
---|
| 258 | } |
---|
[22201] | 259 | ?> |
---|