Completed
Push — development ( cfd391...deed4d )
by Andrij
12:03
created

Banners::render()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 22
nc 10
nop 2
dl 0
loc 34
rs 4.8196
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
if (!defined('BASEPATH')) {
4
    exit('No direct script access allowed');
5
}
6
7
/**
8
 * Class for Banners module
9
 * @uses MY_Controller
10
 * @author L.Andriy <[email protected]>
11
 * @copyright (c) 2013, ImageCMS
12
 * @package ImageCMSModule
13
 * @property Banner_model $banner_model
14
 */
15
class Banners 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...
16
{
17
18
    public $no_install = true;
19
20 View Code Duplication
    public function __construct() {
21
        parent::__construct();
22
        if (count($this->db->where('name', 'banners')->get('components')->result_array()) == 0) {
23
            $this->no_install = false;
24
        }
25
        $this->load->module('core');
26
        $this->load->model('banner_model');
27
        $lang = new MY_Lang();
28
        $lang->load('banners');
29
    }
30
31
    public function index() {
32
        if ($this->no_install === false) {
33
            return false;
34
        }
35
    }
36
37
    /**
38
     * Render banner into template
39
     * @access public
40
     * @param integer $id is id entity (brand, category, product, page) .... for main id = 0
0 ignored issues
show
introduced by
Paramater tags must be grouped together in a doc commment
Loading history...
41
     * @param integer $group
42
     * @return false|null
43
     * @author L.Andriy <[email protected]>
44
     * @copyright (c) 2013, ImageCMS
45
     */
46
    public function render($id = 0, $group = 0) {
47
        if ($this->no_install === false) {
48
            return false;
49
        }
50
        $type = $this->core->core_data['data_type'];
51
        $lang = $this->get_main_lang('identif');
52
        $painting = $type . '_' . (int) $id;
53
54
        $hash = 'baners' . $type . $id . \CI_Controller::get_instance()->config->item('template');
0 ignored issues
show
Unused Code introduced by
$hash is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
56
        $banners = $this->banner_model->get_all_banner($lang, $group);
57
        foreach ($banners as $banner) {
58
            $data = unserialize($banner['where_show']);
59
60
            if ((in_array($painting, $data) || in_array($type . '_0', $data)) && $banner['active'] && (time() < $banner['active_to'] or $banner['active_to'] == '-1')) {
61
                $ban[] = $banner;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$ban was never initialized. Although not strictly required by PHP, it is generally a good practice to add $ban = 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...
62
            }
63
        }
64
        if (count($ban) > 0) {
65
66
            $tpl = $this->banner_model->get_settings_tpl() ? $type . '_slider' : 'slider';
67
68
            ob_start();
69
            \CMSFactory\assetManager::create()
70
                    ->registerStyle('style')
71
                    ->registerScript('jquery.cycle.all.min')
72
                    ->setData(['banners' => $ban])
73
                    ->render($tpl, TRUE);
74
75
            $baners_view = ob_get_clean();
76
77
            echo $baners_view;
78
        }
79
    }
80
81
    public function getByGroup($group) {
82
        $banners = $this->banner_model->get_all_banner(MY_Controller::getCurrentLocale(), $group);
83
        return $banners;
84
    }
85
86
    /**
87
     * install module and create table
88
     * @access public
89
     * @author L.Andriy <[email protected]>
90
     * @copyright (c) 2013, ImageCMS
91
     */
92
    public function _install() {
93
94
        $sql = 'CREATE TABLE IF NOT EXISTS `mod_banner` (
95
          `id` int(11) NOT NULL AUTO_INCREMENT,
96
          `active` tinyint(4) NOT NULL,
97
          `active_to` int(11) DEFAULT NULL,
98
          `where_show` text CHARACTER SET utf8,
99
          `position` int(11) DEFAULT NULL,
100
          PRIMARY KEY (`id`)
101
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;';
102
103
        $this->db->query($sql);
104
105
        $sql = 'CREATE TABLE IF NOT EXISTS `mod_banner_i18n` (
106
          `id` int(11) NOT NULL,
107
          `url` text CHARACTER SET utf8,
108
          `locale` varchar(5) CHARACTER SET utf8 NOT NULL,
109
          `name` varchar(25) CHARACTER SET utf8 DEFAULT NULL,
110
          `description` text CHARACTER SET utf8,
111
          `photo` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
112
          KEY `id` (`id`,`locale`)
113
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;';
114
115
        $this->db->query($sql);
116
117
        $this->db->where('name', 'banners');
118
        $this->db->update('components', ['enabled' => 1]);
119
        $this->banner_model->createGroupsTable();
120
    }
121
122
    /**
123
     * deinstall module and drop tables
124
     * @access public
125
     * @author L.Andriy <[email protected]>
126
     * @copyright (c) 2013, ImageCMS
127
     */
128
    public function _deinstall() {
129
130
        if ($this->dx_auth->is_admin() == FALSE) {
131
            exit;
132
        }
133
134
        $this->load->dbforge();
135
        $this->dbforge->drop_table('mod_banner');
136
        $this->dbforge->drop_table('mod_banner_i18n');
137
    }
138
139
    /**
140
     * check current language
141
     * @access public
142
     * @author L.Andriy <[email protected]>
143
     * @copyright (c) 2013, ImageCMS
144
     * @param string $flag
0 ignored issues
show
introduced by
Paramater tags must be grouped together in a doc commment
Loading history...
145
     */
146
    public function get_main_lang($flag = null) {
147
148
        $lang = $this->db->get('languages')->result_array();
149
        $lan_array = [];
150
        foreach ($lang as $l) {
151
            $lan_array[$l['identif']] = $l['id'];
152
            $lan_array_rev[$l['id']] = $l['identif'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$lan_array_rev was never initialized. Although not strictly required by PHP, it is generally a good practice to add $lan_array_rev = 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...
153
        }
154
155
        $lang_uri = $this->uri->segment(1);
156
        if (in_array($lang_uri, $lan_array_rev)) {
157
            $lang_id = $lan_array[$lang_uri];
158
            $lang_ident = $lang_uri;
159
        } else {
160
            $lang = $this->db->where('default', 1)->get('languages')->result_array();
161
            $lang_id = $lang[0]['id'];
162
            $lang_ident = $lang[0]['identif'];
163
        }
164
        if ($flag == 'id') {
165
            return $lang_id;
166
        }
167
        if ($flag == 'identif') {
168
            return $lang_ident;
169
        }
170
        if ($flag == null) {
171
            return [
172
                    'id'      => $lang_id,
173
                    'identif' => $lang_ident,
174
                   ];
175
        }
176
    }
177
178
    public static function addMenu() {
179
        //        return array(
180
        //            1 =>
181
        //            array(
182
        //                'identifier' => 'banners',
183
        //                'text' => lang("Banners management", "banners"),
184
        //                'link' => '/admin/components/cp/banners',
185
        //                'subMenu' =>
186
        //                array(
187
        //                    array(
188
        //                        'identifier' => 'banners_man',
189
        //                        'text' => lang("Banners management", "banners"),
190
        //                        'link' => '/admin/components/cp/banners',
191
        //                        'class' => '',
192
        //                        'id' => '',
193
        //                        'pjax' => '',
194
        //                        'icon' => '',
195
        //                        'divider' => false,
196
        //                    ),
197
        //                    array(
198
        //                        'identifier' => 'create_banner',
199
        //                        'text' => lang("Create a banner", "banners"),
200
        //                        'link' => '/admin/components/init_window/banners/create',
201
        //                        'class' => '',
202
        //                        'id' => '',
203
        //                        'pjax' => '',
204
        //                        'icon' => '',
205
        //                        'divider' => false,
206
        //                    ),
207
        //                ),
208
        //            )
209
        //        );
210
    }
211
212
}
213
214
/* End of file banners.php */