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

Gallery_m::load_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 9.4285
1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
7
/**
8
 * Image CMS
9
 *
10
 * Gallery main model
11
 */
12
class Gallery_m extends CI_Model
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
    public function Base() {
16
        parent::__construct();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__construct() instead of Base()). Are you sure this is correct? If so, you might want to change this to $this->__construct().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
17
    }
18
19
    public function load_settings() {
20
        $this->db->select('settings');
21
        $query = $this->db->get_where('components', ['name' => 'gallery'])->row_array();
22
23
        return unserialize($query['settings']);
24
    }
25
26
    /**
27
     * Create new album
28
     */
29
    public function create_album() {
30
        $locale = $this->chose_locale();
31
        $this->db->select_max('position');
32
        $pos = $this->db->get('gallery_albums')->row_array();
33
34
        // Increase album position
35
        $data['position'] = $pos['position'] + 1;
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...
36
37
        $data = [
38
            //'name' => $this->input->post('name'),
39
            //'description' => trim($this->input->post('description')),
40
                 'created'     => time(),
41
                 'category_id' => $this->input->post('category_id'),
42
                 'tpl_file'    => $this->input->post('tpl_file'),
43
                ];
44
45
        $this->db->insert('gallery_albums', $data);
46
        $lid = $this->db->insert_id();
47
48
        $data_locale = [
49
                        'id'          => $lid,
50
                        'locale'      => $locale,
51
                        'name'        => $this->input->post('name'),
52
                        'description' => trim($this->input->post('description')),
53
                       ];
54
55
        $this->db->insert('gallery_albums_i18n', $data_locale);
56
57
        return $lid;
58
    }
59
60
    public function update_album($id, $data = []) {
61
        $this->db->where('id', $id);
62
        $this->db->update('gallery_albums', $data);
63
    }
64
65
    public function getTotalImages() {
66
        $query = $this->db->get('gallery_images')->result_array();
67
        return count($query);
68
    }
69
70
    public function delete_album($id) {
71
        // delete album
72
        $this->db->where('id', $id);
73
        $this->db->delete('gallery_albums');
74
        $this->db->where('id', $id);
75
        $this->db->delete('gallery_albums_i18n');
76
77
        // delete images
78
        $ids = $this->db->where('album_id', $id)->get('gallery_images')->row_array();
79
        $this->db->where('album_id', $id);
80
        $this->db->delete('gallery_images');
81
82
        foreach ($ids as $i) {
83
            $this->db->where('album_id', $i->id);
84
            $this->db->delete('gallery_images_i18n');
85
        }
86
    }
87
88
    /**
89
     * Get all albums
90
     * @param string $order_by
91
     * @param string $sort_order
92
     * @param int $category_id
93
     * @return bool
94
     */
95
    public function get_albums($order_by = 'date', $sort_order = 'desc', $category_id = 0) {
96
        // Select albums
97
98
        $locale = $this->chose_locale();
99
        $this->joinI18n($this->db, 'gallery_albums', $locale);
100
        if ($category_id > 0) {
101
            $this->db->where('gallery_albums.category_id', $category_id);
102
        }
103
104
        $this->db->select('gallery_albums.*, gallery_albums_i18n.name as name, gallery_albums_i18n.description as description');
105
        $this->db->select('gallery_images.file_name as cover_name', FALSE);
106
        $this->db->select('gallery_images.file_ext as cover_ext', FALSE);
107
        $this->db->join('gallery_images', 'gallery_images.id = gallery_albums.cover_id', 'left');
108
109
        // Subquery. album views.
110
        $this->db->select('(SELECT SUM(gallery_images.views) FROM gallery_images WHERE gallery_images.album_id = gallery_albums.id) AS views');
111
112
        // Subquery. album image count.
113
        $this->db->select('(SELECT COUNT(gallery_images.file_name) FROM gallery_images WHERE gallery_images.album_id = gallery_albums.id) AS count');
114
115
        if ($sort_order != 'desc' AND $sort_order != 'asc') {
116
            $sort_order = 'desc';
117
        }
118
119 View Code Duplication
        switch ($order_by) {
120
            case 'date':
121
                $this->db->order_by('created', $sort_order);
122
                break;
123
124
            case 'name':
125
                $this->db->order_by('gallery_albums_i18n.name', $sort_order);
126
                break;
127
128
            case 'position':
129
                $this->db->order_by('position', $sort_order);
130
                break;
131
        }
132
133
        $query = $this->db->get('gallery_albums');
134
135
        if ($query->num_rows() > 0) {
136
            return $query->result_array();
137
        } else {
138
            return FALSE;
139
        }
140
    }
141
142
    public function get_last_image($album_id) {
143
        $this->db->order_by('uploaded', 'desc');
144
        $this->db->where('album_id', $album_id);
145
        $query = $this->db->get('gallery_images', 1);
146
147
        if ($query->num_rows() == 1) {
148
            return $query->row_array();
149
        } else {
150
            return FALSE;
151
        }
152
    }
153
154
    /**
155
     * Get Album info
156
     * @param int $id
157
     * @param bool $include_images
158
     * @param integer $limit
159
     * @param integer $page
160
     * @param null|string $locale
161
     * @return bool
162
     */
163
    public function get_album($id = 0, $include_images = TRUE, $limit, $page, $locale = null) {
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...
164
        if (null === $locale) {
165
            $locale = $this->chose_locale();
166
        }
167
        $this->db->limit(1);
168
        $this->db->where('gallery_albums.id', $id);
169
        $this->joinI18n($this->db, 'gallery_albums', $locale);
170
        $this->db->select('*, gallery_albums.id as id');
171
        $query = $this->db->get('gallery_albums');
172
173
        if ($query->num_rows() > 0) {
174
            $album = $query->row_array();
175
176
            if ($include_images == TRUE) {
177
                $album['images'] = $this->get_album_images($album['id'], $limit, $page, $locale);
178
                $album['count'] = $this->db->query("SELECT COUNT(file_name) AS count FROM gallery_images WHERE album_id = $id")->row()->count;
179
            }
180
            return $album;
181
        } else {
182
            return FALSE;
183
        }
184
    }
185
186
    /**
187
     *
188
     * @param integer $album_id
189
     * @param integer $limit
190
     * @param integer $offset
191
     * @param string $locale
192
     * @return false|array
193
     */
194
    public function get_album_images($album_id, $limit, $offset, $locale) {
195
        //$this->db->order_by('uploaded', 'asc');
196
197
        $this->db->select('*, gallery_images.id as id');
198
        $this->joinI18n($this->db, 'gallery_images', $locale);
199
        $this->db->select('CONCAT_WS("", file_name, file_ext) as full_name', FALSE);
200
        $this->db->order_by('position', 'asc');
201
        $this->db->where('album_id', $album_id);
202
        if ($limit || $offset) {
203
            $this->db->limit($limit, $offset);
204
        }
205
        $query = $this->db->get('gallery_images');
206
207
        if ($query->num_rows() > 0) {
208
            return $query->result_array();
209
        } else {
210
            return FALSE;
211
        }
212
    }
213
214
    /**
215
     * @param int $album_id
216
     * @param int $image_id
217
     */
218
    public function set_album_cover($album_id, $image_id) {
219
        $this->db->where('id', $album_id);
220
        $this->db->update('gallery_albums', ['cover_id' => $image_id]);
221
    }
222
223
    // --------------------------------------------------------------------
224
225
    /**
226
     * @param array $data
227
     * @return int
228
     */
229
    public function add_image($data = []) {
230
        $this->db->select_max('position');
231
        $pos = $this->db->get('gallery_images')->row_array();
232
233
        // Increase position
234
        $data['position'] = $pos['position'] + 1;
235
236
        $this->db->insert('gallery_images', $data);
237
238
        // Update album date
239
        $this->db->where('id', $data['album_id']);
240
        $this->db->update('gallery_albums', ['updated' => time()]);
241
242
        return $this->db->insert_id();
243
    }
244
245
    /**
246
     * @param int $id
247
     * @param null|string $locale
248
     * @return bool
249
     */
250 View Code Duplication
    public function get_image_info($id, $locale = null) {
251
252
        if (null === $locale) {
253
            $locale = $this->chose_locale();
254
        }
255
256
        $this->db->limit(1);
257
        $this->joinI18n($this->db, 'gallery_images', $locale);
258
        $this->db->select('*, gallery_images.id as id');
259
        $this->db->where('gallery_images.id', $id);
260
        $query = $this->db->get('gallery_images');
261
262
        if ($query->num_rows() == 1) {
263
            return $query->row_array();
264
        } else {
265
            return FALSE;
266
        }
267
    }
268
269
    /**
270
     * @param int $id
271
     */
272
    public function increase_image_views($id) {
273
        $this->db->limit(1);
274
        $this->db->set('views', 'views + 1', FALSE);
275
        $this->db->where('id', $id);
276
        $this->db->update('gallery_images');
277
    }
278
279
    /**
280
     * @param int $id
281
     * @param string $name
282
     */
283
    public function rename_image($id, $name) {
284
        $this->db->where('id', $id);
285
        $this->db->update('gallery_images', ['file_name' => $name]);
286
    }
287
288
    /**
289
     * @param int $id
290
     */
291
    public function delete_image($id) {
292
        $this->db->limit(1);
293
        $this->db->where('id', $id);
294
        $this->db->delete('gallery_images');
295
296
        $this->db->where('id', $id);
297
        $this->db->delete('gallery_images_i18n');
298
    }
299
300
    /**
301
     * @param int $id
302
     * @param array $data
303
     * @param string $locale
304
     */
305
    public function update_description($id, $data, $locale) {
306
307
        if ($this->db->where('id', $id)->where('locale', $locale)->get('gallery_images_i18n')->num_rows()) {
308
            $this->db->where('id', $id)->where('locale', $locale);
309
            $this->db->update('gallery_images_i18n', ['description' => $data['description'], 'title' => $data['title']]);
310 View Code Duplication
        } else {
311
            $this->db->insert('gallery_images_i18n', ['id' => $id, 'locale' => $locale, 'title' => $data['title'], 'description' => $data['description']]);
312
        }
313
    }
314
315
    public function update_position($id, $position = 0) {
316
        $this->db->limit(1);
317
        $this->db->where('id', $id);
318
        $this->db->update('gallery_images', ['position' => $position]);
319
    }
320
321
    // --------------------------------------------------------------------
322
323
    public function create_category($data = []) {
324
        $this->db->insert('gallery_category', $data);
325
        return $this->db->insert_id();
326
    }
327
328 View Code Duplication
    public function get_category($id, $locale = null) {
329
        if (null === $locale) {
330
            $locale = $this->chose_locale();
331
        }
332
        $this->db->limit(1);
333
        $this->db->where('gallery_category.id', $id);
334
        $this->db->select('*, gallery_category.id as id');
335
        $this->joinI18n($this->db, 'gallery_category', $locale);
336
        $query = $this->db->get('gallery_category');
337
338
        if ($query->num_rows() == 1) {
339
            return $query->row_array();
340
        } else {
341
            return FALSE;
342
        }
343
    }
344
345
    /**
346
     * @param CI_DB_mysqli_driver|CI_DB_mysql_driver $con
347
     * @param string $table
348
     * @param string $locale
349
     * @return CI_DB_mysql_driver|CI_DB_mysqli_driver
350
     */
351
    public function joinI18n($con, $table, $locale) {
352
        $con->join($table . '_i18n', $table . '_i18n.id = ' . $table . '.id and ' . $table . "_i18n.locale = '" . $locale . "'", 'left');
353
        return $con;
354
    }
355
356
    /**
357
     * @return string
358
     */
359
    public function chose_locale() {
360
        return MY_Controller::getCurrentLocale();
361
    }
362
363
    public function get_categories($order_by = 'name', $sort_order = 'desc') {
364
        if ($sort_order != 'desc' AND $sort_order != 'asc') {
365
            $sort_order = 'desc';
366
        }
367
368
        $locale = $this->chose_locale();
369
        $this->joinI18n($this->db, 'gallery_category', $locale);
370
        $this->db->select('*, gallery_category.id as id, gallery_category_i18n.name as name');
371
372 View Code Duplication
        switch ($order_by) {
373
            case 'date':
374
                $this->db->order_by('created', $sort_order);
375
                break;
376
377
            case 'name':
378
                $this->db->order_by('gallery_category_i18n.name', $sort_order);
379
                break;
380
381
            case 'position':
382
                $this->db->order_by('position', $sort_order);
383
                break;
384
        }
385
386
        $query = $this->db->get('gallery_category');
387
388
        if (!$query) {
389
            return FALSE;
390
        }
391
392
        if ($query->num_rows() > 0) {
393
            return $query->result_array();
394
        } else {
395
            return FALSE;
396
        }
397
    }
398
399
    public function update_category($data = [], $id) {
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...
400
        $this->db->limit(1);
401
        $this->db->where('id', $id);
402
403
        $this->db->update('gallery_category', $data);
404
    }
405
406
    public function delete_category($id) {
407
        $this->db->limit(1);
408
        $this->db->where('id', $id);
409
        $this->db->delete('gallery_category');
410
        $this->db->where('id', $id);
411
        $this->db->delete('gallery_category_i18n');
412
    }
413
414
}
415
416
/* End of file gallery_m.php */