Completed
Push — development ( c8f467...de45b7 )
by Andrij
10:53
created

Base::setVoutesAfterDelete()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 25
rs 8.8571
1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
7
class Base extends CI_Model
8
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
9
10
    /**
11
     * @var array
12
     */
13
    public $comments_product_id = [];
14
15
    public function __construct() {
16
        parent::__construct();
17
    }
18
19
    /**
20
     * @param int $item_id
21
     * @param int $status
22
     * @param string $module
23
     * @param int $limit
24
     * @param string $order_by
25
     * @return bool|array
26
     */
27
    public function get($item_id, $status = 0, $module, $limit = 999999, $order_by) {
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
28
        if ($this->db->table_exists('comments')) {
29
30
            $this->db->where('item_id', $item_id);
31
            $this->db->where('status', $status);
32
            $this->db->where('module', $module);
33
34
            $order_by = $order_by ?: 'date.desc';
35
            $order_column = array_shift(explode('.', $order_by));
0 ignored issues
show
Bug introduced by
explode('.', $order_by) cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
36
            $order_way = array_pop(explode('.', $order_by));
0 ignored issues
show
Bug introduced by
explode('.', $order_by) cannot be passed to array_pop() as the parameter $array expects a reference.
Loading history...
37
            $this->db->order_by($order_column, $order_way);
38
            $query = $this->db->get('comments', $limit);
39
40
            if ($query->num_rows() > 0) {
41
                return $query->result_array();
42
            }
43
        }
44
45
        return FALSE;
46
    }
47
48
    public function get_one($id) {
49
        $this->db->limit(1);
50
        return $this->db->get_where('comments', ['id' => $id])->row_array();
51
    }
52
53
    public function add($data) {
54
        $this->db->insert('comments', $data);
55
56
        return $this->db->insert_id();
57
    }
58
59
    public function all($row_count, $offset) {
60
        $this->db->order_by('date', 'desc');
61
62 View Code Duplication
        if ($row_count > 0 AND $offset >= 0) {
63
            $query = $this->db->get('comments', $row_count, $offset);
64
        } else {
65
            $query = $this->db->get('comments');
66
        }
67
68
        if ($query->num_rows() > 0) {
69
            return $query->result_array();
70
        } else {
71
            return FALSE;
72
        }
73
    }
74
75
    public function get_item_comments_status($item_id) {
76
        $this->db->select('id, comments_status');
77
        $this->db->where('id', $item_id);
78
        $query = $this->db->get('content', 1);
79
80
        if ($query->num_rows() == 1) {
81
            $status = $query->row_array();
82
83
            if ($status['comments_status'] == 1) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return $status['comments_status'] == 1;.
Loading history...
84
                return TRUE;
85
            } else {
86
                return FALSE;
87
            }
88
        } else {
89
            return FALSE;
90
        }
91
    }
92
93
    public function update($id, $data = []) {
94
        $this->db->where('id', $id);
95
        $this->db->update('comments', $data);
96
97
        return TRUE;
98
    }
99
100 View Code Duplication
    public function setYes($id) {
101
        $row = $this->db->where('id', $id)->get('comments')->row();
102
        $like = $row->like + 1;
103
        $data = ['like' => $like];
104
        $this->db->where('id', $id);
105
        $res = $this->db->update('comments', $data);
106
        return $res ? $like : false;
107
    }
108
109 View Code Duplication
    public function setNo($id) {
110
        $row = $this->db->where('id', $id)->get('comments')->row();
111
        $disslike = $row->disslike + 1;
112
        $data = ['disslike' => $disslike];
113
        $this->db->where('id', $id);
114
        $res = $this->db->update('comments', $data);
115
        return $res ? $disslike : false;
116
    }
117
118
    /**
119
     * @param int $id
120
     * @return bool
121
     */
122
    public function delete($id) {
123
        if (is_array($id)) {
124
            foreach ($id as $item) {
125
                $this->changeVotesAfterDelete($item);
126
            }
127
128
            $this->db->where_in('id', $id);
129
            $this->db->delete('comments');
130
            //delete child comments
131
            $this->db->where_in('parent', $id);
132
            $this->db->delete('comments');
133
        } else {
134
            $this->changeVotesAfterDelete($id);
135
            $this->db->limit(1);
136
            $this->db->where('id', $id);
137
            $this->db->delete('comments');
138
            //delete child comments
139
            $this->db->where('parent', $id);
140
            $this->db->delete('comments');
141
        }
142
143
        $this->setVoutesAfterDelete();
144
145
        return TRUE;
146
    }
147
148
    /**
149
     * @param int $id
150
     */
151
    public function changeVotesAfterDelete($id) {
152
153
            $comments = $this->db->get_where('comments', ['id' => (int) $id])->row_array();
154
155
        if (!in_array($comments['item_id'], $this->comments_product_id)) {
156
            $this->comments_product_id[] = $comments['item_id'];
157
        }
158
    }
159
160
    /**
161
     * @throws \Propel\Runtime\Exception\PropelException
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
162
     */
163
    public function setVoutesAfterDelete() {
164
165
        foreach ($this->comments_product_id as $item) {
166
167
            $query = $this->db->select('SUM(rate) AS rate , COUNT(rate) AS votes')
168
                ->from('comments')
169
                ->where('item_id', (int) $item)
170
                ->where('rate >', 0)
171
                ->get();
172
173
            $rate = 0;
174
            $votes = 0;
175
            /** Если значений больше 0 вставляем их в rate и votes */
176
            if ($query->num_rows() > 0) {
177
                list($rate, $votes) = array_values($query->row_array());
178
179
            }
180
                $products = SProductsRatingQuery::create()
181
                    ->findOneByProductId($item);
182
                $products->setVotes($votes);
183
                $products->setRating($rate);
184
                $products->save();
185
186
        }
187
    }
188
189
    /**
190
     * @param int $status
191
     * @return int
192
     */
193
    public function count_by_status($status = 0) {
194
        $this->db->where('status', $status);
195
        $this->db->from('comments');
196
197
        return $this->db->count_all_results();
198
    }
199
200
    /**
201
     * @return array
202
     */
203
    public function get_settings() {
204
        $this->db->where('name', 'comments');
205
        $query = $this->db->get('components')->row_array();
206
207
        return unserialize($query['settings']);
208
    }
209
210
    /**
211
     * @param array $data
212
     */
213
    public function save_settings($data) {
214
        $this->db->where('name', 'comments');
215
        $this->db->update('components', ['settings' => serialize($data)]);
216
    }
217
218
    /**
219
     * @param array $ids
220
     * @return mixed
221
     */
222
    public function get_many($ids) {
223
        if (is_array($ids)) {
224
            return $this->db->where_in('id', $ids)->get('comments')->result_array();
225
        }
226
    }
227
228
}
229
230
/* End of base.php */