/
ExampleController.php
307 lines (265 loc) · 8.53 KB
/
ExampleController.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
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/**
* ExampleController - handles frontend ExtJS requests
*
* PHP Version 5.3+
*
* @category Controller
* @package AgileExtJsZf
* @author Chris Cornutt <ccornutt@phpdeveloper.org>
* @license MIT http://google.com?q=mit+license
* @version 1.0
* @link https://github.com/enygma/Agile-ExtJS-ZF
*/
/**
* ExampleController - handles frontend ExtJS requests
*
* @category Controller
* @package AgileExtJsZf
* @author Chris Cornutt <ccornutt@phpdeveloper.org>
* @license MIT http://google.com?q=mit+license
* @version 1.0
* @link https://github.com/enygma/Agile-ExtJS-ZF
*/
class ExampleController extends Zend_Controller_Action
{
/**
* Initialize the controller object
*
* @return void
*/
public function init()
{
$this->_helper->contextSwitch()
->addActionContext('example2', array('xml', 'json'))
->addActionContext('example3', array('xml', 'json'))
->addActionContext('example4', array('xml', 'json'))
->addActionContext('example5', array('xml', 'json'))
->addActionContext('example5update', array('xml', 'json'))
->addActionContext('example6submit', array('xml', 'json'))
->addActionContext('example8submit', array('xml', 'json'))
->addActionContext('example8read', array('xml', 'json'))
->addActionContext('example8users', array('xml', 'json'))
->setAutoJsonSerialization(true)
->initContext();
}
/**
* Outputs the list of the examples (defined in the view)
*
* @return void
*/
public function indexAction()
{
//nothing to see, just used for outputting the examples list
}
/**
* Outputs Example #1 (simple window)
*
* @return void
*/
public function example1Action()
{
//nothing to see, just used for outputting the example
}
/**
* This is used in the example related to models and stores
*
* @return void
*/
public function example2Action()
{
// NOTE: hard-coded sleep to show the loader
sleep(1);
$this->view->products = array(
array(
'item_name' => 'Tekno Black Light Bubbles',
'item_type' => 'Toy',
'amount' => '$3.99 USD'
),
array(
'item_name' => 'Sheldon\'s Spot',
'item_type' => 'T-Shirt',
'amount' => '$17.99 USD'
)
);
}
/**
* Output to show tree view example
*
* @return void
*/
public function example3Action()
{
$this->view->tree_items = array(
array('text' => 'Item #1'),
array('text' => 'Item #2'),
);
}
/**
* Working with Layouts
*
* @return void
*/
public function example4Action()
{
$this->view->mytree = array(
array('id'=>'test1','text'=>'text field 1'),
array('id'=>'test2','text'=>'text field 2')
);
}
/**
* Pulling data from the database for user informat
*
* @return void
*/
public function example5Action()
{
$users = new Application_Model_User();
$user = $users->fetchAll();
$this->view->users = array();
foreach ($user as $u) {
$this->view->users[] = array(
'name' => $u->name,
'id' => $u->ID
);
}
$this->view->success = true;
}
/**
* Update action for the example5
*
* @return void
*/
public function example5updateAction()
{
// the frontend sends the request back in the POST body
// so we have to fetch it
$request = json_decode($this->getRequest()->getRawBody());
//error_log('request: '.print_r($request,true));
$method = $this->getRequest()->getParam('method');
//error_log('method: '.var_export($method,true));
try {
$users = new Application_Model_User();
$db = $users::getDefaultAdapter();
switch($method) {
case 'update':
$stmt = $db->prepare('UPDATE users SET name = :name where id = :id');
$stmt->bindParam('id', $request->id);
$stmt->bindParam('name', $request->name);
$stmt->execute();
break;
case 'create':
$stmt = $db->prepare('INSERT INTO users VALUES (:name,NULL)');
$stmt->bindParam('name', $request->name);
$stmt->execute();
break;
case 'delete':
$stmt = $db->prepare('DELETE FROM users WHERE id = :id');
$stmt->bindParam('id', $request->id);
$stmt->execute();
break;
default:
// pull the records
}
$this->view->success = 'true';
} catch (Exception $e) {
error_log('ERROR saving user: '.$e->getMessage());
$this->view->success = 'false';
}
}
/**
* Example6
*
* @return void
*/
public function example6Action()
{
}
public function example6submitAction()
{
$request = $this->getRequest();
$form = new Application_Form_Login();
$success = false;
$message = null;
$username = $request->getParam('username');
$password = $request->getParam('password');
//error_log('uname: '.$username.' -> '.$password);
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
$result = $this->_checkDbLogin($form->getValues());
$success = $result['success'];
$message = $result['message'];
}
}
$this->view->success = $success;
$this->view->message = $message;
}
public function example7Action()
{
// unit testing with Jasmine
}
public function example8Action()
{
// chat application
}
public function example8readAction()
{
$request = json_decode($this->getRequest()->getRawBody());
$filterUserId = $this->getRequest()->getParam('filterBy');
$username = $this->getRequest()->getParam('username');
$msg = new Application_Model_Chatmessage();
if (!empty($username)) {
// we're changing our username...
$user = new Application_Model_Chatuser();
$found = $user->findByUsername($username);
if (count($found)==0) {
// new user - add to the table!
$insertId = $user->addUser($username);
$this->view->insertUserId = $insertId;
} else {
$this->view->insertUserId = $found[0]->ID;
}
}
if (isset($request->message)) {
// add the message!
$userId = (!empty($request->chatUser)) ? $request->chatUser : null;
$msg->add($request->message,$userId);
}
// see if we need to filter the list
$filterUserId = ($filterUserId !== null && (int)$filterUserId !== 0)
? $filterUserId : null;
// get the latest messages from the list
$this->view->messages = $msg->getLatest(20,$filterUserId);
$this->view->success = true;
}
public function example8usersAction()
{
$user = new Application_Model_Chatuser();
$this->view->users = $user->getCurrent();
}
private function _checkDbLogin($values)
{
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
$authAdapter->setTableName('users')
->setIdentityColumn('name')
->setCredentialColumn('password')
->setCredentialTreatment('MD5(?)');
$authAdapter->setIdentity($values['username']);
$authAdapter->setCredential($values['password']);
$auth = Zend_Auth::getInstance();
$authResult = $auth->authenticate($authAdapter);
if ($authResult->isValid()) {
return array(
'success' => true,
'message' => $authResult->getMessages()
);
} else {
return array(
'success' => false,
'message' => $authResult->getMessages()
);
}
}
}
?>