PatientInterface.class.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. <?php
  2. namespace Models {
  3. require_once 'Models/User.class.php';
  4. require_once 'Tools/Random.class.php';
  5. class PatientInterface {
  6. //
  7. protected $DataInterface;
  8. /**
  9. *
  10. */
  11. public function __construct($DataInterface) {
  12. $this->DataInterface = $DataInterface;
  13. }
  14. /**
  15. * Get patient files existing data.
  16. */
  17. public function patientFilesExistingGet($User) {
  18. $ID_user = $User->ID;
  19. $where = ' P.ID = V.fk_patient ';
  20. if($User->type=='physician') {
  21. $where .= ' AND P.fk_user = '.$User->ID;
  22. }
  23. else if($User->type=='reader') {
  24. $where .= ' AND V.fk_reader = '.$User->ID.' AND V.completed IS NULL ';
  25. }
  26. else {
  27. $where .= ' AND P.fk_user = '.$User->ID;
  28. }
  29. $statement = $this->DataInterface->DatabaseConnection->prepare(
  30. "SELECT JSON_EXTRACT(data, '$.patientListFields') AS data FROM settings"
  31. );
  32. if(!$statement->execute()) {
  33. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  34. }
  35. $settings = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  36. $patientListFields=[];
  37. foreach($settings as $S) {
  38. $patientListFields[str_replace(' ', '_', $S->name)] = $S->display;
  39. }
  40. $statement = $this->DataInterface->DatabaseConnection->prepare(
  41. "SELECT
  42. P.*,
  43. (SELECT COUNT(visit.ID) FROM visit WHERE fk_patient = P.ID) as visitCount,
  44. (SELECT GROUP_CONCAT(CONCAT(visit.ID, ',', visit.visitDate, ',', COALESCE(visit.area, '')) ORDER BY visit.visitDate DESC SEPARATOR ';') FROM visit WHERE fk_patient = P.ID) as visits,
  45. (SELECT GROUP_CONCAT(CONCAT(visit.ID, ',', visit.number) ORDER BY visit.visitDate DESC SEPARATOR ';') FROM visit WHERE fk_patient = P.ID) as reader_visits,
  46. (SELECT COUNT(media.ID) FROM media, visit WHERE JSON_VALUE(media.metrics, '$.fps') IS NULL AND media.fk_visit = visit.ID AND visit.fk_patient = P.ID) as imageCount,
  47. (SELECT COUNT(media.ID) FROM media, visit WHERE media.fk_visit = visit.ID AND visit.fk_patient = P.ID) as mediaCount,
  48. V.visitDate AS lastVisit,
  49. V.number AS visitNumber,
  50. V.ID AS visitID
  51. FROM patient P, visit V
  52. WHERE $where
  53. GROUP BY P.ID
  54. ORDER BY V.visitDate DESC, V.ID DESC"
  55. );
  56. if(!$statement->execute()) {
  57. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  58. }
  59. $visits = $statement->fetchAll(\PDO::FETCH_ASSOC);
  60. return [
  61. 'result' => 'OK',
  62. 'visits' => $visits,
  63. 'user' => $User,
  64. 'patientListFields' => $patientListFields
  65. ];
  66. }
  67. /**
  68. * Get patient files existing data.
  69. */
  70. public function patientFilesExistingPost($User, $data) {
  71. $ID_user = $User->ID;
  72. $where = ' P.ID = V.fk_patient ';
  73. if($User->type=='physician') {
  74. $where .= ' AND P.fk_user = '.$User->ID;
  75. }
  76. else if($User->type=='reader') {
  77. $where .= ' AND V.fk_reader = '.$User->ID.' AND V.completed IS NULL ';
  78. }
  79. else {
  80. $where .= ' AND P.fk_user = '.$User->ID;
  81. }
  82. $filter = '(P.lastname LIKE \'%'.$data['filter'].'%\' OR P.firstname LIKE \'%'.$data['filter'].'%\')';
  83. $statement = $this->DataInterface->DatabaseConnection->prepare(
  84. "SELECT JSON_EXTRACT(data, '$.patientListFields') AS data FROM settings"
  85. );
  86. if(!$statement->execute()) {
  87. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  88. }
  89. $settings = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  90. $patientListFields=[];
  91. foreach($settings as $S) {
  92. $patientListFields[str_replace(' ', '_', $S->name)] = $S->display;
  93. }
  94. $statement = $this->DataInterface->DatabaseConnection->prepare(
  95. "SELECT
  96. P.*,
  97. (SELECT COUNT(visit.ID) FROM visit WHERE fk_patient = P.ID) as visitCount,
  98. (SELECT GROUP_CONCAT(CONCAT(visit.ID, ',', visit.visitDate, ',', COALESCE(visit.area, '')) ORDER BY visit.visitDate DESC SEPARATOR ';') FROM visit WHERE fk_patient = P.ID) as visits,
  99. (SELECT GROUP_CONCAT(CONCAT(visit.ID, ',', visit.number) ORDER BY visit.visitDate DESC SEPARATOR ';') FROM visit WHERE fk_patient = P.ID) as reader_visits,
  100. (SELECT COUNT(media.ID) FROM media, visit WHERE JSON_VALUE(media.metrics, '$.fps') IS NULL AND media.fk_visit = visit.ID AND visit.fk_patient = P.ID) as imageCount,
  101. (SELECT COUNT(media.ID) FROM media, visit WHERE media.fk_visit = visit.ID AND visit.fk_patient = P.ID) as mediaCount,
  102. V.visitDate AS lastVisit,
  103. V.number AS visitNumber,
  104. V.ID AS visitID
  105. FROM patient P, visit V
  106. WHERE $where AND $filter
  107. GROUP BY P.ID
  108. ORDER BY V.visitDate DESC, V.ID DESC"
  109. );
  110. if(!$statement->execute()) {
  111. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  112. }
  113. $visits = $statement->fetchAll(\PDO::FETCH_ASSOC);
  114. return [
  115. 'result' => 'OK',
  116. 'visits' => $visits,
  117. 'user' => $User,
  118. 'patientListFields' => $patientListFields
  119. ];
  120. }
  121. /**
  122. * Get patient files new data.
  123. */
  124. public function patientFilesNewGet($User, $lang) {
  125. $statement = $this->DataInterface->DatabaseConnection->prepare(
  126. "SELECT JSON_EXTRACT(data, '$.newPatientFields') AS data FROM settings"
  127. );
  128. if(!$statement->execute()) {
  129. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  130. }
  131. $settings = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  132. $patientListFields=[];
  133. foreach($settings as $S) {
  134. $patientListFields[str_replace(' ', '_', $S->name)] = $S->display;
  135. $patientListFields[str_replace(' ', '_', $S->name).'_required'] = $S->required;
  136. }
  137. $statement = $this->DataInterface->DatabaseConnection->prepare(
  138. "SELECT *, name_$lang AS name FROM country ORDER BY name_$lang"
  139. );
  140. if(!$statement->execute()) {
  141. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  142. }
  143. $countries = $statement->fetchAll(\PDO::FETCH_ASSOC);
  144. return [
  145. 'result' => 'OK',
  146. 'countries' => $countries,
  147. 'patientID' => \Tools\Random::getString(10),
  148. 'visitNumber' => 1,
  149. 'patientListFields' => $patientListFields
  150. ];
  151. }
  152. /**
  153. * Get patient files pacs data.
  154. */
  155. public function patientFilesPacsGet($User) {
  156. //
  157. return [
  158. 'result' => 'OK'
  159. ];
  160. }
  161. /**
  162. * Post patient data.
  163. */
  164. public function patientCreatePost($User, $data) {
  165. $statement = $this->DataInterface->DatabaseConnection->prepare(
  166. "SELECT JSON_EXTRACT(data, '$.newPatientFields') AS data FROM settings"
  167. );
  168. if(!$statement->execute()) {
  169. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  170. }
  171. $settings = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  172. $patientListFields=[];
  173. foreach($settings as $S) {
  174. $patientListFields[str_replace(' ', '_', $S->name)] = $S->required;
  175. }
  176. if($data['type']=='investigator' && empty($data['patient']['patientID'])) {
  177. return ['result' => 'ERROR', 'reason' => 'patientID'];
  178. }
  179. if($data['type']=='investigator' && empty($data['patient']['visitNumber'])) {
  180. return ['result' => 'ERROR', 'reason' => 'visitNumber'];
  181. }
  182. if($patientListFields['Visit_date'] && empty($data['patient']['visitDate'])) {
  183. return ['result' => 'ERROR', 'reason' => 'visitDate'];
  184. }
  185. if(empty($data['patient']['visitDate'])) {
  186. $data['patient']['visitDate']=date('Y-m-d');
  187. }
  188. if($patientListFields['Firstname'] && empty($data['patient']['firstname'])) {
  189. return ['result' => 'ERROR', 'reason' => 'firstname'];
  190. }
  191. if(empty($data['patient']['firstname'])) {
  192. $data['patient']['firstname']='';
  193. }
  194. if($patientListFields['Lastname'] && empty($data['patient']['lastname'])) {
  195. return ['result' => 'ERROR', 'reason' => 'lastname'];
  196. }
  197. if(empty($data['patient']['lastname'])) {
  198. $data['patient']['lastname']='';
  199. }
  200. if($patientListFields['Sex'] && empty($data['patient']['gender'])) {
  201. return ['result' => 'ERROR', 'reason' => 'gender'];
  202. }
  203. if($patientListFields['Birthdate'] && empty($data['patient']['birthDate'])) {
  204. return ['result' => 'ERROR', 'reason' => 'birthDate'];
  205. }
  206. if(empty($data['patient']['birthDate'])) {
  207. $data['patient']['birthDate']=date('Y-m-d');
  208. }
  209. if($patientListFields['Birth_country'] && empty($data['patient']['birthCountry'])) {
  210. return ['result' => 'ERROR', 'reason' => 'birthCountry'];
  211. }
  212. if($patientListFields['Residence_country'] && empty($data['patient']['residenceCountry'])) {
  213. return ['result' => 'ERROR', 'reason' => 'residenceCountry'];
  214. }
  215. if($patientListFields['Height'] && empty($data['patient']['height'])) {
  216. return ['result' => 'ERROR', 'reason' => 'height'];
  217. }
  218. if($patientListFields['Weight'] && empty($data['patient']['weight'])) {
  219. return ['result' => 'ERROR', 'reason' => 'weight'];
  220. }
  221. if(empty($data['patient']['height'])) {
  222. $data['patient']['height'] = null;
  223. }
  224. if(empty($data['patient']['weight'])) {
  225. $data['patient']['weight'] = null;
  226. }
  227. // Begin transaction
  228. $this->DataInterface->DatabaseConnection->beginTransaction();
  229. // Insert patient
  230. if($data['type']=='investigator') {
  231. $statement = $this->DataInterface->DatabaseConnection->prepare(
  232. "INSERT INTO patient(patientID, ctPatientID, firstname, lastname, gender, birthDate, height, weight, race, fk_birthCountry, fk_residenceCountry, fk_user) VALUES(:patientID, :ctPatientID, :firstname, :lastname, :gender, :birthDate, :height, :weight, :race, :fk_birthCountry, :fk_residenceCountry, :fk_user)"
  233. );
  234. $statement->bindParam(':patientID', $data['patient']['patientID']);
  235. $statement->bindParam(':ctPatientID', $data['patient']['patientID']);
  236. }
  237. else if($data['type']=='physician') {
  238. $statement = $this->DataInterface->DatabaseConnection->prepare(
  239. "INSERT INTO patient(patientID, firstname, lastname, gender, birthDate, height, weight, race, fk_birthCountry, fk_residenceCountry, fk_user) VALUES(:patientID, :firstname, :lastname, :gender, :birthDate, :height, :weight, :race, :fk_birthCountry, :fk_residenceCountry, :fk_user)"
  240. );
  241. $statement->bindParam(':patientID', $data['patient']['patientID']);
  242. }
  243. $statement->bindParam(':firstname', $data['patient']['firstname']);
  244. $statement->bindParam(':lastname', $data['patient']['lastname']);
  245. $statement->bindParam(':gender', $data['patient']['gender']);
  246. $statement->bindParam(':birthDate', $data['patient']['birthDate']);
  247. $statement->bindParam(':height', $data['patient']['height']);
  248. $statement->bindParam(':weight', $data['patient']['weight']);
  249. $statement->bindParam(':race', $data['patient']['patientRace']);
  250. $statement->bindParam(':fk_birthCountry', $data['patient']['birthCountry']);
  251. $statement->bindParam(':fk_residenceCountry', $data['patient']['residenceCountry']);
  252. $statement->bindParam(':fk_user', $User->ID);
  253. // Error check
  254. if(!$statement->execute()) {
  255. $this->DataInterface->DatabaseConnection->rollback();
  256. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo(), 'data2' => $data];
  257. }
  258. $fk_patient = $this->DataInterface->DatabaseConnection->lastInsertId();
  259. // Insert visit
  260. $statement = $this->DataInterface->DatabaseConnection->prepare(
  261. "INSERT INTO visit(number, visitDate, fk_patient) VALUES(:number, :visitDate, :fk_patient)"
  262. );
  263. $statement->bindParam(':number', $data['patient']['visitNumber']);
  264. $statement->bindParam(':visitDate', $data['patient']['visitDate']);
  265. $statement->bindParam(':fk_patient', $fk_patient);
  266. // Error check
  267. if(!$statement->execute()) {
  268. $this->DataInterface->DatabaseConnection->rollback();
  269. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  270. }
  271. $fk_visit = $this->DataInterface->DatabaseConnection->lastInsertId();
  272. // Insert context
  273. $statement = $this->DataInterface->DatabaseConnection->prepare(
  274. "INSERT INTO context(fk_visit) VALUES(:fk_visit)"
  275. );
  276. $statement->bindParam(':fk_visit', $fk_visit);
  277. // Error check
  278. if(!$statement->execute()) {
  279. $this->DataInterface->DatabaseConnection->rollback();
  280. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  281. }
  282. // Commit
  283. $this->DataInterface->DatabaseConnection->commit();
  284. return [
  285. 'result' => 'OK',
  286. 'fk_patient' => $fk_patient,
  287. 'fk_visit' => $fk_visit
  288. ];
  289. }
  290. /**
  291. * Post patient data modification.
  292. */
  293. public function patientModifyPost($User, $data) {
  294. $statement = $this->DataInterface->DatabaseConnection->prepare(
  295. "SELECT JSON_EXTRACT(data, '$.newPatientFields') AS data FROM settings"
  296. );
  297. if(!$statement->execute()) {
  298. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  299. }
  300. $settings = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  301. $patientListFields=[];
  302. foreach($settings as $S) {
  303. $patientListFields[str_replace(' ', '_', $S->name)] = $S->required;
  304. }
  305. if(empty($data['patient']['patientID'])) {
  306. return ['result' => 'ERROR', 'reason' => 'patientID'];
  307. }
  308. if($patientListFields['Firstname'] && empty($data['patient']['firstname'])) {
  309. return ['result' => 'ERROR', 'reason' => 'firstname'];
  310. }
  311. if(empty($data['patient']['firstname'])) {
  312. $data['patient']['firstname']='';
  313. }
  314. if($patientListFields['Lastname'] && empty($data['patient']['lastname'])) {
  315. return ['result' => 'ERROR', 'reason' => 'lastname'];
  316. }
  317. if(empty($data['patient']['lastname'])) {
  318. $data['patient']['lastname']='';
  319. }
  320. if($patientListFields['Sex'] && empty($data['patient']['gender'])) {
  321. return ['result' => 'ERROR', 'reason' => 'gender'];
  322. }
  323. if($patientListFields['Birthdate'] && empty($data['patient']['birthDate'])) {
  324. return ['result' => 'ERROR', 'reason' => 'birthDate'];
  325. }
  326. if(empty($data['patient']['birthDate'])) {
  327. $data['patient']['birthDate']=date('Y-m-d');
  328. }
  329. if($patientListFields['Birth_country'] && empty($data['patient']['birthCountry'])) {
  330. return ['result' => 'ERROR', 'reason' => 'birthCountry'];
  331. }
  332. if($patientListFields['Residence_country'] && empty($data['patient']['residenceCountry'])) {
  333. return ['result' => 'ERROR', 'reason' => 'residenceCountry'];
  334. }
  335. if($patientListFields['Height'] && empty($data['patient']['height'])) {
  336. return ['result' => 'ERROR', 'reason' => 'height'];
  337. }
  338. if($patientListFields['Weight'] && empty($data['patient']['weight'])) {
  339. return ['result' => 'ERROR', 'reason' => 'weight'];
  340. }
  341. if(empty($data['patient']['height'])) {
  342. $data['patient']['height'] = null;
  343. }
  344. if(empty($data['patient']['weight'])) {
  345. $data['patient']['weight'] = null;
  346. }
  347. // Insert patient
  348. $patientID = $data['patient']['patientID'];
  349. $statement = $this->DataInterface->DatabaseConnection->prepare(
  350. "UPDATE patient SET firstname=:firstname, lastname=:lastname, gender=:gender, birthDate=:birthDate, height=:height, weight=:weight, race=:race, fk_birthCountry=:fk_birthCountry, fk_residenceCountry=:fk_residenceCountry WHERE patientID='$patientID'"
  351. );
  352. $statement->bindParam(':firstname', $data['patient']['firstname']);
  353. $statement->bindParam(':lastname', $data['patient']['lastname']);
  354. $statement->bindParam(':gender', $data['patient']['gender']);
  355. $statement->bindParam(':birthDate', $data['patient']['birthDate']);
  356. $statement->bindParam(':height', $data['patient']['height']);
  357. $statement->bindParam(':weight', $data['patient']['weight']);
  358. $statement->bindParam(':race', $data['patient']['patientRace']);
  359. $statement->bindParam(':fk_birthCountry', $data['patient']['birthCountry']);
  360. $statement->bindParam(':fk_residenceCountry', $data['patient']['residenceCountry']);
  361. // Error check
  362. if(!$statement->execute()) {
  363. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  364. }
  365. return [
  366. 'result' => 'OK'
  367. ];
  368. }
  369. /**
  370. * Post patient data.
  371. */
  372. public function patientCreateVisitPost($User, $data) {
  373. // Count visit
  374. $statement = $this->DataInterface->DatabaseConnection->prepare(
  375. "SELECT COUNT(ID) AS cnt FROM visit WHERE fk_patient = :fk_patient"
  376. );
  377. $statement->bindParam(':fk_patient', $data['patientID']);
  378. // Error check
  379. if(!$statement->execute()) {
  380. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  381. }
  382. $visitCount = intval($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['cnt']) + 1;
  383. // Insert visit
  384. $statement = $this->DataInterface->DatabaseConnection->prepare(
  385. "INSERT INTO visit(number, visitDate, fk_patient) VALUES(:number, NOW(), :fk_patient)"
  386. );
  387. $statement->bindParam(':number', $visitCount);
  388. $statement->bindParam(':fk_patient', $data['patientID']);
  389. // Error check
  390. if(!$statement->execute()) {
  391. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  392. }
  393. $fk_visit = $this->DataInterface->DatabaseConnection->lastInsertId();
  394. // Insert context
  395. $statement = $this->DataInterface->DatabaseConnection->prepare(
  396. "INSERT INTO context(fk_visit) VALUES(:fk_visit)"
  397. );
  398. $statement->bindParam(':fk_visit', $fk_visit);
  399. // Error check
  400. if(!$statement->execute()) {
  401. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  402. }
  403. return [
  404. 'result' => 'OK',
  405. 'fk_visit' => $fk_visit
  406. ];
  407. }
  408. /**
  409. * Post patient context data.
  410. */
  411. public function patientContextPost($User, $data) {
  412. $field = $data['what'];
  413. $values = json_encode($data['data']);
  414. $fk_visit = $data['patient']['visitID'];
  415. $statement = $this->DataInterface->DatabaseConnection->prepare(
  416. "UPDATE context SET $field = '$values' WHERE fk_visit = $fk_visit"
  417. );
  418. if(!$statement->execute()) {
  419. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  420. }
  421. return [
  422. 'result' => 'OK',
  423. 'field' => $data['what'],
  424. 'values' => $data['data']
  425. ];
  426. }
  427. /**
  428. * Get patient fix data.
  429. */
  430. public function patientFixGet($User, $patientID, $lang) {
  431. $statement = $this->DataInterface->DatabaseConnection->prepare(
  432. "SELECT JSON_EXTRACT(data, '$.newPatientFields') AS data FROM settings"
  433. );
  434. if(!$statement->execute()) {
  435. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  436. }
  437. $settings = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  438. $patientListFields=[];
  439. foreach($settings as $S) {
  440. $patientListFields[str_replace(' ', '_', $S->name)] = $S->display;
  441. $patientListFields[str_replace(' ', '_', $S->name).'_required'] = $S->required;
  442. }
  443. $statement = $this->DataInterface->DatabaseConnection->prepare(
  444. "SELECT *, name_$lang AS name FROM country ORDER BY name_$lang"
  445. );
  446. if(!$statement->execute()) {
  447. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  448. }
  449. $countries = $statement->fetchAll(\PDO::FETCH_ASSOC);
  450. $statement = $this->DataInterface->DatabaseConnection->prepare(
  451. "SELECT patient.* FROM patient WHERE patient.ID = $patientID"
  452. );
  453. if(!$statement->execute()) {
  454. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  455. }
  456. $patient = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  457. //
  458. return [
  459. 'result' => 'OK',
  460. 'patientListFields' => $patientListFields,
  461. 'countries' => $countries,
  462. 'patient' => $patient
  463. ];
  464. }
  465. /**
  466. * Get patient risks data.
  467. */
  468. public function patientRisksGet($User, $fk_visit) {
  469. $statement = $this->DataInterface->DatabaseConnection->prepare(
  470. "SELECT patient.* FROM patient, visit WHERE patient.ID = visit.fk_patient AND visit.ID = $fk_visit"
  471. );
  472. if(!$statement->execute()) {
  473. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  474. }
  475. $patient = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  476. $statement = $this->DataInterface->DatabaseConnection->prepare(
  477. "SELECT ID, risks FROM context WHERE fk_visit = $fk_visit"
  478. );
  479. if(!$statement->execute()) {
  480. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  481. }
  482. $context = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  483. //
  484. return [
  485. 'result' => 'OK',
  486. 'patient' => $patient,
  487. 'context' => $context
  488. ];
  489. }
  490. /**
  491. * Get patient history data.
  492. */
  493. public function patientHistoryGet($User, $fk_visit, $lang) {
  494. $statement = $this->DataInterface->DatabaseConnection->prepare(
  495. "SELECT patient.* FROM patient, visit WHERE patient.ID = visit.fk_patient AND visit.ID = $fk_visit"
  496. );
  497. if(!$statement->execute()) {
  498. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  499. }
  500. $patient = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  501. $statement = $this->DataInterface->DatabaseConnection->prepare(
  502. "SELECT ID, phistory FROM context WHERE fk_visit = $fk_visit"
  503. );
  504. if(!$statement->execute()) {
  505. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  506. }
  507. $context = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  508. $statement = $this->DataInterface->DatabaseConnection->prepare(
  509. "SELECT ID, JSON_UNQUOTE(JSON_EXTRACT(code, '$.$lang')) AS code FROM lst_coronary_type"
  510. );
  511. if(!$statement->execute()) {
  512. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  513. }
  514. $lst_coronary_type = $statement->fetchAll(\PDO::FETCH_ASSOC);
  515. $statement = $this->DataInterface->DatabaseConnection->prepare(
  516. "SELECT ID, JSON_UNQUOTE(JSON_EXTRACT(code, '$.$lang')) AS code FROM lst_stroke_type"
  517. );
  518. if(!$statement->execute()) {
  519. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  520. }
  521. $lst_stroke_type = $statement->fetchAll(\PDO::FETCH_ASSOC);
  522. //
  523. return [
  524. 'result' => 'OK',
  525. 'patient' => $patient,
  526. 'context' => $context,
  527. 'lst_coronary_type' => $lst_coronary_type,
  528. 'lst_stroke_type' => $lst_stroke_type
  529. ];
  530. }
  531. /**
  532. * Get patient family data.
  533. */
  534. public function patientFamilyGet($User, $fk_visit) {
  535. $statement = $this->DataInterface->DatabaseConnection->prepare(
  536. "SELECT patient.* FROM patient, visit WHERE patient.ID = visit.fk_patient AND visit.ID = $fk_visit"
  537. );
  538. if(!$statement->execute()) {
  539. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  540. }
  541. $patient = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  542. $statement = $this->DataInterface->DatabaseConnection->prepare(
  543. "SELECT ID, fhistory FROM context WHERE fk_visit = $fk_visit"
  544. );
  545. if(!$statement->execute()) {
  546. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  547. }
  548. $context = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  549. //
  550. return [
  551. 'result' => 'OK',
  552. 'patient' => $patient,
  553. 'context' => $context
  554. ];
  555. }
  556. /**
  557. * Get patient examination data.
  558. */
  559. public function patientExaminationGet($User, $fk_visit) {
  560. $statement = $this->DataInterface->DatabaseConnection->prepare(
  561. "SELECT patient.* FROM patient, visit WHERE patient.ID = visit.fk_patient AND visit.ID = $fk_visit"
  562. );
  563. if(!$statement->execute()) {
  564. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  565. }
  566. $patient = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  567. $statement = $this->DataInterface->DatabaseConnection->prepare(
  568. "SELECT ID, examination FROM context WHERE fk_visit = $fk_visit"
  569. );
  570. if(!$statement->execute()) {
  571. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  572. }
  573. $context = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  574. //
  575. return [
  576. 'result' => 'OK',
  577. 'patient' => $patient,
  578. 'context' => $context
  579. ];
  580. }
  581. /**
  582. * Get patient treatments data.
  583. */
  584. public function patientTreatmentsGet($User, $fk_visit) {
  585. $statement = $this->DataInterface->DatabaseConnection->prepare(
  586. "SELECT patient.* FROM patient, visit WHERE patient.ID = visit.fk_patient AND visit.ID = $fk_visit"
  587. );
  588. if(!$statement->execute()) {
  589. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  590. }
  591. $patient = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  592. $statement = $this->DataInterface->DatabaseConnection->prepare(
  593. "SELECT ID, treatments FROM context WHERE fk_visit = $fk_visit"
  594. );
  595. if(!$statement->execute()) {
  596. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  597. }
  598. $context = $statement->fetchAll(\PDO::FETCH_ASSOC)[0];
  599. //
  600. return [
  601. 'result' => 'OK',
  602. 'patient' => $patient,
  603. 'context' => $context
  604. ];
  605. }
  606. /**
  607. *
  608. */
  609. public function patientPacsQueryPost($User, $data) {
  610. $userID = $User->ID;
  611. $statement = $this->DataInterface->DatabaseConnection->prepare(
  612. "SELECT settings_pacs.data AS data FROM settings_pacs, user WHERE settings_pacs.fk_physician = $userID OR (settings_pacs.fk_center = user.fk_center AND user.ID = $userID)"
  613. );
  614. if(!$statement->execute()) {
  615. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  616. }
  617. $conf = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  618. $cmdLines = [];
  619. $cmdLine = 'findscu '.$conf->serverAddress.' '.$conf->queryPort.' -aet '.$conf->callingAET.' -aec '.$conf->calledAET.
  620. ' -P -k QueryRetrieveLevel="PATIENT" -k PatientID="*" -k PatientName="*'.$data['patient'].'*" -k PatientBirthDate="*" -k PatientSex="*"'.
  621. ' -v 2>&1';
  622. $cmdLines[] = $cmdLine;
  623. $output=null;
  624. $retval=null;
  625. exec($cmdLine, $output, $retval);
  626. if($retval !== 0 || count($output)<1) {
  627. return [
  628. 'result' => 'ERROR',
  629. 'cmdLine' => $cmdLine,
  630. 'output' => $output,
  631. 'retval' => $retval
  632. ];
  633. }
  634. // clean
  635. for($i=0; $i<count($output); $i++) {
  636. if($output[$i]=="I:") {
  637. array_splice($output, $i, 1);
  638. }
  639. }
  640. // parse
  641. $patients = [];
  642. $patient = [];
  643. for($i=0; $i<count($output); $i++) {
  644. if(strpos($output[$i], "I: Find Response: ")===0) {
  645. if(count($patient)!=0) {
  646. $patients[] = $patient;
  647. $patient = [];
  648. }
  649. }
  650. else if(strpos($output[$i], "I: (0010,0010)")===0) {
  651. $patient['PatientName'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  652. //$patient['PatientName'] = trim(str_replace('^',' ',$patient['PatientName']));
  653. }
  654. else if(strpos($output[$i], "I: (0010,0020)")===0) {
  655. $patient['PatientID'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  656. }
  657. else if(strpos($output[$i], "I: (0010,0030)")===0) {
  658. $patient['PatientBirthDate'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  659. }
  660. else if(strpos($output[$i], "I: (0010,0040)")===0) {
  661. $patient['PatientSex'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  662. }
  663. }
  664. if(count($patient)!=0) {
  665. $patients[] = $patient;
  666. }
  667. array_splice($patients, 0, 1);
  668. // studies
  669. $studies = [];
  670. foreach($patients as $P) {
  671. $cmdLine = 'findscu '.$conf->serverAddress.' '.$conf->queryPort.' -aet '.$conf->callingAET.' -aec '.$conf->calledAET.
  672. ' -P -k QueryRetrieveLevel="STUDY" -k PatientID="'.$P['PatientID'].'" -k StudyInstanceUID="" -k StudyID="" -k StudyDate="" -k StudyTime=""'.
  673. ' -v 2>&1';
  674. $cmdLines[] = $cmdLine;
  675. $output=null;
  676. $retval=null;
  677. exec($cmdLine, $output, $retval);
  678. if($retval !== 0 || count($output)<1) {
  679. return [
  680. 'result' => 'ERROR',
  681. 'cmdLine' => $cmdLine,
  682. 'output' => $output,
  683. 'retval' => $retval
  684. ];
  685. }
  686. $study = [
  687. 'PatientName' => $P['PatientName'],
  688. 'PatientBirthDate' => $P['PatientBirthDate'],
  689. 'PatientSex' => $P['PatientSex']
  690. ];
  691. $hasFound = false;
  692. for($i=0; $i<count($output); $i++) {
  693. if(strpos($output[$i], "I: Find Response: ")===0) {
  694. if($hasFound) {
  695. $studies[] = $study;
  696. $study = [
  697. 'PatientName' => $P['PatientName'],
  698. 'PatientBirthDate' => $P['PatientBirthDate'],
  699. 'PatientSex' => $P['PatientSex']
  700. ];
  701. }
  702. $hasFound = true;
  703. }
  704. else if(strpos($output[$i], "I: (0008,0020)")===0) {
  705. $study['StudyDate'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  706. }
  707. else if(strpos($output[$i], "I: (0008,0030)")===0) {
  708. $study['StudyTime'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  709. }
  710. else if(strpos($output[$i], "I: (0020,0010)")===0) {
  711. $study['StudyID'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  712. }
  713. else if(strpos($output[$i], "I: (0020,000d)")===0) {
  714. $study['StudyInstanceUID'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  715. }
  716. else if(strpos($output[$i], "I: (0010,0020)")===0) {
  717. $study['PatientID'] = trim(explode(']', explode('[', $output[$i])[1])[0]);
  718. }
  719. }
  720. $studies[] = $study;
  721. $studies2[] = $output;
  722. }
  723. /*return [
  724. 'result' => 'ERROR',
  725. 'studies2' => $studies2,
  726. 'studies' => $studies,
  727. 'cmdLines' => $cmdLines
  728. ];*/
  729. //sudo movescu www.dicomserver.co.uk 11112 +P 11112 -P -k QueryRetrieveLevel="PATIENT" -k PatientID="874688" -v
  730. return [
  731. 'studies' => $studies,
  732. 'studies2' => $studies2,
  733. 'result' => 'OK',
  734. 'cmdLines' => $cmdLines
  735. ];
  736. }
  737. /**
  738. *
  739. */
  740. public function patientPacsRetrievePost($User, $data) {
  741. /*return [
  742. 'result' => 'ERROR',
  743. 'data' => $data
  744. ];*/
  745. $userID = $User->ID;
  746. $statement = $this->DataInterface->DatabaseConnection->prepare(
  747. "SELECT settings_pacs.data AS data FROM settings_pacs, user WHERE settings_pacs.fk_physician = $userID OR (settings_pacs.fk_center = user.fk_center AND user.ID = $userID)"
  748. );
  749. if(!$statement->execute()) {
  750. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  751. }
  752. $conf = json_decode($statement->fetchAll(\PDO::FETCH_ASSOC)[0]['data']);
  753. // create or select patient
  754. $patientID = 0;
  755. $statement = $this->DataInterface->DatabaseConnection->prepare(
  756. "SELECT * FROM patient WHERE patientID = :patientID OR ctPatientID = :ctPatientID"
  757. );
  758. $statement->bindParam(':patientID', $data['all']['PatientID']);
  759. $statement->bindParam(':ctPatientID', $data['all']['PatientID']);
  760. if(!$statement->execute()) {
  761. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  762. }
  763. $patients = $statement->fetchAll(\PDO::FETCH_ASSOC);
  764. // insert patient
  765. if(count($patients)==0) {
  766. $statement = $this->DataInterface->DatabaseConnection->prepare(
  767. "INSERT INTO patient(ID, patientID, ctPatientID, firstname, lastname, gender, birthDate, fk_user) VALUES(0, :patientID, :ctPatientID, :firstname, :lastname, :gender, :birthDate, :fk_user)"
  768. );
  769. $lastname = $data['all']['PatientName'];
  770. $firstname = "";
  771. $t = explode('^', $lastname);
  772. if(count($t==2)) {
  773. $lastname = $t[0];
  774. $firstname = $t[1];
  775. }
  776. $birth = substr($data['all']['PatientBirthDate'],0,4).'-'.substr($data['all']['PatientBirthDate'],4,2).'-'.substr($data['all']['PatientBirthDate'],6,2);
  777. $statement->bindParam(':patientID', $data['all']['PatientID']);
  778. $statement->bindParam(':ctPatientID', $data['all']['PatientID']);
  779. $statement->bindParam(':firstname', $firstname);
  780. $statement->bindParam(':lastname', $lastname);
  781. $statement->bindParam(':gender', $data['all']['PatientSex']);
  782. $statement->bindParam(':birthDate', $birth);
  783. $statement->bindParam(':fk_user', $userID);
  784. if(!$statement->execute()) {
  785. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  786. }
  787. $patientID = $this->DataInterface->DatabaseConnection->lastInsertId();
  788. }
  789. // select patient
  790. else {
  791. $patientID = $patients[0]['ID'];
  792. }
  793. // create or select visit
  794. $visitID = 0;
  795. $statement = $this->DataInterface->DatabaseConnection->prepare(
  796. "SELECT visit.* FROM visit, patient WHERE patient.ID = visit.fk_patient AND visit.studyInstanceUID = :studyInstanceUID"
  797. );
  798. $statement->bindParam(':studyInstanceUID', $data['all']['StudyInstanceUID']);
  799. if(!$statement->execute()) {
  800. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  801. }
  802. $visits = $statement->fetchAll(\PDO::FETCH_ASSOC);
  803. // insert visit
  804. if(count($visits)==0) {
  805. $statement = $this->DataInterface->DatabaseConnection->prepare(
  806. "INSERT INTO visit(ID, number, visitDate, fk_patient, studyInstanceUID) VALUES(0, :number, :visitDate, :fk_patient, :studyInstanceUID)"
  807. );
  808. $sdate = substr($data['all']['StudyDate'],0,4).'-'.substr($data['all']['StudyDate'],4,2).'-'.substr($data['all']['StudyDate'],6,2);
  809. $statement->bindParam(':number', $data['all']['StudyID']);
  810. $statement->bindParam(':visitDate', $sdate);
  811. $statement->bindParam(':fk_patient', $patientID);
  812. $statement->bindParam(':studyInstanceUID', $data['all']['StudyInstanceUID']);
  813. if(!$statement->execute()) {
  814. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  815. }
  816. $visitID = $this->DataInterface->DatabaseConnection->lastInsertId();
  817. // Insert context
  818. $statement = $this->DataInterface->DatabaseConnection->prepare(
  819. "INSERT INTO context(fk_visit) VALUES(:fk_visit)"
  820. );
  821. $statement->bindParam(':fk_visit', $visitID);
  822. // Error check
  823. if(!$statement->execute()) {
  824. $this->DataInterface->DatabaseConnection->rollback();
  825. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  826. }
  827. }
  828. // select visit
  829. else {
  830. $visitID = $visits[0]['ID'];
  831. }
  832. $od = '../../storage/media/'.$visitID.'/';
  833. \Tools\FS::mkpath($od);
  834. //sudo www.dicomserver.co.uk 11112 +P 11112 -P -k QueryRetrieveLevel="PATIENT" -k PatientID="874688" -v
  835. $cmdLine = 'movescu '.$conf->serverAddress.' '.$conf->queryPort.' +P 11112 -aet '.$conf->callingAET.' -aec '.$conf->calledAET.' -aem '.$conf->callingAET.
  836. ' -P -k QueryRetrieveLevel="STUDY" -k StudyInstanceUID="'.$data['all']['StudyInstanceUID'].'"'.
  837. ' -od '.$od.
  838. ' -v 2>&1';
  839. $output=null;
  840. $retval=null;
  841. exec($cmdLine, $output, $retval);
  842. if($retval !== 0 || count($output)<1) {
  843. return [
  844. 'result' => 'ERROR',
  845. 'cmdLine' => $cmdLine,
  846. 'output' => $output,
  847. 'retval' => $retval
  848. ];
  849. }
  850. // get files
  851. $files = glob("$od*");
  852. $existingFiles = 0;
  853. $newFiles = 0;
  854. foreach($files as $F) {
  855. // skip previously existing files
  856. if(strpos($F, ".dicom")!==false || strpos($F, ".jpeg")!==false || strpos($F, ".mp4")!==false) {
  857. // do nothing
  858. }
  859. // new dicom file
  860. else {
  861. // existing file
  862. if(file_exists("$F.dicom")) {
  863. // do nothing
  864. $existingFiles++;
  865. unlink($F);
  866. }
  867. // real new file
  868. else {
  869. $newFiles++;
  870. rename($F, "$F.dicom");
  871. // multiframe?
  872. $cmdLine = 'dcmdump +P 0028,0008 "'.$F.'.dicom"';
  873. $output=null;
  874. $retval=null;
  875. exec($cmdLine, $output, $retval);
  876. // OK
  877. if($retval === 0 && count($output)==1) {
  878. $tab = explode(' ',trim($output[0]));
  879. $frameCount = intval(str_replace(['[', ']'], '', $tab[2]));
  880. // error
  881. if($frameCount===0) {
  882. return [
  883. 'result' => 'ERROR',
  884. 'output' => $output,
  885. ];
  886. }
  887. // make JPEG
  888. $frames = [];
  889. for($i=1; $i<=$frameCount; $i++) {
  890. $src = "$F.dicom";
  891. $pad = str_pad($i, 3, "0", STR_PAD_LEFT);
  892. $dst = str_replace('.dicom', '-'.$pad.'.jpeg', $src);
  893. $cmdLine = 'dcmj2pnm "'.$src.'" "'.$dst.'" +oj +Jq 100 +F '.$i.' -v 2>&1';
  894. $output=null;
  895. $retval=null;
  896. exec($cmdLine, $output, $retval);
  897. // error
  898. if($retval !== 0 || count($output)<1) {
  899. return [
  900. 'result' => 'ERROR',
  901. 'cmdLine' => $cmdLine,
  902. 'output' => $output,
  903. 'retval' => $retval
  904. ];
  905. }
  906. $frames[] = $dst;
  907. }
  908. // make MP4
  909. $src = "$F.dicom";
  910. $dst = str_replace('.dicom', '-%03d.jpeg', $src);
  911. $mp4 = str_replace('.dicom', '.mp4', $src);
  912. $cmdLine = 'ffmpeg -f image2 -pattern_type sequence -r 11 -i '.$dst.' -y -c:v libx264 -pix_fmt yuv420p '.$mp4;
  913. $output=null;
  914. $retval=null;
  915. exec($cmdLine, $output, $retval);
  916. // error
  917. if($retval !== 0) {
  918. return [
  919. 'result' => 'ERROR',
  920. 'cmdLine' => $cmdLine,
  921. 'output' => $output,
  922. 'retval' => $retval
  923. ];
  924. }
  925. // cleanup
  926. for($i=0; $i<count($frames); $i++) {
  927. unlink($frames[$i]);
  928. }
  929. // get metrics
  930. $cmdLine = 'ffprobe -v error -show_entries stream=width,height,r_frame_rate -of csv=p=0 '.$mp4;
  931. $output=null;
  932. $retval=null;
  933. exec($cmdLine, $output, $retval);
  934. // error
  935. if($retval !== 0 || count($output)!=1) {
  936. return [
  937. 'result' => 'ERROR',
  938. 'cmdLine' => $cmdLine,
  939. 'output' => $output,
  940. 'retval' => $retval
  941. ];
  942. }
  943. $tab = explode(',',trim($output[0]));
  944. if(count($tab)!=3) {
  945. return [
  946. 'result' => 'ERROR',
  947. 'cmdLine' => $cmdLine,
  948. 'output' => $output,
  949. 'retval' => $retval
  950. ];
  951. }
  952. $metrics['width'] = intval($tab[0]);
  953. $metrics['height'] = intval($tab[1]);
  954. $fps = $tab[2];
  955. $tab = explode('/', $fps);
  956. if(count($tab) == 2) {
  957. $fps = intval($tab[0]);
  958. }
  959. else {
  960. $fps = intval($fps);
  961. }
  962. $metrics['fps'] = $fps;
  963. }
  964. // singleframe
  965. else {
  966. $cmdLine = "dcmj2pnm $F.dicom $F.jpeg +oj +Jq 100 +F 1 -v 2>&1";
  967. $output=null;
  968. $retval=null;
  969. exec($cmdLine, $output, $retval);
  970. // error
  971. if($retval !== 0 || count($output)<1) {
  972. return [
  973. 'result' => 'ERROR',
  974. 'cmdLine' => $cmdLine,
  975. 'output' => $output,
  976. 'retval' => $retval
  977. ];
  978. }
  979. // image metrics
  980. $metrics = ['width' => 0, 'height' => 0, 'pxwidth' => 0, 'pxheight' => 0];
  981. list($metrics['width'], $metrics['height'], $type, $attr) = getimagesize("$F.jpeg");
  982. }
  983. // dicom metrics
  984. $cmdLine = 'dcmdump -s +P 0018,602c +P 0018,602e +P 0028,0008 "'.$F.'.dicom"';
  985. $output=null;
  986. $retval=null;
  987. exec($cmdLine, $output, $retval);
  988. // error
  989. if($retval !== 0) {
  990. return [
  991. 'result' => 'ERROR',
  992. 'cmdLine' => $cmdLine,
  993. 'output' => $output,
  994. 'retval' => $retval
  995. ];
  996. }
  997. if(count($output)>=2) {
  998. $tab = explode(' ',trim($output[0]));
  999. $metrics['pxwidth'] = floatval($tab[2])*10.0;
  1000. $tab = explode(' ',trim($output[1]));
  1001. $metrics['pxheight'] = floatval($tab[2])*10.0;
  1002. }
  1003. if(count($output)==3) {
  1004. $tab = explode(' ',trim($output[2]));
  1005. $metrics['frameCount'] = intval(str_replace(['[', ']'], '', $tab[2]));
  1006. }
  1007. // insert new media
  1008. $statement = $this->DataInterface->DatabaseConnection->prepare(
  1009. "INSERT INTO media(ID, side, filename, metrics, fk_visit) VALUES(0, 'unknown', :filename, :metrics, :fk_visit)"
  1010. );
  1011. $statement->bindParam(':filename', basename("$F.dicom"));
  1012. $statement->bindParam(':metrics', json_encode($metrics));
  1013. $statement->bindParam(':fk_visit', $visitID);
  1014. if(!$statement->execute()) {
  1015. return ['result' => 'ERROR', 'reason' => 'internal_error', 'message' => 'Database error', 'data' => $statement->errorInfo()];
  1016. }
  1017. }
  1018. }
  1019. }
  1020. return [
  1021. 'result' => 'OK',
  1022. 'cmdLine' => $cmdLine,
  1023. 'files' => $files,
  1024. 'output' => $output,
  1025. 'patientID' => $patientID,
  1026. 'visitID' => $visitID,
  1027. 'existingFiles' => $existingFiles,
  1028. 'newFiles' => $newFiles
  1029. ];
  1030. }
  1031. }
  1032. }