helpers.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tatu
  5. * Date: 6/23/20
  6. * Time: 12:10 AM
  7. */
  8. use App\Models\AppSession;
  9. use App\Models\Client;
  10. use App\Models\Pro;
  11. use App\Models\Bill;
  12. //require_once './class.Diff.php';
  13. use Illuminate\Support\Facades\Http;
  14. use Soundasleep\Html2Text as Html2Text;
  15. if(!function_exists('currentSortForKey')) {
  16. function currentSortForKey($current, $key) {
  17. $found = false;
  18. $current = $current ? $current : [];
  19. for ($i = 0; $i < count($current); $i++) {
  20. if($current[$i]['key'] === $key) { // if exists, reverse
  21. return ["index" => ($i + 1), "order" => $current[$i]['order']];
  22. }
  23. }
  24. return FALSE;
  25. }
  26. }
  27. if(!function_exists('includeOrReverseInMultiSort')) {
  28. function includeOrReverseInMultiSort($current, $key) {
  29. $found = false;
  30. $current = $current ? $current : [];
  31. for ($i = 0; $i < count($current); $i++) {
  32. if($current[$i]['key'] === $key) { // if exists, reverse
  33. $current[$i]['order'] = ($current[$i]['order'] === 'DESC' ? 'ASC' : 'DESC');
  34. $found = true;
  35. break;
  36. }
  37. }
  38. if (!$found) { // if not exists, include
  39. $current[] = ["key" => $key, "order" => 'ASC'];
  40. }
  41. return $current;
  42. }
  43. }
  44. if(!function_exists('removeFromMultiSort')) {
  45. function removeFromMultiSort($current, $key) {
  46. $found = false;
  47. $current = $current ? $current : [];
  48. $current = array_filter($current, function($_x) use ($key) {
  49. return $_x['key'] !== $key;
  50. });
  51. return array_values($current);
  52. }
  53. }
  54. if(!function_exists('toFeetAndInches')) {
  55. function toFeetAndInches($value, $ftLabel = ' ft.', $inLabel = ' in.') {
  56. if(!$value) return '-';
  57. $value = round($value);
  58. $ft = round(floor($value / 12));
  59. $in = $value % 12;
  60. return "$ft$ftLabel $in$inLabel";
  61. }
  62. }
  63. if(!function_exists('callJava')) {
  64. function callJava($endPoint, $data, $sessionKey) {
  65. $url = config('stag.backendUrl') . $endPoint;
  66. $response = Http::asForm()
  67. ->withHeaders([
  68. 'sessionKey' => $sessionKey
  69. ])
  70. ->post($url, $data)
  71. ->body();
  72. return $response;
  73. }
  74. }
  75. if(!function_exists('feetFromInches')) {
  76. function feetFromInches($value) {
  77. if(!$value) return 0;
  78. $value = round($value);
  79. return round(floor($value / 12));
  80. }
  81. }
  82. if(!function_exists('inchesAfterFeetFromInches')) {
  83. function inchesAfterFeetFromInches($value) {
  84. if(!$value) return 0;
  85. $value = round($value);
  86. return round($value % 12);
  87. }
  88. }
  89. if(!function_exists('genericBills')) {
  90. function genericBills(Pro $performerPro, $patient, $entityType, $entityUid) {
  91. $genericBills = Bill::where('bill_service_type', 'GENERIC');
  92. if($performerPro->pro_type !== 'ADMIN') {
  93. $genericBills = $genericBills->where('generic_pro_id', $performerPro->id);
  94. }
  95. if($patient) {
  96. $genericBills = $genericBills->where('client_id', $patient->id);
  97. }
  98. if($entityType && $entityUid) {
  99. $genericBills = $genericBills
  100. ->where('generic_target_entity_type', $entityType)
  101. ->where('generic_target_entity_uid', $entityUid);
  102. }
  103. return $genericBills->orderBy('created_at', 'DESC')->get();
  104. }
  105. }
  106. if(!function_exists('hasActiveGenericBill')) {
  107. function hasActiveGenericBill(Pro $performerPro, $patient, $entityType, $entityUid) {
  108. $genericBills = Bill::where('bill_service_type', 'GENERIC')->where('is_cancelled', false);
  109. if($performerPro->pro_type !== 'ADMIN') {
  110. $genericBills = $genericBills->where('generic_pro_id', $performerPro->id);
  111. }
  112. if($patient) {
  113. $genericBills = $genericBills->where('client_id', $patient->id);
  114. }
  115. if($entityType && $entityUid) {
  116. $genericBills = $genericBills
  117. ->where('generic_target_entity_type', $entityType)
  118. ->where('generic_target_entity_uid', $entityUid);
  119. }
  120. return $genericBills->count() > 0;
  121. }
  122. }
  123. if(!function_exists('queryLineExcept')) {
  124. function queryLineExcept($except = []) {
  125. $params = request()->all();
  126. $final = [];
  127. foreach ($params as $k => $v) {
  128. if(in_array($k, $except) === FALSE) {
  129. if(is_array($v)) $v = implode(',', $v);
  130. $final[] = "$k=" . urlencode($v);
  131. }
  132. }
  133. return implode('&', $final);
  134. }
  135. }
  136. if(!function_exists('sortColumnHead')) {
  137. function sortColumnHead($url, $label, $sortKey, $defaultDirection = 'ASC') {
  138. $currentSortKey = request()->input('sort');
  139. $currentDir = request()->input('dir');
  140. $targetDir = $currentDir ? ($currentDir === 'ASC' ? 'DESC' : 'ASC') : $defaultDirection;
  141. echo '<a href="' . $url . '?sort=' . $sortKey . '&dir=' . $targetDir . '&' . queryLineExcept(['sort', 'dir']) . '">' . $label . '</a>';
  142. if($currentSortKey === $sortKey) {
  143. if($currentDir === 'ASC') {
  144. echo "&nbsp;&nbsp;↑";
  145. }
  146. elseif($currentDir === 'DESC') {
  147. echo "&nbsp;&nbsp;↓";
  148. }
  149. }
  150. }
  151. }
  152. if(!function_exists('html2Text')) {
  153. function html2Text($old, $new){
  154. }
  155. }
  156. if(!function_exists('diff')) {
  157. function diff($old, $new){
  158. // return Diff::toHTML(Diff::compare($old, $new));
  159. }
  160. }
  161. if(!function_exists('get_current_session')) {
  162. function get_current_session(){
  163. return AppSession::where('session_key', request()->cookie('sessionKey'))->first();
  164. }
  165. }
  166. if(!function_exists('get_current_date')) {
  167. function get_current_date($timezone = null){
  168. if(!$timezone){
  169. $timezone = 'US\Eastern';
  170. }
  171. $date = new DateTime("now", new DateTimeZone('US/Eastern') );
  172. return $date->format('Y-m-d');
  173. }
  174. }
  175. if(!function_exists('friendly_date_est')) {
  176. function friendly_date_est($value, $default = '-') {
  177. if(!$value || empty($value)) return $default;
  178. try {
  179. $realTimezone = resolve_timezone('EASTERN');
  180. $date = new DateTime($value);
  181. $date->setTimezone(new DateTimeZone($realTimezone));
  182. return $date->format("m/d/y");
  183. }
  184. catch (Exception $e) {
  185. return $e->getMessage();
  186. }
  187. }
  188. }
  189. if(!function_exists('friendly_date_est_compact')) {
  190. function friendly_date_est_compact($value, $default = '-') {
  191. if(!$value || empty($value)) return $default;
  192. try {
  193. $realTimezone = resolve_timezone('EASTERN');
  194. $date = new DateTime($value);
  195. $date->setTimezone(new DateTimeZone($realTimezone));
  196. return $date->format('y') === date('y') ? $date->format("m/d") : $date->format("m/d/y");
  197. }
  198. catch (Exception $e) {
  199. return $e->getMessage();
  200. }
  201. }
  202. }
  203. if(!function_exists('friendly_date_time')) {
  204. function friendly_date_time($value, $includeTime = true, $default = '-', $long_year=false) {
  205. if(!$value || empty($value)) return $default;
  206. try {
  207. if($includeTime) {
  208. $realTimezone = 'US/Eastern';
  209. $date = new DateTime($value);
  210. $date->setTimezone(new DateTimeZone($realTimezone));
  211. return $date->format("m/d/y" . ($includeTime ? ", h:ia" : "")) . ($includeTime ? ' EST' : '');
  212. }
  213. else {
  214. $result = strtotime($value);
  215. if($long_year){
  216. $result = date("m/d/Y" . ($includeTime ? ", h:ia" : ""), $result);
  217. }else{
  218. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  219. }
  220. return $result;
  221. }
  222. }
  223. catch (Exception $e) {
  224. return $value;
  225. }
  226. }
  227. }
  228. if(!function_exists('friendly_date_time_with_seconds')) {
  229. function friendly_date_time_with_seconds($value, $includeTime = true, $default = '-', $long_year=false) {
  230. if(!$value || empty($value)) return $default;
  231. try {
  232. if($includeTime) {
  233. $realTimezone = 'US/Eastern';
  234. $date = new DateTime($value);
  235. $date->setTimezone(new DateTimeZone($realTimezone));
  236. return $date->format("m/d/y" . ($includeTime ? ", h:i:sa" : "")) . ($includeTime ? ' EST' : '');
  237. }
  238. else {
  239. $result = strtotime($value);
  240. if($long_year){
  241. $result = date("m/d/Y" . ($includeTime ? ", h:i:sa" : ""), $result);
  242. }else{
  243. $result = date("m/d/y" . ($includeTime ? ", h:i:sa" : ""), $result);
  244. }
  245. return $result;
  246. }
  247. }
  248. catch (Exception $e) {
  249. return $value;
  250. }
  251. }
  252. }
  253. if(!function_exists('friendly_date_month_year')) {
  254. function friendly_date_month_year($value, $default = '-', $long_year=true) {
  255. if(!$value || empty($value)) return $default;
  256. try {
  257. $result = strtotime($value);
  258. if($long_year){
  259. $result = date("m/Y", $result);
  260. }else{
  261. $result = date("m/y", $result);
  262. }
  263. return $result;
  264. }
  265. catch (Exception $e) {
  266. return $value;
  267. }
  268. }
  269. }
  270. if(!function_exists('friendlier_date_time')) {
  271. function friendlier_date_time($value, $includeTime = true, $default = '-') {
  272. return friendly_date_time($value, $includeTime, $default);
  273. }
  274. }
  275. if(!function_exists('friendly_date_time_short')) {
  276. function friendly_date_time_short($value, $includeTime = true, $default = '-') {
  277. if(!$value || empty($value)) return $default;
  278. try {
  279. $result = strtotime($value);
  280. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  281. return $result;
  282. }
  283. catch (Exception $e) {
  284. return $value;
  285. }
  286. }
  287. }
  288. if(!function_exists('friendly_date_time_short_with_tz')) {
  289. function friendly_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  290. if(!$value || empty($value)) return $default;
  291. try {
  292. $realTimezone = resolve_timezone($tz);
  293. $date = new DateTime($value);
  294. $date->setTimezone(new DateTimeZone($realTimezone));
  295. return $date->format("m/d/y" . ($includeTime ? ", h:iA" : ""));
  296. }
  297. catch (Exception $e) {
  298. return $e->getMessage();
  299. }
  300. }
  301. }
  302. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp')) {
  303. function friendly_date_time_short_with_tz_from_timestamp($value, $tz='UTC', $default = '-') {
  304. if(!$value || empty($value)) return $default;
  305. try {
  306. $realTimezone = resolve_timezone($tz);
  307. $date = new DateTime("@$value");
  308. $date->setTimezone(new DateTimeZone($realTimezone));
  309. return $date->format("m/d/y, h:iA");
  310. }
  311. catch (Exception $e) {
  312. return $e->getMessage();
  313. }
  314. }
  315. }
  316. if(!function_exists('postgres_date_time_short_with_tz')) {
  317. function postgres_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  318. if(!$value || empty($value)) return $default;
  319. try {
  320. $realTimezone = resolve_timezone($tz);
  321. $date = new DateTime($value);
  322. $date->setTimezone(new DateTimeZone($realTimezone));
  323. return $date->format("Y-m-d" . ($includeTime ? " h:i:s" : ""));
  324. }
  325. catch (Exception $e) {
  326. return $e->getMessage();
  327. }
  328. }
  329. }
  330. if(!function_exists('postgres_date_time_short_with_tz_from_timestamp')) {
  331. function postgres_date_time_short_with_tz_from_timestamp($value, $tz='UTC', $default = '-') {
  332. if(!$value || empty($value)) return $default;
  333. try {
  334. $realTimezone = resolve_timezone($tz);
  335. $date = new DateTime("@$value");
  336. $date->setTimezone(new DateTimeZone($realTimezone));
  337. return $date->format("Y-m-d h:i:s");
  338. }
  339. catch (Exception $e) {
  340. return $e->getMessage();
  341. }
  342. }
  343. }
  344. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp_divide1000')) {
  345. function friendly_date_time_short_with_tz_from_timestamp_divide1000($value, $tz='UTC', $default = '-') {
  346. if(!$value || empty($value)) return $default;
  347. try {
  348. $value = (floor($value / 1000));
  349. $realTimezone = resolve_timezone($tz);
  350. $date = new DateTime("@$value");
  351. $date->setTimezone(new DateTimeZone($realTimezone));
  352. return $date->format("m/d/y, h:iA");
  353. }
  354. catch (Exception $e) {
  355. return $e->getMessage();
  356. }
  357. }
  358. }
  359. if(!function_exists('friendly_date_short_with_tz_from_timestamp_divide1000')) {
  360. function friendly_date_short_with_tz_from_timestamp_divide1000($value, $tz='EASTERN', $default = '-') {
  361. if(!$value || empty($value)) return $default;
  362. try {
  363. $value = (floor($value / 1000));
  364. $realTimezone = resolve_timezone($tz);
  365. $date = new DateTime("@$value");
  366. $date->setTimezone(new DateTimeZone($realTimezone));
  367. return $date->format("m/d/y");
  368. }
  369. catch (Exception $e) {
  370. return $e->getMessage();
  371. }
  372. }
  373. }
  374. if(!function_exists('date_short_with_tz_from_timestamp_divide1000')) {
  375. function date_short_with_tz_from_timestamp_divide1000($value, $tz='EASTERN', $default = '-') {
  376. if(!$value || empty($value)) return $default;
  377. try {
  378. $value = (floor($value / 1000));
  379. $realTimezone = resolve_timezone($tz);
  380. $date = new DateTime("@$value");
  381. $date->setTimezone(new DateTimeZone($realTimezone));
  382. return $date->format("Y-m-d");
  383. }
  384. catch (Exception $e) {
  385. return $e->getMessage();
  386. }
  387. }
  388. }
  389. if(!function_exists('friendly_date')) {
  390. function friendly_date($value) {
  391. if(!$value || empty($value)) return '';
  392. try {
  393. $result = strtotime($value);
  394. $result = date("m/d/Y", $result);
  395. return $result;
  396. }
  397. catch (Exception $e) {
  398. return $value;
  399. }
  400. }
  401. }
  402. if(!function_exists('friendly_date_short')) {
  403. function friendly_date_short($value) {
  404. if(!$value || empty($value)) return '';
  405. try {
  406. $result = strtotime($value);
  407. $result = date("m/d/y", $result);
  408. return $result;
  409. }
  410. catch (Exception $e) {
  411. return $value;
  412. }
  413. }
  414. }
  415. if(!function_exists('friendly_date_month_year')) {
  416. function friendly_date_month_year($value) {
  417. if(!$value || empty($value)) return '';
  418. try {
  419. $result = strtotime($value);
  420. $result = date("M Y", $result);
  421. return $result;
  422. }
  423. catch (Exception $e) {
  424. return $value;
  425. }
  426. }
  427. }
  428. if(!function_exists('friendlier_date')) {
  429. function friendlier_date($value) {
  430. if(!$value || empty($value)) return '';
  431. try {
  432. $result = strtotime($value);
  433. $result = date("m/d/Y", $result);
  434. return $result;
  435. }
  436. catch (Exception $e) {
  437. return $value;
  438. }
  439. }
  440. }
  441. if(!function_exists('relative_friendly_date')) {
  442. function relative_friendly_date($value) {
  443. if(!$value || empty($value)) return '';
  444. try {
  445. $result = date_diff(date_create($value), date_create('now'))->days;
  446. if ($result > 30) $result = date('F Y', strtotime($value));
  447. else if ($result > 1) $result .= ' days ago';
  448. else if ($result === 1) $result = 'Yesterday';
  449. else if ($result === 0) $result = 'Today';
  450. return $result;
  451. }
  452. catch (Exception $e) {
  453. return $value;
  454. }
  455. }
  456. }
  457. if(!function_exists('unfriendly_date')) {
  458. function unfriendly_date($value) {
  459. if(!$value || empty($value)) return '';
  460. try {
  461. $result = strtotime($value);
  462. $result = date("Y-m-d", $result);
  463. return $result;
  464. }
  465. catch (Exception $e) {
  466. return $value;
  467. }
  468. }
  469. }
  470. if(!function_exists('friendly_time')) {
  471. function friendly_time($value, $default = '-') {
  472. if(!$value || empty($value)) return $default;
  473. try {
  474. $result = strtotime($value);
  475. $result = date("h:i a", $result);
  476. return $result;
  477. }
  478. catch (Exception $e) {
  479. return $value;
  480. }
  481. }
  482. }
  483. if(!function_exists('military_time')) {
  484. function military_time($value, $tz='UTC', $default = '-') {
  485. if(!$value || empty($value)) return $default;
  486. try {
  487. $realTimezone = resolve_timezone($tz);
  488. $date = new DateTime($value);
  489. $date->setTimezone(new DateTimeZone($realTimezone));
  490. return $date->format("H:i");
  491. }
  492. catch (Exception $e) {
  493. return $value;
  494. }
  495. }
  496. }
  497. if(!function_exists('resolve_timezone')) {
  498. function resolve_timezone($value) {
  499. try {
  500. switch ($value) {
  501. case 'ALASKA': {
  502. return 'US/Alaska';
  503. }
  504. case 'CENTRAL': {
  505. return 'US/Central';
  506. }
  507. case 'EASTERN': {
  508. return 'US/Eastern';
  509. }
  510. case 'HAWAII': {
  511. return 'US/Hawaii';
  512. }
  513. case 'MOUNTAIN': {
  514. return 'US/Mountain';
  515. }
  516. case 'PACIFIC': {
  517. return 'US/Pacific';
  518. }
  519. case 'PUERTO_RICO': {
  520. return 'America/Puerto_Rico';
  521. }
  522. case 'UTC': {
  523. return 'UTC';
  524. }
  525. }
  526. }
  527. catch (Exception $e) {
  528. return $value;
  529. }
  530. }
  531. }
  532. // $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
  533. // echo $date->format('Y-m-d H:i:sP') . "\n";
  534. if(!function_exists('friendly_month')) {
  535. function friendly_month($value) {
  536. if(!$value || empty($value)) return "-";
  537. try {
  538. $result = strtotime($value);
  539. $result = date("F o", $result);
  540. return $result;
  541. }
  542. catch (Exception $e) {
  543. return $value;
  544. }
  545. }
  546. }
  547. if(!function_exists('day_part_from_date')) {
  548. function day_part_from_date($value) {
  549. if(!$value || empty($value)) return "-";
  550. try {
  551. $result = strtotime($value);
  552. $result = date("d", $result);
  553. return $result;
  554. }
  555. catch (Exception $e) {
  556. return $value;
  557. }
  558. }
  559. }
  560. if(!function_exists('month_part_from_date')) {
  561. function month_part_from_date($value) {
  562. if(!$value || empty($value)) return "-";
  563. try {
  564. $result = strtotime($value);
  565. $result = date("m", $result);
  566. return $result;
  567. }
  568. catch (Exception $e) {
  569. return $value;
  570. }
  571. }
  572. }
  573. if(!function_exists('year_part_from_date')) {
  574. function year_part_from_date($value) {
  575. if(!$value || empty($value)) return "-";
  576. try {
  577. $result = strtotime($value);
  578. $result = date("Y", $result);
  579. return $result;
  580. }
  581. catch (Exception $e) {
  582. return $value;
  583. }
  584. }
  585. }
  586. if(!function_exists('friendly_money')){
  587. function friendly_money($value, $decimals = 2) {
  588. // if($decimals === 0) return intval($value);
  589. return number_format((float)$value, $decimals, '.', '');
  590. }
  591. }
  592. if(!function_exists('time_in_hrminsec')) {
  593. function time_in_hrminsec($value, $default = '-') {
  594. if(!$value || empty($value)) return $default;
  595. $value = intval($value);
  596. $minutes = intval($value / 60);
  597. $seconds = $value % 60;
  598. $hours = 0;
  599. if($minutes >= 60) {
  600. $hours = intval($minutes / 60);
  601. $minutes = $minutes % 60;
  602. }
  603. $output = [];
  604. if($hours > 0) {
  605. $output[] = "{$hours}h";
  606. }
  607. if($minutes > 0) {
  608. $output[] = "{$minutes}m";
  609. }
  610. if($seconds > 0) {
  611. $output[] = "{$seconds}s";
  612. }
  613. return implode(" ", $output);
  614. }
  615. }
  616. if(!function_exists('sanitize_field_name')) {
  617. function sanitize_field_name($name) {
  618. $result = strtolower($name);
  619. return preg_replace("/[^0-9a-z]/i", "_", $result);
  620. }
  621. }
  622. if(!function_exists('sanitize_state_name')) {
  623. function sanitize_state_name($name) {
  624. $result = strtolower($name);
  625. return ucwords(preg_replace("/_/i", " ", $result));
  626. }
  627. }
  628. if(!function_exists('renderNoteTemplate')) {
  629. function renderNoteTemplate($template, $topLevel)
  630. {
  631. echo
  632. '<div class="note-template-item" ' .
  633. 'template="' . (isset($template->template) ? $template->template : $template->text) . '" ' .
  634. 'type="' . (isset($template->type) ? $template->type : "value") . '" ' .
  635. '>' .
  636. '<div class="note-template-text d-flex align-items-center">' .
  637. '<span class="label">' .
  638. '<input type="checkbox" />' .
  639. '<span>' . $template->text . '</span>' .
  640. '</span>';
  641. if (isset($template->type) && $template->type === 'plus-minus') {
  642. echo '<div class="ml-auto mr-2 text-nowrap">';
  643. echo '<a href="#" class="plus-trigger"><i class="fa fa-plus-circle"></i></a>';
  644. echo '<a href="#" class="minus-trigger ml-1"><i class="fa fa-minus-circle"></i></a>';
  645. echo '</div>';
  646. }
  647. echo '</div>';
  648. if (isset($template->children) && count($template->children)) {
  649. echo '<i class="fa fa-chevron-right has-children"></i>';
  650. echo '<div class="note-template-children">';
  651. foreach ($template->children as $t) {
  652. renderNoteTemplate($t, false);
  653. }
  654. echo '</div>';
  655. } else if (isset($template->type) && $template->type !== 'plus-minus') {
  656. echo '<i class="fa fa-chevron-right has-children"></i>';
  657. echo '<div class="note-template-children">';
  658. if ($template->type === 'alpha') {
  659. echo '<textarea class="form-control form-control-sm"></textarea>';
  660. } else {
  661. echo '<input type="' . $template->type . '" class="form-control form-control-sm">';
  662. }
  663. echo '</div>';
  664. }
  665. echo '</div>';
  666. }
  667. }
  668. if(!function_exists('renderNoteTemplates')) {
  669. function renderNoteTemplates($path)
  670. {
  671. $templates = json_decode(file_get_contents($path));
  672. foreach ($templates->templates as $template) {
  673. renderNoteTemplate($template, true);
  674. }
  675. }
  676. }
  677. if(!function_exists('renderNoteExamTemplates')) {
  678. function renderNoteExamTemplates($parentPath, $childPath)
  679. {
  680. $templates = json_decode(file_get_contents($parentPath));
  681. $templates = $templates->templates;
  682. // override as needed with what is in template set
  683. if(file_exists($childPath)) {
  684. $orTemplates = json_decode(file_get_contents($parentPath));
  685. $orTemplates = $orTemplates->templates;
  686. for ($i = 0; $i < count($templates); $i++) {
  687. for ($j = 0; $j < count($orTemplates); $j++) {
  688. if($templates[$i]->text === $orTemplates[$j]->text) {
  689. $templates[$i] = $orTemplates[$j];
  690. }
  691. }
  692. }
  693. }
  694. foreach ($templates as $template) {
  695. renderNoteTemplate($template, true);
  696. }
  697. }
  698. }
  699. if(!function_exists('getVal')) {
  700. function getVal($object, $prop)
  701. {
  702. if (isset($object->$prop)) {
  703. return $object->$prop;
  704. } else {
  705. return '';
  706. }
  707. }
  708. }
  709. if(!function_exists('appTZtoPHPTZ')) {
  710. function appTZtoPHPTZ($_timezone)
  711. {
  712. switch ($_timezone) {
  713. case 'ALASKA':
  714. $timezone = "US/Alaska";
  715. break;
  716. case 'CENTRAL':
  717. $timezone = "US/Central";
  718. break;
  719. case 'HAWAII':
  720. $timezone = "US/Hawaii";
  721. break;
  722. case 'MOUNTAIN':
  723. $timezone = "US/Mountain";
  724. break;
  725. case 'PACIFIC':
  726. $timezone = "US/Pacific";
  727. break;
  728. case 'PUERTO_RICO':
  729. $timezone = "America/Puerto_Rico";
  730. break;
  731. default:
  732. $timezone = "US/Eastern";
  733. break;
  734. }
  735. return $timezone;
  736. }
  737. }
  738. if(!function_exists('convertToTimezone')) {
  739. function convertToTimezone($_dateTime, $_targetTimezone, $_sourceTimezone = 'UTC', $_returnRaw = false)
  740. {
  741. if (!$_dateTime) return $_dateTime;
  742. $date = new \DateTime($_dateTime, new \DateTimeZone($_sourceTimezone));
  743. $date->setTimezone(new \DateTimeZone(appTZtoPHPTZ($_targetTimezone)));
  744. return $_returnRaw ? $date : $date->format('Y-m-d H:i:s');
  745. }
  746. }
  747. if(!function_exists('minutes_to_hhmm')) {
  748. function minutes_to_hhmm($_minutes)
  749. {
  750. $h = intval(floor($_minutes / 60));
  751. $m = $_minutes;
  752. if($h > 0) {
  753. $m = $_minutes - $h * 60;
  754. }
  755. $h = ($h < 10 ? '0' : '') . $h;
  756. $m = ($m < 10 ? '0' : '') . $m;
  757. return $h . ':' . $m;
  758. }
  759. }
  760. if(!function_exists('vsValue')) {
  761. function vsValue($_v, $patient = null, $_direct = false)
  762. {
  763. if ($_direct) {
  764. return $_v ? $_v : '<span class="font-weight-normal text-info font-italic">empty</span>';
  765. }
  766. return @($patient->{$_v}) ? $patient->{$_v} : '<span class="font-weight-normal text-info font-italic">empty</span>';
  767. }
  768. }
  769. if(!function_exists('vsElement')) {
  770. function vsElement($_v, $type, $name, $patient, $class = '')
  771. {
  772. return '<input type="' . $type . '" class="form-control form-control-sm min-width-unset rounded-0 ' . $class . '" ' .
  773. 'name="' . $name . '" ' .
  774. 'value="' . (@($patient->{$_v}) ? $patient->{$_v} : '') . '">';
  775. }
  776. }
  777. if(!function_exists('vsRoElement')) {
  778. function vsRoElement($_v, $type, $name, $patient, $class = '')
  779. {
  780. return '<input type="' . $type . '" readonly class="form-control form-control-sm min-width-unset rounded-0 ' . $class . '" ' .
  781. 'name="' . $name . '" ' .
  782. 'value="' . (@($patient->{$_v}) ? $patient->{$_v} : '') . '">';
  783. }
  784. }
  785. if(!function_exists('str_compact')) {
  786. function str_compact($_str)
  787. {
  788. return preg_replace("/[^a-zA-Z0-9]/", "", strip_tags($_str));
  789. }
  790. }
  791. if(!function_exists('noteMethodDisplay')) {
  792. function noteMethodDisplay($method)
  793. {
  794. if($method === 'IN_CLINIC') return 'In-Clinic';
  795. $method = str_replace('_', ' ', $method);
  796. return ucwords(strtolower($method));
  797. }
  798. }
  799. if(!function_exists('friendly_timezone')) {
  800. function friendly_timezone($tz) {
  801. $map = [
  802. 'EASTERN' => 'EST',
  803. 'CENTRAL' => 'CST',
  804. 'MOUNTAIN' => 'MST',
  805. 'PACIFIC' => 'PST',
  806. 'ALASKA' => 'AST',
  807. 'HAWAII' => 'HST',
  808. 'PUERTO_RICO' => 'PR'
  809. ];
  810. return $map[$tz] ?? str_replace("_", " ", $tz);
  811. }
  812. }
  813. if(!function_exists('segment_template_summary_value_display')) {
  814. function segment_template_summary_value_display($value, $default='________', $class = '') {
  815. if($value && strlen($value)) return '<span class="segment-template-summary-value '.$class.'">' . $value . '</span>';
  816. return $default;
  817. }
  818. }
  819. if(!function_exists('starts_with')) {
  820. function starts_with($haystack, $needle) {
  821. $length = strlen($needle);
  822. return substr($haystack, 0, $length) === $needle;
  823. }
  824. }
  825. if(!function_exists('get_doc_templates')){
  826. function get_doc_templates(){
  827. $basePath = 'views/document-templates-generic';
  828. $dir = opendir(resource_path($basePath));
  829. $templates = [];
  830. if($dir) {
  831. while ( $entry = readdir($dir) ) {
  832. if($entry !== '.' && $entry !== '..' && is_dir(resource_path($basePath . '/' . $entry))) {
  833. $spec = json_decode(file_get_contents(resource_path($basePath . '/' . $entry) . '/spec.json'));
  834. if(isset($spec->active) && $spec->active) {
  835. $html = file_get_contents(resource_path($basePath . '/' . $entry) . '/content.blade.php');
  836. // get fields
  837. $proVariables = [];
  838. $htmlDisplay = preg_replace_callback(
  839. '/{([^}]+)}/',
  840. function ($match) use (&$proVariables) {
  841. $token = $match[1];
  842. $replacement = '';
  843. if(strpos($token, '.') === FALSE) {
  844. $adminField = false;
  845. if($token[0] === '@') {
  846. // $token = substr($token, 1);
  847. $adminField = true;
  848. }
  849. switch ($token) {
  850. case 'TODAY':
  851. $replacement = date('m/d/Y');
  852. break;
  853. default:
  854. $replacement = '<span class="text-info document-variable" data-variable="' . $token . '">' .
  855. ($token[0] === '@' ? substr($token, 1) : $token) .
  856. '</span>';
  857. if (!isset($proVariables[$token])) {
  858. $proVariables[$token] = [
  859. 'token' => $token,
  860. 'adminField' => $adminField
  861. ];
  862. }
  863. break;
  864. }
  865. }
  866. return $replacement;
  867. },
  868. $html
  869. );
  870. $templates[] = [
  871. "name" => $entry,
  872. "title" => $spec->title,
  873. "html" => $html,
  874. "htmlDisplay" => $htmlDisplay,
  875. "proVariables" => $proVariables
  876. ];
  877. }
  878. }
  879. }
  880. }
  881. closedir($dir);
  882. // sort by document name
  883. $names = array_column($templates, 'title');
  884. array_multisort($names, SORT_ASC, $templates);
  885. return $templates;
  886. }
  887. }
  888. if(!function_exists('format_phone_number')) {
  889. function format_phone_number($number, $default = '-') {
  890. if(empty($number)) return $default;
  891. return preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '($1) $2-$3', $number). "\n";
  892. }
  893. }
  894. if(!function_exists('format_number')) {
  895. function format_number($number, $places = 2) {
  896. return number_format((float)$number, $places, '.', '');
  897. }
  898. }
  899. if(!function_exists('toHumanReadable')) {
  900. function toHumanReadable($name) {
  901. return ucwords(preg_replace("/[^0-9a-z]/i", " ", $name));
  902. }
  903. }
  904. if(!function_exists('parseRender')) {
  905. function parseRender($_data) {
  906. if($_data) {
  907. $type = gettype($_data);
  908. if(is_string($_data) || is_numeric($_data)) {
  909. echo $_data;
  910. }
  911. else {
  912. echo "<table class='table table-sm border w-100'>";
  913. foreach($_data as $k => $v) {
  914. echo "<tr>";
  915. echo "<td><b class='text-secondary'>" . toHumanReadable($k) . "</b></td>";
  916. echo "<td>";
  917. if(is_object($v)) {
  918. parseRender($v);
  919. }
  920. elseif(is_array($v)) {
  921. foreach($v as $k2 => $v2) {
  922. parseRender($v2);
  923. }
  924. }
  925. else {
  926. echo $v;
  927. }
  928. echo "</td>";
  929. echo "</tr>";
  930. }
  931. echo "</table>";
  932. }
  933. }
  934. }
  935. }
  936. if(!function_exists('mask_string')) {
  937. function mask_string($string, $mask = "*", $lengthFromLastCharacter = null) {
  938. if(!$lengthFromLastCharacter){
  939. return str_repeat($mask, strlen($string));
  940. }
  941. $firstCharacters = substr($string, ($lengthFromLastCharacter));
  942. $lastCharacters = substr($string, -$lengthFromLastCharacter);
  943. $maskedFirstCharacters = str_repeat($mask, strlen($firstCharacters));
  944. return $maskedFirstCharacters . $lastCharacters;
  945. }
  946. }