/
Logic.php
104 lines (99 loc) · 3.61 KB
/
Logic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
class VladimirPopov_WebForms_Model_Logic
extends VladimirPopov_WebForms_Model_Abstract
{
const VISIBILITY_HIDDEN = 'hidden';
const VISIBILITY_VISIBLE = 'visible';
public function _construct()
{
parent::_construct();
$this->_init('webforms/logic');
}
public function ruleCheck($data)
{
$flag = false;
$input = "";
if (!empty($data[$this->getFieldId()]))
$input = $data[$this->getFieldId()];
if (!is_array($input)) $input = array($input);
if (
$this->getAggregation() == VladimirPopov_WebForms_Model_Logic_Aggregation::AGGREGATION_ANY ||
(
$this->getAggregation() == VladimirPopov_WebForms_Model_Logic_Aggregation::AGGREGATION_ALL &&
$this->getLogicCondition() == VladimirPopov_WebForms_Model_Logic_Condition::CONDITION_NOTEQUAL
)
) {
if ($this->getLogicCondition() == VladimirPopov_WebForms_Model_Logic_Condition::CONDITION_EQUAL) {
foreach ($input as $input_value) {
if (in_array($input_value, $this->getFrontendValue()))
$flag = true;
}
} else {
$flag = true;
foreach ($input as $input_value) {
if (in_array($input_value, $this->getFrontendValue())) $flag = false;
}
if (!count($input)) $flag = false;
}
} else {
$flag = true;
foreach ($this->getFrontendValue() as $trigger_value) {
if (!in_array($trigger_value, $input)) {
$flag = false;
}
}
}
return $flag;
}
public function getTargetVisibility($target, $logic_rules, $data)
{
$flag = false;
foreach ($logic_rules as $logic) {
foreach ($logic->getTarget() as $t) {
if ($target["id"] == $t) {
if ($logic->ruleCheck($data)) {
$action = $logic->getAction();
$flag = true;
break;
}
}
}
}
$visibility = false;
if ($target["logic_visibility"] == self::VISIBILITY_VISIBLE)
$visibility = true;
if ($flag) {
$visibility = true;
if ($action == VladimirPopov_WebForms_Model_Logic_Action::ACTION_HIDE) {
$visibility = false;
}
}
return $visibility;
}
public function getFrontendValue()
{
if (Mage::app()->getStore()->isAdmin())
return $this->getValue();
$field = Mage::getModel('webforms/fields')->setStoreId($this->getStoreId())->load($this->getFieldId());
if ($field->getType() == 'select/contact') {
$return = array();
$options = $field->getOptionsArray();
foreach ($options as $i => $option) {
foreach ($this->getValue() as $trigger) {
$contact = $field->getContactArray($option['value']);
$trigger_contact = $field->getContactArray($trigger);
if ($contact == $trigger_contact) {
$value = $option["value"];
if ($option['null']) {
$value = '';
}
if ($contact['email']) $return[] = $i;
else $return[] = $value;
}
}
}
return $return;
}
return $this->getValue();
}
}