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

Comments_Widgets::recent_product_comments()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 40
Code Lines 24

Duplication

Lines 5
Ratio 12.5 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 24
c 3
b 0
f 0
nc 18
nop 1
dl 5
loc 40
rs 5.3846
1
<?php
2
3
use CMSFactory\assetManager;
4
5
if (!defined('BASEPATH')) {
6
    exit('No direct script access allowed');
7
}
8
9
/**
10
 * Image CMS
11
 *
12
 * Comments widgets
13
 */
14
class Comments_Widgets extends MY_Controller
15
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
16
17
    /**
18
     * @var array
19
     */
20
    private $defaults = [
21
                         'comments_count' => 100,
22
                         'symbols_count'  => 0,
23
                        ];
24
25
    public function __construct() {
26
        parent::__construct();
27
        $lang = new MY_Lang();
28
        $lang->load('comments');
29
    }
30
31
    /**
32
     * Get and display recent comments
33
     * @param array $widget
34
     * @return string
35
     */
36
    public function recent_comments(array $widget = []) {
37 View Code Duplication
        if ($widget['settings'] === FALSE) {
38
            $settings = $this->defaults;
39
        } else {
40
            $settings = $widget['settings'];
41
        }
42
43
        $this->db->select('comments.*');
44
        $this->db->select('CONCAT_WS("", ,content.cat_url, content.url) as url', FALSE); // page full url
45
        $this->db->where('content.lang', $this->config->item('cur_lang'));
46
        $this->db->where('comments.module', 'core');
47
        $this->db->where('comments.status', 0);
48
        $this->db->join('content', 'content.id = comments.item_id', 'left');
49
        $this->db->order_by('date', 'desc');
50
        $query = $this->db->get('comments', $settings['comments_count']);
51
52
        if ($query->num_rows() > 0) {
53
            $comments = $query->result_array();
54
55
            if ($settings['symbols_count'] > 0) {
56
                $cnt = count($comments);
57
                for ($i = 0; $i < $cnt; $i++) {
58
                    if (mb_strlen($comments[$i]['text'], 'utf-8') > $settings['symbols_count']) {
59
                        $comments[$i]['text'] = mb_substr($comments[$i]['text'], 0, $settings['symbols_count'], 'utf-8') . '...';
60
                    }
61
                }
62
            }
63
64
            return $this->template->fetch('widgets/' . $widget['name'], ['comments' => $comments]);
65
        }
66
    }
67
68
    /**
69
     * @param string $viewName
70
     * @param array $data
71
     */
72 View Code Duplication
    public function render($viewName, array $data = []) {
73
        if (!empty($data)) {
74
            $this->template->add_array($data);
75
        }
76
77
        $this->template->show('file:application/' . getModContDirName('comments') . '/comments/templates/' . $viewName);
78
    }
79
80
    /**
81
     * Configure widget settings
82
     * @param string $action
83
     * @param array $widget_data
84
     */
85 View Code Duplication
    public function recent_comments_configure($action = 'show_settings', array $widget_data = []) {
86
        if ($this->dx_auth->is_admin() == FALSE) {
87
            exit; // Only admin access
88
        }
89
        switch ($action) {
90
            case 'show_settings':
91
                assetManager::create()->setData('widget', $widget_data)->renderAdmin('recent_comments_form');
92
                break;
93
94
            case 'update_settings':
95
                $this->form_validation->set_rules('comments_count', lang('Number of comments', 'comments'), 'trim|required|is_natural_no_zero|min_length[1]');
96
                $this->form_validation->set_rules('symbols_count', lang('Number of characters', 'comments'), 'required|trim|is_natural');
97
98
                if ($this->form_validation->run($this) == FALSE) {
99
                    showMessage(validation_errors());
100
                } else {
101
                    $data = [
102
                             'comments_count' => $this->input->post('comments_count'),
103
                             'symbols_count'  => $this->input->post('symbols_count'),
104
                            ];
105
106
                    $this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $data);
107
                    showMessage(lang('Settings have been saved', 'comments'));
108
                    if ($this->input->post('action') == 'tomain') {
109
                        pjax('/admin/widgets_manager/index');
110
                    }
111
                }
112
                break;
113
114
            case 'install_defaults':
115
                $this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $this->defaults);
116
                break;
117
        }
118
    }
119
120
    /**
121
     * Get and display recent product comments
122
     * @param array $widget
123
     * @return string
124
     */
125
    public function recent_product_comments(array $widget = []) {
126 View Code Duplication
        if ($widget['settings'] === FALSE) {
127
            $settings = $this->defaults;
128
        } else {
129
            $settings = $widget['settings'];
130
        }
131
132
        $this->db->select('comments.*');
133
        $this->db->select('CONCAT_WS("", ,content.cat_url, content.url) as url', FALSE); // page full url
134
        $this->db->where('comments.module', 'shop');
135
        $this->db->where('comments.status', 0);
136
        $this->db->join('content', 'content.id = comments.item_id', 'left');
137
        $this->db->order_by('date', 'desc');
138
        $query = $this->db->get('comments', $settings['comments_count']);
139
140
        if ($query->num_rows() > 0) {
141
            $comments = $query->result_array();
142
143
            if ($settings['symbols_count'] > 0) {
144
                $cnt = count($comments);
145
                for ($i = 0; $i < $cnt; $i++) {
146
                    if (mb_strlen($comments[$i]['text'], 'utf-8') > $settings['symbols_count']) {
147
                        $comments[$i]['text'] = mb_substr($comments[$i]['text'], 0, $settings['symbols_count'], 'utf-8') . '...';
148
                    }
149
                }
150
            }
151
152
            /** Перебор комментариев, и подставка url продукта */
153
            foreach ($comments as $key => $comment) {
154
155
                $url = SProductsQuery::create()
156
                    ->findOneById($comment['item_id']);
157
158
                $comments[$key]['product_url'] = $url ? $url->getUrl() : $url;
159
160
            }
161
162
            return $this->template->fetch('widgets/' . $widget['name'], ['comments' => $comments]);
163
        }
164
    }
165
166
    /**
167
     * Configure widget settings
168
     * @param string $action
169
     * @param array $widget_data
170
     */
171 View Code Duplication
    public function recent_product_comments_configure($action = 'show_settings', array $widget_data = []) {
172
        if ($this->dx_auth->is_admin() == FALSE) {
173
            exit; // Only admin access
174
        }
175
        switch ($action) {
176
            case 'show_settings':
177
                assetManager::create()->setData('widget', $widget_data)->renderAdmin('recent_product_comments_form');
178
                break;
179
180
            case 'update_settings':
181
                $this->form_validation->set_rules('comments_count', lang('Number of responses'), 'trim|required|is_natural_no_zero|min_length[1]');
182
                $this->form_validation->set_rules('symbols_count', lang('Number of characters'), 'required|trim|is_natural');
183
184
                if ($this->form_validation->run($this) == FALSE) {
185
                    showMessage(validation_errors());
186
                } else {
187
                    $data = [
188
                             'comments_count' => $this->input->post('comments_count'),
189
                             'symbols_count'  => $this->input->post('symbols_count'),
190
                            ];
191
192
                    $this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $data);
193
                    showMessage(lang('Settings have been saved', 'comments'));
194
                    if ($this->input->post('action') == 'tomain') {
195
                        pjax('/admin/widgets_manager/index');
196
                    }
197
                }
198
                break;
199
200
            case 'install_defaults':
201
                $this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $this->defaults);
202
                break;
203
        }
204
    }
205
206
}