Highway to PSR2
[openemr.git] / portal / patient / libs / Controller / UserController.php
blob55b28c89117aed0a3a14adec97abb2a2fcae76e4
1 <?php
2 /** @package OpenHealthEMR::Controller */
4 /**
6 * Copyright (C) 2016-2017 Jerry Padgett <sjpadgett@gmail.com>
8 * LICENSE: This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @package OpenEMR
22 * @author Jerry Padgett <sjpadgett@gmail.com>
23 * @link http://www.open-emr.org
26 /** import supporting libraries */
27 require_once("AppBaseController.php");
28 require_once("Model/User.php");
30 /**
31 * UserController is the controller class for the User object. The
32 * controller is responsible for processing input from the user, reading/updating
33 * the model as necessary and displaying the appropriate view.
35 * @package OpenHealthEMR::Controller
36 * @author ClassBuilder
37 * @version 1.0
39 class UserController extends AppBaseController
42 /**
43 * Override here for any controller-specific functionality
45 * @inheritdocs
47 protected function Init()
49 parent::Init();
51 // TODO: add controller-wide bootstrap code
53 // TODO: if authentiation is required for this entire controller, for example:
54 // $this->RequirePermission(SecureApp::$PERMISSION_USER,'SecureApp.LoginForm');
57 /**
58 * Displays a list view of User objects
60 public function ListView()
62 $rid=0;
63 if (isset($_GET['id'])) {
64 $rid = (int) $_GET['id'];
67 $this->Assign('recid', $rid);
68 $this->Render();
71 /**
72 * API Method queries for User records and render as JSON
74 public function Query()
76 try {
77 $criteria = new UserCriteria();
78 $recnum = RequestUtil::Get('recId');
79 $criteria->Id_Equals = $recnum;
81 $output = new stdClass();
83 // if a sort order was specified then specify in the criteria
84 $output->orderBy = RequestUtil::Get('orderBy');
85 $output->orderDesc = RequestUtil::Get('orderDesc') != '';
86 if ($output->orderBy) {
87 $criteria->SetOrder($output->orderBy, $output->orderDesc);
90 $page = RequestUtil::Get('page');
92 // return all results
93 $users = $this->Phreezer->Query('User', $criteria);
94 $output->rows = $users->ToObjectArray(true, $this->SimpleObjectParams());
95 $output->totalResults = count($output->rows);
96 $output->totalPages = 1;
97 $output->pageSize = $output->totalResults;
98 $output->currentPage = 1;
100 $this->RenderJSON($output, $this->JSONPCallback());
101 } catch (Exception $ex) {
102 $this->RenderExceptionJSON($ex);
107 * API Method retrieves a single User record and render as JSON
109 public function Read()
111 try {
112 $pk = $this->GetRouter()->GetUrlParam('id');
113 $user = $this->Phreezer->Get('User', $pk);
114 $this->RenderJSON($user, $this->JSONPCallback(), true, $this->SimpleObjectParams());
115 } catch (Exception $ex) {
116 $this->RenderExceptionJSON($ex);
121 * API Method inserts a new User record and render response as JSON
123 public function Create()
125 try {
126 $json = json_decode(RequestUtil::GetBody());
128 if (!$json) {
129 throw new Exception('The request body does not contain valid JSON');
132 $user = new User($this->Phreezer);
134 // TODO: any fields that should not be inserted by the user should be commented out
136 // this is an auto-increment. uncomment if updating is allowed
137 // $user->Id = $this->SafeGetVal($json, 'id');
139 $user->Username = $this->SafeGetVal($json, 'username');
140 $user->Password = $this->SafeGetVal($json, 'password');
141 $user->Authorized = $this->SafeGetVal($json, 'authorized');
142 $user->Info = $this->SafeGetVal($json, 'info');
143 $user->Source = $this->SafeGetVal($json, 'source');
144 $user->Fname = $this->SafeGetVal($json, 'fname');
145 $user->Mname = $this->SafeGetVal($json, 'mname');
146 $user->Lname = $this->SafeGetVal($json, 'lname');
147 $user->Federaltaxid = $this->SafeGetVal($json, 'federaltaxid');
148 $user->Federaldrugid = $this->SafeGetVal($json, 'federaldrugid');
149 $user->Upin = $this->SafeGetVal($json, 'upin');
150 $user->Facility = $this->SafeGetVal($json, 'facility');
151 $user->FacilityId = $this->SafeGetVal($json, 'facilityId');
152 $user->SeeAuth = $this->SafeGetVal($json, 'seeAuth');
153 $user->Active = $this->SafeGetVal($json, 'active');
154 $user->Npi = $this->SafeGetVal($json, 'npi');
155 $user->Title = $this->SafeGetVal($json, 'title');
156 $user->Specialty = $this->SafeGetVal($json, 'specialty');
157 $user->Billname = $this->SafeGetVal($json, 'billname');
158 $user->Email = $this->SafeGetVal($json, 'email');
159 $user->EmailDirect = $this->SafeGetVal($json, 'emailDirect');
160 $user->EserUrl = $this->SafeGetVal($json, 'eserUrl');
161 $user->Assistant = $this->SafeGetVal($json, 'assistant');
162 $user->Organization = $this->SafeGetVal($json, 'organization');
163 $user->Valedictory = $this->SafeGetVal($json, 'valedictory');
164 $user->Street = $this->SafeGetVal($json, 'street');
165 $user->Streetb = $this->SafeGetVal($json, 'streetb');
166 $user->City = $this->SafeGetVal($json, 'city');
167 $user->State = $this->SafeGetVal($json, 'state');
168 $user->Zip = $this->SafeGetVal($json, 'zip');
169 $user->Street2 = $this->SafeGetVal($json, 'street2');
170 $user->Streetb2 = $this->SafeGetVal($json, 'streetb2');
171 $user->City2 = $this->SafeGetVal($json, 'city2');
172 $user->State2 = $this->SafeGetVal($json, 'state2');
173 $user->Zip2 = $this->SafeGetVal($json, 'zip2');
174 $user->Phone = $this->SafeGetVal($json, 'phone');
175 $user->Fax = $this->SafeGetVal($json, 'fax');
176 $user->Phonew1 = $this->SafeGetVal($json, 'phonew1');
177 $user->Phonew2 = $this->SafeGetVal($json, 'phonew2');
178 $user->Phonecell = $this->SafeGetVal($json, 'phonecell');
179 $user->Notes = $this->SafeGetVal($json, 'notes');
180 $user->CalUi = $this->SafeGetVal($json, 'calUi');
181 $user->Taxonomy = $this->SafeGetVal($json, 'taxonomy');
182 $user->SsiRelayhealth = $this->SafeGetVal($json, 'ssiRelayhealth');
183 $user->Calendar = $this->SafeGetVal($json, 'calendar');
184 $user->AbookType = $this->SafeGetVal($json, 'abookType');
185 $user->PwdExpirationDate = date('Y-m-d H:i:s', strtotime($this->SafeGetVal($json, 'pwdExpirationDate')));
186 $user->PwdHistory1 = $this->SafeGetVal($json, 'pwdHistory1');
187 $user->PwdHistory2 = $this->SafeGetVal($json, 'pwdHistory2');
188 $user->DefaultWarehouse = $this->SafeGetVal($json, 'defaultWarehouse');
189 $user->Irnpool = $this->SafeGetVal($json, 'irnpool');
190 $user->StateLicenseNumber = $this->SafeGetVal($json, 'stateLicenseNumber');
191 $user->NewcropUserRole = $this->SafeGetVal($json, 'newcropUserRole');
192 $user->Cpoe = $this->SafeGetVal($json, 'cpoe');
193 $user->PhysicianType = $this->SafeGetVal($json, 'physicianType');
195 $user->Validate();
196 $errors = $user->GetValidationErrors();
198 if (count($errors) > 0) {
199 $this->RenderErrorJSON('Please check the form for errors', $errors);
200 } else {
201 $user->Save();
202 $this->RenderJSON($user, $this->JSONPCallback(), true, $this->SimpleObjectParams());
204 } catch (Exception $ex) {
205 $this->RenderExceptionJSON($ex);
210 * API Method updates an existing User record and render response as JSON
212 public function Update()
214 try {
215 $json = json_decode(RequestUtil::GetBody());
217 if (!$json) {
218 throw new Exception('The request body does not contain valid JSON');
221 $pk = $this->GetRouter()->GetUrlParam('id');
222 $user = $this->Phreezer->Get('User', $pk);
224 // TODO: any fields that should not be updated by the user should be commented out
226 // this is a primary key. uncomment if updating is allowed
227 // $user->Id = $this->SafeGetVal($json, 'id', $user->Id);
229 $user->Username = $this->SafeGetVal($json, 'username', $user->Username);
230 $user->Password = $this->SafeGetVal($json, 'password', $user->Password);
231 $user->Authorized = $this->SafeGetVal($json, 'authorized', $user->Authorized);
232 $user->Info = $this->SafeGetVal($json, 'info', $user->Info);
233 $user->Source = $this->SafeGetVal($json, 'source', $user->Source);
234 $user->Fname = $this->SafeGetVal($json, 'fname', $user->Fname);
235 $user->Mname = $this->SafeGetVal($json, 'mname', $user->Mname);
236 $user->Lname = $this->SafeGetVal($json, 'lname', $user->Lname);
237 $user->Federaltaxid = $this->SafeGetVal($json, 'federaltaxid', $user->Federaltaxid);
238 $user->Federaldrugid = $this->SafeGetVal($json, 'federaldrugid', $user->Federaldrugid);
239 $user->Upin = $this->SafeGetVal($json, 'upin', $user->Upin);
240 $user->Facility = $this->SafeGetVal($json, 'facility', $user->Facility);
241 $user->FacilityId = $this->SafeGetVal($json, 'facilityId', $user->FacilityId);
242 $user->SeeAuth = $this->SafeGetVal($json, 'seeAuth', $user->SeeAuth);
243 $user->Active = $this->SafeGetVal($json, 'active', $user->Active);
244 $user->Npi = $this->SafeGetVal($json, 'npi', $user->Npi);
245 $user->Title = $this->SafeGetVal($json, 'title', $user->Title);
246 $user->Specialty = $this->SafeGetVal($json, 'specialty', $user->Specialty);
247 $user->Billname = $this->SafeGetVal($json, 'billname', $user->Billname);
248 $user->Email = $this->SafeGetVal($json, 'email', $user->Email);
249 $user->EmailDirect = $this->SafeGetVal($json, 'emailDirect', $user->EmailDirect);
250 $user->EserUrl = $this->SafeGetVal($json, 'eserUrl', $user->EserUrl);
251 $user->Assistant = $this->SafeGetVal($json, 'assistant', $user->Assistant);
252 $user->Organization = $this->SafeGetVal($json, 'organization', $user->Organization);
253 $user->Valedictory = $this->SafeGetVal($json, 'valedictory', $user->Valedictory);
254 $user->Street = $this->SafeGetVal($json, 'street', $user->Street);
255 $user->Streetb = $this->SafeGetVal($json, 'streetb', $user->Streetb);
256 $user->City = $this->SafeGetVal($json, 'city', $user->City);
257 $user->State = $this->SafeGetVal($json, 'state', $user->State);
258 $user->Zip = $this->SafeGetVal($json, 'zip', $user->Zip);
259 $user->Street2 = $this->SafeGetVal($json, 'street2', $user->Street2);
260 $user->Streetb2 = $this->SafeGetVal($json, 'streetb2', $user->Streetb2);
261 $user->City2 = $this->SafeGetVal($json, 'city2', $user->City2);
262 $user->State2 = $this->SafeGetVal($json, 'state2', $user->State2);
263 $user->Zip2 = $this->SafeGetVal($json, 'zip2', $user->Zip2);
264 $user->Phone = $this->SafeGetVal($json, 'phone', $user->Phone);
265 $user->Fax = $this->SafeGetVal($json, 'fax', $user->Fax);
266 $user->Phonew1 = $this->SafeGetVal($json, 'phonew1', $user->Phonew1);
267 $user->Phonew2 = $this->SafeGetVal($json, 'phonew2', $user->Phonew2);
268 $user->Phonecell = $this->SafeGetVal($json, 'phonecell', $user->Phonecell);
269 $user->Notes = $this->SafeGetVal($json, 'notes', $user->Notes);
270 $user->CalUi = $this->SafeGetVal($json, 'calUi', $user->CalUi);
271 $user->Taxonomy = $this->SafeGetVal($json, 'taxonomy', $user->Taxonomy);
272 $user->SsiRelayhealth = $this->SafeGetVal($json, 'ssiRelayhealth', $user->SsiRelayhealth);
273 $user->Calendar = $this->SafeGetVal($json, 'calendar', $user->Calendar);
274 $user->AbookType = $this->SafeGetVal($json, 'abookType', $user->AbookType);
275 $user->PwdExpirationDate = date('Y-m-d H:i:s', strtotime($this->SafeGetVal($json, 'pwdExpirationDate', $user->PwdExpirationDate)));
276 $user->PwdHistory1 = $this->SafeGetVal($json, 'pwdHistory1', $user->PwdHistory1);
277 $user->PwdHistory2 = $this->SafeGetVal($json, 'pwdHistory2', $user->PwdHistory2);
278 $user->DefaultWarehouse = $this->SafeGetVal($json, 'defaultWarehouse', $user->DefaultWarehouse);
279 $user->Irnpool = $this->SafeGetVal($json, 'irnpool', $user->Irnpool);
280 $user->StateLicenseNumber = $this->SafeGetVal($json, 'stateLicenseNumber', $user->StateLicenseNumber);
281 $user->NewcropUserRole = $this->SafeGetVal($json, 'newcropUserRole', $user->NewcropUserRole);
282 $user->Cpoe = $this->SafeGetVal($json, 'cpoe', $user->Cpoe);
283 $user->PhysicianType = $this->SafeGetVal($json, 'physicianType', $user->PhysicianType);
285 $user->Validate();
286 $errors = $user->GetValidationErrors();
288 if (count($errors) > 0) {
289 $this->RenderErrorJSON('Please check the form for errors', $errors);
290 } else {
291 $user->Save();
292 $this->RenderJSON($user, $this->JSONPCallback(), true, $this->SimpleObjectParams());
294 } catch (Exception $ex) {
295 $this->RenderExceptionJSON($ex);
300 * API Method deletes an existing User record and render response as JSON
302 public function Delete()
304 try {
305 // TODO: if a soft delete is prefered, change this to update the deleted flag instead of hard-deleting
307 $pk = $this->GetRouter()->GetUrlParam('id');
308 $user = $this->Phreezer->Get('User', $pk);
310 $user->Delete();
312 $output = new stdClass();
314 $this->RenderJSON($output, $this->JSONPCallback());
315 } catch (Exception $ex) {
316 $this->RenderExceptionJSON($ex);