FHIR Appointment/Patient/Encounter/ValueSet (#7066)
[openemr.git] / src / Services / ListService.php
blob2d118c853d3f3b1471bf56f6268663afbad31d6d
1 <?php
3 /**
4 * ListService
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Matthew Vita <matthewvita48@gmail.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2018 Matthew Vita <matthewvita48@gmail.com>
11 * @copyright Copyright (c) 2018 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 namespace OpenEMR\Services;
17 use OpenEMR\Common\Database\QueryUtils;
18 use Particle\Validator\Validator;
19 use OpenEMR\Common\Uuid\UuidRegistry;
21 // TODO: @adunsulag should we rename this to be ListOptions service since that is the table it corresponds to? The lists table is a patient issues table so this could confuse new developers
22 class ListService
24 /**
25 * Default constructor.
27 public function __construct()
31 public function validate($list)
33 $validator = new Validator();
35 $validator->required('title')->lengthBetween(2, 255);
36 $validator->required('type')->lengthBetween(2, 255);
37 $validator->required('pid')->numeric();
38 $validator->optional('diagnosis')->lengthBetween(2, 255);
39 $validator->optional('begdate')->datetime('Y-m-d H:i:s');
40 $validator->optional('enddate')->datetime('Y-m-d H:i:s');
42 return $validator->validate($list);
45 public function getAll($pid, $list_type)
47 $sql = "SELECT * FROM lists WHERE pid=? AND type=? ORDER BY date DESC";
49 $statementResults = sqlStatement($sql, array($pid, $list_type));
51 $results = array();
52 while ($row = sqlFetchArray($statementResults)) {
53 $row['uuid'] = UuidRegistry::uuidToString($row['uuid']);
54 array_push($results, $row);
57 return $results;
60 public function getListOptionsForLists($lists)
62 $sql = "SELECT * FROM list_options WHERE list_id IN (" . str_repeat('?,', count($lists) - 1) . "?) "
63 . " ORDER BY list_id, seq";
64 $records = QueryUtils::fetchRecords($sql, $lists, false);
65 return $records;
68 public function getListIds()
70 $sql = "SELECT DISTINCT list_id FROM list_options ORDER BY list_id";
71 return QueryUtils::fetchTableColumn($sql, 'list_id', []);
74 public function getOptionsByListName($list_name, $search = array())
76 $sql = "SELECT * FROM list_options WHERE list_id = ? ";
77 $binding = [$list_name];
80 $whitelisted_columns = [
81 "option_id", "seq", "is_default", "option_value", "mapping", "notes", "codes", "activity", "edit_options", "toggle_setting_1", "toggle_setting_2", "subtype"
83 foreach ($whitelisted_columns as $column) {
84 if (!empty($search[$column])) {
85 $sql .= " AND $column = ? ";
86 $binding[] = $search[$column];
89 $sql .= " ORDER BY `seq` ";
91 $statementResults = sqlStatementThrowException($sql, $binding);
93 $results = array();
94 while ($row = sqlFetchArray($statementResults)) {
95 array_push($results, $row);
98 return $results;
102 * Returns the list option record that was found
103 * @param $list_id
104 * @param $option_id
105 * @param array $search
106 * @return array Record
108 public function getListOption($list_id, $option_id)
110 $records = $this->getOptionsByListName($list_id, ['option_id' => $option_id]);
111 if (!empty($records)) { // should only be one record
112 return $records[0];
114 return null;
117 public function getOne($pid, $list_type, $list_id)
119 $sql = "SELECT * FROM lists WHERE pid=? AND type=? AND id=? ORDER BY date DESC";
121 return sqlQuery($sql, array($pid, $list_type, $list_id));
124 public function insert($data)
126 $sql = " INSERT INTO lists SET";
127 $sql .= " date=NOW(),";
128 $sql .= " activity=1,";
129 $sql .= " pid=?,";
130 $sql .= " type=?,";
131 $sql .= " title=?,";
132 $sql .= " begdate=?,";
133 $sql .= " enddate=?,";
134 $sql .= " diagnosis=?";
136 return sqlInsert(
137 $sql,
138 array(
139 $data['pid'],
140 $data['type'],
141 $data["title"],
142 $data["begdate"],
143 $data["enddate"],
144 $data["diagnosis"]
149 public function update($data)
151 $sql = " UPDATE lists SET";
152 $sql .= " title=?,";
153 $sql .= " begdate=?,";
154 $sql .= " enddate=?,";
155 $sql .= " diagnosis=?";
156 $sql .= " WHERE id=?";
158 return sqlStatement(
159 $sql,
160 array(
161 $data["title"],
162 $data["begdate"],
163 $data["enddate"],
164 $data["diagnosis"],
165 $data["id"]
170 public function delete($pid, $list_id, $list_type)
172 $sql = "DELETE FROM lists WHERE pid=? AND id=? AND type=?";
174 return sqlStatement($sql, array($pid, $list_id, $list_type));