Gallery::create_albums_covers()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
use CMSFactory\assetManager;
4
use CMSFactory\Events;
5
6
if (!defined('BASEPATH')) {
7
    exit('No direct script access allowed');
8
}
9
10
/**
11
 * Image CMS
12
 *
13
 * Gallery module
14
 * Need Imagebox module
15
 * @property Gallery_m $gallery_m
16
 * @property Components comments
17
 * @property Gallery_install gallery_install
18
 */
19
class Gallery extends MY_Controller
20
{
21
22
    public $conf = [
23
                    'upload_url'    => 'uploads/gallery/',
24
                    'thumbs_folder' => '_thumbs',
25
                   ];
26
27
    private $settings = [];
28
29 View Code Duplication
    public function __construct() {
30
        parent::__construct();
31
        $lang = new MY_Lang();
32
        $lang->load('gallery');
33
34
        $this->load->module('core');
35
        $this->load->model('gallery_m');
36
37
        // Load gallery settings
38
        $this->settings = $this->gallery_m->load_settings();
39
        $this->load->helper('gallery');
40
    }
41
42
    public function autoload() {
43
44
    }
45
46
    /**
47
     * List categories and get albums from first category
48
     */
49
    public function index() {
50
        $this->core->set_meta_tags(lang('Gallery', 'gallery'));
51
52
        $categories = $this->gallery_m->get_categories($this->settings['order_by'], $this->settings['sort_order']);
53
        $albums = $this->gallery_m->get_albums($this->settings['order_by'], $this->settings['sort_order']);
54
55
        $data = [
56
                 'gallery_category' => $categories,
57
                 'total'            => $this->gallery_m->getTotalImages(),
58
                 'categories'       => $categories,
59
                ];
60
61
        // Get covers
62
        $data['albums'] = is_array($albums) ? $this->create_albums_covers($albums) : NULL;
63
64
        Events::create()->raiseEvent($data, 'gallery:load');
65
        assetManager::create()
66
            ->setData($data)
67
            ->registerStyle('style', FAlSE)
68
            ->render('index');
69
    }
70
71
    public function albums() {
72
        $this->core->set_meta_tags(lang('Gallery', 'gallery'));
73
74
        $categories = $this->gallery_m->get_categories($this->settings['order_by'], $this->settings['sort_order']);
75
        $albums = $this->gallery_m->get_albums($this->settings['order_by'], $this->settings['sort_order']);
76
77
        $data = [
78
                 'gallery_category' => $categories,
79
                 'total'            => $this->gallery_m->getTotalImages(),
80
                ];
81
82
        // Get covers
83
        $data['albums'] = is_array($albums) ? $this->create_albums_covers($albums) : NULL;
84
85
        assetManager::create()
86
            ->setData($data)
87
            ->registerStyle('style', FAlSE)
88
            ->render('albums');
89
    }
90
91
    /**
92
     * Display category albums
93
     * @param int $id
94
     */
95
    public function category($id = 0) {
96
        $this->core->set_meta_tags(lang('Gallery', 'gallery'));
97
98
        $category = $this->gallery_m->get_category($id);
99
100
        if ($category === FALSE) {
101
            redirect('gallery');
102
        } else {
103
            $albums = $this->gallery_m->get_albums($this->settings['order_by'], $this->settings['sort_order'], $category['id']);
104
105
            if ($albums !== FALSE) {
106
                $albums = $this->create_albums_covers($albums);
107
            }
108
109
            $data = [
110
                     'albums'           => $albums,
111
                     'current_category' => $category,
112
                     'gallery_category' => $this->gallery_m->get_categories($this->settings['order_by'], $this->settings['sort_order']),
113
                    ];
114
115
            Events::create()->raiseEvent($data, 'gallery:category');
116
            assetManager::create()
117
                ->setData($data)
118
                ->render('albums');
119
        }
120
    }
121
122
    /**
123
     * Display album images
124
     * @param int $id
125
     */
126
    public function album($id = 0) {
127 View Code Duplication
        if (preg_match('/[A-Z]/', $this->uri->uri_string())) {
128
            redirect(site_url(strtolower($this->uri->uri_string())), 'location', 301);
129
        }
130
131
        $album = $this->gallery_m->get_album($id);
132
        if ($this->uri->total_segments() > 5) {
133
            $params = $this->uri->uri_to_assoc(5);
134
        } else {
135
            $params = $this->uri->uri_to_assoc(4);
136
        }
137
138
        if ($album == FALSE) {
139
            $this->core->error_404();
140
        }
141
142
        if ($params['image'] > 0) {
143
            $n = 0;
144
            foreach ($album['images'] as $image) {
145
                if ($image['id'] == $params['image']) {
146
                    $prev_img = $image;
147
148
                    // Create prev/next links
149
                    $next = $album['images'][$n + 1];
150
                    $prev = $album['images'][$n - 1];
151
152
                    $current_pos = $n + 1;
153
                }
154
                $n++;
155
            }
156
        } else {
157
            // Display first image prev
158
            $prev_img = $album['images'][0];
159
160
            $next = $album['images'][1];
161
            $prev = NULL;
162
            $current_pos = 1;
163
        }
164
165
        if ($prev_img == NULL) {
166
            $this->core->error_404();
167
            exit;
168
        }
169
170
        $prev_img['url'] = $this->conf['upload_url'] . $album['id'] . '/' . $prev_img['file_name'] . '_prev' . $prev_img['file_ext'];
171
172
        $data = [
173
                 'album'            => $album,
174
                 'thumb_url'        => $this->conf['upload_url'] . $album['id'] . '/' . $this->conf['thumbs_folder'] . '/',
175
                 'album_link'       => 'gallery/album/' . $album['id'] . '/',
176
                 'album_url'        => $this->conf['upload_url'] . $album['id'] . '/',
177
                 'prev_img'         => $prev_img,
178
                 'next'             => $next,
179
                 'prev'             => $prev,
180
                 'current_pos'      => $current_pos,
181
                 'current_category' => $this->gallery_m->get_category($album['category_id']),
182
                 'gallery_category' => $this->gallery_m->get_categories($this->settings['order_by'], $this->settings['sort_order']),
183
                ];
184
185
        $this->gallery_m->increase_image_views($prev_img['id']);
186
        $this->core->set_meta_tags([$album['name']]);
187
        Events::create()->raiseEvent($data, 'gallery:album');
188
189
        assetManager::create()
190
            ->setData($data)
191
            // ->registerStyle('jquery.fancybox-1.3.4', FAlSE)
192
            ->registerStyle('style', FAlSE)
193
            // ->registerScript('jquery.fancybox-1.3.4.pack', TRUE)
194
            // ->registerScript('jquery.easing-1.3.pack', TRUE)
195
            // ->registerScript('jquery.mousewheel-3.0.4.pack', TRUE)
196
            ->render($album['tpl_file'] ?: 'album');
197
    }
198
199
    /**
200
     * @param int $id
201
     * @param int $page
202
     */
203
    public function thumbnails($id = 0, $page = 0) {
204 View Code Duplication
        if (preg_match('/[A-Z]/', $this->uri->uri_string())) {
205
            redirect(site_url(strtolower($this->uri->uri_string())), 'location', 301);
206
        }
207
208
        $album = $this->gallery_m->get_album($id, true, 15, $page * 15);
209
210
        if ($album == FALSE) {
211
            $this->core->error_404();
212
            exit;
213
        }
214
        $this->load->library('Pagination');
215
        $this->pagination = new \CI_Pagination();
216
        $paginationConfig['uri_segment'] = 4;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$paginationConfig was never initialized. Although not strictly required by PHP, it is generally a good practice to add $paginationConfig = 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...
217
        $paginationConfig['base_url'] = site_url('gallery/thumbnails/' . $id);
218
        $paginationConfig['total_rows'] = ceil($album[count] / 15);
219
        $paginationConfig['last_link'] = ceil($album[count] / 15);
220
        $paginationConfig['per_page'] = 1;
221
222
        $paginationConfig['page_query_string'] = true;
223
        $paginationConfig['first_link'] = '1';
224
        $paginationConfig['num_links'] = 3;
225
        include_once "./templates/{$this->config->item('template')}/paginations.php";
226
227
        $this->pagination->initialize($paginationConfig);
228
229
        $data = [
230
                 'album'            => $album,
231
                 'thumb_url'        => $this->conf['upload_url'] . $album['id'] . '/' . $this->conf['thumbs_folder'] . '/',
232
                 'album_link'       => 'gallery/album/' . $album['id'] . '/',
233
                 'album_url'        => $this->conf['upload_url'] . $album['id'] . '/',
234
                 'current_category' => $this->gallery_m->get_category($album['category_id']),
235
                 'pagination'       => $this->pagination->create_links(),
236
                ];
237
238
        $this->core->set_meta_tags([$album['name']]);
239
240
        assetManager::create()
241
            ->setData($data)
242
            ->render('thumbnails');
243
    }
244
245
    public function post_comment() {
246
        $image_id = (int) $this->input->post('comment_item_id');
247
248
        $this->load->module('comments');
249
        $this->comments->module = 'gallery';
250
251
        if ($this->db->get_where('gallery_images', ['id' => $image_id])->num_rows() > 0) {
252
            $this->comments->add($image_id);
253
        } else {
254
            $this->core->error_404();
255
        }
256
    }
257
258
    /**
259
     * Create cover url to each album
260
     * @param array $albums
261
     * @return array
262
     */
263
    public function create_albums_covers($albums = []) {
264
        $cnt = count($albums);
265
        for ($i = 0; $i < $cnt; $i++) {
266
            $thumb_url = '/' . $this->conf['upload_url'] . $albums[$i]['id'] . '/' . $this->conf['thumbs_folder'] . '/';
267
268
            $albums[$i]['cover_url'] = $thumb_url . $albums[$i]['cover_name'] . $albums[$i]['cover_ext'];
269
270
            if ($albums[$i]['cover_name'] == NULL) {
271
                $image = $this->gallery_m->get_last_image($albums[$i]['id']);
272
                if ($image) {
273
                    $albums[$i]['cover_url'] = $thumb_url . $image['file_name'] . $image['file_ext'];
274
                } else {
275
                    $albums[$i]['cover_url'] = media_url('application/modules/gallery/assets/images/nophoto.jpg');
276
                }
277
            } else {
278
                $albums[$i]['cover_url'] = $thumb_url . $albums[$i]['cover_name'] . $albums[$i]['cover_ext'];
279
            }
280
        }
281
        return $albums;
282
    }
283
284
    // Install
285
286
    public function _install() {
287
        if ($this->dx_auth->is_admin() == FALSE) {
288
            exit;
289
        }
290
        $this->load->model('gallery_install');
291
        $this->gallery_install->make_install();
292
293
    }
294
295
    // Delete module
296
297
    public function _deinstall() {
298
        if ($this->dx_auth->is_admin() == FALSE) {
299
            exit;
300
        }
301
        $this->load->model('gallery_install');
302
        $this->gallery_install->deinstall();
303
    }
304
305
}
306
307
/* End of file gallery.php */