Add appointment service status methods. (#4757)
[openemr.git] / src / Rx / RxList.php
blobf887d8208aac18b4a869064e192742d8501ddd4a
1 <?php
3 /**
4 * RXList.class.php
6 * Revision 1.6 2019/02/18 avanss llc
7 * replaced rxlist API with rxnav.nlm.nih.gov
9 * Revision 1.5 2016/02/016 sherwin gaddis
10 * fix broken url
12 * Revision 1.4 2008/05/09 20:10:28 cfapress
13 * Changes to handle to HTML returned by rxlist.com
15 * Revision 1.3 2007/06/19 02:34:45 sunsetsystems
16 * drug lookup fix from Sam Rajan
18 * Revision 1.2 2005/12/27 00:45:19 sunsetsystems
19 * fix broken url
21 * Revision 1.1.1.3 2005/06/23 05:25:49 drbowen
22 * Initial import.
24 * Revision 1.1.1.2 2005/03/28 00:44:54 drbowen
25 * Initial import.
27 * Revision 1.1.1.1 2005/03/09 19:59:43 wpennington
28 * First import of OpenEMR
30 * Revision 1.2 2002/08/06 13:49:08 rufustfirefly
31 * updated rxlist class for changes in RxList.com
33 * Revision 1.1 2002/07/08 14:42:25 rufustfirefly
34 * RxList.com prescription "module" for formulary
36 * @package OpenEMR
37 * @link https://www.open-emr.org
38 * @author rufustfirefly
39 * @author wpennington
40 * @author drbowen
41 * @author sunsetsystems
42 * @author cfapress
43 * @author sherwin gaddis
44 * @author avanss llc
45 * @author Brady Miller <brady.g.miller@gmail.com>
46 * @author Jerry Padgett <sjpadgett@gmail.com>
47 * @author Stephen Waite <stephen.waite@cmsvt.com>
48 * @copyright Copyright (c) 2002 rufustfirefly
49 * @copyright Copyright (c) 2005 wpennington
50 * @copyright Copyright (c) 2005 drbowen
51 * @copyright Copyright (c) 2005-2007 sunsetsystems
52 * @copyright Copyright (c) 2008 cfapress
53 * @copyright Copyright (c) 2016 sherwin gaddis
54 * @copyright Copyright (c) 2019 avanss llc
55 * @copyright Copyright (c) 2019 Brady Miller <brady.g.miller@gmail.com>
56 * @copyright Copyright (c) 2019-2021 Jerry Padgett <sjpadgett@gmail.com>
57 * @copyright Copyright (c) 2021 Stephen Waite <stephen.waite@cmsvt.com>
58 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
61 namespace OpenEMR\Rx;
63 use OpenEMR\Common\Http\oeHttp;
65 class RxList
68 public function getPage($query)
70 $url = "https://rxnav.nlm.nih.gov/REST/Prescribe/drugs";
71 $response = oeHttp::get($url, ['name' => $query]);
72 $buffer = $response->body();
73 return $buffer ?: false;
76 public function getList($query)
78 $page = $this->getPage($query);
79 $tokens = $this->parseToTokens($page);
80 $hash = $this->tokensToHash($tokens);
81 if (!empty($hash)) {
82 foreach ($hash as $index => $data) {
83 unset($my_data);
84 foreach ($data as $k => $v) {
85 $my_data[$k] = $v;
88 $rxcui = '';
90 if (trim($my_data['rxcui']) !== '') {
91 $rxcui = " (RxCUI:" . trim($my_data['rxcui'] . ")");
94 $synonym = '';
95 if (trim($my_data['synonym']) !== '') {
96 $synonym = " | (" . trim($my_data['synonym']) . ")";
99 $list[trim($my_data['name'] . $rxcui) . $synonym] =
100 trim($my_data['name']);
103 return $list;
106 /* break the web page into a collection of TAGS
107 * such as <input ..> or <img ... >
109 public function parseToTokens($page)
111 $pos = 0;
112 $token = 0;
113 unset($tokens);
114 $in_token = false;
115 while ($pos < strlen($page)) {
116 switch (substr($page, $pos, 1)) {
117 case "<":
118 if ($in_token) {
119 $token++;
120 $in_token = false;
123 $tokens[$token] .= substr($page, $pos, 1);
124 $in_token = true;
125 break;
127 case ">":
128 $tokens[$token] .= substr($page, $pos, 1);
129 $in_token = false;
130 $token++;
131 break;
133 default:
134 $tokens[$token] .= substr($page, $pos, 1);
135 $in_token = false;
136 break;
138 $pos++;
140 return $tokens;
143 public function tokensToHash($tokens)
145 $record = false;
146 $current = 0;
147 unset($hash);
148 $hash = [];
149 unset($all);
150 for ($pos = 0, $posMax = count($tokens); $pos < $posMax; $pos++) {
151 if ((bool)str_contains($tokens[$pos], "<name>") && $pos !== 3) {
152 // found a brand line 'token'
153 $type = "name";
154 $record = $pos;
155 $ending = "</name>";
158 if ((bool)str_contains($tokens[$pos], "<synonym>")) {
159 // found a generic line 'token'
160 $type = "synonym";
161 //print "generic_name record start at $pos<BR>\n";
162 $ending = "</synonym>";
163 $record = $pos;
166 if ((bool)str_contains($tokens[$pos], "<rxcui>")) {
167 // found a drug-class 'token'
168 $type = "rxcui";
169 $ending = "</rxcui>";
170 $record = $pos;
173 if (isset($hash["synonym"])) {
174 // Reset record
175 $record = false;
176 // Add hash to all
177 $all[] = $hash;
178 // Unset hash and type
179 unset($hash);
180 $type = "";
181 $ending = "";
184 if ($pos === ($record + 1) and ($ending != "")) {
185 $my_pos = stripos($tokens[$pos], "<");
186 $hash[$type] = substr($tokens[$pos], 0, $my_pos);
187 $hash[$type] = str_replace("&amp;", "&", $hash[$type]);
188 //print "hash[$type] = ".htmlentities($hash[$type])."<BR>\n";
189 $type = "";
190 $ending = "";
193 return $all;