Passed
Push — master ( e643a0...1f75e2 )
by Michael
03:19
created

Attributes::resetOptions()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 0
1
<?php
2
3
namespace XoopsModules\Oledrion;
4
5
/**
6
 * ****************************************************************************
7
 * oledrion - MODULE FOR XOOPS
8
 * Copyright (c) Hervé Thouzard (http://www.herve-thouzard.com/)
9
 *
10
 * You may not change or alter any portion of this comment or credits
11
 * of supporting developers from this source code or any supporting source code
12
 * which is considered copyrighted (c) material of the original comment or credit authors.
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 *
17
 * @copyright       Hervé Thouzard (http://www.herve-thouzard.com/)
18
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
19
 * @author          Hervé Thouzard (http://www.herve-thouzard.com/)
20
 *
21
 * Version :
22
 * ****************************************************************************
23
 */
24
25
use XoopsModules\Oledrion;
26
27
/**
28
 * Gestion des options (attributs) de produits
29
 *
30
 * @since 2.3.2009.03.10
31
 */
32
33
34
//// Les types d'option
35
//define('OLEDRION_ATTRIBUTE_RADIO', 1);
36
//define('OLEDRION_ATTRIBUTE_CHECKBOX', 2);
37
//define('OLEDRION_ATTRIBUTE_SELECT', 3);
38
//
39
//// Le séparateur de données utilisé en interne
40
//define('OLEDRION_ATTRIBUTE_SEPARATOR', '|');
41
//define('OLEDRION_EMPTY_OPTION', '');
42
//
43
//// Le séparateur de ligne lorsque l'option est un bouton radio ou des cases à cocher
44
//define('OLEDRION_ATTRIBUTE_CHECKBOX_WHITE_SPACE', 1);     // Séparateur de ligne = espace blanc
45
//define('OLEDRION_ATTRIBUTE_CHECKBOX_NEW_LINE', 2);        // Séparateur de ligne = retour à la ligne
46
//
47
//// Les options par défaut lorsque l'option est une liste déroulante
48
//define('OLEDRION_ATTRIBUTE_SELECT_VISIBLE_OPTIONS', 1);    // Valeur par défaut, nombre d'options visibles
49
//define('OLEDRION_ATTRIBUTE_SELECT_MULTIPLE', false);       // Valeur par défaut, sélecteur multiple ?
50
51
/**
52
 * Class Oledrion_attributes
53
 */
54
class Attributes extends OledrionObject
55
{
56
    /**
57
     * constructor
58
     *
59
     * normally, this is called from child classes only
60
     */
61
    public function __construct()
62
    {
63
        $this->initVar('attribute_id', XOBJ_DTYPE_INT, null, false);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Oledrion\XOBJ_DTYPE_INT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
64
        $this->initVar('attribute_weight', XOBJ_DTYPE_INT, null, false);
65
        $this->initVar('attribute_title', XOBJ_DTYPE_TXTBOX, null, false);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Oledrion\XOBJ_DTYPE_TXTBOX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
66
        $this->initVar('attribute_name', XOBJ_DTYPE_TXTBOX, null, false);
67
        $this->initVar('attribute_type', XOBJ_DTYPE_INT, null, false);
68
        $this->initVar('attribute_mandatory', XOBJ_DTYPE_INT, null, false);
69
        $this->initVar('attribute_names', XOBJ_DTYPE_OTHER, null, false);
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Oledrion\XOBJ_DTYPE_OTHER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
70
        $this->initVar('attribute_values', XOBJ_DTYPE_OTHER, null, false);
71
        $this->initVar('attribute_prices', XOBJ_DTYPE_OTHER, null, false);
72
        $this->initVar('attribute_stocks', XOBJ_DTYPE_OTHER, null, false);
73
        $this->initVar('attribute_product_id', XOBJ_DTYPE_INT, null, false);
74
        $this->initVar('attribute_default_value', XOBJ_DTYPE_TXTBOX, null, false);
75
        $this->initVar('attribute_option1', XOBJ_DTYPE_INT, null, false);
76
        $this->initVar('attribute_option2', XOBJ_DTYPE_INT, null, false);
77
    }
78
79
    /**
80
     * Indique si l'attribut courant a une valeur par défaut
81
     *
82
     * @return bool
83
     * @since 2.3.2009.03.20
84
     */
85
    public function hasDefaultValue()
86
    {
87
        return '' !== xoops_trim($this->getVar('attribute_default_value'));
0 ignored issues
show
Bug introduced by
The function xoops_trim was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        return '' !== /** @scrutinizer ignore-call */ xoops_trim($this->getVar('attribute_default_value'));
Loading history...
88
    }
89
90
    /**
91
     * Retourne le nom du champs tel qu'il est construit dans le formulaire sur la fiche produit
92
     *
93
     * @return string
94
     */
95
    public function getAttributeNameInForm()
96
    {
97
        return $this->getVar('attribute_name') . '_' . $this->getVar('attribute_id');
98
    }
99
100
    /**
101
     * Retourne une option de l'attribut
102
     *
103
     * @param  string $valueToGet
104
     * @param  string $format
105
     * @return array
106
     * @since 2.3.2009.03.11
107
     */
108
    public function getOption($valueToGet, $format = 'e')
109
    {
110
        $names = [];
111
        if ('' !== xoops_trim($this->getVar($valueToGet, $format))) {
0 ignored issues
show
Bug introduced by
The function xoops_trim was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        if ('' !== /** @scrutinizer ignore-call */ xoops_trim($this->getVar($valueToGet, $format))) {
Loading history...
112
            $names = explode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $this->getVar($valueToGet, $format));
113
        }
114
115
        return $names;
116
    }
117
118
    /**
119
     * Retourne le nombre d'options de l'attribut courant
120
     *
121
     * @return int
122
     * @since 2.3.2009.03.12
123
     */
124
    public function getOptionsCount()
125
    {
126
        return substr_count($this->getVar('attribute_names', 's'), Constants::OLEDRION_ATTRIBUTE_SEPARATOR) + 1;
127
    }
128
129
    /**
130
     * Ajout d'une option à l'attribut (soit une option vide soit une option valorisée)
131
     *
132
     * @param  string $name
133
     * @param  string $value
134
     * @param  string $price
135
     * @param  string $stock
136
     * @return bool
137
     * @since 2.3.2009.03.16
138
     */
139
    private function appendOption($name, $value, $price = '', $stock = '')
140
    {
141
        $names  = $values = $prices = $stocks = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $values is dead and can be removed.
Loading history...
Unused Code introduced by
The assignment to $names is dead and can be removed.
Loading history...
142
        $format = 'e';
143
        $names  = $this->getOption('attribute_names', $format);
144
        $values = $this->getOption('attribute_values', $format);
145
        if (Oledrion\Utility::getModuleOption('use_price')) {
146
            $prices = $this->getOption('attribute_prices', $format);
147
        }
148
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
149
            $stocks = $this->getOption('attribute_stocks', $format);
150
        }
151
        $names[]  = $name;
152
        $values[] = $value;
153
        if (Oledrion\Utility::getModuleOption('use_price')) {
154
            $prices[] = $price;
155
        }
156
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
157
            $stocks[] = $stock;
158
        }
159
        $this->setVar('attribute_names', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $names));
160
        $this->setVar('attribute_values', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $values));
161
        if (Oledrion\Utility::getModuleOption('use_price')) {
162
            $this->setVar('attribute_prices', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $prices));
163
        }
164
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
165
            $this->setVar('attribute_stocks', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $stocks));
166
        }
167
168
        return true;
169
    }
170
171
    /**
172
     * Ajoute une option vide à la fin (avec des valeurs par défaut)
173
     *
174
     * @return bool
175
     * @since 2.3.2009.03.12
176
     */
177
    public function addEmptyOption()
178
    {
179
        return $this->appendOption(_AM_OLEDRION_ATTRIBUTE_DEF_VALUE, _AM_OLEDRION_ATTRIBUTE_DEF_VALUE, _AM_OLEDRION_ATTRIBUTE_DEF_AMOUNT, _AM_OLEDRION_ATTRIBUTE_DEF_AMOUNT);
180
    }
181
182
    /**
183
     * Ajoute une nouvelle option à l'attribut
184
     *
185
     * @param  string $name
186
     * @param  string $value
187
     * @param  string $price
188
     * @param  string $stock
189
     * @return bool
190
     * @since 2.3.2009.03.16
191
     */
192
    public function addOption($name, $value, $price = '', $stock = '')
193
    {
194
        return $this->appendOption($name, $value, $price, $stock);
195
    }
196
197
    /**
198
     * Réinitialisation des options de l'attribut
199
     *
200
     * @return bool True
201
     * @since 2.3.2009.03.10
202
     */
203
    public function resetOptions()
204
    {
205
        $this->setVar('attribute_names', Constants::OLEDRION_EMPTY_OPTION);
206
        $this->setVar('attribute_values', Constants::OLEDRION_EMPTY_OPTION);
207
        if (Oledrion\Utility::getModuleOption('use_price')) {
208
            $this->setVar('attribute_prices', Constants::OLEDRION_EMPTY_OPTION);
209
        }
210
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
211
            $this->setVar('attribute_stocks', Constants::OLEDRION_EMPTY_OPTION);
212
        }
213
214
        return true;
215
    }
216
217
    /**
218
     * Renseigne une option
219
     *
220
     * @param int     $optionNumber (de 0 à N)
221
     * @param  string $name         Valeur pour name
222
     * @param  string $value        Valeur pour value
223
     * @param  string $price        Valeur pour prix
224
     * @param  string $stock        Valeur pour stock
225
     * @return bool True si la mise à jour s'est faite sinon false
226
     * @since 2.3.2009.03.10
227
     */
228
    public function setOptionValue($optionNumber, $name, $value, $price = '', $stock = '')
229
    {
230
        $optionNumber = (int)$optionNumber;
231
        if ($optionNumber < 0 || $optionNumber > $this->getOptionsCount()) {
232
            return false;
233
        }
234
        $names   = $values = $prices = $stocks = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $values is dead and can be removed.
Loading history...
235
        $format  = 'e';
236
        $names[] = $this->getOption('attribute_names', $format);
237
        $values  = $this->getOption('attribute_values', $format);
238
        if (Oledrion\Utility::getModuleOption('use_price')) {
239
            $prices = $this->getOption('attribute_prices', $format);
240
        }
241
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
242
            $stocks = $this->getOption('attribute_stocks', $format);
243
        }
244
        if (isset($names[$optionNumber])) {
245
            $names[$optionNumber] = $name;
246
        }
247
        if (isset($values[$optionNumber])) {
248
            $values[$optionNumber] = $value;
249
        }
250
        if (Oledrion\Utility::getModuleOption('use_price')) {
251
            if (isset($prices[$optionNumber])) {
252
                $prices[$optionNumber] = $price;
253
            }
254
        }
255
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
256
            if (isset($stocks[$optionNumber])) {
257
                $stocks[$optionNumber] = $stock;
258
            }
259
        }
260
        $this->setVar('attribute_names', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $names));
261
        $this->setVar('attribute_values', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $values));
262
        if (Oledrion\Utility::getModuleOption('use_price')) {
263
            $this->setVar('attribute_prices', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $prices));
264
        }
265
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
266
            $this->setVar('attribute_stocks', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $stocks));
267
        }
268
269
        return true;
270
    }
271
272
    /**
273
     * Echange deux contenus dans un tableau
274
     *
275
     * @param  array $array
276
     * @param int    $from
277
     * @param int    $to
278
     * @since 2.3.2009.03.10
279
     */
280
    private function swapValues(&$array, $from, $to)
281
    {
282
        $tempValue    = $array[$to];
283
        $array[$to]   = $array[$from];
284
        $array[$from] = $tempValue;
285
    }
286
287
    /**
288
     * Fonction interne chargée du déplacement d'une option soit vers le haut soit vers le bas
289
     *
290
     * @param int $optionNumber
291
     * @param int $upDown 1=Up, 2=Down
292
     * @return bool
293
     * @since 2.3.2009.03.10
294
     */
295
    private function moveOption($optionNumber, $upDown)
296
    {
297
        $prices       = $stocks = [];
298
        $optionNumber = (int)$optionNumber;
299
        if (1 === $upDown) {
300
            // Up
301
            $newPosition = $optionNumber - 1;
302
        } else {
303
            // Down
304
            $newPosition = $optionNumber + 1;
305
        }
306
        if ($optionNumber < 0 || $optionNumber > $this->getOptionsCount()) {
307
            return false;
308
        }
309
        $format = 'e';
310
        $names  = $this->getOption('attribute_names', $format);
311
        $values = $this->getOption('attribute_values', $format);
312
        if (Oledrion\Utility::getModuleOption('use_price')) {
313
            $prices = $this->getOption('attribute_prices', $format);
314
        }
315
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
316
            $stocks = $this->getOption('attribute_stocks', $format);
317
        }
318
        if (isset($names[$optionNumber])) {
319
            $this->swapValues($names, $optionNumber, $newPosition);
320
        }
321
        if (isset($values[$optionNumber])) {
322
            $this->swapValues($values, $optionNumber, $newPosition);
323
        }
324
        if (Oledrion\Utility::getModuleOption('use_price')) {
325
            if (isset($prices[$optionNumber])) {
326
                $this->swapValues($prices, $optionNumber, $newPosition);
327
            }
328
        }
329
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
330
            if (isset($stocks[$optionNumber])) {
331
                $this->swapValues($stocks, $optionNumber, $newPosition);
332
            }
333
        }
334
        $this->setVar('attribute_names', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $names));
335
        $this->setVar('attribute_values', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $values));
336
        if (Oledrion\Utility::getModuleOption('use_price')) {
337
            $this->setVar('attribute_prices', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $prices));
338
        }
339
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
340
            $this->setVar('attribute_stocks', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $stocks));
341
        }
342
343
        return true;
344
    }
345
346
    /**
347
     * Déplace une option vers le haut
348
     *
349
     * @param int $optionNumber
350
     * @return bool
351
     * @since 2.3.2009.03.10
352
     */
353
    public function moveOptionUp($optionNumber)
354
    {
355
        return $this->moveOption($optionNumber, 1);
356
    }
357
358
    /**
359
     * Déplace une option vers le bas
360
     *
361
     * @param int $optionNumber
362
     * @return bool
363
     * @since 2.3.2009.03.10
364
     */
365
    public function moveOptionDown($optionNumber)
366
    {
367
        return $this->moveOption($optionNumber, 2);
368
    }
369
370
    /**
371
     * Supprime une option de l'attribut
372
     *
373
     * @param int $optionNumber (de 0 à n)
374
     * @return bool false si l'indice est hors borne sinon true
375
     * @since 2.3.2009.03.12
376
     */
377
    public function deleteOption($optionNumber)
378
    {
379
        $optionNumber = (int)$optionNumber;
380
        if ($optionNumber < 0 || $optionNumber > $this->getOptionsCount()) {
381
            return false;
382
        }
383
        $format = 'e';
384
        $names  = $this->getOption('attribute_names', $format);
385
        $values = $this->getOption('attribute_values', $format);
386
        if (Oledrion\Utility::getModuleOption('use_price')) {
387
            $prices = $this->getOption('attribute_prices', $format);
388
        }
389
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
390
            $stocks = $this->getOption('attribute_stocks', $format);
391
        }
392
        if (isset($names[$optionNumber])) {
393
            unset($names[$optionNumber]);
394
        }
395
        if (isset($values[$optionNumber])) {
396
            unset($values[$optionNumber]);
397
        }
398
        if (Oledrion\Utility::getModuleOption('use_price')) {
399
            if (isset($prices[$optionNumber])) {
400
                unset($prices[$optionNumber]);
401
            }
402
        }
403
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
404
            if (isset($stocks[$optionNumber])) {
405
                unset($stocks[$optionNumber]);
406
            }
407
        }
408
        $this->setVar('attribute_names', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $names));
409
        $this->setVar('attribute_values', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $values));
410
        if (Oledrion\Utility::getModuleOption('use_price')) {
411
            $this->setVar('attribute_prices', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $prices));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $prices does not seem to be defined for all execution paths leading up to this point.
Loading history...
412
        }
413
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
414
            $this->setVar('attribute_stocks', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $stocks));
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $stocks does not seem to be defined for all execution paths leading up to this point.
Loading history...
415
        }
416
417
        return true;
418
    }
419
420
    /**
421
     * Retourne le prix de l'attribut par défaut
422
     *
423
     * @param  string $format
424
     * @return float
425
     * @since 2.3.2009.03.19
426
     */
427
    public function getDefaultAttributePrice($format = 'e')
428
    {
429
        $defaultValue = xoops_trim($this->getVar('attribute_default_value', 'e'));
0 ignored issues
show
Bug introduced by
The function xoops_trim was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

429
        $defaultValue = /** @scrutinizer ignore-call */ xoops_trim($this->getVar('attribute_default_value', 'e'));
Loading history...
430
        if ('' !== $defaultValue) {
431
            // Il y a une option par défaut donc un prix
432
            $values  = $this->getOption('attribute_values', $format);
433
            $prices  = $this->getOption('attribute_prices', $format);
434
            $counter = 0;
435
            if (count($values) > 0) {
436
                foreach ($values as $value) {
437
                    if (xoops_trim($value) == $defaultValue) {
438
                        if (isset($prices[$counter])) {
439
                            return (float)$prices[$counter];
440
                        }
441
442
                        return 0;
443
                    }
444
                    ++$counter;
445
                }
446
            }
447
        }
448
449
        return 0;
450
    }
451
452
    /**
453
     * Retourne la valeur par défaut de l'attribut courant
454
     *
455
     * @param  string $format
456
     * @return string
457
     * @since 2.3.2009.03.20
458
     */
459
    public function getAttributeDefaultValue($format = 'e')
460
    {
461
        return xoops_trim($this->getVar('attribute_default_value', $format));
0 ignored issues
show
Bug introduced by
The function xoops_trim was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

461
        return /** @scrutinizer ignore-call */ xoops_trim($this->getVar('attribute_default_value', $format));
Loading history...
462
    }
463
464
    /**
465
     * Retourne une liste combinée des options de l'attribut
466
     *
467
     * @param  string   $format             Format dans lequel renvoyer les données
468
     * @param  bool     $withFormatedPrices Faut il retourner les prix formatés ?
469
     * @param  Products $product            Le produit de travail
470
     * @return array
471
     * @since 2.3.2009.03.11
472
     */
473
    public function getAttributeOptions($format = 's', $withFormatedPrices = false, Products $product = null)
474
    {
475
        $ret              = [];
476
        $counter          = 0;
477
        $oledrionCurrency = 0;
478
        $vat_id           = $vat = 0;
479
        if (null !== $product) {
480
            $vat_id = $product->getVar('product_vat_id');
481
        }
482
        $names  = $this->getOption('attribute_names', $format);
483
        $values = $this->getOption('attribute_values', $format);
484
        if (Oledrion\Utility::getModuleOption('use_price')) {
485
            $prices = $this->getOption('attribute_prices', $format);
486
        }
487
        if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
488
            $stocks = $this->getOption('attribute_stocks', $format);
489
        }
490
491
        if ($withFormatedPrices) {
492
            $oledrionCurrency = Oledrion\Currency::getInstance();
493
        }
494
        if (count($names) > 0) {
495
            foreach ($names as $key => $name) {
496
                $price = $stock = 0;
497
                if (Oledrion\Utility::getModuleOption('use_price')) {
498
                    $price = $prices[$key];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $prices does not seem to be defined for all execution paths leading up to this point.
Loading history...
499
                    if ($withFormatedPrices) {
500
                        $priceFormated    = $oledrionCurrency->amountForDisplay($price);
501
                        $priceTtc         = Oledrion\Utility::getAmountWithVat($price, $vat_id);
502
                        $priceTtcFormated = $oledrionCurrency->amountForDisplay($priceTtc);
503
                        $vat              = $priceTtc - $price;
504
                        $vatFormated      = $oledrionCurrency->amountForDisplay($vat);
505
                    }
506
                }
507
                if (Oledrion\Utility::getModuleOption('attributes_stocks')) {
508
                    $stock = $stocks[$key];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $stocks does not seem to be defined for all execution paths leading up to this point.
Loading history...
509
                }
510
                if (!$withFormatedPrices) {
511
                    $ret[] = ['name' => $name, 'value' => $values[$key], 'price' => $price, 'stock' => $stock];
512
                } else {
513
                    $ret[] = [
514
                        'name'             => $name,
515
                        'value'            => $values[$key],
516
                        'price'            => $price,
517
                        'priceFormated'    => $priceFormated,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $priceFormated does not seem to be defined for all execution paths leading up to this point.
Loading history...
518
                        'priceTTC'         => $priceTtc,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $priceTtc does not seem to be defined for all execution paths leading up to this point.
Loading history...
519
                        'priceTTCFormated' => $priceTtcFormated,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $priceTtcFormated does not seem to be defined for all execution paths leading up to this point.
Loading history...
520
                        'vat'              => $vat,
521
                        'vatFormated'      => $vatFormated,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $vatFormated does not seem to be defined for all execution paths leading up to this point.
Loading history...
522
                        'counter'          => $counter,
523
                        'stock'            => $stock,
524
                    ];
525
                }
526
                ++$counter;
527
            }
528
        }
529
530
        return $ret;
531
    }
532
533
    /**
534
     * Retourne la liste des types d'attributs
535
     *
536
     * @return array
537
     * @since 2.3.2009.03.10
538
     */
539
    public function getTypesList()
540
    {
541
        $attributeTypeName = [
542
            Constants::OLEDRION_ATTRIBUTE_RADIO    => _AM_OLEDRION_TYPE_RADIO,
543
            Constants::OLEDRION_ATTRIBUTE_CHECKBOX => _AM_OLEDRION_TYPE_CHECKBOX,
544
            Constants::OLEDRION_ATTRIBUTE_SELECT   => _AM_OLEDRION_TYPE_LIST,
545
        ];
546
547
        return $attributeTypeName;
548
    }
549
550
    /**
551
     * Retourne le type de l'attribut courant (son libellé)
552
     *
553
     * @return mixed Soit le type de l'attribut soit null;
554
     * @since 2.3.2009.03.10
555
     */
556
    public function getTypeName()
557
    {
558
        $attributeTypeName = $this->getTypesList();
559
        if (isset($attributeTypeName[$this->getVar('attribute_type')])) {
560
            return $attributeTypeName[$this->getVar('attribute_type')];
561
        }
562
563
        return null;
564
    }
565
566
    /**
567
     * Retourne le prix d'une option en fonction de son nom
568
     *
569
     * @param  string $optionName
570
     * @return float
571
     */
572
    public function getOptionPriceFromValue($optionName)
573
    {
574
        $ret     = 0;
575
        $format  = 's';
576
        $counter = 0;
577
        $values  = $this->getOption('attribute_values', $format);
578
        $prices  = $this->getOption('attribute_prices', $format);
579
        foreach ($values as $value) {
580
            if (xoops_trim($value) == $optionName) {
0 ignored issues
show
Bug introduced by
The function xoops_trim was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

580
            if (/** @scrutinizer ignore-call */ xoops_trim($value) == $optionName) {
Loading history...
581
                if (isset($prices[$counter])) {
582
                    return (float)$prices[$counter];
583
                }
584
            }
585
            ++$counter;
586
        }
587
588
        return $ret;
589
    }
590
591
    /**
592
     * Retourne le libellé d'une option en fonction de son nom
593
     *
594
     * @param  string $optionName
595
     * @return string
596
     */
597
    public function getOptionNameFromValue($optionName)
598
    {
599
        $ret     = '';
600
        $format  = 's';
601
        $counter = 0;
602
        $values  = $this->getOption('attribute_values', $format);
603
        $names   = $this->getOption('attribute_names', $format);
604
        foreach ($values as $value) {
605
            if (xoops_trim($value) == $optionName) {
0 ignored issues
show
Bug introduced by
The function xoops_trim was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

605
            if (/** @scrutinizer ignore-call */ xoops_trim($value) == $optionName) {
Loading history...
606
                if (isset($names[$counter])) {
607
                    return $names[$counter];
608
                }
609
            }
610
            ++$counter;
611
        }
612
613
        return $ret;
614
    }
615
616
    /**
617
     * Création du code html de l'attribut
618
     *
619
     * On utilise le contenu de templates html (réalisés en Smarty) pour créer le contenu de l'attribut
620
     * Templates utilisés (selon le type d'attribut) :
621
     *      oledrion_attribute_checkbox.html
622
     *      oledrion_attribute_radio.html
623
     *      oledrion_attribute_select.html
624
     *
625
     * @param  Products $product Le produit de "travail"
626
     * @return string                   Le contenu html
627
     * @since 2.3.2009.03.16
628
     */
629
    public function render(Products $product)
630
    {
631
        require_once XOOPS_ROOT_PATH . '/class/template.php';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Oledrion\XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
632
        $template     = new \XoopsTpl();
0 ignored issues
show
Bug introduced by
The type XoopsTpl was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
633
        $db           = \XoopsDatabaseFactory::getDatabaseConnection();
0 ignored issues
show
Bug introduced by
The type XoopsDatabaseFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
634
        $caddyHandler = new Oledrion\CaddyHandler($db);
635
        $delimiter    = '';
636
637
        $options      = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $options is dead and can be removed.
Loading history...
638
        $ret          = $templateName = '';
639
        $elementName  = $this->getVar('attribute_name', 'e');
640
        $elementTitle = $this->getVar('attribute_title');
641
        $option1      = $this->getVar('attribute_option1');
642
        $option2      = $this->getVar('attribute_option2');
643
644
        //        $handlers = HandlerManager::getInstance();
645
        $isInCart = $caddyHandler->isInCart($product->getVar('product_id'));
646
        if (false === $isInCart) {
647
            // Le produit n'est pas dans le panier, on prend la valeur par défaut
648
            $defaultValue = [$this->getVar('attribute_default_value')];
649
        } else {
650
            // Le produit est dans le panier, on va chercher les options qui sont sélectionnées
651
            $Productattributes = $caddyHandler->getProductAttributesFromCart($product->getVar('product_id'));
652
            if (isset($Productattributes[$this->getVar('attribute_id')])) {
653
                $defaultValue = $Productattributes[$this->getVar('attribute_id')];
654
            } else {
655
                // On prend la valeur par défaut
656
                $defaultValue = [$this->getVar('attribute_default_value')];
657
                if (Constants::OLEDRION_ATTRIBUTE_RADIO == $this->getVar('attribute_type')) {
658
                    // Pour les boutons radio, il ne peut y avoir qu'un élément de sélectionné
659
                    $defaultValue = $this->getVar('attribute_default_value');
660
                }
661
            }
662
            if (!is_array($defaultValue)) {
663
                $defaultValue = [$defaultValue];
664
            }
665
            $newDefaultValue = [];
666
            foreach ($defaultValue as $oneValue) {
667
                $newDefaultValue[] = Oledrion\Utility::getName($oneValue);
668
            }
669
            $defaultValue = $newDefaultValue;
670
        }
671
        $options = $this->getAttributeOptions('s', true, $product);
672
673
        // Les valeurs communes
674
        $template->assign('options', $options);
675
        $template->assign('attributeTitle', $elementTitle);
676
        $template->assign('defaultValue', $defaultValue);
677
        $template->assign('attributeName', $this->getVar('attribute_title'));
678
        $template->assign('name', $elementName);
679
        $template->assign('attribute_id', $this->getVar('attribute_id'));
680
        $template->assign('attribute_mandatory', (bool)$this->getVar('attribute_mandatory'));
681
682
        switch ($this->getVar('attribute_type')) {
683
            case Constants::OLEDRION_ATTRIBUTE_SELECT:        // Liste déroulante
684
685
                $templateName = 'oledrion_attribute_select.tpl';
686
                $multiple     = '';
687
                if (1 == $option2) {
688
                    // La sélection multiple est autorisée
689
                    $multiple = "multiple='multiple' ";
690
                }
691
                $template->assign('multiple', $multiple);
692
                $template->assign('size', $option1);
693
694
                break;
695
            case Constants::OLEDRION_ATTRIBUTE_CHECKBOX:      // Cases à cocher
696
697
                $templateName = 'oledrion_attribute_checkbox.tpl';
698
                $delimiter    = '<br>';
699
                if (Constants::OLEDRION_ATTRIBUTE_CHECKBOX_WHITE_SPACE == $option1) {
700
                    $delimiter = ' ';
701
                }
702
                $template->assign('delimiter', $delimiter);
703
704
                break;
705
            case Constants::OLEDRION_ATTRIBUTE_RADIO:         // Boutons radio
706
707
                $templateName = 'oledrion_attribute_radio.tpl';
708
                //                $delimiter    = '';
709
                $delimiter = '<br>';
710
                if (Constants::OLEDRION_ATTRIBUTE_CHECKBOX_WHITE_SPACE == $option1) {
711
                    $delimiter = ' ';
712
                }
713
                $template->assign('delimiter', $delimiter);
714
715
                break;
716
        }
717
        if ('' !== $templateName) {
718
            $ret = $template->fetch('db:' . $templateName);
719
        }
720
721
        return $ret;
722
    }
723
}
724