Example #1
0
 public function action_notes($id = null)
 {
     if ($_POST) {
         $val = \Validation::forge();
         $val->add_field('did', 'Device id', 'required|min_length[1]|max_length[18]');
         if ($id == 'add') {
             $val->add_field('txt', 'notes', 'required|min_length[1]');
             if ($val->run()) {
                 $device = Model_Device::find($val->validated('did'));
                 if ($device) {
                     $prop = array('deviceID' => $device->id, 'txt' => $val->validated('txt'), 'meta_update_user' => $this->user, 'meta_update_time' => time());
                     $note = new Model_Notes($prop);
                     $note->save();
                     $n = array('id' => $note['id'], 'txt' => $note['txt'], 'user' => $this->username, 'time' => $note['meta_update_time']);
                     echo json_encode($n);
                 }
             }
         }
         if ($id == 'rem') {
             $val->add_field('nid', 'notes id', 'required|min_length[1]|max_length[18]');
             if ($val->run()) {
                 $device = Model_Device::find($val->validated('did'));
                 if ($device) {
                     $note = Model_Notes::find($val->validated('nid'));
                     if ($note) {
                         $note->delete();
                     }
                 }
             }
         }
     }
 }