|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
if (!defined('BASEPATH')) { |
|
4
|
|
|
exit('No direct script access allowed'); |
|
5
|
|
|
} |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Image CMS |
|
9
|
|
|
* @property Similar_Posts similar_library |
|
10
|
|
|
* @property Lib_category lib_category |
|
11
|
|
|
*/ |
|
12
|
|
|
class Core_Widgets extends MY_Controller |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
private $defaults = [ |
|
16
|
|
|
'news_count' => 10, |
|
17
|
|
|
'max_symdols' => 150, |
|
18
|
|
|
'display' => 'recent',//possible values: recent/popular |
|
19
|
|
|
'sort_order' => 'desc', |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct() { |
|
23
|
|
|
parent::__construct(); |
|
24
|
|
|
$obj = new MY_Lang(); |
|
25
|
|
|
$obj->load('core'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Display recent or popular news |
|
30
|
|
|
* |
|
31
|
|
|
* @param array $widget |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function recent_news($widget = []) { |
|
35
|
|
View Code Duplication |
if ($widget['settings'] == FALSE) { |
|
36
|
|
|
$settings = $this->defaults; |
|
37
|
|
|
} else { |
|
38
|
|
|
$settings = $widget['settings']; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->db->select("IF(route.parent_url <> '', concat(route.parent_url, '/', route.url), route.url) as full_url, content.id, content.title, prev_text, publish_date, showed, content.position, comments_count, author, category.name as cat_name", FALSE); |
|
42
|
|
|
$this->db->join('category', 'category.id=content.category'); |
|
43
|
|
|
$this->db->join('route', 'route.id=content.route_id'); |
|
44
|
|
|
$this->db->where('post_status', 'publish'); |
|
45
|
|
|
$this->db->where('prev_text !=', 'null'); |
|
46
|
|
|
$this->db->where('publish_date <=', time()); |
|
47
|
|
|
$this->db->where('lang', $this->config->item('cur_lang')); |
|
48
|
|
|
|
|
49
|
|
|
if (count($settings['categories']) > 0) { |
|
50
|
|
|
$this->db->where_in('category', $settings['categories']); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
switch ($settings['display']) { |
|
54
|
|
|
case 'recent': |
|
55
|
|
|
$settings['display'] = 'publish_date'; // Recent news |
|
56
|
|
|
break; |
|
57
|
|
|
|
|
58
|
|
|
case 'popular': |
|
59
|
|
|
$settings['display'] = 'showed'; // Popular news |
|
60
|
|
|
break; |
|
61
|
|
|
|
|
62
|
|
|
case 'position': |
|
63
|
|
|
$settings['display'] = 'position'; // Position news |
|
64
|
|
|
break; |
|
65
|
|
|
|
|
66
|
|
|
default: |
|
67
|
|
|
$settings['display'] = 'position'; // Position news |
|
68
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
switch ($settings['sort_order']) { |
|
72
|
|
|
case 'asc': |
|
73
|
|
|
$settings['sort_order'] = 'asc'; |
|
74
|
|
|
break; |
|
75
|
|
|
|
|
76
|
|
|
case 'desc': |
|
77
|
|
|
$settings['sort_order'] = 'desc'; |
|
78
|
|
|
break; |
|
79
|
|
|
|
|
80
|
|
|
default: |
|
81
|
|
|
$settings['sort_order'] = 'desc'; |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$this->db->order_by($settings['display'], $settings['sort_order']); |
|
86
|
|
|
|
|
87
|
|
|
$query = $this->db->get('content', $settings['news_count']); |
|
88
|
|
|
|
|
89
|
|
|
if ($query->num_rows() > 0) { |
|
90
|
|
|
$news = $query->result_array(); |
|
91
|
|
|
|
|
92
|
|
|
$cnt = count($news); |
|
93
|
|
|
for ($i = 0; $i < $cnt; $i++) { |
|
94
|
|
|
$news[$i]['prev_text'] = htmlspecialchars_decode($news[$i]['prev_text']); |
|
95
|
|
|
|
|
96
|
|
|
// Truncate text |
|
97
|
|
|
if ($settings['max_symdols'] > 0 AND mb_strlen($news[$i]['prev_text'], 'utf-8') > $settings['max_symdols']) { |
|
98
|
|
|
$news[$i]['prev_text'] = mb_substr(strip_tags($news[$i]['prev_text']), 0, $settings['max_symdols'], 'utf-8') . '...'; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
foreach ($news as $k => $item) { |
|
103
|
|
|
$news[$k] = $this->load->module('cfcm')->connect_fields($item, 'page'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$data['recent_news'] = $news; |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
return $this->template->fetch('widgets/' . $widget['name'], $data); |
|
109
|
|
|
} else { |
|
110
|
|
|
return ''; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Configure form |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $action |
|
118
|
|
|
* @param array $widget_data |
|
119
|
|
|
*/ |
|
120
|
|
|
public function recent_news_configure($action = 'show_settings', $widget_data = []) { |
|
121
|
|
|
if ($this->dx_auth->is_admin() == FALSE) { |
|
122
|
|
|
exit; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
switch ($action) { |
|
126
|
|
View Code Duplication |
case 'show_settings': |
|
127
|
|
|
$this->load->library('lib_category'); |
|
128
|
|
|
$cats = $this->lib_category->build(); |
|
129
|
|
|
|
|
130
|
|
|
$this->render('recent_news_form', ['widget' => $widget_data, 'cats' => $cats]); |
|
131
|
|
|
break; |
|
132
|
|
|
|
|
133
|
|
|
case 'update_settings': |
|
134
|
|
|
$this->form_validation->set_rules('news_count', lang('Amount of news', 'core'), 'trim|required|is_natural|min_length[1]'); |
|
135
|
|
|
$this->form_validation->set_rules('max_symdols', lang('Maximum number of characters', 'core'), 'trim|required|is_natural|min_length[1]'); |
|
136
|
|
|
|
|
137
|
|
|
if ($this->form_validation->run($this) == FALSE) { |
|
138
|
|
|
showMessage(validation_errors(), '', 'r'); |
|
139
|
|
|
exit; |
|
140
|
|
|
} else { |
|
141
|
|
|
$data = [ |
|
142
|
|
|
'news_count' => $this->input->post('news_count'), |
|
143
|
|
|
'max_symdols' => $this->input->post('max_symdols'), |
|
144
|
|
|
'categories' => $this->input->post('categories'), |
|
145
|
|
|
'display' => $this->input->post('display'), |
|
146
|
|
|
'sort_order' => $this->input->post('sort_order'), |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
|
|
$this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $data); |
|
150
|
|
|
|
|
151
|
|
|
showMessage(lang('Settings have been saved', 'core')); |
|
152
|
|
|
|
|
153
|
|
|
if ($this->input->post('action') == 'tomain') { |
|
154
|
|
|
pjax('/admin/widgets_manager/index'); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
break; |
|
158
|
|
|
|
|
159
|
|
|
case 'install_defaults': |
|
160
|
|
|
$this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $this->defaults); |
|
161
|
|
|
break; |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $action |
|
168
|
|
|
* @param array $widget_data |
|
169
|
|
|
*/ |
|
170
|
|
|
public function similar_posts_configure($action = 'show_settings', $widget_data = []) { |
|
171
|
|
|
if ($this->dx_auth->is_admin() == FALSE) { |
|
172
|
|
|
exit; |
|
173
|
|
|
} |
|
174
|
|
|
$this->load->library('similar_posts', null, 'similar_library'); |
|
175
|
|
|
|
|
176
|
|
|
switch ($action) { |
|
177
|
|
View Code Duplication |
case 'show_settings': |
|
178
|
|
|
$this->load->library('lib_category'); |
|
179
|
|
|
$cats = $this->lib_category->build(); |
|
180
|
|
|
|
|
181
|
|
|
$this->render('similar_posts_form', ['widget' => $widget_data, 'cats' => $cats]); |
|
182
|
|
|
break; |
|
183
|
|
|
|
|
184
|
|
|
case 'update_settings': |
|
185
|
|
|
$settings = $this->input->post('settings'); |
|
186
|
|
|
|
|
187
|
|
|
$this->form_validation->set_rules('settings[limit]', lang('Similar pages limit', 'core'), 'trim'); |
|
188
|
|
|
$this->form_validation->set_rules('settings[max_short_description_words]', lang('Maximum short description words count', 'core'), 'trim'); |
|
189
|
|
|
|
|
190
|
|
|
if (!$this->form_validation->run($this)) { |
|
191
|
|
|
showMessage(validation_errors(), '', 'r'); |
|
192
|
|
|
exit; |
|
193
|
|
|
} else { |
|
194
|
|
|
$this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $settings); |
|
195
|
|
|
|
|
196
|
|
|
showMessage(lang('Settings have been saved', 'core')); |
|
197
|
|
|
|
|
198
|
|
|
if ($this->input->post('action') == 'tomain') { |
|
199
|
|
|
pjax('/admin/widgets_manager/index'); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
break; |
|
203
|
|
|
|
|
204
|
|
|
case 'install_defaults': |
|
205
|
|
|
$this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $this->similar_library->getDefaultSettings()); |
|
206
|
|
|
break; |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
// Similar posts |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* @param array $widget |
|
214
|
|
|
* @return string|null |
|
215
|
|
|
*/ |
|
216
|
|
|
public function similar_posts($widget = []) { |
|
217
|
|
|
$this->load->library('similar_posts', null, 'similar_library'); |
|
218
|
|
|
|
|
219
|
|
|
$this->load->module('core'); |
|
220
|
|
|
if ($this->core->core_data['data_type'] == 'page') { |
|
221
|
|
|
$title = $this->core->page_content['title']; |
|
222
|
|
|
$similarPages = $this->similar_library->find($this->core->page_content['id'], $title, $widget['settings']); |
|
223
|
|
|
|
|
224
|
|
|
$data = [ |
|
225
|
|
|
'pages' => $similarPages ?: [], |
|
226
|
|
|
]; |
|
227
|
|
|
return $this->template->fetch('widgets/' . $widget['name'], $data); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @param string $viewName |
|
233
|
|
|
* @param array $data |
|
234
|
|
|
*/ |
|
235
|
|
|
public function render($viewName, array $data = []) { |
|
236
|
|
|
if (!empty($data)) { |
|
237
|
|
|
$this->template->add_array($data); |
|
238
|
|
|
} |
|
239
|
|
|
$this->template->show('file:' . APPPATH . getModContDirName('core') . '/core/templates/' . $viewName); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.