-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.module
194 lines (154 loc) · 8.1 KB
/
autocomplete.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
//print('<pre>'.print_r().'</pre>');
function autocomplete_form_alter(&$form, &$form_state, $form_id) {
// Vemos que no se muestre en el backend (edit, webform, results), solo en front
// Y solo en el nodo que queremos:
preg_match('/(edit)|(webform)|(results)/',request_uri(), $matches);
if( empty($matches) && $form['#node']->nid == 298 ) {
// Provincia
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_state_province_id']['#ajax'] = array(
'callback' => '_cities_update',
'wrapper' => 'city_div',
'method' => 'replace',
);
// Ciudad
$province = $form_state['values']['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_state_province_id'];
$cp = $form_state['values']['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_postal_code'];
$province_id = (empty($province) || $province == '' ? '0' : $province);
$postal_code = (empty($cp) || $cp == '' ? '0' : $cp);
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_city']['#autocomplete_path'] = 'city/autocomplete/aa/'.$province_id.'/'.$postal_code;
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_city']['#prefix'] = '<div id="city_div">';
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_city']['#suffix'] = '</div>';
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_city']['#ajax'] = array(
'callback' => '_streets_update',
'wrapper' => 'street_div',
'method' => 'replace',
);
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_city']['#description'] =
t("Province") . " : '" . (!empty($province_id) ? $province_id : t("No cambiada")) . "'</br>" .
t("CP") . " : '" . (!empty($postal_code) ? $postal_code : t("No cambiado")) . "'";
// Calle
$city_id = $form_state['values']['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_city']; // default
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_street_address']['#autocomplete_path'] = 'street/autocomplete/'.$city_id;
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_street_address']['#prefix'] = '<div id="street_div">';
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_street_address']['#suffix'] = '</div>';
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_street_address']['#description'] = t("City") . " : '"
. (!empty($city_id) ? $city_id : t("No cambiada")) . "'";
// Código Postal
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_postal_code']['#ajax'] = array(
'callback' => '_postal_code_updater',
'wrapper' => 'city_div',
'method' => 'replace',
);
}
return $form;
}
/********** Callbacks *************/
function _postal_code_updater($form, &$form_state){
//provincia a partir de cp
$cp = $form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_postal_code']['#value'];
$province_id = substr($cp,0,2);
$result = db_select('correos_provincias', 'p')
->fields('p', array('id','name'))
->condition('province_id', db_like($province_id), '=')
->execute();
$record = $result->fetchAssoc();
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_city']['#description'].=
"</br>". t("Province_id") . " : '"
. (!empty($record) ? $record->id : t("No hay")) . "'";
//$result = _cities_update($form, &$form_state);
return $form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_city'];
}
function _cities_update($form, &$form_state){
// vaciar ciudad y calle
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_city']['#value'] = '';
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_street_address']['#value'] = '';
// leemos provincia del formulario
$province = ['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_state_province_id'];
$province_id = (empty($province) || $province == '' ? '0' : $province);
// leemos cp del formulario
$cp = ['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_postal_code'];
$postal_code = (empty($cp) || $cp == '' ? '0' : $cp);
// cambiamos el autocompletado
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_city']['#autocomplete_path'] = 'city/autocomplete/aa/'.$province_id.'/'.$postal_code;
return $form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_city'];
}
function _streets_update($form, &$form_state){
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_street_address']['#value']='';
$city_id = ['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_city'];
$form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_street_address']['#autocomplete_path'] = 'street/autocomplete/'.$city_id;
return $form['submitted']['civicrm_1_contact_1_fieldset_fieldset']['civicrm_1_contact_1_address_street_address'];
}
/*********** Autocomplete Paths *************/
function autocomplete_menu() {
$items['province/autocomplete'] = array(
'page callback' => '_autocomplete_province',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['city/autocomplete/%/%'] = array(
'page callback' => '_autocomplete_cities_cp',
'access arguments' => array('access content'),
'page arguments' => array(3),
'type' => MENU_CALLBACK,
);
$items['street/autocomplete/%'] = array(
'page callback' => '_autocomplete_streets',
'access arguments' => array('access content'),
'page arguments' => array(2),
'type' => MENU_CALLBACK,
);
return $items;
}
/********** Functions of Autocomplete Paths *************/
function _autocomplete_province($string=''){
$matches = array();
$result = db_select('correos_provincias', 'p')
->fields('p', array('id','name'))
->condition('name', db_like($string) . '%', 'LIKE')
->execute();
// save the query to matches
foreach ($result as $row) {
$matches[$row->id] = check_plain($row->name);
}
// Return the result to the form in json
drupal_json_output($matches);
}
function _autocomplete_streets($city=1, $string=''){
$matches = array();
$result = db_select('correos_vias', 'v')
//$result->join('correos_provincias', 'p', 'c.province_id=p.id');
->fields('v', array('name'))
->condition('name', db_like($string) . '%', 'LIKE')
->condition('city_id', db_like($city), '=')
->execute();
// save the query to matches
foreach ($result as $row) {
$matches[$row->name] = check_plain($row->name);
}
// Return the result to the form in json
drupal_json_output($matches);
}
function _autocomplete_cities_cp($province=0, $postal_code=0, $string=''){
$matches = array();
$query1 = db_select('correos_ciudades', 'c');
$query1->join('correos_codpostales', 'cp', 'cp.city_id = c.id');
if($province > 0){
$query1->condition('c.province_id', db_like($province), '=');
}
if($postal_code > 0){
$query1->condition('cp.codigo', db_like($postal_code), 'LIKE');
}
if($string <> ''){
$query1->condition('c.name', db_like($string) . '%', 'LIKE');
}
$result = $query1
->fields('c', array('id','name'))
->execute();
// save the query to matches
foreach ($result as $row) {
$matches[$row->id] = check_plain($row->name);
}
// Return the result to the form in json
drupal_json_output($matches);
}