4 * A simple application controller extension
6 class Users extends ApplicationController {
9 * Retrieves rows from database.
11 public function view() {
12 $res = new Response();
14 $res->message = "Loaded data";
15 $res->data = User::all();
16 return $res->to_json();
21 public function create() {
22 $res = new Response();
24 // Ugh, php...check if !hash
25 if (is_array($this->params) && !empty($this->params) && preg_match('/^\d+$/', implode('', array_keys($this->params)))) {
26 foreach ($this->params as $data) {
27 array_push($res->data, User::create($data)->to_hash());
30 $res->message = "Created " . count($res->data) . ' records';
32 if ($rec = User::create($this->params)) {
34 $res->data = $rec->to_hash();
35 $res->message = "Created record";
37 $res->success = false;
38 $res->message = "Failed to create record";
41 return $res->to_json();
47 public function update() {
48 $res = new Response();
49 if (is_array($this->id)) {
51 foreach ($this->id as $idx => $id) {
52 if ($rec = User::update($id, $this->params[$idx])) {
53 array_push($res->data, $rec->to_hash());
57 $res->message = "Updated " . count($res->data) . " records";
59 if ($rec = User::update($this->id, $this->params)) {
60 $res->data = $rec->to_hash();
62 $res->message = "Updated record";
64 $res->message = "Failed to updated record";
65 $res->success = false;
67 // SIMULATE ERROR: All records having odd-numbered ID have error.
69 $res->success = false;
70 $res->message = "SIMULATED ERROR: Lorem ipsum dolor sit amet, placerat consectetuer, nec lacus imperdiet velit dui interdum vestibulum, sagittis lectus morbi, urna aliquet minus natoque commodo egestas non, libero libero arcu sed sed.";
73 return $res->to_json();
79 public function destroy() {
80 $res = new Response();
82 if (is_array($this->params)) {
84 foreach ($this->params as $id) {
85 if ($rec = User::destroy($id)) {
86 array_push($destroyed, $rec);
90 $res->message = 'Destroyed ' . count($destroyed) . ' records';
92 if ($rec = User::destroy($this->params)) {
93 $res->message = "Destroyed User";
96 $res->message = "Failed to Destroy user";
99 return $res->to_json();