Completed
Push — development ( a150a5...f82eb6 )
by Andrij
17:01
created

Core_Widgets::display_tpl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
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
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
14
15
    private $defaults = [
16
                         'news_count'  => 10,
17
                         'max_symdols' => 150,
18
                         'display'     => 'recent',//possible values: recent/popular
0 ignored issues
show
introduced by
Expected one space after the comma, 0 found
Loading history...
19
                        ];
20
21
    public function __construct() {
22
        parent::__construct();
23
        $obj = new MY_Lang();
24
        $obj->load('core');
25
    }
26
27
    /**
28
     * Display recent or popular news
29
     *
30
     * @param array $widget
31
     * @return string
32
     */
33
    public function recent_news($widget = []) {
34 View Code Duplication
        if ($widget['settings'] == FALSE) {
35
            $settings = $this->defaults;
36
        } else {
37
            $settings = $widget['settings'];
38
        }
39
40
        $this->db->select('CONCAT_WS("", content.cat_url, content.url) as full_url, content.id, content.title, prev_text, publish_date, showed, comments_count, author, category.name as cat_name, content.cat_url', FALSE);
41
        $this->db->join('category', 'category.id=content.category');
42
        $this->db->where('post_status', 'publish');
43
        $this->db->where('prev_text !=', 'null');
44
        $this->db->where('publish_date <=', time());
45
        $this->db->where('lang', $this->config->item('cur_lang'));
46
47
        if (count($settings['categories']) > 0) {
48
            $this->db->where_in('category', $settings['categories']);
49
        }
50
51
        if ($settings['display'] == 'recent') {
52
            $this->db->order_by('publish_date', 'desc'); // Recent news
53
        } elseif ($settings['display'] == 'popular') {
54
            $this->db->order_by('showed', 'desc'); // Pupular news
55
        }
56
57
        $query = $this->db->get('content', $settings['news_count']);
58
59
        if ($query->num_rows() > 0) {
60
            $news = $query->result_array();
61
62
            $cnt = count($news);
63
            for ($i = 0; $i < $cnt; $i++) {
64
                $news[$i]['prev_text'] = htmlspecialchars_decode($news[$i]['prev_text']);
65
66
                // Truncate text
67
                if ($settings['max_symdols'] > 0 AND mb_strlen($news[$i]['prev_text'], 'utf-8') > $settings['max_symdols']) {
68
                    $news[$i]['prev_text'] = mb_substr(strip_tags($news[$i]['prev_text']), 0, $settings['max_symdols'], 'utf-8') . '...';
69
                }
70
            }
71
72
            foreach ($news as $k => $item) {
73
                $news[$k] = $this->load->module('cfcm')->connect_fields($item, 'page');
74
            }
75
76
            $data['recent_news'] = $news;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key 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.

Loading history...
77
78
            return $this->template->fetch('widgets/' . $widget['name'], $data);
79
        } else {
80
            return '';
81
        }
82
    }
83
84
    /**
85
     * Configure form
86
     *
87
     * @param string $action
88
     * @param array $widget_data
89
     */
90
    public function recent_news_configure($action = 'show_settings', $widget_data = []) {
91
        if ($this->dx_auth->is_admin() == FALSE) {
92
            exit;
93
        }
94
95
        switch ($action) {
96 View Code Duplication
            case 'show_settings':
97
                $this->load->library('lib_category');
98
                $cats = $this->lib_category->build();
99
100
                $this->render('recent_news_form', ['widget' => $widget_data, 'cats' => $cats]);
101
                break;
102
103
            case 'update_settings':
104
                $this->form_validation->set_rules('news_count', lang('Amount of news', 'core'), 'trim|required|is_natural|min_length[1]');
105
                $this->form_validation->set_rules('max_symdols', lang('Maximum number of characters', 'core'), 'trim|required|is_natural|min_length[1]');
106
107
                if ($this->form_validation->run($this) == FALSE) {
108
                    showMessage(validation_errors(), '', 'r');
109
                    exit;
110
                } else {
111
                    $data = [
112
                             'news_count'  => $this->input->post('news_count'),
113
                             'max_symdols' => $this->input->post('max_symdols'),
114
                             'categories'  => $this->input->post('categories'),
115
                             'display'     => $this->input->post('display'),
116
                            ];
117
118
                    $this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $data);
119
120
                    showMessage(lang('Settings have been saved', 'core'));
121
122
                    if ($this->input->post('action') == 'tomain') {
123
                        pjax('/admin/widgets_manager/index');
124
                    }
125
                }
126
                break;
127
128
            case 'install_defaults':
129
                $this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $this->defaults);
130
                break;
131
        }
132
    }
133
134
    /**
135
     *
136
     * @param string $action
137
     * @param array $widget_data
138
     */
139
    public function similar_posts_configure($action = 'show_settings', $widget_data = []) {
140
        if ($this->dx_auth->is_admin() == FALSE) {
141
            exit;
142
        }
143
        $this->load->library('similar_posts', null, 'similar_library');
144
145
        switch ($action) {
146 View Code Duplication
            case 'show_settings':
147
                $this->load->library('lib_category');
148
                $cats = $this->lib_category->build();
149
150
                $this->render('similar_posts_form', ['widget' => $widget_data, 'cats' => $cats]);
151
                break;
152
153
            case 'update_settings':
154
                $settings = $this->input->post('settings');
155
156
                $this->form_validation->set_rules('settings[limit]', lang('Similar pages limit', 'core'), 'trim');
157
                $this->form_validation->set_rules('settings[max_short_description_words]', lang('Maximum short description words count', 'core'), 'trim');
158
159
                if (!$this->form_validation->run($this)) {
160
                    showMessage(validation_errors(), '', 'r');
161
                    exit;
162
                } else {
163
                    $this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $settings);
164
165
                    showMessage(lang('Settings have been saved', 'core'));
166
167
                    if ($this->input->post('action') == 'tomain') {
168
                        pjax('/admin/widgets_manager/index');
169
                    }
170
                }
171
                break;
172
173
            case 'install_defaults':
174
                $this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $this->similar_library->getDefaultSettings());
175
                break;
176
        }
177
    }
178
179
    // Similar posts
180
181
    /**
182
     * @param array $widget
183
     * @return string|null
184
     */
185
    public function similar_posts($widget = []) {
186
        $this->load->library('similar_posts', null, 'similar_library');
187
188
        $this->load->module('core');
189
        if ($this->core->core_data['data_type'] == 'page') {
190
            $title = $this->core->page_content['title'];
191
            $similarPages = $this->similar_library->find($this->core->page_content['id'], $title, $widget['settings']);
192
193
            $data = [
194
                     'pages' => $similarPages ?: [],
195
                    ];
196
            return $this->template->fetch('widgets/' . $widget['name'], $data);
197
        }
198
    }
199
200
    /**
201
     * @param string $viewName
202
     * @param array $data
203
     */
204
    public function render($viewName, array $data = []) {
205
        if (!empty($data)) {
206
            $this->template->add_array($data);
207
        }
208
        $this->template->show('file:' . APPPATH . getModContDirName('core') . '/core/templates/' . $viewName);
209
    }
210
211
}