Completed
Pull Request — development (#107)
by
unknown
09:56
created

Gallery_Widgets::album_images()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 1
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
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
class Gallery_Widgets extends MY_Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
13
{
14
15
    /**
16
     * Gallery_Widgets constructor.
17
     */
18
    public function __construct() {
19
        parent::__construct();
20
        $lang = new MY_Lang();
21
        $lang->load('gallery');
22
        $this->load->helper('gallery');
23
        $this->load->model('gallery_m');
24
    }
25
26
    /**
27
     * @param array $widget
28
     * @return string
29
     */
30
    public function latest_fotos(array $widget = []) {
31
32
        if ($widget['settings']['order'] == 'latest') {
33
            $images = gallery_latest_images($widget['settings']['limit']);
34
        } else {
35
            $images = gallery_latest_images($widget['settings']['limit'], 'random');
36
        }
37
38
        if (!empty($images)) {
39
            $countImages = count($images);
40
            for ($i = 0; $i < $countImages; $i++) {
41
                $images[$i]['url'] = site_url($images[$i]['url']);
42
                $images[$i]['file_path'] = media_url($images[$i]['file_path']);
43
                $images[$i]['thumb_path'] = media_url('uploads/gallery/' . $images[$i]['album_id'] . '/_thumbs/' . $images[$i]['file_name'] . $images[$i]['file_ext']);
44
            }
45
        }
46
47
        $this->template->add_array(
48
            ['images' => $images]
49
        );
50
51
        return $this->template->fetch('widgets/' . $widget['name'], $data);
0 ignored issues
show
Bug introduced by
The variable $data does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
52
    }
53
54
    /**
55
     * @param string $action
56
     * @param array $widget_data
57
     */
58
    public function latest_fotos_configure($action = 'show_settings', array $widget_data = []) {
59
        if ($this->dx_auth->is_admin() == FALSE) {
60
            exit;
61
        }
62
63
        switch ($action) {
64
            case 'show_settings':
65
                assetManager::create()
66
                    ->setData('widget', $widget_data)
67
                    ->renderAdmin('../../templates/latest_fotos_form');
68
                break;
69
70
            case 'update_settings':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
71
72
                $this->load->library('Form_validation');
73
                $this->form_validation->set_rules('limit', lang('Image limit', 'gallery'), 'trim|required|integer');
74
75 View Code Duplication
                if ($this->form_validation->run($this) == FALSE) {
76
                    showMessage(validation_errors(), false, 'r');
77
                    exit;
78
                }
79
80
                $data = [
81
                         'limit' => $this->input->post('limit'),
82
                         'order' => $this->input->post('order'),
83
                        ];
84
85
                $this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $data);
86
87
                showMessage(lang('Settings have been saved', 'gallery'));
88
                if ($this->input->post('action') == 'tomain') {
89
                    pjax('/admin/widgets_manager/index');
90
                }
91
                break;
92
93
            case 'install_defaults':
94
                $data = [
95
                         'limit' => 5,
96
                         'order' => 'latest',
97
                        ];
98
99
                $this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $data);
100
                break;
101
        }
102
    }
103
104
105
    /**
106
     * @param array $widget
107
     * @return string
108
     */
109
    public function album_images(array $widget = []) {
110
111
        $images = $this->gallery_m->get_album_images($widget['settings']['album_id'], $widget['settings']['limit']);
112
113
        if (!empty($images)) {
114
            $countImages = count($images);
115
            for ($i = 0; $i < $countImages; $i++) {
116
                $images[$i]['url'] = site_url('gallery/album/' . $images[$i]['album_id'] . '/image/' . $images[$i]['id']);
117
                $images[$i]['file_path'] = media_url('uploads/gallery/' . $images[$i]['album_id'] . '/' . $images[$i]['file_name'] .  $images[$i]['file_ext']);
118
                $images[$i]['thumb_path'] = media_url('uploads/gallery/' . $images[$i]['album_id'] . '/_thumbs/' . $images[$i]['file_name'] . $images[$i]['file_ext']);
119
            }
120
        }
121
122
        return assetManager::create()
123
            ->setData('images', $images)
124
            ->fetchTemplate('../widgets/' . $widget['name']);
125
    }
126
127
128
    /**
129
     * @param string $action
130
     * @param array $widget_data
131
     */
132
    public function album_images_configure($action = 'show_settings', array $widget_data = []) {
133
        if ($this->dx_auth->is_admin() == FALSE) {
134
            exit;
135
        }
136
137
        switch ($action) {
138
            case 'show_settings':
139
                assetManager::create()
140
                    ->setData('widget', $widget_data)
141
                    ->setData('albums', $this->gallery_m->get_albums())
142
                    ->renderAdmin('../../templates/album_images_form');
143
                break;
144
145
            case 'update_settings':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
146
147
                $this->load->library('Form_validation');
148
                $this->form_validation->set_rules('limit', lang('Image limit', 'gallery'), 'trim|required|integer');
149
                $this->form_validation->set_rules('album_id', lang('Choose Album', 'gallery'), 'trim|required|integer');
150
151 View Code Duplication
                if ($this->form_validation->run($this) == FALSE) {
152
                    showMessage(validation_errors(), false, 'r');
153
                    exit;
154
                }
155
156
                $data = [
157
                    'limit' => $this->input->post('limit'),
158
                    'album_id' => $this->input->post('album_id'),
159
                ];
160
161
                $this->load->module('admin/widgets_manager')->update_config($widget_data['id'], $data);
162
163
                showMessage(lang('Settings have been saved', 'gallery'));
164
                if ($this->input->post('action') == 'tomain') {
165
                    pjax('/admin/widgets_manager/index');
166
                }
167
                break;
168
169
            case 'install_defaults':
170
                $this->load->module('admin/widgets_manager')->update_config($widget_data['id'], ['limit' => 15]);
171
                break;
172
        }
173
    }
174
}