-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseclass_action.php
139 lines (122 loc) · 4.12 KB
/
baseclass_action.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
require_once("baseclass_module.php");
require_once("util_actions.php");
class baseclass_action extends baseclass_module
{
protected $actions_array;
public function __construct($user_id, $module_data_dictionary)
{
parent::__construct($user_id, $module_data_dictionary);
$this->actions_array = array();
$this->retrieve_actions();
}
/*
* Subclass to override this function and perform their magic
* Return null to indicate that no action was performed
* Otherwise, return an associated array with result parameters
*/
public function action($action_id, $event_param, $action_param)
{
//critical errors
if (!isset($event_param) || $event_param == null || $event_param == "") {
echo " <span class='error'>ERROR</span>: event_param is empty<br/>\n";
return null;
}
/* $action_param can be optional for hardware action (?)
if (!isset($action_param) || $action_param == null || $action_param == "") {
echo "error: action_param is empty";
return array();
}
*/
//no such action, let it pass through
$action_alias = $this->getActionAliasByID($action_id);
if ($action_alias == null) {
echo " <span class='error'>ERROR</span>: no action_alias for action_id $action_id <br/>\n";
return null;
}
//parse event parameters string, if any
$event_params_array = null;
if (isset($event_param)) {
$event_params_array = json_decode(func_json_clean_param_string($event_param), true);
if (gettype($event_params_array) != "array")
$event_params_array = null;
}
//parse action parameters string, if any
$action_params_array = null;
if (isset($action_param)) {
$action_params_array = json_decode(func_json_clean_param_string($action_param), true);
if (gettype($action_params_array) != "array")
$action_params_array = null;
}
return $this->action_by_name($action_alias, $event_params_array, $action_params_array);
}
/*
* Subclass to override this function and perform their magic
*/
protected function action_by_name($action_alias, $event_params_array, $action_params_array)
{
echo " <span class='error'>ERROR</span>: subclass need to override 'action_by_name(...)' function<br/>\n";
return null;
}
/*
* Get number of possible actions for this module
*/
public function getNumberOfActions()
{
return count($this->actions_array);
}
/*
* Retrieve all possible actions for this module from database
*/
private function retrieve_actions()
{
$sql_result_actions = func_getActionsByModuleID( $this->module_id );
if ($sql_result_actions == null)
return;
//transfer to private array, hash by action_name for convenient access later
while ($one_record = mysql_fetch_assoc($sql_result_actions)) {
$action_id = $one_record['action_id'];
$this->actions_array[$action_id] = $one_record;
}
$sql_result_actions = null;
}
/*
* Convenient function to get an action object by its name (case insensitive)
* Return null if not found
*/
protected function getActionByID($action_id)
{
if ($action_id == null || $action_id == 0 || $action_id == "")
return null;
if ($this->actions_array == null || !isset($this->actions_array[$action_id]))
return null;
if (!isset($this->actions_array[$action_id]['action_id'])) //verify valid action object
return null;
return $this->actions_array[$action_id];
}
/*
* Convenient function to get an action object by its name (case insensitive)
* Return null if not found
*/
protected function getActionAliasByID($action_id)
{
if ($action_id == null || $action_id == 0 || $action_id == "")
return null;
if ($this->actions_array == null || !isset($this->actions_array[$action_id]))
return null;
if (!isset($this->actions_array[$action_id]['action_id'])) //verify valid action object
return null;
if (!isset($this->actions_array[$action_id]['action_alias'])) //verify valid action object
return null;
return $this->actions_array[$action_id]['action_alias'];
}
/*
* Return JavaScript code for given action alias
*/
public function getUICodeForActionAlias($action_alias)
{
//Subclass to override this function
return "";
}
}
?>