| @@ 446-470 (lines=25) @@ | ||
| 443 | * @throws \October\Rain\Database\ModelException |
|
| 444 | * @return void |
|
| 445 | */ |
|
| 446 | protected function validateInventories() |
|
| 447 | { |
|
| 448 | if (! is_array($this->optionsInventories) || |
|
| 449 | ! is_array($this->optionsInventories['options'])) { |
|
| 450 | return; |
|
| 451 | } |
|
| 452 | ||
| 453 | $takenValueCombinations = []; |
|
| 454 | foreach ($this->optionsInventories['inventories'] as $inventory) { |
|
| 455 | // validate the inventory |
|
| 456 | $model = new Inventory($inventory); |
|
| 457 | $model->validate(); |
|
| 458 | ||
| 459 | // validate that the value combinations are unique |
|
| 460 | sort($inventory['valueIds']); |
|
| 461 | $valueCombination = json_encode($inventory['valueIds']); |
|
| 462 | ||
| 463 | if (in_array($valueCombination, $takenValueCombinations)) { |
|
| 464 | Flash::error(Lang::get('bedard.shop::lang.products.form.duplicate_inventories_error')); |
|
| 465 | throw new ModelException($this); |
|
| 466 | } |
|
| 467 | ||
| 468 | $takenValueCombinations[] = $valueCombination; |
|
| 469 | } |
|
| 470 | } |
|
| 471 | ||
| 472 | /** |
|
| 473 | * Validate options. |
|
| @@ 478-500 (lines=23) @@ | ||
| 475 | * @throws \October\Rain\Database\ModelException |
|
| 476 | * @return void |
|
| 477 | */ |
|
| 478 | protected function validateOptions() |
|
| 479 | { |
|
| 480 | if (! is_array($this->optionsInventories) || |
|
| 481 | ! is_array($this->optionsInventories['options'])) { |
|
| 482 | return; |
|
| 483 | } |
|
| 484 | ||
| 485 | $names = []; |
|
| 486 | foreach ($this->optionsInventories['options'] as $option) { |
|
| 487 | // validate the option |
|
| 488 | $model = new Option($option); |
|
| 489 | $model->validate(); |
|
| 490 | ||
| 491 | // validate that names are unique |
|
| 492 | $name = strtolower(trim($option['name'])); |
|
| 493 | ||
| 494 | if (in_array($name, $names)) { |
|
| 495 | Flash::error(Lang::get('bedard.shop::lang.products.form.duplicate_options_error')); |
|
| 496 | throw new ModelException($this); |
|
| 497 | } |
|
| 498 | ||
| 499 | $names[] = $name; |
|
| 500 | } |
|
| 501 | } |
|
| 502 | } |
|
| 503 | ||