forked from intralanman/fs_curl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_chatplan.php
233 lines (226 loc) · 7.19 KB
/
fs_chatplan.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* @package FS_CURL
* fs_chatplan.php
*/
if (basename($_SERVER['PHP_SELF']) == basename(__FILE__)) {
header('Location: index.php');
}
/**
* @package FS_CURL
* @license BSD
* @author Raymond Chandler (intralanman) <intralanman@gmail.com>
* @version 0.1
* Class for XML chatplan
*/
class fs_chatplan extends fs_curl {
private $special_class_file;
public function fs_chatplan() {
$this -> fs_curl();
}
/**
* This is the method that determines the XML output. Customized chatplans can
* be easily created by adding a record to the chatplan_special table with the
* appropriate values. The php class MUST contain a "main()" method. The method
* should write directly to the xmlw obj that's pased or take care of writing
* out the xml itself and exiting as to not return.
*
*/
public function main() {
$this -> comment($this -> request);
$context = $this -> request['context'];
if ($this -> is_specialized_chatplan($context)) {
$this->debug("$context should be handled in a specialized chatplan class file");
if (!include_once($this -> special_class_file)) {
$this -> file_not_found();
}
$class = sprintf('chatplan_%s', $context);
if (!class_exists($class)) {
$this -> comment("No Class of name $class");
$this -> file_not_found();
}
$obj = new $class;
/**
* recieving method should take incoming parameter as &$something
*/
$obj -> main($this);
} else {
$dp_array = $this -> get_chatplan($context);
$this -> writeChatplan($dp_array);
}
$this -> output_xml();
}
public function is_specialized_chatplan($context) {
$query = sprintf(
"SELECT * FROM chatplan_special WHERE context='%s'", $context
);
$this -> debug($query);
$res = $this -> db -> query($query);
if (FS_PDO::isError($res)) {
$this -> comment($query);
$this -> comment($this -> db -> getMessage());
$this -> file_not_found();
}
if ($res -> numRows() == 1) {
$this -> debug("numRows() == 1");
$row = $res -> fetchRow();
$this->debug($row);
$this -> special_class_file = sprintf('chatplans/%s', $row['class_file']);
return true;
} else {
return false;
}
}
/**
* This method will pull chatplan from database
*
* @param string $context context name for XML chatplan
* @return array
*/
private function get_chatplan($context) {
$dp_array = array();
$dpQuery = sprintf('SELECT
%1$scontext%1$s,
%1$sname%1$s as extension,
%1$sapplication%1$s as application_name,
%1$sdata%1$s as application_data,
%1$sfield%1$s as condition_field,
%1$sexpression%1$s as condition_expression,
%1$scontinue%1$s as ext_continue,
%1$stype%1$s
FROM chatplan
INNER JOIN chatplan_context USING(chatplan_id)
INNER JOIN chatplan_extension USING(context_id)
INNER JOIN chatplan_condition USING(extension_id)
INNER JOIN chatplan_actions USING(condition_id)
WHERE context = \'%2$s\'
ORDER BY chatplan_context.weight,
chatplan_extension.weight,
chatplan_condition.weight,
chatplan_actions.weight'
, DB_FIELD_QUOTE, $context
);
$this->debug($dpQuery);
$res = $this -> db -> query($dpQuery);
if (FS_PDO::isError($res)) {
$this -> comment($this -> db -> getMessage());
$this -> file_not_found();
}
if ($res -> numRows() < 1) {
$this -> debug("nothing to do, let's just return not found");
$this -> file_not_found();
}
$condition_number = 0;
while ($row = $res -> fetchRow()) {
$ct = $row['context'];
$et = $row['extension'];
$ec = $row['ext_continue'];
$app = $row['application_name'];
$data = $row['application_data'];
//$app_cdata = $row['app_cdata'];
$type = $row['type'];
$cf = $row['condition_field'];
$ce = $row['condition_expression'];
//$rcd = $row['re_cdata'];
$cc = empty($row['cond_break']) ? '0' : $row['cond_break'];
$dp_array[$ct]["$et;$ec"]["$cf;$ce;$cc"][] = array(
'type'=>$type,
'application'=>$app,
'data'=>$data,
'is_cdata'=>(empty($app_cdata) ? '' : $app_cdata)
);
}
return $dp_array;
}
/**
* Write XML chatplan from the array returned by get_chatplan
* @see fs_chatplan::get_chatplan
* @param array $dpArray Multi-dimentional array from which we write the XML
* @todo this method should REALLY be broken down into several smaller methods
*
*/
private function writeChatplan($dpArray) {
//print_r($dpArray);
if (is_array($dpArray)) {
$this -> xmlw -> startElement('section');
$this -> xmlw -> writeAttribute('name', 'chatplan');
$this -> xmlw -> writeAttribute('description', 'FreeSWITCH Chatplan');
//$this -> comment('dpArray is an array');
foreach ($dpArray as $context => $extensions_array) {
//$this -> comment($context);
//start the context
$this -> xmlw -> startElement('context');
$this -> xmlw -> writeAttribute('name', $context);
if (is_array($extensions_array)) {
foreach ($extensions_array as $extension => $conditions) {
//start an extension
$ex_split = preg_split('/;/', $extension);
$this -> xmlw -> startElement('extension');
$this -> xmlw -> writeAttribute('name', $ex_split[0]);
if (strlen($ex_split[1]) > 0) {
$this -> xmlw -> writeAttribute('continue', $ex_split[1]);
}
$this -> debug($conditions);
foreach ($conditions as $condition => $app_array) {
$c_split = preg_split('/;/', $condition);
$this -> xmlw -> startElement('condition');
if (!empty($c_split[0])) {
$this -> xmlw -> writeAttribute('field', $c_split[0]);
}
if (!empty($c_split[1])) {
if (array_key_exists(3, $c_split)
&& $c_split[3] == true) {
$this -> xmlw -> startElement('expression');
$this -> xmlw -> writeCdata($c_split[1]);
$this -> xmlw -> endElement();
} else {
$this -> xmlw -> writeAttribute(
'expression', $c_split[1]
);
}
}
//$this -> debug($c_split[2]);
if ($c_split[2] != '0') {
$this -> xmlw -> writeAttribute(
'break', $c_split[2]
);
}
//$this -> debug($app_array);
foreach ($app_array as $app) {
if (empty($app['application'])) {
continue;
}
$this -> xmlw -> startElement($app['type']);
$this -> xmlw -> writeAttribute(
'application', $app['application']
);
if (!empty($app['data'])) {
if (array_key_exists('is_cdata', $app)
&& $app['is_cdata'] == true) {
$this -> xmlw -> writeCdata($app['data']);
} else {
$this -> xmlw -> writeAttribute(
'data', $app['data']
);
}
}
if ($app['application'] == 'set') {
$this->xmlw->writeAttribute('inline', 'true');
}
$this -> xmlw -> endElement();
}
//</condition>
$this -> xmlw -> endElement();
}
// </extension>
$this -> xmlw -> endElement();
}
}
// </context>
$this -> xmlw -> endElement();
}
// </section>
$this -> xmlw -> endElement();
}
}
}