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

Ymarket::ymarketCore()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 2 Features 0
Metric Value
cc 1
eloc 19
c 8
b 2
f 0
nc 1
nop 1
dl 0
loc 25
rs 8.8571
1
<?php
2
3
use Category\CategoryApi;
4
use CMSFactory\assetManager;
5
use CMSFactory\Events;
6
use Currency\Currency;
7
8
(defined('BASEPATH')) OR exit('No direct script access allowed');
9
10
/**
11
 * Image CMS
12
 * Module ymarket
13
 * @property Ymarket_model $ymarket_model
14
 * @property Ymarket_products_fields_model ymarket_products_fields_model
15
 */
16
class Ymarket extends ShopController
17
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
18
19
    const DEFAULT_TYPE = 1;
20
    const PRICE_UA_TYPE = 2;
21
    const NADAVI_UA_TYPE = 3;
22
23
    protected $offers = [];
24
25
    protected $categories = [];
26
27
    protected $brandIds = [];
28
29
    protected $mainCurr = [];
30
31
    protected $currencies;
32
33
    protected $currencyCode;
34
35
    protected $settings;
36
37
    public function __construct() {
38
        parent::__construct();
39
        $lang = new MY_Lang();
40
        $lang->load('ymarket');
41
        $this->load->model('ymarket_model');
42
        $this->load->model('ymarket_products_fields_model');
43
    }
44
45
    /**
46
     *
47
     * @param boolean $ignoreSettings
48
     * @param integer $type
49
     */
50
    private function init($ignoreSettings = false, $type) {
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
51
        if ($ignoreSettings) {
52
            $this->categories = CategoryApi::getInstance()->getCategory();
53
            $this->brandIds = [];
54
        } else {
55
            $this->settings = $this->ymarket_model->init($type);
56
            $this->categories = CategoryApi::getInstance()->getCategory($this->settings['unserCats']);
57
            $this->brandIds = $this->settings['unserBrands'];
58
        }
59
    }
60
61
    /**
62
     * Generates an array of data to create a body xml
63
     *
64
     * @param boolean $ignoreSettings
65
     * @param integer $type
66
     */
67
    public function index($ignoreSettings = false, $type = self::DEFAULT_TYPE) {
68
        $this->init($ignoreSettings, $type);
69
        switch ($type) {
70
            case self::DEFAULT_TYPE:
71
                $this->ymarketCore();
72
                break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
73
            case self::PRICE_UA_TYPE:
74
                $this->priceuaCore();
75
                break;
0 ignored issues
show
Coding Style introduced by
Case breaking statements must be followed by a single blank line
Loading history...
76
            case self::NADAVI_UA_TYPE:
77
                $this->priceuaCore();
78
                break;
79
        }
80
    }
81
82
    /**
83
     *
84
     * @param boolean $ignoreSettings
85
     */
86
    private function ymarketCore($ignoreSettings = false) {
87
        $ci = ShopCore::$ci;
88
89
        $productFields = $this->ymarket_products_fields_model->getProductsFields();
90
91
        $offers = $this->ymarket_model->formOffers($ignoreSettings, $productFields);
92
93
        list($currencies, $mainCurr) = $this->ymarket_model->makeCurrency();
94
95
        $infoXml = [];
96
        $infoXml['categories'] = $this->categories;
97
        $infoXml['offers'] = $offers;
98
        $infoXml['site_short_title'] = $this->settings['site_short_title'];
99
        $infoXml['site_title'] = $this->settings['site_title'];
100
        $infoXml['base_url'] = $ci->config->item('base_url');
101
        $infoXml['imagecms_number'] = IMAGECMS_NUMBER;
102
        $infoXml['siteinfo_adminemail'] = siteinfo('siteinfo_adminemail');
103
        $infoXml['currencyCode'] = $currencies;
104
        $infoXml['mainCurr'] = $mainCurr;
105
106
        assetManager::create()
107
                ->setData('full', $ignoreSettings)
108
                ->setData('infoXml', $infoXml)
109
                ->render('yandex', true);
110
    }
111
112
    public function all() {
113
        $this->index(true);
114
    }
115
116
    /**
117
     * autoload
118
     */
119
    public function autoload() {
120
121
    }
122
123
    public static function adminAutoload() {
124
        Events::create()
125
                ->onShopProductPreUpdate()
126
                ->setListener('_extendYmarketPageAdmin');
127
128
        Events::create()
129
                ->onShopProductUpdate()
130
                ->setListener('_extendYmarketPageAdmin');
131
    }
132
133
    /**
134
     * Extend products admin page
135
     * @param array $data
136
     */
137
    public static function _extendYmarketPageAdmin($data) {
138
        $ci = &get_instance();
139
        $lang = new MY_Lang();
140
        $lang->load('ymarket');
141
        include_once 'models/ymarket_products_fields_model.php';
142
        $model = new Ymarket_products_fields_model();
143
        $countries = include_once 'config/countries.php';
144
        $months = [1, 2, 3, 6, 9, 12, 18, 24, 30, 36, 42, 48];
145
        if ($ci->input->post()) {
146
            $post = $ci->input->post('ymarket');
147
            if ($data['model']) {
148
                $productId = $data['model']->getId();
149
                $dataFields = [
150
                    'country_of_origin' => $post['country_of_origin'] ? $post['country_of_origin'] : null,
151
                    'manufacturer_warranty' => $post['manufacturer_warranty']['exist'] ? self::toISO8601Time($post['manufacturer_warranty']['time']) : 'false',
152
                    'seller_warranty' => $post['seller_warranty']['exist'] ? self::toISO8601Time($post['seller_warranty']['time']) : 'false',
153
                ];
154
                $model->setFields($productId, $dataFields);
155
            }
156
        } else {
157
            $productId = $data['model']->getId();
158
            $fields = $model->getFields($productId);
159
            $fields['manufacturer_warranty'] = self::fromISO8601ToMonths($fields['manufacturer_warranty']);
160
            $fields['seller_warranty'] = self::fromISO8601ToMonths($fields['seller_warranty']);
161
            $view = assetManager::create()
162
                    ->setData(
163
                        [
164
                                'countries' => $countries,
165
                                'months' => $months,
166
                                'fields' => $fields,
167
                            ]
168
                    )
169
                    ->registerScript('script')
170
                    ->fetchAdminTemplate('products_extend');
171
            assetManager::create()
172
                    ->appendData('moduleAdditions', $view);
173
        }
174
    }
175
176
    /**
177
     *
178
     * @param integer|float $months
179
     * @return string
180
     */
181
    public static function toISO8601Time($months) {
182
        if (!$months) {
183
            return 'true';
184
        }
185
        $years = (int) ($months / 12);
186
        $months = $months % 12;
187
        $time = 'P';
188
        $time .= $years ? $years . 'Y' : '';
189
        $time .= $months ? $months . 'M' : '';
190
        return $time;
191
    }
192
193
    /**
194
     *
195
     * @param string $iso
196
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
197
     */
198
    public static function fromISO8601ToMonths($iso) {
199
        if (in_array($iso, ['true', 'false'])) {
200
            return $iso;
201
        }
202
        preg_match('/([\d])+Y/', $iso, $matches_years);
203
        $years = $matches_years[1] ? (int) $matches_years[1] : 0;
204
        preg_match('/([\d])+M/', $iso, $matches_months);
205
        $months = $matches_months[1] ? (int) $matches_months[1] : 0;
206
        $months = $years * 12 + $months;
207
        return $months;
208
    }
209
210
    /**
211
     * Install
212
     */
213
    public function _install() {
214
        $this->load->dbforge();
215
        $fields = [
216
            'id' => ['type' => 'INT', 'constraint' => 11, 'auto_increment' => TRUE],
217
            'categories' => ['type' => 'TEXT'],
218
            'brands' => ['type' => 'TEXT'],
219
            'adult' => ['type' => 'VARCHAR', 'constraint' => 100]
220
        ];
221
        $this->dbforge->add_key('id', TRUE);
222
        $this->dbforge->add_field($fields);
223
        $this->dbforge->create_table('mod_ymarket', TRUE);
224
225
        $fields = [
226
            'id' => [
227
                'type' => 'INT',
228
                'constraint' => 11,
229
                'auto_increment' => TRUE
230
            ],
231
            'product_id' => [
232
                'type' => 'INT',
233
                'constraint' => 11,
234
            ],
235
            'country_of_origin' => [
236
                'type' => 'VARCHAR',
237
                'constraint' => 255,
238
                'null' => true,
239
            ],
240
            'manufacturer_warranty' => [
241
                'type' => 'VARCHAR',
242
                'constraint' => 255,
243
                'null' => true,
244
                'default' => 'false',
245
            ],
246
            'seller_warranty' => [
247
                'type' => 'VARCHAR',
248
                'constraint' => 255,
249
                'null' => true,
250
                'default' => 'false',
251
            ],
252
        ];
253
        $this->dbforge->add_key('id', TRUE);
254
        $this->dbforge->add_field($fields);
255
        $this->dbforge->create_table(Ymarket_products_fields_model::TABLE, TRUE);
256
257
        $this->db->where('name', 'ymarket')
258
            ->update('components', ['enabled' => '1', 'autoload' => '1']);
259
260
        $this->db->insert('mod_ymarket', ['categories' => '', 'adult' => '']);
261
    }
262
263
    /**
264
     * Deinstall
265
     */
266
    public function _deinstall() {
267
        $this->load->dbforge();
268
        $this->dbforge->drop_table('mod_ymarket');
269
    }
270
271
}
272
273
/* End of file sample_module.php */