updates to make oauth2/api compatible with php8 (#4069)
[openemr.git] / src / Services / PractitionerRoleService.php
blob50412675527aedce0e82fb062232f2a6ea616f35
1 <?php
3 /**
4 * PractitionerRoleService
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Yash Bothra <yashrajbothra786gmail.com>
9 * @copyright Copyright (c) 2018 Matthew Vita <matthewvita48@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 namespace OpenEMR\Services;
15 use OpenEMR\Common\Uuid\UuidRegistry;
16 use OpenEMR\Validators\ProcessingResult;
17 use OpenEMR\Validators\BaseValidator;
19 class PractitionerRoleService extends BaseService
22 private const PRACTITIONER_ROLE_TABLE = "facility_user_ids";
23 private $uuidRegistry;
25 /**
26 * Default constructor.
28 public function __construct()
30 parent::__construct('facility_user_ids');
31 $this->uuidRegistry = new UuidRegistry([
32 'table_name' => self::PRACTITIONER_ROLE_TABLE,
33 'table_vertical' => ['uid', 'facility_id']
34 ]);
35 $this->uuidRegistry->createMissingUuids();
36 (new UuidRegistry(['table_name' => 'users']))->createMissingUuids();
37 (new UuidRegistry(['table_name' => 'facility']))->createMissingUuids();
40 /**
41 * Returns a list of practitioner-role matching optional search criteria.
42 * Search criteria is conveyed by array where key = field/column name, value = field value.
43 * If no search criteria is provided, all records are returned.
45 * @param $search search array parameters
46 * @param $isAndCondition specifies if AND condition is used for multiple criteria. Defaults to true.
47 * @return ProcessingResult which contains validation messages, internal error messages, and the data
48 * payload.
50 public function getAll($search = array(), $isAndCondition = true)
52 $sqlBindArray = array();
54 $sql = "SELECT *,
55 role.title as role,
56 spec.title as specialty
57 FROM (
58 SELECT
59 prac_role.id as id,
60 prac_role.uuid as uuid,
61 prac_role.field_id as field,
62 (if( prac_role.field_id = 'role_code', prac_role.field_value, null )) as `role_code`,
63 (if( specialty.field_id = 'specialty_code', specialty.field_value, null )) as `specialty_code`,
64 us.uuid as user_uuid,
65 CONCAT(us.fname,
66 IF(us.mname IS NULL OR us.mname = '','',' '),us.mname,
67 IF(us.lname IS NULL OR us.lname = '','',' '),us.lname
68 ) as user_name,
69 fac.uuid as facility_uuid,
70 fac.name as facility_name
71 FROM facility_user_ids as prac_role
72 LEFT JOIN users as us ON us.id = prac_role.uid
73 LEFT JOIN facility_user_ids as specialty ON specialty.uid = prac_role.uid AND specialty.field_id = 'specialty_code'
74 LEFT JOIN facility as fac ON fac.id = prac_role.facility_id) as p_role
75 LEFT JOIN list_options as role ON role.option_id = p_role.role_code
76 LEFT JOIN list_options as spec ON spec.option_id = p_role.specialty_code
77 WHERE p_role.field = 'role_code' AND p_role.role_code != '' AND p_role.role_code IS NOT NULL";
79 if (!empty($search)) {
80 $sql .= " AND ";
81 $whereClauses = array();
82 $wildcardFields = array('user_name');
83 foreach ($search as $fieldName => $fieldValue) {
84 // support wildcard match on specific fields
85 if (in_array($fieldName, $wildcardFields)) {
86 array_push($whereClauses, $fieldName . ' LIKE ?');
87 array_push($sqlBindArray, '%' . $fieldValue . '%');
88 } else {
89 // equality match
90 array_push($whereClauses, $fieldName . ' = ?');
91 array_push($sqlBindArray, $fieldValue);
94 $sqlCondition = ($isAndCondition == true) ? 'AND' : 'OR';
95 $sql .= implode(' ' . $sqlCondition . ' ', $whereClauses);
97 $sql .= "
98 GROUP BY p_role.uuid";
99 $statementResults = sqlStatement($sql, $sqlBindArray);
101 $processingResult = new ProcessingResult();
102 while ($row = sqlFetchArray($statementResults)) {
103 $row['uuid'] = UuidRegistry::uuidToString($row['uuid']);
104 $row['user_uuid'] = UuidRegistry::uuidToString($row['user_uuid']);
105 $row['facility_uuid'] = UuidRegistry::uuidToString($row['facility_uuid']);
106 $processingResult->addData($row);
109 return $processingResult;
113 * Returns a single practitioner-role record by id.
114 * @param $uuid - The practitioner-role uuid identifier in string format.
115 * @return ProcessingResult which contains validation messages, internal error messages, and the data
116 * payload.
118 public function getOne($uuid)
120 $processingResult = new ProcessingResult();
122 $isValid = BaseValidator::validateId("uuid", "facility_user_ids", $uuid, true);
124 if ($isValid !== true) {
125 $validationMessages = [
126 'uuid' => ["invalid or nonexisting value" => " value " . $uuid]
128 $processingResult->setValidationMessages($validationMessages);
129 return $processingResult;
132 $sql = "SELECT prac_role.id,
133 prac_role.uuid,
134 prac_role.field_value as role_code,
135 specialty.field_value as specialty_code,
136 us.uuid as user_uuid,
137 us.fname as user_fname,
138 us.mname as user_mname,
139 us.lname as user_lname,
140 fac.uuid as facility_uuid,
141 fac.name as facility_name,
142 role.title as role,
143 spec.title as specialty
144 FROM facility_user_ids as prac_role
145 LEFT JOIN users as us ON us.id = prac_role.uid
146 LEFT JOIN facility_user_ids as specialty ON
147 specialty.uid = prac_role.uid AND specialty.field_id = 'specialty_code'
148 LEFT JOIN facility as fac ON fac.id = prac_role.facility_id
149 LEFT JOIN list_options as role ON role.option_id = prac_role.field_value
150 LEFT JOIN list_options as spec ON spec.option_id = specialty.field_value
151 WHERE prac_role.uuid = ? AND prac_role.field_id = 'role_code'";
153 $uuidBinary = UuidRegistry::uuidToBytes($uuid);
154 $sqlResult = sqlQuery($sql, [$uuidBinary]);
155 $sqlResult['uuid'] = UuidRegistry::uuidToString($sqlResult['uuid']);
156 $sqlResult['user_uuid'] = UuidRegistry::uuidToString($sqlResult['user_uuid']);
157 $sqlResult['facility_uuid'] = UuidRegistry::uuidToString($sqlResult['facility_uuid']);
158 $processingResult->addData($sqlResult);
159 return $processingResult;