XoopsModules25x /
oledrion
| 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 | //// Les types d'option |
||||
| 34 | //define('OLEDRION_ATTRIBUTE_RADIO', 1); |
||||
| 35 | //define('OLEDRION_ATTRIBUTE_CHECKBOX', 2); |
||||
| 36 | //define('OLEDRION_ATTRIBUTE_SELECT', 3); |
||||
| 37 | // |
||||
| 38 | //// Le séparateur de données utilisé en interne |
||||
| 39 | //define('OLEDRION_ATTRIBUTE_SEPARATOR', '|'); |
||||
| 40 | //define('OLEDRION_EMPTY_OPTION', ''); |
||||
| 41 | // |
||||
| 42 | //// Le séparateur de ligne lorsque l'option est un bouton radio ou des cases à cocher |
||||
| 43 | //define('OLEDRION_ATTRIBUTE_CHECKBOX_WHITE_SPACE', 1); // Séparateur de ligne = espace blanc |
||||
| 44 | //define('OLEDRION_ATTRIBUTE_CHECKBOX_NEW_LINE', 2); // Séparateur de ligne = retour à la ligne |
||||
| 45 | // |
||||
| 46 | //// Les options par défaut lorsque l'option est une liste déroulante |
||||
| 47 | //define('OLEDRION_ATTRIBUTE_SELECT_VISIBLE_OPTIONS', 1); // Valeur par défaut, nombre d'options visibles |
||||
| 48 | //define('OLEDRION_ATTRIBUTE_SELECT_MULTIPLE', false); // Valeur par défaut, sélecteur multiple ? |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * Class Oledrion_attributes |
||||
| 52 | */ |
||||
| 53 | class Attributes extends OledrionObject |
||||
| 54 | { |
||||
| 55 | /** |
||||
| 56 | * constructor |
||||
| 57 | * |
||||
| 58 | * normally, this is called from child classes only |
||||
| 59 | */ |
||||
| 60 | public function __construct() |
||||
| 61 | { |
||||
| 62 | $this->initVar('attribute_id', XOBJ_DTYPE_INT, null, false); |
||||
| 63 | $this->initVar('attribute_weight', XOBJ_DTYPE_INT, null, false); |
||||
| 64 | $this->initVar('attribute_title', XOBJ_DTYPE_TXTBOX, null, false); |
||||
| 65 | $this->initVar('attribute_name', XOBJ_DTYPE_TXTBOX, null, false); |
||||
| 66 | $this->initVar('attribute_type', XOBJ_DTYPE_INT, null, false); |
||||
| 67 | $this->initVar('attribute_mandatory', XOBJ_DTYPE_INT, null, false); |
||||
| 68 | $this->initVar('attribute_names', XOBJ_DTYPE_OTHER, null, false); |
||||
| 69 | $this->initVar('attribute_values', XOBJ_DTYPE_OTHER, null, false); |
||||
| 70 | $this->initVar('attribute_prices', XOBJ_DTYPE_OTHER, null, false); |
||||
| 71 | $this->initVar('attribute_stocks', XOBJ_DTYPE_OTHER, null, false); |
||||
| 72 | $this->initVar('attribute_product_id', XOBJ_DTYPE_INT, null, false); |
||||
| 73 | $this->initVar('attribute_default_value', XOBJ_DTYPE_TXTBOX, null, false); |
||||
| 74 | $this->initVar('attribute_option1', XOBJ_DTYPE_INT, null, false); |
||||
| 75 | $this->initVar('attribute_option2', XOBJ_DTYPE_INT, null, false); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | /** |
||||
| 79 | * Indique si l'attribut courant a une valeur par défaut |
||||
| 80 | * |
||||
| 81 | * @return bool |
||||
| 82 | * @since 2.3.2009.03.20 |
||||
| 83 | */ |
||||
| 84 | public function hasDefaultValue() |
||||
| 85 | { |
||||
| 86 | return '' !== xoops_trim($this->getVar('attribute_default_value')); |
||||
| 87 | } |
||||
| 88 | |||||
| 89 | /** |
||||
| 90 | * Retourne le nom du champs tel qu'il est construit dans le formulaire sur la fiche produit |
||||
| 91 | * |
||||
| 92 | * @return string |
||||
| 93 | */ |
||||
| 94 | public function getAttributeNameInForm() |
||||
| 95 | { |
||||
| 96 | return $this->getVar('attribute_name') . '_' . $this->getVar('attribute_id'); |
||||
| 97 | } |
||||
| 98 | |||||
| 99 | /** |
||||
| 100 | * Retourne une option de l'attribut |
||||
| 101 | * |
||||
| 102 | * @param string $valueToGet |
||||
| 103 | * @param string $format |
||||
| 104 | * @return array |
||||
| 105 | * @since 2.3.2009.03.11 |
||||
| 106 | */ |
||||
| 107 | public function getOption($valueToGet, $format = 'e') |
||||
| 108 | { |
||||
| 109 | $names = []; |
||||
| 110 | if ('' !== xoops_trim($this->getVar($valueToGet, $format))) { |
||||
| 111 | $names = explode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $this->getVar($valueToGet, $format)); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 112 | } |
||||
| 113 | |||||
| 114 | return $names; |
||||
| 115 | } |
||||
| 116 | |||||
| 117 | /** |
||||
| 118 | * Retourne le nombre d'options de l'attribut courant |
||||
| 119 | * |
||||
| 120 | * @return int |
||||
| 121 | * @since 2.3.2009.03.12 |
||||
| 122 | */ |
||||
| 123 | public function getOptionsCount() |
||||
| 124 | { |
||||
| 125 | return mb_substr_count($this->getVar('attribute_names', 's'), Constants::OLEDRION_ATTRIBUTE_SEPARATOR) + 1; |
||||
|
0 ignored issues
–
show
It seems like
$this->getVar('attribute_names', 's') can also be of type array and array; however, parameter $haystack of mb_substr_count() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 126 | } |
||||
| 127 | |||||
| 128 | /** |
||||
| 129 | * Ajout d'une option à l'attribut (soit une option vide soit une option valorisée) |
||||
| 130 | * |
||||
| 131 | * @param string $name |
||||
| 132 | * @param string $value |
||||
| 133 | * @param string $price |
||||
| 134 | * @param string $stock |
||||
| 135 | * @return bool |
||||
| 136 | * @since 2.3.2009.03.16 |
||||
| 137 | */ |
||||
| 138 | private function appendOption($name, $value, $price = '', $stock = '') |
||||
| 139 | { |
||||
| 140 | $names = $values = $prices = $stocks = []; |
||||
| 141 | $format = 'e'; |
||||
| 142 | $names = $this->getOption('attribute_names', $format); |
||||
| 143 | $values = $this->getOption('attribute_values', $format); |
||||
| 144 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 145 | $prices = $this->getOption('attribute_prices', $format); |
||||
| 146 | } |
||||
| 147 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 148 | $stocks = $this->getOption('attribute_stocks', $format); |
||||
| 149 | } |
||||
| 150 | $names[] = $name; |
||||
| 151 | $values[] = $value; |
||||
| 152 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 153 | $prices[] = $price; |
||||
| 154 | } |
||||
| 155 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 156 | $stocks[] = $stock; |
||||
| 157 | } |
||||
| 158 | $this->setVar('attribute_names', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $names)); |
||||
| 159 | $this->setVar('attribute_values', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $values)); |
||||
| 160 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 161 | $this->setVar('attribute_prices', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $prices)); |
||||
| 162 | } |
||||
| 163 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 164 | $this->setVar('attribute_stocks', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $stocks)); |
||||
| 165 | } |
||||
| 166 | |||||
| 167 | return true; |
||||
| 168 | } |
||||
| 169 | |||||
| 170 | /** |
||||
| 171 | * Ajoute une option vide à la fin (avec des valeurs par défaut) |
||||
| 172 | * |
||||
| 173 | * @return bool |
||||
| 174 | * @since 2.3.2009.03.12 |
||||
| 175 | */ |
||||
| 176 | public function addEmptyOption() |
||||
| 177 | { |
||||
| 178 | return $this->appendOption(_AM_OLEDRION_ATTRIBUTE_DEF_VALUE, _AM_OLEDRION_ATTRIBUTE_DEF_VALUE, _AM_OLEDRION_ATTRIBUTE_DEF_AMOUNT, _AM_OLEDRION_ATTRIBUTE_DEF_AMOUNT); |
||||
| 179 | } |
||||
| 180 | |||||
| 181 | /** |
||||
| 182 | * Ajoute une nouvelle option à l'attribut |
||||
| 183 | * |
||||
| 184 | * @param string $name |
||||
| 185 | * @param string $value |
||||
| 186 | * @param string $price |
||||
| 187 | * @param string $stock |
||||
| 188 | * @return bool |
||||
| 189 | * @since 2.3.2009.03.16 |
||||
| 190 | */ |
||||
| 191 | public function addOption($name, $value, $price = '', $stock = '') |
||||
| 192 | { |
||||
| 193 | return $this->appendOption($name, $value, $price, $stock); |
||||
| 194 | } |
||||
| 195 | |||||
| 196 | /** |
||||
| 197 | * Réinitialisation des options de l'attribut |
||||
| 198 | * |
||||
| 199 | * @return bool True |
||||
| 200 | * @since 2.3.2009.03.10 |
||||
| 201 | */ |
||||
| 202 | public function resetOptions() |
||||
| 203 | { |
||||
| 204 | $this->setVar('attribute_names', Constants::OLEDRION_EMPTY_OPTION); |
||||
| 205 | $this->setVar('attribute_values', Constants::OLEDRION_EMPTY_OPTION); |
||||
| 206 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 207 | $this->setVar('attribute_prices', Constants::OLEDRION_EMPTY_OPTION); |
||||
| 208 | } |
||||
| 209 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 210 | $this->setVar('attribute_stocks', Constants::OLEDRION_EMPTY_OPTION); |
||||
| 211 | } |
||||
| 212 | |||||
| 213 | return true; |
||||
| 214 | } |
||||
| 215 | |||||
| 216 | /** |
||||
| 217 | * Renseigne une option |
||||
| 218 | * |
||||
| 219 | * @param int $optionNumber (de 0 à N) |
||||
| 220 | * @param string $name Valeur pour name |
||||
| 221 | * @param string $value Valeur pour value |
||||
| 222 | * @param string $price Valeur pour prix |
||||
| 223 | * @param string $stock Valeur pour stock |
||||
| 224 | * @return bool True si la mise à jour s'est faite sinon false |
||||
| 225 | * @since 2.3.2009.03.10 |
||||
| 226 | */ |
||||
| 227 | public function setOptionValue($optionNumber, $name, $value, $price = '', $stock = '') |
||||
| 228 | { |
||||
| 229 | $optionNumber = (int)$optionNumber; |
||||
| 230 | if ($optionNumber < 0 || $optionNumber > $this->getOptionsCount()) { |
||||
| 231 | return false; |
||||
| 232 | } |
||||
| 233 | $names = $values = $prices = $stocks = []; |
||||
| 234 | $format = 'e'; |
||||
| 235 | $names[] = $this->getOption('attribute_names', $format); |
||||
| 236 | $values = $this->getOption('attribute_values', $format); |
||||
| 237 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 238 | $prices = $this->getOption('attribute_prices', $format); |
||||
| 239 | } |
||||
| 240 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 241 | $stocks = $this->getOption('attribute_stocks', $format); |
||||
| 242 | } |
||||
| 243 | if (isset($names[$optionNumber])) { |
||||
| 244 | $names[$optionNumber] = $name; |
||||
| 245 | } |
||||
| 246 | if (isset($values[$optionNumber])) { |
||||
| 247 | $values[$optionNumber] = $value; |
||||
| 248 | } |
||||
| 249 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 250 | if (isset($prices[$optionNumber])) { |
||||
| 251 | $prices[$optionNumber] = $price; |
||||
| 252 | } |
||||
| 253 | } |
||||
| 254 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 255 | if (isset($stocks[$optionNumber])) { |
||||
| 256 | $stocks[$optionNumber] = $stock; |
||||
| 257 | } |
||||
| 258 | } |
||||
| 259 | $this->setVar('attribute_names', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $names)); |
||||
| 260 | $this->setVar('attribute_values', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $values)); |
||||
| 261 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 262 | $this->setVar('attribute_prices', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $prices)); |
||||
| 263 | } |
||||
| 264 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 265 | $this->setVar('attribute_stocks', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $stocks)); |
||||
| 266 | } |
||||
| 267 | |||||
| 268 | return true; |
||||
| 269 | } |
||||
| 270 | |||||
| 271 | /** |
||||
| 272 | * Echange deux contenus dans un tableau |
||||
| 273 | * |
||||
| 274 | * @param array $array |
||||
| 275 | * @param int $from |
||||
| 276 | * @param int $to |
||||
| 277 | * @since 2.3.2009.03.10 |
||||
| 278 | */ |
||||
| 279 | private function swapValues(&$array, $from, $to) |
||||
| 280 | { |
||||
| 281 | $tempValue = $array[$to]; |
||||
| 282 | $array[$to] = $array[$from]; |
||||
| 283 | $array[$from] = $tempValue; |
||||
| 284 | } |
||||
| 285 | |||||
| 286 | /** |
||||
| 287 | * Fonction interne chargée du déplacement d'une option soit vers le haut soit vers le bas |
||||
| 288 | * |
||||
| 289 | * @param int $optionNumber |
||||
| 290 | * @param int $upDown 1=Up, 2=Down |
||||
| 291 | * @return bool |
||||
| 292 | * @since 2.3.2009.03.10 |
||||
| 293 | */ |
||||
| 294 | private function moveOption($optionNumber, $upDown) |
||||
| 295 | { |
||||
| 296 | $prices = $stocks = []; |
||||
| 297 | $optionNumber = (int)$optionNumber; |
||||
| 298 | if (1 === $upDown) { |
||||
| 299 | // Up |
||||
| 300 | $newPosition = $optionNumber - 1; |
||||
| 301 | } else { |
||||
| 302 | // Down |
||||
| 303 | $newPosition = $optionNumber + 1; |
||||
| 304 | } |
||||
| 305 | if ($optionNumber < 0 || $optionNumber > $this->getOptionsCount()) { |
||||
| 306 | return false; |
||||
| 307 | } |
||||
| 308 | $format = 'e'; |
||||
| 309 | $names = $this->getOption('attribute_names', $format); |
||||
| 310 | $values = $this->getOption('attribute_values', $format); |
||||
| 311 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 312 | $prices = $this->getOption('attribute_prices', $format); |
||||
| 313 | } |
||||
| 314 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 315 | $stocks = $this->getOption('attribute_stocks', $format); |
||||
| 316 | } |
||||
| 317 | if (isset($names[$optionNumber])) { |
||||
| 318 | $this->swapValues($names, $optionNumber, $newPosition); |
||||
| 319 | } |
||||
| 320 | if (isset($values[$optionNumber])) { |
||||
| 321 | $this->swapValues($values, $optionNumber, $newPosition); |
||||
| 322 | } |
||||
| 323 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 324 | if (isset($prices[$optionNumber])) { |
||||
| 325 | $this->swapValues($prices, $optionNumber, $newPosition); |
||||
| 326 | } |
||||
| 327 | } |
||||
| 328 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 329 | if (isset($stocks[$optionNumber])) { |
||||
| 330 | $this->swapValues($stocks, $optionNumber, $newPosition); |
||||
| 331 | } |
||||
| 332 | } |
||||
| 333 | $this->setVar('attribute_names', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $names)); |
||||
| 334 | $this->setVar('attribute_values', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $values)); |
||||
| 335 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 336 | $this->setVar('attribute_prices', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $prices)); |
||||
| 337 | } |
||||
| 338 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 339 | $this->setVar('attribute_stocks', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $stocks)); |
||||
| 340 | } |
||||
| 341 | |||||
| 342 | return true; |
||||
| 343 | } |
||||
| 344 | |||||
| 345 | /** |
||||
| 346 | * Déplace une option vers le haut |
||||
| 347 | * |
||||
| 348 | * @param int $optionNumber |
||||
| 349 | * @return bool |
||||
| 350 | * @since 2.3.2009.03.10 |
||||
| 351 | */ |
||||
| 352 | public function moveOptionUp($optionNumber) |
||||
| 353 | { |
||||
| 354 | return $this->moveOption($optionNumber, 1); |
||||
| 355 | } |
||||
| 356 | |||||
| 357 | /** |
||||
| 358 | * Déplace une option vers le bas |
||||
| 359 | * |
||||
| 360 | * @param int $optionNumber |
||||
| 361 | * @return bool |
||||
| 362 | * @since 2.3.2009.03.10 |
||||
| 363 | */ |
||||
| 364 | public function moveOptionDown($optionNumber) |
||||
| 365 | { |
||||
| 366 | return $this->moveOption($optionNumber, 2); |
||||
| 367 | } |
||||
| 368 | |||||
| 369 | /** |
||||
| 370 | * Supprime une option de l'attribut |
||||
| 371 | * |
||||
| 372 | * @param int $optionNumber (de 0 à n) |
||||
| 373 | * @return bool false si l'indice est hors borne sinon true |
||||
| 374 | * @since 2.3.2009.03.12 |
||||
| 375 | */ |
||||
| 376 | public function deleteOption($optionNumber) |
||||
| 377 | { |
||||
| 378 | $optionNumber = (int)$optionNumber; |
||||
| 379 | if ($optionNumber < 0 || $optionNumber > $this->getOptionsCount()) { |
||||
| 380 | return false; |
||||
| 381 | } |
||||
| 382 | $format = 'e'; |
||||
| 383 | $names = $this->getOption('attribute_names', $format); |
||||
| 384 | $values = $this->getOption('attribute_values', $format); |
||||
| 385 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 386 | $prices = $this->getOption('attribute_prices', $format); |
||||
| 387 | } |
||||
| 388 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 389 | $stocks = $this->getOption('attribute_stocks', $format); |
||||
| 390 | } |
||||
| 391 | if (isset($names[$optionNumber])) { |
||||
| 392 | unset($names[$optionNumber]); |
||||
| 393 | } |
||||
| 394 | if (isset($values[$optionNumber])) { |
||||
| 395 | unset($values[$optionNumber]); |
||||
| 396 | } |
||||
| 397 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 398 | if (isset($prices[$optionNumber])) { |
||||
| 399 | unset($prices[$optionNumber]); |
||||
| 400 | } |
||||
| 401 | } |
||||
| 402 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 403 | if (isset($stocks[$optionNumber])) { |
||||
| 404 | unset($stocks[$optionNumber]); |
||||
| 405 | } |
||||
| 406 | } |
||||
| 407 | $this->setVar('attribute_names', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $names)); |
||||
| 408 | $this->setVar('attribute_values', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $values)); |
||||
| 409 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 410 | $this->setVar('attribute_prices', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $prices)); |
||||
| 411 | } |
||||
| 412 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 413 | $this->setVar('attribute_stocks', implode(Constants::OLEDRION_ATTRIBUTE_SEPARATOR, $stocks)); |
||||
| 414 | } |
||||
| 415 | |||||
| 416 | return true; |
||||
| 417 | } |
||||
| 418 | |||||
| 419 | /** |
||||
| 420 | * Retourne le prix de l'attribut par défaut |
||||
| 421 | * |
||||
| 422 | * @param string $format |
||||
| 423 | * @return float |
||||
| 424 | * @since 2.3.2009.03.19 |
||||
| 425 | */ |
||||
| 426 | public function getDefaultAttributePrice($format = 'e') |
||||
| 427 | { |
||||
| 428 | $defaultValue = xoops_trim($this->getVar('attribute_default_value', 'e')); |
||||
| 429 | if ('' !== $defaultValue) { |
||||
| 430 | // Il y a une option par défaut donc un prix |
||||
| 431 | $values = $this->getOption('attribute_values', $format); |
||||
| 432 | $prices = $this->getOption('attribute_prices', $format); |
||||
| 433 | $counter = 0; |
||||
| 434 | if (count($values) > 0) { |
||||
| 435 | foreach ($values as $value) { |
||||
| 436 | if (xoops_trim($value) == $defaultValue) { |
||||
| 437 | if (isset($prices[$counter])) { |
||||
| 438 | return (float)$prices[$counter]; |
||||
| 439 | } |
||||
| 440 | |||||
| 441 | return 0; |
||||
| 442 | } |
||||
| 443 | ++$counter; |
||||
| 444 | } |
||||
| 445 | } |
||||
| 446 | } |
||||
| 447 | |||||
| 448 | return 0; |
||||
| 449 | } |
||||
| 450 | |||||
| 451 | /** |
||||
| 452 | * Retourne la valeur par défaut de l'attribut courant |
||||
| 453 | * |
||||
| 454 | * @param string $format |
||||
| 455 | * @return string |
||||
| 456 | * @since 2.3.2009.03.20 |
||||
| 457 | */ |
||||
| 458 | public function getAttributeDefaultValue($format = 'e') |
||||
| 459 | { |
||||
| 460 | return xoops_trim($this->getVar('attribute_default_value', $format)); |
||||
| 461 | } |
||||
| 462 | |||||
| 463 | /** |
||||
| 464 | * Retourne une liste combinée des options de l'attribut |
||||
| 465 | * |
||||
| 466 | * @param string $format Format dans lequel renvoyer les données |
||||
| 467 | * @param bool $withFormatedPrices Faut il retourner les prix formatés ? |
||||
| 468 | * @param Products $product Le produit de travail |
||||
| 469 | * @return array |
||||
| 470 | * @since 2.3.2009.03.11 |
||||
| 471 | */ |
||||
| 472 | public function getAttributeOptions($format = 's', $withFormatedPrices = false, Products $product = null) |
||||
| 473 | { |
||||
| 474 | $ret = []; |
||||
| 475 | $counter = 0; |
||||
| 476 | $oledrionCurrency = 0; |
||||
| 477 | $vat_id = $vat = 0; |
||||
| 478 | if (null !== $product) { |
||||
| 479 | $vat_id = $product->getVar('product_vat_id'); |
||||
| 480 | } |
||||
| 481 | $names = $this->getOption('attribute_names', $format); |
||||
| 482 | $values = $this->getOption('attribute_values', $format); |
||||
| 483 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 484 | $prices = $this->getOption('attribute_prices', $format); |
||||
| 485 | } |
||||
| 486 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 487 | $stocks = $this->getOption('attribute_stocks', $format); |
||||
| 488 | } |
||||
| 489 | |||||
| 490 | if ($withFormatedPrices) { |
||||
| 491 | $oledrionCurrency = Oledrion\Currency::getInstance(); |
||||
| 492 | } |
||||
| 493 | if (count($names) > 0) { |
||||
| 494 | foreach ($names as $key => $name) { |
||||
| 495 | $price = $stock = 0; |
||||
| 496 | if (Oledrion\Utility::getModuleOption('use_price')) { |
||||
| 497 | $price = $prices[$key]; |
||||
| 498 | if ($withFormatedPrices) { |
||||
| 499 | $priceFormated = $oledrionCurrency->amountForDisplay($price); |
||||
| 500 | $priceTtc = Oledrion\Utility::getAmountWithVat($price, $vat_id); |
||||
| 501 | $priceTtcFormated = $oledrionCurrency->amountForDisplay($priceTtc); |
||||
| 502 | $vat = $priceTtc - $price; |
||||
| 503 | $vatFormated = $oledrionCurrency->amountForDisplay($vat); |
||||
| 504 | } |
||||
| 505 | } |
||||
| 506 | if (Oledrion\Utility::getModuleOption('attributes_stocks')) { |
||||
| 507 | $stock = $stocks[$key]; |
||||
| 508 | } |
||||
| 509 | if (!$withFormatedPrices) { |
||||
| 510 | $ret[] = ['name' => $name, 'value' => $values[$key], 'price' => $price, 'stock' => $stock]; |
||||
| 511 | } else { |
||||
| 512 | $ret[] = [ |
||||
| 513 | 'name' => $name, |
||||
| 514 | 'value' => $values[$key], |
||||
| 515 | 'price' => $price, |
||||
| 516 | 'priceFormated' => $priceFormated, |
||||
| 517 | 'priceTTC' => $priceTtc, |
||||
| 518 | 'priceTTCFormated' => $priceTtcFormated, |
||||
| 519 | 'vat' => $vat, |
||||
| 520 | 'vatFormated' => $vatFormated, |
||||
| 521 | 'counter' => $counter, |
||||
| 522 | 'stock' => $stock, |
||||
| 523 | ]; |
||||
| 524 | } |
||||
| 525 | ++$counter; |
||||
| 526 | } |
||||
| 527 | } |
||||
| 528 | |||||
| 529 | return $ret; |
||||
| 530 | } |
||||
| 531 | |||||
| 532 | /** |
||||
| 533 | * Retourne la liste des types d'attributs |
||||
| 534 | * |
||||
| 535 | * @return array |
||||
| 536 | * @since 2.3.2009.03.10 |
||||
| 537 | */ |
||||
| 538 | public function getTypesList() |
||||
| 539 | { |
||||
| 540 | $attributeTypeName = [ |
||||
| 541 | Constants::OLEDRION_ATTRIBUTE_RADIO => _AM_OLEDRION_TYPE_RADIO, |
||||
| 542 | Constants::OLEDRION_ATTRIBUTE_CHECKBOX => _AM_OLEDRION_TYPE_CHECKBOX, |
||||
| 543 | Constants::OLEDRION_ATTRIBUTE_SELECT => _AM_OLEDRION_TYPE_LIST, |
||||
| 544 | ]; |
||||
| 545 | |||||
| 546 | return $attributeTypeName; |
||||
| 547 | } |
||||
| 548 | |||||
| 549 | /** |
||||
| 550 | * Retourne le type de l'attribut courant (son libellé) |
||||
| 551 | * |
||||
| 552 | * @return mixed Soit le type de l'attribut soit null; |
||||
| 553 | * @since 2.3.2009.03.10 |
||||
| 554 | */ |
||||
| 555 | public function getTypeName() |
||||
| 556 | { |
||||
| 557 | $attributeTypeName = $this->getTypesList(); |
||||
| 558 | if (isset($attributeTypeName[$this->getVar('attribute_type')])) { |
||||
| 559 | return $attributeTypeName[$this->getVar('attribute_type')]; |
||||
| 560 | } |
||||
| 561 | |||||
| 562 | return null; |
||||
| 563 | } |
||||
| 564 | |||||
| 565 | /** |
||||
| 566 | * Retourne le prix d'une option en fonction de son nom |
||||
| 567 | * |
||||
| 568 | * @param string $optionName |
||||
| 569 | * @return float |
||||
| 570 | */ |
||||
| 571 | public function getOptionPriceFromValue($optionName) |
||||
| 572 | { |
||||
| 573 | $ret = 0; |
||||
| 574 | $format = 's'; |
||||
| 575 | $counter = 0; |
||||
| 576 | $values = $this->getOption('attribute_values', $format); |
||||
| 577 | $prices = $this->getOption('attribute_prices', $format); |
||||
| 578 | foreach ($values as $value) { |
||||
| 579 | if (xoops_trim($value) == $optionName) { |
||||
| 580 | if (isset($prices[$counter])) { |
||||
| 581 | return (float)$prices[$counter]; |
||||
| 582 | } |
||||
| 583 | } |
||||
| 584 | ++$counter; |
||||
| 585 | } |
||||
| 586 | |||||
| 587 | return $ret; |
||||
| 588 | } |
||||
| 589 | |||||
| 590 | /** |
||||
| 591 | * Retourne le libellé d'une option en fonction de son nom |
||||
| 592 | * |
||||
| 593 | * @param string $optionName |
||||
| 594 | * @return string |
||||
| 595 | */ |
||||
| 596 | public function getOptionNameFromValue($optionName) |
||||
| 597 | { |
||||
| 598 | $ret = ''; |
||||
| 599 | $format = 's'; |
||||
| 600 | $counter = 0; |
||||
| 601 | $values = $this->getOption('attribute_values', $format); |
||||
| 602 | $names = $this->getOption('attribute_names', $format); |
||||
| 603 | foreach ($values as $value) { |
||||
| 604 | if (xoops_trim($value) == $optionName) { |
||||
| 605 | if (isset($names[$counter])) { |
||||
| 606 | return $names[$counter]; |
||||
| 607 | } |
||||
| 608 | } |
||||
| 609 | ++$counter; |
||||
| 610 | } |
||||
| 611 | |||||
| 612 | return $ret; |
||||
| 613 | } |
||||
| 614 | |||||
| 615 | /** |
||||
| 616 | * Création du code html de l'attribut |
||||
| 617 | * |
||||
| 618 | * On utilise le contenu de templates html (réalisés en Smarty) pour créer le contenu de l'attribut |
||||
| 619 | * Templates utilisés (selon le type d'attribut) : |
||||
| 620 | * oledrion_attribute_checkbox.html |
||||
| 621 | * oledrion_attribute_radio.html |
||||
| 622 | * oledrion_attribute_select.html |
||||
| 623 | * |
||||
| 624 | * @param Products $product Le produit de "travail" |
||||
| 625 | * @return string Le contenu html |
||||
| 626 | * @since 2.3.2009.03.16 |
||||
| 627 | */ |
||||
| 628 | public function render(Products $product) |
||||
| 629 | { |
||||
| 630 | require_once XOOPS_ROOT_PATH . '/class/template.php'; |
||||
| 631 | $template = new \XoopsTpl(); |
||||
| 632 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||||
| 633 | $caddyHandler = new Oledrion\CaddyHandler($db); |
||||
| 634 | $delimiter = ''; |
||||
| 635 | |||||
| 636 | $options = []; |
||||
| 637 | $ret = $templateName = ''; |
||||
| 638 | $elementName = $this->getVar('attribute_name', 'e'); |
||||
| 639 | $elementTitle = $this->getVar('attribute_title'); |
||||
| 640 | $option1 = $this->getVar('attribute_option1'); |
||||
| 641 | $option2 = $this->getVar('attribute_option2'); |
||||
| 642 | |||||
| 643 | // $handlers = HandlerManager::getInstance(); |
||||
| 644 | $isInCart = $caddyHandler->isInCart($product->getVar('product_id')); |
||||
| 645 | if (false === $isInCart) { |
||||
| 646 | // Le produit n'est pas dans le panier, on prend la valeur par défaut |
||||
| 647 | $defaultValue = [$this->getVar('attribute_default_value')]; |
||||
| 648 | } else { |
||||
| 649 | // Le produit est dans le panier, on va chercher les options qui sont sélectionnées |
||||
| 650 | $Productattributes = $caddyHandler->getProductAttributesFromCart($product->getVar('product_id')); |
||||
| 651 | if (isset($Productattributes[$this->getVar('attribute_id')])) { |
||||
| 652 | $defaultValue = $Productattributes[$this->getVar('attribute_id')]; |
||||
| 653 | } else { |
||||
| 654 | // On prend la valeur par défaut |
||||
| 655 | $defaultValue = [$this->getVar('attribute_default_value')]; |
||||
| 656 | if (Constants::OLEDRION_ATTRIBUTE_RADIO == $this->getVar('attribute_type')) { |
||||
| 657 | // Pour les boutons radio, il ne peut y avoir qu'un élément de sélectionné |
||||
| 658 | $defaultValue = $this->getVar('attribute_default_value'); |
||||
| 659 | } |
||||
| 660 | } |
||||
| 661 | if (!is_array($defaultValue)) { |
||||
| 662 | $defaultValue = [$defaultValue]; |
||||
| 663 | } |
||||
| 664 | $newDefaultValue = []; |
||||
| 665 | foreach ($defaultValue as $oneValue) { |
||||
| 666 | $newDefaultValue[] = Oledrion\Utility::getName($oneValue); |
||||
| 667 | } |
||||
| 668 | $defaultValue = $newDefaultValue; |
||||
| 669 | } |
||||
| 670 | $options = $this->getAttributeOptions('s', true, $product); |
||||
| 671 | |||||
| 672 | // Les valeurs communes |
||||
| 673 | $template->assign('options', $options); |
||||
| 674 | $template->assign('attributeTitle', $elementTitle); |
||||
| 675 | $template->assign('defaultValue', $defaultValue); |
||||
| 676 | $template->assign('attributeName', $this->getVar('attribute_title')); |
||||
| 677 | $template->assign('name', $elementName); |
||||
| 678 | $template->assign('attribute_id', $this->getVar('attribute_id')); |
||||
| 679 | $template->assign('attribute_mandatory', (bool)$this->getVar('attribute_mandatory')); |
||||
| 680 | |||||
| 681 | switch ($this->getVar('attribute_type')) { |
||||
| 682 | case Constants::OLEDRION_ATTRIBUTE_SELECT: // Liste déroulante |
||||
| 683 | |||||
| 684 | $templateName = 'oledrion_attribute_select.tpl'; |
||||
| 685 | $multiple = ''; |
||||
| 686 | if (1 == $option2) { |
||||
| 687 | // La sélection multiple est autorisée |
||||
| 688 | $multiple = "multiple='multiple' "; |
||||
| 689 | } |
||||
| 690 | $template->assign('multiple', $multiple); |
||||
| 691 | $template->assign('size', $option1); |
||||
| 692 | |||||
| 693 | break; |
||||
| 694 | case Constants::OLEDRION_ATTRIBUTE_CHECKBOX: // Cases à cocher |
||||
| 695 | |||||
| 696 | $templateName = 'oledrion_attribute_checkbox.tpl'; |
||||
| 697 | $delimiter = '<br>'; |
||||
| 698 | if (Constants::OLEDRION_ATTRIBUTE_CHECKBOX_WHITE_SPACE == $option1) { |
||||
| 699 | $delimiter = ' '; |
||||
| 700 | } |
||||
| 701 | $template->assign('delimiter', $delimiter); |
||||
| 702 | |||||
| 703 | break; |
||||
| 704 | case Constants::OLEDRION_ATTRIBUTE_RADIO: // Boutons radio |
||||
| 705 | |||||
| 706 | $templateName = 'oledrion_attribute_radio.tpl'; |
||||
| 707 | // $delimiter = ''; |
||||
| 708 | $delimiter = '<br>'; |
||||
| 709 | if (Constants::OLEDRION_ATTRIBUTE_CHECKBOX_WHITE_SPACE == $option1) { |
||||
| 710 | $delimiter = ' '; |
||||
| 711 | } |
||||
| 712 | $template->assign('delimiter', $delimiter); |
||||
| 713 | |||||
| 714 | break; |
||||
| 715 | } |
||||
| 716 | if ('' !== $templateName) { |
||||
| 717 | $ret = $template->fetch('db:' . $templateName); |
||||
| 718 | } |
||||
| 719 | |||||
| 720 | return $ret; |
||||
| 721 | } |
||||
| 722 | } |
||||
| 723 |