PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/ART/includes/patients_con.php

https://bitbucket.org/vincentbii/amurt
PHP | 447 lines | 325 code | 14 blank | 108 comment | 16 complexity | 6baef1fc1361e54276d5fa96afb47ec9 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, MIT, BSD-3-Clause, LGPL-2.0, GPL-3.0
  1. <?
  2. // vim: ts=4:sw=4:fdc=4:nu:nospell
  3. /**
  4. 4. * Lightweight SQL class suitable for public use and sqlite access
  5. 5. *
  6. 6. * @author Ing. Jozef Sakáloš
  7. 7. * @copyright (c) 2008, by Ing. Jozef Sakáloš
  8. 8. * @date 31. March 2008
  9. 9. * @version $Id: csql.php 820 2010-02-14 18:30:45Z jozo $
  10. 10. */
  11. // {{{
  12. /**
  13. 14. * regexp: callback for sqlite REGEXP operator
  14. 15. *
  15. 16. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  16. 17. * @date 01. April 2008
  17. 18. * @return boolean true if value matches regular expression
  18. 19. * @param string $search string to find
  19. 20. * @param string $value string to search in
  20. 21. */
  21. function regexp($search, $value) {
  22. // case insensitive hard coded
  23. return @preg_match("/$search/i", $value);
  24. }
  25. // }}}
  26. /**
  27. 29. * concat_ws: sqlite custom function
  28. 30. *
  29. 31. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  30. 32. * @date 11. May 2008
  31. 33. * @return string
  32. 34. * @param mixed
  33. 35. */
  34. function concat_ws() {
  35. $args = func_get_args();
  36. $sep = array_shift($args);
  37. return implode($sep, $args);
  38. } // eo function concat_ws
  39. // {{{
  40. /**
  41. 43. * Quote array callback function
  42. 44. *
  43. 45. * This is walk array callback function that
  44. 46. * surrounds array element with quotes.
  45. 47. *
  46. 48. * @date 08. September 2003
  47. 49. * @access public
  48. 50. * @return void
  49. 51. * @param string &$val Array element to be quouted
  50. 52. * @param mixed $key Dummy. Not used in the function.
  51. 53. * @param string $quot Quoute to use. Double quote by default
  52. 54. */
  53. function quote_array(&$val, $key, $quot = '"') {
  54. $quot_right = array_key_exists(1, (array) $quot) ? $quot[1] : $quot[0];
  55. $val = is_null($val) ? "null" : $quot[0] . preg_replace("/'/", "''", $val) . $quot_right;
  56. }
  57. // }}}
  58. /**
  59. 62. * csql class
  60. 63. *
  61. 64. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  62. 65. * @copyright (c) 2008 by Ing. Jozef Sakáloš
  63. 66. * @date 31. March 2008
  64. 67. */
  65. class csql {
  66. // protected functions
  67. // {{{
  68. /**
  69. 73. * getOdb: Creates PDO object
  70. 74. *
  71. 75. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  72. 76. * @date 31. March 2008
  73. 77. * @access protected
  74. 78. * @return PDO
  75. 79. * @param string $engine
  76. 80. * @param string $file
  77. 81. */
  78. protected function getOdb($engine, $file) {
  79. switch($engine) {
  80. case "sqlite":
  81. if("/" !== $file[0]) {
  82. $file = realpath(".") . "/$file";
  83. }
  84. $odb = new PDO("sqlite:$file");
  85. $odb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  86. $odb->sqliteCreateFunction("regexp", "regexp", 2);
  87. $odb->sqliteCreateFunction("concat_ws", "concat_ws");
  88. break;
  89. }
  90. return $odb;
  91. } // eo function getOdb
  92. // }}}
  93. // {{{
  94. /**
  95. 100. * getWhere: return where clause
  96. 101. *
  97. 102. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  98. 103. * @date 01. April 2008
  99. 104. * @access protected
  100. 105. * @return string where clause including where keyword
  101. 106. * @param array $params
  102. 107. */
  103. protected function getWhere($params, $ignoreFilter = "") {
  104. extract($params);
  105. $where = isset($where) ? "where $where" : "";
  106. if($filters) {
  107. $a = array();
  108. foreach($filters as $f=>$value) {
  109. if($f === $ignoreFilter) {
  110. continue;
  111. }
  112. if(is_array($value)) {
  113. if(sizeof($value)) {
  114. array_walk($value, "quote_array", "'");
  115. $a[] = "$f in (" . implode(",", $value) . ")";
  116. }
  117. }
  118. else {
  119. $value = trim($value);
  120. $ab = preg_split("/(>)|(<)|(=)|(!)/", $value, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  121. if(preg_match("/\|/", $value)) {
  122. $value = preg_replace("/\s*\|\s*/", "|", $value);
  123. $aa = explode("|", $value);
  124. for($i = 0; $i < sizeof($aa); $i++) {
  125. $aa[$i] = "'" . $aa[$i] . "'";
  126. }
  127. $a[] = "$f in (" . implode(",", $aa) . ")";
  128. }
  129. else if(1 < sizeof($ab)) {
  130. $value = array_pop($ab);
  131. $operator = "";
  132. for($i = 0; $i < sizeof($ab); $i++) {
  133. // $operator .= ($ab[$i] === "!" ? "not " : $ab[$i]);
  134. $operator .= $ab[$i];
  135. }
  136. $a[] = "$f $operator '$value'";
  137. }
  138. else {
  139. $a[] = "$f regexp '$value'";
  140. }
  141. }
  142. }
  143. if(sizeof($a)) {
  144. $where .= $where ? " and(" : "where (";
  145. $where .= implode(" and ", $a) . ")";
  146. }
  147. }
  148. if($query && is_array($search) && sizeof($search)) {
  149. $a = array();
  150. foreach($search as $f) {
  151. $a[] = "$f regexp '$query'";
  152. }
  153. $where .= $where ? " and(" : "where (";
  154. $where .= implode(" or ", $a) . ")";
  155. }
  156. error_log("Where = " . $where);
  157. return $where;
  158. } // eo function getWhere
  159. // }}}
  160. // public functions
  161. // {{{
  162. /**
  163. 174. * __construct: Constructs the csql instance
  164. 175. *
  165. 176. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  166. 177. * @date 31. March 2008
  167. 178. * @access public
  168. 179. * @return void
  169. 180. * @param string $engine Engine to use
  170. 181. */
  171. public function __construct($engine = "sqlite", $file = "db.sqlite") {
  172. $this->odb = $this->getOdb($engine, $file);
  173. } // eo constructor
  174. // }}}
  175. // {{{
  176. /**
  177. 188. * getCount: Returns count of records in a table
  178. 189. *
  179. 190. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  180. 191. * @date 31. March 2008
  181. 192. * @access public
  182. 193. * @return integer number of records
  183. 194. * @param array $params
  184. 195. */
  185. public function getCount($params) {
  186. $count = null;
  187. $countArg = $params["distinct"] ? "distinct " . $params["distinct"] : "*";
  188. $ostmt = $this->odb->prepare("select count($countArg) from {$params['table']} " . $this->getWhere($params));
  189. $ostmt->bindColumn(1, $count);
  190. $ostmt->execute();
  191. $ostmt->fetch();
  192. return (int) $count;
  193. } // eo function getCount
  194. // }}}
  195. // {{{
  196. /**
  197. 208. * getData: Retrieves data and returns array of objects
  198. 209. *
  199. 210. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  200. 211. * @date 31. March 2008
  201. 212. * @access public
  202. 213. * @return array
  203. 214. * @param array $params Associative array with:
  204. 215. * - string table (Mandatory)
  205. 216. * - array field (Mandatory)
  206. 217. * - integer start (Optional)
  207. 218. * - integer limit (Optional)
  208. 219. * - string sort (Optional)
  209. 220. * - array search (Optional) fields to search in
  210. 221. * - string where (Optional)
  211. 222. */
  212. public function getData($params) {
  213. // params to variables
  214. extract($params);
  215. $sql = "select " . ($distinct ? "distinct " : "");
  216. $sql .= implode(",", $fields);
  217. $sql .= " from $table " . $this->getWhere($params);
  218. $sql .= isset($groupBy) && $groupBy ? " group by $groupBy" : "";
  219. if(!is_null($sort)) {
  220. $sql .= " order by $sort";
  221. $sql .= is_null($dir) ? "" : " $dir";
  222. }
  223. if(!is_null($start) && !is_null($limit)) {
  224. $sql .= " limit $start,$limit";
  225. }
  226. $ostmt = $this->odb->query($sql);
  227. return $ostmt->fetchAll(PDO::FETCH_OBJ);
  228. } // eo function getData
  229. // }}}
  230. /**
  231. 247. * getFilterRows: short description of
  232. 248. *
  233. 249. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  234. 250. * @date 05. February 2010
  235. 251. * @access public
  236. 252. * @return mixed Description
  237. 253. * @param
  238. 254. */
  239. public function getFilterRows($name, $params, $ignoreMyself = false) {
  240. extract($params);
  241. $ignoreFilter = $ignoreMyself ? $name : "";
  242. $sql = "select distinct $name from $table " . $this->getWhere($params, $ignoreFilter) . " order by $name";
  243. error_log($sql);
  244. $ostmt = $this->odb->query($sql);
  245. $rows = $ostmt->fetchAll(PDO::FETCH_NUM);
  246. $a = array();
  247. foreach($rows as $row) {
  248. $a[] = $row[0];
  249. }
  250. return $a;
  251. // return $rows;
  252. } // eo function getFilterRows
  253. // {{{
  254. /**
  255. 272. * insertRecord: inserts record to table
  256. 273. *
  257. 274. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  258. 275. * @date 01. April 2008
  259. 276. * @access public
  260. 277. * @return integer id of the inserted record
  261. 278. * @param array $params
  262. 279. */
  263. public function insertRecord($params) {
  264. // params to vars
  265. extract($params);
  266. $o = new stdClass();
  267. $o->success = false;
  268. $a = "object" === gettype($data) ? get_object_vars($data) : $data;
  269. unset($a["newRecord"]);
  270. if($idName) {
  271. unset($a[$idName]);
  272. }
  273. $fields = array_keys($a);
  274. $values = array_values($a);
  275. array_walk($values, "quote_array", "'");
  276. $sql = "insert into $table (" . implode(",", $fields) . ") values (" . implode(",", $values) . ")";
  277. try {
  278. $this->odb->exec($sql);
  279. $o->success = true;
  280. $o->insertId = $this->odb->lastInsertId();
  281. }
  282. catch(PDOException $e) {
  283. $o->error = "$e";
  284. }
  285. return $o;
  286. } // eo function insertRecord
  287. // }}}
  288. // {{{
  289. /**
  290. 313. * saveData: saves data (updates or inserts)
  291. 314. *
  292. 315. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  293. 316. * @date 01. April 2008
  294. 317. * @access public
  295. 318. * @return object either {success:true} or {success:false,error:message}
  296. 319. * @param array $params
  297. 320. */
  298. public function saveData($params) {
  299. // params to vars
  300. // return object
  301. $o = new stdClass;
  302. $o->success = false;
  303. if(!$table || !is_array($data) || !$idName) {
  304. $o->error = "Table, data or idName is missing";
  305. return $o;
  306. }
  307. // record loop
  308. $p = array(
  309. "table"=>$table
  310. ,"idName"=>$idName
  311. );
  312. $this->odb->exec("begin transaction");
  313. foreach($data as $orec) {
  314. $p["data"] = $orec;
  315. // insert/update switch
  316. if(isset($orec->newRecord) && $orec->newRecord) {
  317. $result = $this->insertRecord($p);
  318. }
  319. else {
  320. $result = $this->updateRecord($p);
  321. }
  322. // handle error
  323. if(true !== $result->success) {
  324. $o->success = false;
  325. $o->error = $result->error;
  326. $this->odb->exec("rollback");
  327. return $o;
  328. }
  329. else {
  330. $o->success = true;
  331. }
  332. // handle insertId if any
  333. if(isset($result->insertId)) {
  334. $o->insertIds[] = $result->insertId;
  335. }
  336. } // eo record loop
  337. $this->odb->exec("commit");
  338. return $o;
  339. } // eo function saveData
  340. // }}}
  341. // {{{
  342. /**
  343. 375. * updateRecord: updtates one record in table
  344. 376. *
  345. 377. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  346. 378. * @date 31. March 2008
  347. 379. * @access public
  348. 380. * @return object either {success:true} or {success:false,error:message}
  349. 381. * @param array $params with:
  350. 382. * - string table (Mandatory)
  351. 383. * - string idName (Mandatory) name of id field
  352. 384. * - object data (Mandatory) Name/value pairs object or associative array
  353. 385. */
  354. public function updateRecord($params) {
  355. // array to vars
  356. extract($params);
  357. $o = new stdClass();
  358. $o->success = false;
  359. if(!isset($table) || !isset($idName) || !isset($data)) {
  360. $o->error = "Table, idName or data not set.";
  361. return $o;
  362. }
  363. $asets = array();
  364. $where = "";
  365. foreach($data as $field => $value) {
  366. if($idName === $field) {
  367. $where = " where $field='$value'";
  368. continue;
  369. }
  370. array_push($asets, "$field=" . (is_null($value) ? "null" : "'$value'"));
  371. }
  372. if(!$where) {
  373. $o->error = "idName not found in data";
  374. return $o;
  375. }
  376. $sql = "update $table set " . implode(",", $asets) . $where;
  377. try {
  378. $this->odb->exec($sql);
  379. $o->success = true;
  380. }
  381. catch(PDOException $e) {
  382. $o->error = "$e";
  383. }
  384. return $o;
  385. } // eo function updateRecord
  386. // }}}
  387. // {{{
  388. /**
  389. * output: Encodes json object and sends it to client
  390. *
  391. * @author Ing. Jozef Sakáloš <jsakalos@aariadne.com>
  392. * @date 31. March 2008
  393. * @access protected
  394. * @return void
  395. * @param object/array $o
  396. * @param string $contentType
  397. */
  398. public function output($o = null, $contentType = "application/json; charset=utf-8") {
  399. $o = $o ? $o : $this->o;
  400. $buff = json_encode($o);
  401. header("Content-Type: {$contentType}");
  402. header("Content-Size: " . strlen($buff));
  403. echo $buff;
  404. } // eo function output
  405. // }}}
  406. } // eo class csql
  407. // eof