6 * @link http://www.open-emr.org
7 * @author Victor Kofia <victor.kofia@gmail.com>
8 * @author Brady Miller <brady.g.miller@gmail.com>
9 * @copyright Copyright (c) 2017 Victor Kofia <victor.kofia@gmail.com>
10 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
11 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 namespace OpenEMR\Services
;
17 use Particle\Validator\Validator
;
23 * In the case where a patient doesn't have a picture uploaded,
24 * this value will be returned so that the document controller
25 * can return an empty response.
27 private $patient_picture_fallback_id = -1;
32 * Default constructor.
34 public function __construct()
38 public function validate($patient)
40 $validator = new Validator();
42 $validator->required('fname')->lengthBetween(2, 255);
43 $validator->required('lname')->lengthBetween(2, 255);
44 $validator->required('sex')->lengthBetween(4, 30);
45 $validator->required('dob')->datetime('Y-m-d');
48 return $validator->validate($patient);
51 public function setPid($pid)
56 public function getPid()
62 * TODO: This should go in the ChartTrackerService and doesn't have to be static.
63 * @param $pid unique patient id
66 public static function getChartTrackerInformationActivity($pid)
68 $sql = "SELECT ct.ct_when,
75 FROM chart_tracker AS ct
76 LEFT OUTER JOIN users AS u ON u.id = ct.ct_userid
78 ORDER BY ct.ct_when DESC";
79 return sqlStatement($sql, array($pid));
83 * TODO: This should go in the ChartTrackerService and doesn't have to be static.
86 public static function getChartTrackerInformation()
88 $sql = "SELECT ct.ct_when,
97 FROM chart_tracker AS ct
98 JOIN cttemp ON cttemp.ct_pid = ct.ct_pid AND cttemp.ct_when = ct.ct_when
99 LEFT OUTER JOIN users AS u ON u.id = ct.ct_userid
100 LEFT OUTER JOIN patient_data AS p ON p.pid = ct.ct_pid
101 WHERE ct.ct_userid != 0
103 return sqlStatement($sql);
106 public function getFreshPid()
108 $pid = sqlQuery("SELECT MAX(pid)+1 AS pid FROM patient_data");
110 return $pid['pid'] === null ?
1 : $pid['pid'];
113 public function insert($data)
115 $fresh_pid = $this->getFreshPid();
117 $sql = " INSERT INTO patient_data SET";
123 $sql .= " street=?,";
124 $sql .= " postal_code=?,";
127 $sql .= " country_code=?,";
128 $sql .= " phone_contact=?,";
132 $sql .= " ethnicity=?";
134 $results = sqlInsert(
143 $data["postal_code"],
146 $data["country_code"],
147 $data["phone_contact"],
162 public function update($pid, $data)
164 $sql = " UPDATE patient_data SET";
169 $sql .= " street=?,";
170 $sql .= " postal_code=?,";
173 $sql .= " country_code=?,";
174 $sql .= " phone_contact=?,";
178 $sql .= " ethnicity=?";
179 $sql .= " where pid=?";
189 $data["postal_code"],
192 $data["country_code"],
193 $data["phone_contact"],
203 public function getAll($search)
205 $sqlBindArray = array();
227 if ($search['name'] ||
$search['fname'] ||
$search['lname'] ||
$search['dob']) {
230 $whereClauses = array();
231 if ($search['name']) {
232 $search['name'] = '%' . $search['name'] . '%';
233 array_push($whereClauses, "CONCAT(lname,' ', fname) LIKE ?");
234 array_push($sqlBindArray, $search['name']);
236 if ($search['fname']) {
237 array_push($whereClauses, "fname=?");
238 array_push($sqlBindArray, $search['fname']);
240 if ($search['lname']) {
241 array_push($whereClauses, "lname=?");
242 array_push($sqlBindArray, $search['lname']);
244 if ($search['dob'] ||
$search['birthdate']) {
245 $search['dob'] = !empty($search['dob']) ?
$search['dob'] : $search['birthdate'];
246 array_push($whereClauses, "dob=?");
247 array_push($sqlBindArray, $search['dob']);
250 $sql .= implode(" AND ", $whereClauses);
253 $statementResults = sqlStatement($sql, $sqlBindArray);
256 while ($row = sqlFetchArray($statementResults)) {
257 array_push($results, $row);
263 public function getOne()
286 return sqlQuery($sql, $this->pid
);
292 public function getPatientPictureDocumentId()
294 $sql = "SELECT doc.id AS id
296 JOIN categories_to_documents cate_to_doc
297 ON doc.id = cate_to_doc.document_id
299 ON cate.id = cate_to_doc.category_id
300 WHERE cate.name LIKE ? and doc.foreign_id = ?";
302 $result = sqlQuery($sql, array($GLOBALS['patient_photo_category_name'], $this->pid
));
304 if (empty($result) ||
empty($result['id'])) {
305 return $this->patient_picture_fallback_id
;
308 return $result['id'];