quick minor path updates (#1968)
[openemr.git] / library / registry.inc
blob4d7e3f5820552fd93fded28241b4b308a2a79a20
1 <?php
2 //these are the functions used to access the forms registry database
3 //
6 function registerForm($directory, $sql_run = 0, $unpackaged = 1, $state = 0)
8     $check = sqlQuery("select state from registry where directory=?", array($directory));
9     if ($check == false) {
10         $lines = @file($GLOBALS['srcdir']."/../interface/forms/$directory/info.txt");
11         if ($lines) {
12             $name = $lines[0];
13         } else {
14             $name = $directory;
15         }
17         return sqlInsert("insert into registry set
18                         name=?,
19                         state=?,
20                         directory=?,
21                         sql_run=?,
22                         unpackaged=?,
23                         date=NOW()
24                 ", array($name, $state, $directory, $sql_run, $unpackaged));
25     }
27     return false;
30 function updateRegistered($id, $mod)
32     return sqlInsert("update registry set $mod, date=NOW() where id=?", array($id));
35 /**
36  * @param string $state
37  * @param string $limit
38  * @param string $offset
39  * @param string $encounterType all|patient|therapy_group
40  */
41 function getRegistered($state = "1", $limit = "unlimited", $offset = "0", $encounterType = 'all')
43     $sql = "select * from registry where state like ? ";
44     if ($encounterType !== 'all') {
45         switch ($encounterType) {
46             case 'patient':
47                 $sql .= 'AND patient_encounter = 1 ';
48                 break;
49             case 'therapy_group':
50                 $sql .= 'AND therapy_group_encounter = 1 ';
51                 break;
52         }
53     }
54     $sql .= "order by priority, name ";
55     if ($limit != "unlimited") {
56         $sql .= " limit " . escape_limit($limit) . ", " . escape_limit($offset);
57     }
59     $res = sqlStatement($sql, array($state));
60     if ($res) {
61         for ($iter=0; $row=sqlFetchArray($res); $iter++) {
62             $all[$iter] = $row;
63         }
64     } else {
65         return false;
66     }
68     return $all;
71 function getRegistryEntry($id, $cols = "*")
73     $sql = "select $cols from registry where id=?";
74     return sqlQuery($sql, array($id));
77 function getRegistryEntryByDirectory($directory, $cols = "*")
79     $sql = "select $cols from registry where directory = ?";
80     return sqlQuery($sql, $directory);
83 function installSQL($dir)
85     $sqltext = $dir."/table.sql";
86     if ($sqlarray = @file($sqltext)) {
87         $sql = implode("", $sqlarray);
88         //echo "<br>$sql<br><br>";
89         $sqla = explode(";", $sql);
90         foreach ($sqla as $sqlq) {
91             if (strlen($sqlq) > 5) {
92                 sqlStatement(rtrim("$sqlq"));
93             }
94         }
96         return true;
97     } else {
98         return false;
99     }
104  * is a form registered
105  *  (optional - and active)
106  * in the database?
108  * NOTE - sometimes the Name of a form has a line-break at the end, thus this function might be better
110  *  INPUT =   directory => form directory
111  *            state => 0=inactive / 1=active
112  *  OUTPUT = true or false
113  */
114 function isRegistered($directory, $state = 1)
116     $sql = "select id from registry where directory=? and state=?";
117     $result = sqlQuery($sql, array($directory, $state));
118     if ($result['id'] != '') {
119         return true;
120     }
122     return false;
125 function getTherapyGroupCategories()
128     return array('');
131 // This gets an array including both standard and LBF visit form types,
132 // one row per form type, sorted by category, priority, is lbf, name.
134 function getFormsByCategory($state = '1', $lbfonly = false)
136     $all = array();
137     if (!$lbfonly) {
138       // First get the traditional form types from the registry table.
139         $sql = "SELECT category, nickname, name, state, directory, id, sql_run, " .
140         "unpackaged, date, priority, aco_spec FROM registry WHERE " .
141         "state LIKE ? ORDER BY category, priority, name";
142         $res = sqlStatement($sql, array($state));
143         if ($res) {
144             while ($row = sqlFetchArray($res)) {
145               // Skip fee_sheet from list of registered forms.
146                 if ($row['directory'] != 'fee_sheet') {
147                   // Flag this entry as not LBF
148                     $row['LBF'] = false;
149                     $all[] = $row;
150                 }
151             }
152         }
153     }
155   // Merge LBF form types into the registry array of form types.
156   // Note that the mapping value is used as the category name.
157     $lres = sqlStatement("SELECT * FROM layout_group_properties " .
158     "WHERE grp_form_id LIKE 'LBF%' AND grp_group_id = '' AND grp_activity = 1 " .
159     "ORDER BY grp_mapping, grp_seq, grp_title");
160     while ($lrow = sqlFetchArray($lres)) {
161         $rrow = array();
162         $rrow['category']  = $lrow['grp_mapping'] ? $lrow['grp_mapping'] : 'Clinical';
163         $rrow['name']      = $lrow['grp_title'];
164         $rrow['nickname']  = $lrow['grp_title'];
165         $rrow['directory'] = $lrow['grp_form_id']; // should start with LBF
166         $rrow['priority']  = $lrow['grp_seq'];
167         $rrow['aco_spec']  = $lrow['grp_aco_spec'];
168         $rrow['LBF']       = true; // Flag this form as LBF
169         $all[] = $rrow;
170     }
172   // Sort by category, priority, is lbf, name.
173     usort($all, function ($a, $b) {
174       // Anonymous functions supported as of PHP 5.3. Yay!
175         if ($a['category'] == $b['category']) {
176             if ($a['priority'] == $b['priority']) {
177                 if ($a['LBF'] == $b['LBF']) {
178                     $name1 = $a['nickname'] ? $a['nickname'] : $a['name'];
179                     $name2 = $b['nickname'] ? $b['nickname'] : $b['name'];
180                     if ($name1 == $name2) {
181                         return 0;
182                     }
183                     return $name1 < $name2 ? -1 : 1;
184                 } else {
185                   // Sort LBF with the same priority after standard forms
186                     return $b['LBF'] ? -1 : 1;
187                 }
188             }
189             return $a['priority'] < $b['priority'] ? -1 : 1;
190         }
191         return $a['category'] < $b['category'] ? -1 : 1;
192     });
193     return $all;