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

Trash   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 0
loc 241
rs 10
wmc 27
lcom 0
cbo 6

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A index() 0 4 1
A adminAutoload() 0 12 1
B autoload() 0 22 4
A formRedirectUrl() 0 15 3
B create_redirect() 0 23 5
A delProductWhenCreate() 0 7 1
B addProductWhenAjaxChangeActive() 0 32 5
A addProductWhenDelete() 0 16 2
B _install() 0 43 2
A _deinstall() 0 6 2
1
<?php
2
3
use CMSFactory\Events;
4
use CMSFactory\Exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Exception.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use Propel\Runtime\Collection\ObjectCollection as PropelObjectCollection;
6
7
(defined('BASEPATH')) OR exit('No direct script access allowed');
8
9
/**
10
 * Image CMS
11
 *
12
 * Класс редиректа удаленных товаров
13
 * @property Cms_base $cms_base
14
 */
15
class Trash extends MY_Controller
16
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
17
18
    /**
19
     * Construct.
20
     */
21
    public function __construct() {
22
23
        parent::__construct();
24
        $lang = new MY_Lang();
25
        $lang->load('trash');
26
        $this->load->module('core');
27
    }
28
29
    /**
30
     * Index method.
31
     *
32
     * @return void
0 ignored issues
show
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
33
     */
34
    public function index() {
35
36
        $this->core->error_404();
37
    }
38
39
    /**
40
     * AdminAutoload method.
41
     *
42
     * @return void
0 ignored issues
show
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
43
     */
44
    public static function adminAutoload() {
45
46
        parent::adminAutoload();
47
        Events::create()->onShopProductDelete()->setListener('addProductWhenDelete');
48
        Events::create()->onShopProductCreate()->setListener('delProductWhenCreate');
49
        Events::create()->onShopProductAjaxChangeActive()->setListener('addProductWhenAjaxChangeActive');
50
        Events::create()->onShopCategoryDelete()->setListener('addProductsWhenCatDelete');
51
        Events::create()->onShopCategoryEdit()->setListener('updateCatUrl');
52
        Events::create()->onShopProductUpdate()->setListener('addProductWhenAjaxChangeActive');
53
        Events::create()->onShopProductCreate()->setListener('addProductWhenAjaxChangeActive');
54
        Events::create()->on('ShopAdminProducts:fastProdCreate')->setListener('addProductWhenAjaxChangeActive');
55
    }
56
57
    /**
58
     * Autoload method.
59
     *
60
     * @return void
0 ignored issues
show
introduced by
If there is no return value for a function, there must not be a @return tag.
Loading history...
61
     */
62
    public function autoload() {
63
64
        $url = ltrim(str_replace('/' . MY_Controller::getCurrentLocale() . '/', '', $this->input->server('REQUEST_URI')), '/'); //locale fix
65
        $row = $this->db
66
            ->where('trash_url', $url)
67
            ->or_where('trash_url', $this->uri->uri_string())
68
            ->get('trash')->row();
69
70
        if ($row != null) {
71
            /** Подключает модули, в авто лоаде они идут после*/
72
            $this->load->helper('menu/menu');
73
            $this->load->helper('xbanners/xbanners');
74
75
            ($row->trash_redirect_type != '404') OR $this->core->error_404();
76
            redirect($this->formRedirectUrl($row->trash_redirect), 'location', $row->trash_type);
77
        } else {
78
            $url = $this->uri->getBaseUrl() . $this->input->server('REQUEST_URI');
79
            if ($url != $this->formRedirectUrl($url)) {
80
                redirect($this->formRedirectUrl($url), 'location', 301);
81
            }
82
        }
83
    }
84
85
    /**
86
     * Form URL redirect to
87
     * @param $url - url string
0 ignored issues
show
introduced by
Missing parameter type
Loading history...
88
     * @return mixed
89
     */
90
    public function formRedirectUrl($url) {
91
92
        $siteSettings = $this->cms_base->get_settings();
93
94
        switch ($siteSettings['www_redirect']) {
95
            case 'from_www':
96
                return str_replace('://www.', '://', $url);
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
97
            case 'to_www':
98
                $url = str_replace('://www.', '://', $url);
99
                return str_replace('://', '://www.', $url);
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
100
            default:
101
                return $url;
102
        }
103
104
    }
105
106
    /**
107
     *
108
     * @param string $trash_url
109
     * @param string $redirect_url
110
     * @param int $type
111
     * @throws Exception
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
112
     */
113
    public function create_redirect($trash_url, $redirect_url, $type = 301) {
114
115
        if (!isset($trash_url)) {
116
            throw new Exception(lang('Old URL is not specified', 'trash'));
117
        }
118
119
        if (!isset($redirect_url)) {
120
            throw new Exception(lang('New URL is not specified', 'trash'));
121
        }
122
123
        $array = [
124
                  'trash_url'           => ltrim($trash_url, '/'),
125
                  'trash_redirect_type' => 'url',
126
                  'trash_type'          => in_array($type, [301, 302]) ? $type : 301,
127
                  'trash_redirect'      => '/' . str_replace(['http://', 'https://'], '', $redirect_url),
128
                 ];
129
130
        $this->db->insert('trash', $array);
131
132
        if ($this->db->_error_message()) {
133
            throw new Exception($this->db->_error_message());
134
        }
135
    }
136
137
    /**
138
     * @param array $arg
139
     */
140
    public static function delProductWhenCreate($arg) {
141
142
        /** @var SProducts $model */
143
        $model = $arg['model'];
144
        $ci = &get_instance();
145
        $ci->db->where('trash_url', 'shop/product/' . $model->getUrl())->delete('trash');
146
    }
147
148
    /**
149
     * @param array $arg
150
     */
151
    public static function addProductWhenAjaxChangeActive($arg) {
152
153
        /* @var $model SProducts */
154
        $models = $arg['model'];
155
156
        /* @var $ci MY_Controller */
157
        $ci = &get_instance();
158
159
        if (!$models instanceof PropelObjectCollection) {
160
            $model = $models;
161
            $models = new PropelObjectCollection();
162
            $models->append($model);
163
        }
164
165
        foreach ($models as $model) {
166
            if (!$model) {
167
                continue;
168
            }
169
170
            $ci->db->where('trash_url', 'shop/product/' . $model->getUrl())->delete('trash');
171
            if (!$model->getActive()) {
172
                $array = [
173
                          'trash_id'            => $model->getCategoryId(),
174
                          'trash_url'           => 'shop/product/' . $model->getUrl(),
175
                          'trash_redirect_type' => 'category',
176
                          'trash_type'          => '302',
177
                          'trash_redirect'      => shop_url('category/' . $model->getMainCategory()->getFullPath()),
178
                         ];
179
                $ci->db->insert('trash', $array);
180
            }
181
        }
182
    }
183
184
    /**
185
     * @param array $arg
186
     */
187
    public static function addProductWhenDelete($arg) {
188
189
        $models = $arg['model'];
190
        $ci = &get_instance();
191
        /** @var SProducts $model */
192
        foreach ($models as $model) {
193
            $array = [
194
                      'trash_id'            => $model->getCategoryId(),
195
                      'trash_url'           => 'shop/product/' . $model->getUrl(),
196
                      'trash_redirect_type' => 'category',
197
                      'trash_type'          => '301',
198
                      'trash_redirect'      => shop_url('category/' . $model->getMainCategory()->getFullPath()),
199
                     ];
200
            $ci->db->insert('trash', $array);
201
        }
202
    }
203
204
    public function _install() {
205
206
        $this->load->dbforge();
207
        ($this->dx_auth->is_admin()) OR exit;
208
        $fields = [
209
                   'id'                  => [
210
                                             'type'           => 'INT',
211
                                             'auto_increment' => true,
212
                                            ],
213
                   'trash_id'            => [
214
                                             'type'       => 'VARCHAR',
215
                                             'constraint' => '255',
216
                                             'null'       => true,
217
                                            ],
218
                   'trash_url'           => [
219
                                             'type'       => 'VARCHAR',
220
                                             'constraint' => '255',
221
                                             'null'       => true,
222
                                            ],
223
                   'trash_redirect_type' => [
224
                                             'type'       => 'VARCHAR',
225
                                             'constraint' => '20',
226
                                             'null'       => true,
227
                                            ],
228
                   'trash_redirect'      => [
229
                                             'type'       => 'VARCHAR',
230
                                             'constraint' => '255',
231
                                             'null'       => true,
232
                                            ],
233
                   'trash_type'          => [
234
                                             'type'       => 'VARCHAR',
235
                                             'constraint' => '3',
236
                                             'null'       => true,
237
                                            ],
238
                  ];
239
240
        $this->dbforge->add_field($fields);
241
        $this->dbforge->add_key('id', true);
242
        $this->dbforge->create_table('trash');
243
244
        $this->db->where('name', 'trash');
245
        $this->db->update('components', ['enabled' => 0, 'autoload' => 1]);
246
    }
247
248
    public function _deinstall() {
249
250
        $this->load->dbforge();
251
        ($this->dx_auth->is_admin()) OR exit;
252
        $this->dbforge->drop_table('trash');
253
    }
254
255
}
256
257
/* End of file trash.php */