|
37 | 37 |
|
38 | 38 | class PluginFormcreatorForm extends CommonDBTM implements |
39 | 39 | PluginFormcreatorExportableInterface, |
40 | | -PluginFormcreatorDuplicatableInterface |
| 40 | +PluginFormcreatorDuplicatableInterface, |
| 41 | +PluginFormcreatorConditionnableInterface |
41 | 42 | { |
42 | 43 | static $rightname = 'entity'; |
43 | 44 |
|
@@ -1718,6 +1719,14 @@ function export($remove_uuid = false) { |
1718 | 1719 | $form['_sections'][] = $form_section->export($remove_uuid); |
1719 | 1720 | } |
1720 | 1721 |
|
| 1722 | + // get submit conditions |
| 1723 | + $form['_conditions'] = []; |
| 1724 | + $condition = new PluginFormcreatorCondition(); |
| 1725 | + $all_conditions = $condition->getConditionsFromItem($this); |
| 1726 | + foreach ($all_conditions as $condition) { |
| 1727 | + $form['_conditions'][] = $condition->export($remove_uuid); |
| 1728 | + } |
| 1729 | + |
1721 | 1730 | // get validators |
1722 | 1731 | $form['_validators'] = []; |
1723 | 1732 | $all_validators = $DB->request([ |
@@ -2042,6 +2051,13 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con |
2042 | 2051 | ]); |
2043 | 2052 | } |
2044 | 2053 |
|
| 2054 | + // Import submit conditions |
| 2055 | + if (isset($input['_conditions'])) { |
| 2056 | + foreach ($input['_conditions'] as $condition) { |
| 2057 | + PluginFormcreatorCondition::import($linker, $condition, $itemId); |
| 2058 | + } |
| 2059 | + } |
| 2060 | + |
2045 | 2061 | // import form's targets |
2046 | 2062 | if (isset($input['_targets'])) { |
2047 | 2063 | foreach ((new self())->getTargetTypes() as $targetType) { |
@@ -2582,4 +2598,59 @@ public function post_getFromDB() { |
2582 | 2598 | $this->fields += \PluginFormcreatorSection::getFullData($this->fields['id']); |
2583 | 2599 | } |
2584 | 2600 | } |
| 2601 | + |
| 2602 | + public function updateConditions($input) { |
| 2603 | + if (!isset($input['plugin_formcreator_questions_id']) || !isset($input['show_condition']) |
| 2604 | + || !isset($input['show_value']) || !isset($input['show_logic'])) { |
| 2605 | + return false; |
| 2606 | + } |
| 2607 | + |
| 2608 | + if (!is_array($input['plugin_formcreator_questions_id']) || !is_array($input['show_condition']) |
| 2609 | + || !is_array($input['show_value']) || !is_array($input['show_logic'])) { |
| 2610 | + return false; |
| 2611 | + } |
| 2612 | + |
| 2613 | + // All arrays of condition exists |
| 2614 | + if ($input['show_rule'] == PluginFormcreatorCondition::SHOW_RULE_ALWAYS) { |
| 2615 | + return false; |
| 2616 | + } |
| 2617 | + |
| 2618 | + if (!(count($input['plugin_formcreator_questions_id']) == count($input['show_condition']) |
| 2619 | + && count($input['show_value']) == count($input['show_logic']) |
| 2620 | + && count($input['plugin_formcreator_questions_id']) == count($input['show_value']))) { |
| 2621 | + return false; |
| 2622 | + } |
| 2623 | + |
| 2624 | + // Delete all existing conditions for the question |
| 2625 | + $condition = new PluginFormcreatorCondition(); |
| 2626 | + $condition->deleteByCriteria([ |
| 2627 | + 'itemtype' => static::class, |
| 2628 | + 'items_id' => $input['id'], |
| 2629 | + ]); |
| 2630 | + |
| 2631 | + // Arrays all have the same count and have at least one item |
| 2632 | + $order = 0; |
| 2633 | + while (count($input['plugin_formcreator_questions_id']) > 0) { |
| 2634 | + $order++; |
| 2635 | + $value = array_shift($input['show_value']); |
| 2636 | + $questionID = (int) array_shift($input['plugin_formcreator_questions_id']); |
| 2637 | + $showCondition = html_entity_decode(array_shift($input['show_condition'])); |
| 2638 | + $showLogic = array_shift($input['show_logic']); |
| 2639 | + $condition = new PluginFormcreatorCondition(); |
| 2640 | + $condition->add([ |
| 2641 | + 'itemtype' => static::class, |
| 2642 | + 'items_id' => $input['id'], |
| 2643 | + 'plugin_formcreator_questions_id' => $questionID, |
| 2644 | + 'show_condition' => $showCondition, |
| 2645 | + 'show_value' => $value, |
| 2646 | + 'show_logic' => $showLogic, |
| 2647 | + 'order' => $order, |
| 2648 | + ]); |
| 2649 | + if ($condition->isNewItem()) { |
| 2650 | + return false; |
| 2651 | + } |
| 2652 | + } |
| 2653 | + |
| 2654 | + return true; |
| 2655 | + } |
2585 | 2656 | } |
0 commit comments