-
Notifications
You must be signed in to change notification settings - Fork 20
/
keywords.php
116 lines (85 loc) · 3.79 KB
/
keywords.php
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
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Keywords extends MY_Controller {
protected $website_part = 'settings';
/**
* Form with keywords list
*/
public function index() {
$this->load->config('site_config', TRUE);
$keywords_config = $this->config->item('keywords', 'site_config');
$keywords_count = isset($keywords_config['count']) && $keywords_config['count'] ? $keywords_config['count'] : 10;
// get user additional info (address)
$user_additional = User_additional::inst()->get_by_user_id($this->c_user->id);
// get available keywords
$keywords = Keyword::inst()->get_user_keywords($this->c_user->id);
if ($this->input->post()) {
// validate posted address name
$new_address_name = $this->input->post('address');
$adress_error_string = User_additional::validate_address($this->input->post());
// validate posted keywords
$new_keywords_names = array_slice($this->input->post('keywords'), 0, $keywords_count);
$keywords_errors = Keyword::validate_keywords($new_keywords_names);
// chek for errors
if (empty($adress_error_string) && empty($keywords_errors)) {
$user_additional = $user_additional->update_address($this->input->post(), $this->c_user->id);
$keywords = $keywords->update_keywords($new_keywords_names, $this->c_user->id);
$this->addFlash('Keywords settings saved.', 'success');
} else {
$address_name = $new_address_name;
$keywords_names = array_slice(array_values($this->input->post('keywords')), 0, $keywords_count);
$errors = array(
'keywords' => $keywords_errors,
'address' => $adress_error_string,
);
$this->template->set('errors', $errors);
}
}
// escape keywords names and website name
$address_name = isset($address_name)
? HTML::chars($address_name)
: HTML::chars($user_additional->address);
$keywords_names = isset($keywords_names)
? HTML::chars_arr($keywords_names)
: HTML::chars_arr(array_values($keywords->all_to_single_array('keyword')));
JsSettings::instance()->add(array(
'autocomplete_url' => site_url('settings/keywords/google_autocomplete'),
));
CssJs::getInst()->c_js();
$this->template->set('address_id', $user_additional->address_id);
$this->template->set('address_name', $address_name);
$this->template->set('keywords_names', $keywords_names);
$this->template->set('keywords_count', $keywords_count);
$this->template->render();
}
/**
* Autocomplete for company address
*/
public function google_autocomplete(){
$term = $this->input->get('term');
$term = trim($term);
try {
$developer_key = Api_key::value('google', 'developer_key');
$gls = $this->load->library('gls');
$gls->set(array(
'key' => $developer_key,
));
$rows = $gls->autocomplete($term);
if ( ! $rows OR ! isset($rows['predictions'])) {
throw new Exception;
}
$rows = $rows['predictions'];
$data = array();
foreach($rows as $row) {
$data[] = array(
'id' => $row['id'],
'label' => $row['description'],
);
}
} catch (Exception $e) {
exit;
}
header('Content-Type: application/json');
echo json_encode($data);
exit;
}
}