-
Notifications
You must be signed in to change notification settings - Fork 6
/
Helloworld.class.php
295 lines (274 loc) · 6.84 KB
/
Helloworld.class.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
namespace FreePBX\modules;
use BMO;
use FreePBX_Helpers;
use PDO;
class Helloworld extends FreePBX_Helpers implements BMO
{
public $FreePBX = null;
public function __construct($freepbx = null)
{
if ($freepbx == null) {
throw new Exception("Not given a FreePBX Object");
}
$this->FreePBX = $freepbx;
$this->Database = $freepbx->Database;
}
/**
* Installer run on fwconsole ma install
*
* @return void
*/
public function install()
{
}
/**
* Uninstaller run on fwconsole ma uninstall
*
* @return void
*/
public function uninstall()
{
}
/**
* backup and restore methods not needed after framwork
* 13.0.195.21
* 14.0.5.1
* 15.0.1.38
* */
/**
* Processes form submission and pre-page actions.
*
* @param string $page Display name
* @return void
*/
public function doConfigPageInit($page)
{
/** getReq provided by FreePBX_Helpers see https://wiki.freepbx.org/x/0YGUAQ */
$action = $this->getReq('action', '');
$id = $this->getReq('id', '');
$subject = $this->getReq('subject', '');
$body = $this->getReq('body');
if ('add' == $action) {
return $this->addItem($subject, $body);
}
if ('delete' == $action) {
return $this->deleteItem($id);
}
if ('edit' == $action) {
$this->updateItem($id, $subject, $body);
}
}
/**
* Adds buttons to the bottom of pages per set conditions
*
* @param array $request $_REQUEST
* @return void
*/
public function getActionBar($request)
{
if ('helloworld' == $request['display']) {
if (!isset($_GET['view'])) {
return [];
}
$buttons = [
'delete' => [
'name' => 'delete',
'id' => 'delete',
'value' => _('Delete')
],
'reset' => [
'name' => 'reset',
'id' => 'reset',
'value' => _("Reset")
],
'submit' => [
'name' => 'submit',
'id' => 'submit',
'value' => _("Submit")
]
];
if (!isset($_GET['id']) || empty($_GET['id'])) {
unset($buttons['delete']);
}
return $buttons;
}
}
/**
* Returns bool permissions for AJAX commands
* https://wiki.freepbx.org/x/XoIzAQ
* @param string $command The ajax command
* @param array $setting ajax settings for this command typically untouched
* @return bool
*/
public function ajaxRequest($command, &$setting)
{
//The ajax request
if ("getJSON" == $command) {
return true;
}
return false;
}
/**
* Handle Ajax request
* @url ajax.php?module=helloworld&command=getJSON&jdata=grid
*
* @return array
*/
public function ajaxHandler()
{
if ('getJSON' == $_REQUEST['command'] && 'grid' == $_REQUEST['jdata']) {
return $this->getList();
}
return json_encode([
'status' => false,
'message' => _("Invalid Request")
]);
}
//Module getters These are all custom methods
/**
* getOne Gets an individual item by ID
* @param int $id Item ID
* @return array Returns an associative array with id, subject and body.
*/
public function getOne($id)
{
$sql = "SELECT id,subject,body FROM helloworld WHERE id = :id";
$stmt = $this->Database->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetchObject();
return [
'id' => $row->id,
'subject' => $row->subject,
'body' => $row->body
];
}
/**
* getList gets a list od subjects and their respective id.
* @return array id => subject
*/
public function getList()
{
$ret = [];
$sql = 'SELECT id, subject FROM helloworld';
$data = $this->Database->query($sql)->fetchAll(PDO::FETCH_KEY_PAIR);
array_walk($data, function (&$value, $key) {
$value = ['id' => $key, 'subject' => $value];
});
return $data;
}
//Module setters these are all custom methods.
/**
* addItem Add an Item
* @param string $subject The Subject of the item
* @param [type] $body The body of the item
*/
public function addItem($subject, $body)
{
$sql =
'INSERT INTO helloworld (subject, body) VALUES (:subject, :body)';
$stmt = $this->Database->prepare($sql);
$stmt->bindParam(':subject', $subject, \PDO::PARAM_STR);
$stmt->bindParam(':body', $body, \PDO::PARAM_STR);
$stmt->execute();
return $this;
}
/**
* updateItem Updates the given ID
* @param int $id Item ID
* @param string $subject The new subject
* @param string $body The new body
* @return bool Returns true on success or false on failure
*/
public function updateItem($id, $subject, $body)
{
$sql =
'UPDATE helloworld SET subject = :subject, body = :body WHERE id = :id';
$stmt = $this->Database->prepare($sql);
$stmt->bindParam(':subject', $subject, \PDO::PARAM_STR);
$stmt->bindParam(':body', $body, \PDO::PARAM_STR);
$stmt->bindParam(':id', $id, \PDO::PARAM_INT);
$stmt->execute();
return $this;
}
/**
* deleteItem Deletes the given ID
* @param int $id Item ID
* @return bool Returns true on success or false on failure
*/
public function deleteItem($id)
{
$sql = 'DELETE FROM helloworld WHERE id = :id';
$stmt = $this->Database->prepare($sql);
$stmt->bindParam(':id', $id, \PDO::PARAM_INT);
$stmt->execute();
return $this;
}
/**
* Do we want to add to the dialplan?
* https://wiki.freepbx.org/display/FOP/BMO+Hooks#BMOHooks-DialplanHooks
*
* @return bool or int 500 priority
*/
public function myDialplanHooks()
{
return true;
}
/**
* Dialplan generation
*
* @param object $ext The dialplan object we add to
* @param string $engine This will always be asterisk
* @param int $priority 500?
* @return void
*/
public function doDialplanHook(&$ext, $engine, $priority)
{
$modulename = 'helloworld';
// Retrieve module's feature code
$fcc = new \featurecode($modulename, 'helloworld');
$hw_fc = $fcc->getCodeActive();
unset($fcc);
$id = 'app-helloworld';
$ext->addInclude('from-internal-additional', $id); // Add the include to from-internal
$ext->add(
$id,
$hw_fc,
'',
new \ext_goto('1', 's', 'app-helloworld-playback')
); // feature code goes to playback context
$id = 'app-helloworld-playback';
$c = 's';
$ext->add($id, $c, 'label', new \ext_answer());
$ext->add($id, $c, '', new \ext_wait(1));
$ext->add($id, $c, '', new \ext_playback('hello-world'));
$ext->add($id, $c, '', new \ext_playback('demo-congrats'));
$ext->add($id, $c, 'hangup', new \ext_hangup());
}
/**
* This returns html to the main page
*
* @return string html
*/
public function showPage()
{
$subhead = _('Item List');
$content = load_view(__DIR__ . '/views/grid.php');
if ('form' == $_REQUEST['view']) {
$subhead = _('Add Item');
$content = load_view(__DIR__ . '/views/form.php');
if (isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
$subhead = _('Edit Item');
$content = load_view(
__DIR__ . '/views/form.php',
$this->getOne($_REQUEST['id'])
);
}
}
echo load_view(__DIR__ . '/views/default.php', array(
'subhead' => $subhead,
'content' => $content
));
}
}