psr12 fixes for new PHP_CodeSniffer (#4795)
[openemr.git] / portal / patient / libs / Controller / OnsitePortalActivityController.php
blob89934e9b518b5ef8f43ff4efb1d735aadc5b9f7e
1 <?php
3 /**
4 * OnsitePortalActivityController.php
6 * @package OpenEMR
7 * @link https://www.open-emr.org
8 * @author Jerry Padgett <sjpadgett@gmail.com>
9 * @copyright Copyright (c) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 /** import supporting libraries */
14 require_once("AppBasePortalController.php");
15 require_once("Model/OnsitePortalActivity.php");
17 /**
18 * OnsitePortalActivityController is the controller class for the OnsitePortalActivity object. The
19 * controller is responsible for processing input from the user, reading/updating
20 * the model as necessary and displaying the appropriate view.
22 * @package Patient Portal::Controller
23 * @author ClassBuilder
24 * @version 1.0
26 class OnsitePortalActivityController extends AppBasePortalController
28 /**
29 * Override here for any controller-specific functionality
31 * @inheritdocs
33 protected function Init()
35 parent::Init();
38 /**
39 * Displays a list view of OnsitePortalActivity objects
41 public function ListView()
43 $this->Render();
46 /**
47 * API Method queries for OnsitePortalActivity records and render as JSON
49 public function Query()
51 try {
52 $criteria = new OnsitePortalActivityCriteria();
54 // only allow patient to see their own activity
55 if (!empty($GLOBALS['bootstrap_pid'])) {
56 $pid = $GLOBALS['bootstrap_pid'];
57 } else {
58 $pid = RequestUtil::Get('patientId');
61 $activity = RequestUtil::Get('activity');
62 $doc = RequestUtil::Get('doc');
63 $doc = $doc ? $doc : 0;
64 $criteria->PatientId_Equals = $pid;
65 $criteria->Activity_Equals = $activity;
66 $criteria->TableArgs_Equals = $doc;
68 $output = new stdClass();
70 // if a sort order was specified then specify in the criteria
71 $output->orderBy = RequestUtil::Get('orderBy');
72 $output->orderDesc = RequestUtil::Get('orderDesc') != '';
73 if ($output->orderBy) {
74 $criteria->SetOrder($output->orderBy, $output->orderDesc);
77 $page = RequestUtil::Get('page');
79 if ($page != '') {
80 // if page is specified, use this instead (at the expense of one extra count query)
81 $pagesize = $this->GetDefaultPageSize();
83 $onsiteportalactivities = $this->Phreezer->Query('OnsitePortalActivity', $criteria)->GetDataPage($page, $pagesize);
84 $output->rows = $onsiteportalactivities->ToObjectArray(true, $this->SimpleObjectParams());
85 $output->totalResults = $onsiteportalactivities->TotalResults;
86 $output->totalPages = $onsiteportalactivities->TotalPages;
87 $output->pageSize = $onsiteportalactivities->PageSize;
88 $output->currentPage = $onsiteportalactivities->CurrentPage;
89 } else {
90 // return all results
91 $onsiteportalactivities = $this->Phreezer->Query('OnsitePortalActivity', $criteria);
92 $output->rows = $onsiteportalactivities->ToObjectArray(true, $this->SimpleObjectParams());
93 $output->totalResults = count($output->rows);
94 $output->totalPages = 1;
95 $output->pageSize = $output->totalResults;
96 $output->currentPage = 1;
100 $this->RenderJSON($output, $this->JSONPCallback());
101 } catch (Exception $ex) {
102 $this->RenderExceptionJSON($ex);
107 * API Method retrieves a single OnsitePortalActivity record and render as JSON
109 public function Read()
111 try {
112 $pk = $this->GetRouter()->GetUrlParam('id');
113 $onsiteportalactivity = $this->Phreezer->Get('OnsitePortalActivity', $pk);
114 // only allow patient to update onsiteportalactivity about themself
115 if (!empty($GLOBALS['bootstrap_pid'])) {
116 if ($GLOBALS['bootstrap_pid'] !== $onsiteportalactivity->PatientId) {
117 $error = 'Unauthorized';
118 throw new Exception($error);
121 $this->RenderJSON($onsiteportalactivity, $this->JSONPCallback(), true, $this->SimpleObjectParams());
122 } catch (Exception $ex) {
123 $this->RenderExceptionJSON($ex);
128 * API Method inserts a new OnsitePortalActivity record and render response as JSON
130 public function Create()
132 try {
133 $json = json_decode(RequestUtil::GetBody());
135 if (!$json) {
136 throw new Exception('The request body does not contain valid JSON');
139 $onsiteportalactivity = new OnsitePortalActivity($this->Phreezer);
141 // TODO: any fields that should not be inserted by the user should be commented out
143 // this is an auto-increment. uncomment if updating is allowed
144 // $onsiteportalactivity->Id = $this->SafeGetVal($json, 'id');
146 $onsiteportalactivity->Date = date('Y-m-d H:i:s', strtotime($this->SafeGetVal($json, 'date')));
148 // only allow patient to create onsiteportalactivity about themself
149 if (!empty($GLOBALS['bootstrap_pid'])) {
150 $onsiteportalactivity->PatientId = $GLOBALS['bootstrap_pid'];
151 } else {
152 $onsiteportalactivity->PatientId = $this->SafeGetVal($json, 'patientId');
155 $onsiteportalactivity->Activity = $this->SafeGetVal($json, 'activity');
156 $onsiteportalactivity->RequireAudit = $this->SafeGetVal($json, 'requireAudit');
157 $onsiteportalactivity->PendingAction = $this->SafeGetVal($json, 'pendingAction');
158 $onsiteportalactivity->ActionTaken = $this->SafeGetVal($json, 'actionTaken');
159 $onsiteportalactivity->Status = $this->SafeGetVal($json, 'status');
160 $onsiteportalactivity->Narrative = $this->SafeGetVal($json, 'narrative');
161 $onsiteportalactivity->TableAction = $this->SafeGetVal($json, 'tableAction');
162 $onsiteportalactivity->TableArgs = $this->SafeGetVal($json, 'tableArgs');
163 $onsiteportalactivity->ActionUser = $this->SafeGetVal($json, 'actionUser');
164 $onsiteportalactivity->ActionTakenTime = date('Y-m-d H:i:s', strtotime($this->SafeGetVal($json, 'actionTakenTime')));
165 $onsiteportalactivity->Checksum = $this->SafeGetVal($json, 'checksum');
167 $onsiteportalactivity->Validate();
168 $errors = $onsiteportalactivity->GetValidationErrors();
170 if (count($errors) > 0) {
171 $this->RenderErrorJSON('Please check the form for errors', $errors);
172 } else {
173 $onsiteportalactivity->Save();
174 $this->RenderJSON($onsiteportalactivity, $this->JSONPCallback(), true, $this->SimpleObjectParams());
176 } catch (Exception $ex) {
177 $this->RenderExceptionJSON($ex);
182 * API Method updates an existing OnsitePortalActivity record and render response as JSON
184 public function Update()
186 try {
187 $json = json_decode(RequestUtil::GetBody());
189 if (!$json) {
190 throw new Exception('The request body does not contain valid JSON');
193 $pk = $this->GetRouter()->GetUrlParam('id');
194 $onsiteportalactivity = $this->Phreezer->Get('OnsitePortalActivity', $pk);
196 // only allow patient to update onsiteportalactivity about themself
197 if (!empty($GLOBALS['bootstrap_pid'])) {
198 if ($GLOBALS['bootstrap_pid'] != $this->SafeGetVal($json, 'patientId', $onsiteportalactivity->PatientId)) {
199 throw new Exception('Bad PID');
203 // TODO: any fields that should not be updated by the user should be commented out
205 // this is a primary key. uncomment if updating is allowed
206 // $onsiteportalactivity->Id = $this->SafeGetVal($json, 'id', $onsiteportalactivity->Id);
208 $onsiteportalactivity->Date = date('Y-m-d H:i:s', strtotime($this->SafeGetVal($json, 'date', $onsiteportalactivity->Date)));
209 $onsiteportalactivity->PatientId = $this->SafeGetVal($json, 'patientId', $onsiteportalactivity->PatientId);
210 $onsiteportalactivity->Activity = $this->SafeGetVal($json, 'activity', $onsiteportalactivity->Activity);
211 $onsiteportalactivity->RequireAudit = $this->SafeGetVal($json, 'requireAudit', $onsiteportalactivity->RequireAudit);
212 $onsiteportalactivity->PendingAction = $this->SafeGetVal($json, 'pendingAction', $onsiteportalactivity->PendingAction);
213 $onsiteportalactivity->ActionTaken = $this->SafeGetVal($json, 'actionTaken', $onsiteportalactivity->ActionTaken);
214 $onsiteportalactivity->Status = $this->SafeGetVal($json, 'status', $onsiteportalactivity->Status);
215 $onsiteportalactivity->Narrative = $this->SafeGetVal($json, 'narrative', $onsiteportalactivity->Narrative);
216 $onsiteportalactivity->TableAction = $this->SafeGetVal($json, 'tableAction', $onsiteportalactivity->TableAction);
217 $onsiteportalactivity->TableArgs = $this->SafeGetVal($json, 'tableArgs', $onsiteportalactivity->TableArgs);
218 $onsiteportalactivity->ActionUser = $this->SafeGetVal($json, 'actionUser', $onsiteportalactivity->ActionUser);
219 $onsiteportalactivity->ActionTakenTime = date('Y-m-d H:i:s', strtotime($this->SafeGetVal($json, 'actionTakenTime', $onsiteportalactivity->ActionTakenTime)));
220 $onsiteportalactivity->Checksum = $this->SafeGetVal($json, 'checksum', $onsiteportalactivity->Checksum);
222 $onsiteportalactivity->Validate();
223 $errors = $onsiteportalactivity->GetValidationErrors();
225 if (count($errors) > 0) {
226 $this->RenderErrorJSON('Please check the form for errors', $errors);
227 } else {
228 $onsiteportalactivity->Save();
229 $this->RenderJSON($onsiteportalactivity, $this->JSONPCallback(), true, $this->SimpleObjectParams());
231 } catch (Exception $ex) {
232 $this->RenderExceptionJSON($ex);
237 * API Method deletes an existing OnsitePortalActivity record and render response as JSON
239 public function Delete()
241 try {
242 // TODO: if a soft delete is prefered, change this to update the deleted flag instead of hard-deleting
244 $pk = $this->GetRouter()->GetUrlParam('id');
245 $onsiteportalactivity = $this->Phreezer->Get('OnsitePortalActivity', $pk);
247 $onsiteportalactivity->Delete();
249 $output = new stdClass();
251 $this->RenderJSON($output, $this->JSONPCallback());
252 } catch (Exception $ex) {
253 $this->RenderExceptionJSON($ex);