source: extensions/rv_autocomplete/admin/functions.inc.php @ 22201

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

rv autocomplete svn first commit

File size: 3.4 KB
Line 
1<?php
2
3function rvac_invalidate_cache()
4{
5  global $conf;
6  conf_update_param('rvac_version', ++$conf['rvac_version'] );
7  if (rand()%10==0)
8  {
9    foreach (glob(PHPWG_ROOT_PATH.PWG_COMBINED_DIR.'acds-*.js') as $file)
10      @unlink($file);
11  }
12}
13
14function rvac_custom_link(&$suggestion, $roots)
15{
16  $url = $suggestion['url'];
17  if (empty($url))
18  {
19    $q = $suggestion['name'];
20    if ( ($pos=strpos($q,'\\')) !== false )
21    {
22      $q = substr($q, 0, $pos);
23    }
24  }
25  elseif (strncmp($url,'q=',2)==0)
26  {
27    $q = substr($url,2);
28  }
29  else
30  {
31    foreach( $roots as $k => $root)
32      $url = str_replace('$'.$k.'/', $root, $url);
33  }
34
35  if (isset($q))
36  {
37    if ( ($pos=strpos($q,' ')) !== false )
38    {
39      $q = '"'.$q.'"';
40    }
41    $url = get_root_url().'qsearch.php?q='.rawurlencode($q);
42  }
43
44  $suggestion['U_LINK'] = $url;
45  return $suggestion;
46}
47
48function rvac_ws_add_methods($srv_arr)
49{
50  $srv = $srv_arr[0];
51  include_once( dirname(__FILE__).'/../functions.inc.php' );
52  $srv->addMethod('rvac.addCustom', 'rv_ac_ws_add_custom_suggestion',
53    array(
54      'name' => array(),
55      'counter' => array('default'=>0),
56      'url' => array('default'=>''),
57    ),
58    '');
59
60  $srv->addMethod('rvac.modCustom', 'rv_ac_ws_mod_custom_suggestion',
61    array(
62      'id' => array(),
63      'name' => array('flags' => WS_PARAM_OPTIONAL),
64      'counter' => array('flags' => WS_PARAM_OPTIONAL),
65      'url' => array('flags' => WS_PARAM_OPTIONAL),
66    ),
67    '');
68
69  $srv->addMethod('rvac.delCustom', 'rv_ac_ws_del_custom_suggestion',
70    array('id'),
71    '');
72}
73
74function rv_ac_ws_add_custom_suggestion($params, $service)
75{
76  global $conf;
77  if (!is_admin() || !$service->isPost())
78    return new PwgError(403, 'post admin');
79
80  $name = trim($params['name']);
81  if (empty($name))
82    return new PwgError(400, 'Bad name');
83
84  $insert = array('name'=>$name);
85
86  if (isset($params['counter']))
87    $insert['counter'] = intval($params['counter']);
88  if ( !empty($params['url']) )
89    $insert['url'] = $params['url'];
90
91  mass_inserts(RVAC_SUGGESTIONS, array_keys($insert), array($insert));
92  $id =  pwg_db_insert_id(RVAC_SUGGESTIONS);
93
94  rvac_invalidate_cache();
95  $row = pwg_db_fetch_assoc( pwg_query('SELECT * FROM '.RVAC_SUGGESTIONS.' WHERE id='.$id) );
96  rvac_custom_link($row, rvac_get_url_roots());
97  return $row;
98}
99
100function rv_ac_ws_mod_custom_suggestion($params, $service)
101{
102  global $conf;
103  if (!is_admin() || !$service->isPost())
104    return new PwgError(403, 'post admin');
105  $id = intval($params['id']);
106  $update = array();
107
108  if (!empty($params['name']))
109    $update['name'] = $params['name'];
110  if (isset($params['counter']))
111    $update['counter'] = intval($params['counter']);
112  if (isset($params['url']))
113    $update['url'] = $params['url'];
114
115  single_update(RVAC_SUGGESTIONS,
116    $update,
117    array('id' => $id)
118    );
119  $changes = pwg_db_changes();
120
121  if ($changes)
122    rvac_invalidate_cache();
123
124  $row = pwg_db_fetch_assoc( pwg_query('SELECT * FROM '.RVAC_SUGGESTIONS.' WHERE id='.$id) );
125  rvac_custom_link($row, rvac_get_url_roots());
126  return $row;
127}
128
129function rv_ac_ws_del_custom_suggestion($params, $service)
130{
131  global $conf;
132  if (!is_admin() || !$service->isPost())
133    return new PwgError(403, 'post admin');
134
135  $id = intval($params['id']);
136  $q = 'DELETE FROM '.RVAC_SUGGESTIONS.' WHERE id='.$id;
137  pwg_query($q);
138  $changes = pwg_db_changes();
139  if ($changes)
140    rvac_invalidate_cache();
141  return $changes;
142}
143
144?>
Note: See TracBrowser for help on using the repository browser.