ControllerBackwardModule::addJS()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
* 2007-2014 PrestaShop
4
*
5
* NOTICE OF LICENSE
6
*
7
* This source file is subject to the Open Software License (OSL 3.0)
8
* that is bundled with this package in the file LICENSE.txt.
9
* It is also available through the world-wide-web at this URL:
10
* http://opensource.org/licenses/osl-3.0.php
11
* If you did not receive a copy of the license and are unable to
12
* obtain it through the world-wide-web, please send an email
13
* to [email protected] so we can send you a copy immediately.
14
*
15
* DISCLAIMER
16
*
17
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
18
* versions in the future. If you wish to customize PrestaShop for your
19
* needs please refer to http://www.prestashop.com for more information.
20
*
21
*  @author PrestaShop SA <[email protected]>
22
*  @copyright  2007-2014 PrestaShop SA
23
*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
24
*  International Registered Trademark & Property of PrestaShop SA
25
*/
26
27
if ((bool)Configuration::get('PS_MOBILE_DEVICE'))
28
    require_once(_PS_MODULE_DIR_ . '/mobile_theme/Mobile_Detect.php');
29
30
// Retro 1.3, 'class_exists' cause problem with autoload...
31
if (version_compare(_PS_VERSION_, '1.4', '<'))
32
{
33
    // Not exist for 1.3
34
    class Shop extends ObjectModel
35
    {
36
        public $id = 1;
37
        public $id_shop_group = 1;
38
39
        public function __construct()
40
        {
41
        }
42
43
44
        public static function getShops()
45
        {
46
            return array(
47
                array('id_shop' => 1, 'name' => 'Default shop')
48
            );
49
        }
50
51
        public static function getCurrentShop()
52
        {
53
                return 1;
54
        }
55
    }
56
57
    class Logger
58
    {
59
        public static function AddLog($message, $severity = 2)
60
        {
61
            $fp = fopen(dirname(__FILE__).'/../logs.txt', 'a+');
62
            fwrite($fp, '['.(int)$severity.'] '.Tools::safeOutput($message));
63
            fclose($fp);
64
        }
65
    }
66
67
}
68
69
// Not exist for 1.3 and 1.4
70
class Context
71
{
72
    /**
73
     * @var Context
74
     */
75
    protected static $instance;
76
77
    /**
78
     * @var Cart
79
     */
80
    public $cart;
81
82
    /**
83
     * @var Customer
84
     */
85
    public $customer;
86
87
    /**
88
     * @var Cookie
89
     */
90
    public $cookie;
91
92
    /**
93
     * @var Link
94
     */
95
    public $link;
96
97
    /**
98
     * @var Country
99
     */
100
    public $country;
101
102
    /**
103
     * @var Employee
104
     */
105
    public $employee;
106
107
    /**
108
     * @var Controller
109
     */
110
    public $controller;
111
112
    /**
113
     * @var Language
114
     */
115
    public $language;
116
117
    /**
118
     * @var Currency
119
     */
120
    public $currency;
121
122
    /**
123
     * @var AdminTab
124
     */
125
    public $tab;
126
127
    /**
128
     * @var Shop
129
     */
130
    public $shop;
131
132
    /**
133
     * @var Smarty
134
     */
135
    public $smarty;
136
137
    /**
138
     * @ var Mobile Detect
139
     */
140
    public $mobile_detect;
141
142
    /**
143
     * @var boolean|string mobile device of the customer
144
     */
145
    protected $mobile_device;
146
147
    public function __construct()
148
    {
149
        global $cookie, $cart, $smarty, $link;
150
151
        $this->tab = null;
152
153
        $this->cookie = $cookie;
154
        $this->cart = $cart;
155
        $this->smarty = $smarty;
156
        $this->link = $link;
157
158
        $this->controller = new ControllerBackwardModule();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ControllerBackwardModule() of type object<ControllerBackwardModule> is incompatible with the declared type object<Controller> of property $controller.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
159
        if (is_object($cookie))
160
        {
161
            $this->currency = new Currency((int)$cookie->id_currency);
162
            $this->language = new Language((int)$cookie->id_lang);
163
            $this->country = new Country((int)$cookie->id_country);
164
            $this->customer = new CustomerBackwardModule((int)$cookie->id_customer);
165
            $this->employee = new Employee((int)$cookie->id_employee);
166
        }
167
        else
168
        {
169
            $this->currency = null;
170
            $this->language = null;
171
            $this->country = null;
172
            $this->customer = null;
173
            $this->employee = null;
174
        }
175
176
        $this->shop = new ShopBackwardModule();
177
178
        if ((bool)Configuration::get('PS_MOBILE_DEVICE'))
179
            $this->mobile_detect = new Mobile_Detect();
180
    }
181
182
    public function getMobileDevice()
183
    {
184
        if (is_null($this->mobile_device))
185
        {
186
            $this->mobile_device = false;
187
            if ($this->checkMobileContext())
188
            {
189
                switch ((int)Configuration::get('PS_MOBILE_DEVICE'))
190
                {
191 View Code Duplication
                    case 0: // Only for mobile device
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
                        if ($this->mobile_detect->isMobile() && !$this->mobile_detect->isTablet())
193
                            $this->mobile_device = true;
194
                        break;
195 View Code Duplication
                    case 1: // Only for touchpads
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
                        if ($this->mobile_detect->isTablet() && !$this->mobile_detect->isMobile())
197
                            $this->mobile_device = true;
198
                        break;
199 View Code Duplication
                    case 2: // For touchpad or mobile devices
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
200
                        if ($this->mobile_detect->isMobile() || $this->mobile_detect->isTablet())
201
                            $this->mobile_device = true;
202
                        break;
203
                }
204
            }
205
        }
206
207
        return $this->mobile_device;
208
    }
209
210
    protected function checkMobileContext()
211
    {
212
        return isset($_SERVER['HTTP_USER_AGENT'])
213
            && (bool)Configuration::get('PS_MOBILE_DEVICE')
214
            && !Context::getContext()->cookie->no_mobile;
215
    }
216
217
    /**
218
     * Get a singleton context
219
     *
220
     * @return Context
221
     */
222
    public static function getContext()
223
    {
224
        if (!isset(self::$instance))
225
            self::$instance = new Context();
226
        return self::$instance;
227
    }
228
229
    /**
230
     * Clone current context
231
     *
232
     * @return Context
233
     */
234
    public function cloneContext()
235
    {
236
        return clone($this);
237
    }
238
239
    /**
240
     * @return int Shop context type (Shop::CONTEXT_ALL, etc.)
241
     */
242
    public static function shop()
243
    {
244
        if (!self::$instance->shop->getContextType())
245
            return ShopBackwardModule::CONTEXT_ALL;
246
        return self::$instance->shop->getContextType();
247
    }
248
}
249
250
/**
251
 * Class Shop for Backward compatibility
252
 */
253
class ShopBackwardModule extends Shop
254
{
255
    const CONTEXT_ALL = 1;
256
257
    public $id = 1;
258
    public $id_shop_group = 1;
259
260
261
    public function getContextType()
262
    {
263
        return ShopBackwardModule::CONTEXT_ALL;
264
    }
265
266
    // Simulate shop for 1.3 / 1.4
267
    public function getID()
268
    {
269
        return 1;
270
    }
271
272
    /**
273
     * Get shop theme name
274
     *
275
     * @return string
276
     */
277
    public function getTheme()
278
    {
279
        return _THEME_NAME_;
280
    }
281
282
    public function isFeatureActive()
283
    {
284
        return false;
285
    }
286
}
287
288
/**
289
 * Class Controller for a Backward compatibility
290
 * Allow to use method declared in 1.5
291
 */
292
class ControllerBackwardModule
293
{
294
    /**
295
     * @param $js_uri
296
     * @return void
297
     */
298
    public function addJS($js_uri)
299
    {
300
        //1.3 compatibility
301
        if (_PS_VERSION_ >= '1.4') {
302
          Tools::addJS($js_uri);
303
        }
304
    }
305
306
    /**
307
     * @param $css_uri
308
     * @param string $css_media_type
309
     * @return void
310
     */
311
    public function addCSS($css_uri, $css_media_type = 'all')
312
    {
313
        Tools::addCSS($css_uri, $css_media_type);
314
    }
315
316
    public function addJquery()
317
    {
318
        if (_PS_VERSION_ < '1.5')
319
            $this->addJS(_PS_JS_DIR_.'jquery/jquery-1.4.4.min.js');
320
        elseif (_PS_VERSION_ >= '1.5')
321
            $this->addJS(_PS_JS_DIR_.'jquery/jquery-1.7.2.min.js');
322
    }
323
324
}
325
326
/**
327
 * Class Customer for a Backward compatibility
328
 * Allow to use method declared in 1.5
329
 */
330
class CustomerBackwardModule extends Customer
331
{
332
    public $logged = false;
333
    /**
334
     * Check customer informations and return customer validity
335
     *
336
     * @since 1.5.0
337
     * @param boolean $with_guest
338
     * @return boolean customer validity
339
     */
340
    public function isLogged($with_guest = false)
341
    {
342
        if (!$with_guest && $this->is_guest == 1)
343
            return false;
344
345
        /* Customer is valid only if it can be load and if object password is the same as database one */
346
        if ($this->logged == 1 && $this->id && Validate::isUnsignedId($this->id) && Customer::checkPassword($this->id, $this->passwd))
347
            return true;
348
        return false;
349
    }
350
}
351