helpers.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. //require_once './class.Diff.php';
  10. use Soundasleep\Html2Text as Html2Text;
  11. if(!function_exists('queryLineExcept')) {
  12. function queryLineExcept($except = []) {
  13. $params = request()->all();
  14. $final = [];
  15. foreach ($params as $k => $v) {
  16. if(in_array($k, $except) === FALSE) {
  17. $final[] = "$k=" . urlencode($v);
  18. }
  19. }
  20. return implode('&', $final);
  21. }
  22. }
  23. if(!function_exists('sortColumnHead')) {
  24. function sortColumnHead($url, $label, $sortKey, $defaultDirection = 'ASC') {
  25. $currentSortKey = request()->input('sort');
  26. $currentDir = request()->input('dir');
  27. $targetDir = $currentDir ? ($currentDir === 'ASC' ? 'DESC' : 'ASC') : $defaultDirection;
  28. echo '<a href="' . $url . '?sort=' . $sortKey . '&dir=' . $targetDir . '&' . queryLineExcept(['sort', 'dir']) . '">' . $label . '</a>';
  29. if($currentSortKey === $sortKey) {
  30. if($currentDir === 'ASC') {
  31. echo "&nbsp;&nbsp;↑";
  32. }
  33. elseif($currentDir === 'DESC') {
  34. echo "&nbsp;&nbsp;↓";
  35. }
  36. }
  37. }
  38. }
  39. if(!function_exists('html2Text')) {
  40. function html2Text($old, $new){
  41. }
  42. }
  43. if(!function_exists('diff')) {
  44. function diff($old, $new){
  45. // return Diff::toHTML(Diff::compare($old, $new));
  46. }
  47. }
  48. if(!function_exists('get_current_session')) {
  49. function get_current_session(){
  50. return AppSession::where('session_key', request()->cookie('sessionKey'))->first();
  51. }
  52. }
  53. if(!function_exists('friendly_date_time')) {
  54. function friendly_date_time($value, $includeTime = true, $default = '-', $long_year=false) {
  55. if(!$value || empty($value)) return $default;
  56. try {
  57. $result = strtotime($value);
  58. if($long_year){
  59. $result = date("m/d/Y" . ($includeTime ? ", h:ia" : ""), $result);
  60. }else{
  61. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  62. }
  63. return $result;
  64. }
  65. catch (Exception $e) {
  66. return $value;
  67. }
  68. }
  69. }
  70. if(!function_exists('friendlier_date_time')) {
  71. function friendlier_date_time($value, $includeTime = true, $default = '-') {
  72. if(!$value || empty($value)) return $default;
  73. try {
  74. $result = strtotime($value);
  75. $result = date("j M, y" . ($includeTime ? ", h:i a" : ""), $result);
  76. return $result;
  77. }
  78. catch (Exception $e) {
  79. return $value;
  80. }
  81. }
  82. }
  83. if(!function_exists('friendly_date_time_short')) {
  84. function friendly_date_time_short($value, $includeTime = true, $default = '-') {
  85. if(!$value || empty($value)) return $default;
  86. try {
  87. $result = strtotime($value);
  88. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  89. return $result;
  90. }
  91. catch (Exception $e) {
  92. return $value;
  93. }
  94. }
  95. }
  96. if(!function_exists('friendly_date_time_short_with_tz')) {
  97. function friendly_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  98. if(!$value || empty($value)) return $default;
  99. try {
  100. $realTimezone = resolve_timezone($tz);
  101. $date = new DateTime($value);
  102. $date->setTimezone(new DateTimeZone($realTimezone));
  103. return $date->format("m/d/y" . ($includeTime ? ", h:iA" : ""));
  104. }
  105. catch (Exception $e) {
  106. return $e->getMessage();
  107. }
  108. }
  109. }
  110. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp')) {
  111. function friendly_date_time_short_with_tz_from_timestamp($value, $tz='UTC', $default = '-') {
  112. if(!$value || empty($value)) return $default;
  113. try {
  114. $realTimezone = resolve_timezone($tz);
  115. $date = new DateTime("@$value");
  116. $date->setTimezone(new DateTimeZone($realTimezone));
  117. return $date->format("m/d/y, h:iA");
  118. }
  119. catch (Exception $e) {
  120. return $e->getMessage();
  121. }
  122. }
  123. }
  124. if(!function_exists('postgres_date_time_short_with_tz')) {
  125. function postgres_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  126. if(!$value || empty($value)) return $default;
  127. try {
  128. $realTimezone = resolve_timezone($tz);
  129. $date = new DateTime($value);
  130. $date->setTimezone(new DateTimeZone($realTimezone));
  131. return $date->format("Y-m-d" . ($includeTime ? " h:i:s" : ""));
  132. }
  133. catch (Exception $e) {
  134. return $e->getMessage();
  135. }
  136. }
  137. }
  138. if(!function_exists('postgres_date_time_short_with_tz_from_timestamp')) {
  139. function postgres_date_time_short_with_tz_from_timestamp($value, $tz='UTC', $default = '-') {
  140. if(!$value || empty($value)) return $default;
  141. try {
  142. $realTimezone = resolve_timezone($tz);
  143. $date = new DateTime("@$value");
  144. $date->setTimezone(new DateTimeZone($realTimezone));
  145. return $date->format("Y-m-d h:i:s");
  146. }
  147. catch (Exception $e) {
  148. return $e->getMessage();
  149. }
  150. }
  151. }
  152. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp_divide1000')) {
  153. function friendly_date_time_short_with_tz_from_timestamp_divide1000($value, $tz='UTC', $default = '-') {
  154. if(!$value || empty($value)) return $default;
  155. try {
  156. $value = (floor($value / 1000));
  157. $realTimezone = resolve_timezone($tz);
  158. $date = new DateTime("@$value");
  159. $date->setTimezone(new DateTimeZone($realTimezone));
  160. return $date->format("m/d/y, h:iA");
  161. }
  162. catch (Exception $e) {
  163. return $e->getMessage();
  164. }
  165. }
  166. }
  167. if(!function_exists('friendly_date')) {
  168. function friendly_date($value) {
  169. if(!$value || empty($value)) return '';
  170. try {
  171. $result = strtotime($value);
  172. $result = date("m/d/Y", $result);
  173. return $result;
  174. }
  175. catch (Exception $e) {
  176. return $value;
  177. }
  178. }
  179. }
  180. if(!function_exists('friendlier_date')) {
  181. function friendlier_date($value) {
  182. if(!$value || empty($value)) return '';
  183. try {
  184. $result = strtotime($value);
  185. $result = date("j M Y", $result);
  186. return $result;
  187. }
  188. catch (Exception $e) {
  189. return $value;
  190. }
  191. }
  192. }
  193. if(!function_exists('unfriendly_date')) {
  194. function unfriendly_date($value) {
  195. if(!$value || empty($value)) return '';
  196. try {
  197. $result = strtotime($value);
  198. $result = date("Y-m-d", $result);
  199. return $result;
  200. }
  201. catch (Exception $e) {
  202. return $value;
  203. }
  204. }
  205. }
  206. if(!function_exists('friendly_time')) {
  207. function friendly_time($value, $default = '-') {
  208. if(!$value || empty($value)) return $default;
  209. try {
  210. $result = strtotime($value);
  211. $result = date("h:i a", $result);
  212. return $result;
  213. }
  214. catch (Exception $e) {
  215. return $value;
  216. }
  217. }
  218. }
  219. if(!function_exists('military_time')) {
  220. function military_time($value, $tz='UTC', $default = '-') {
  221. if(!$value || empty($value)) return $default;
  222. try {
  223. $realTimezone = resolve_timezone($tz);
  224. $date = new DateTime($value);
  225. $date->setTimezone(new DateTimeZone($realTimezone));
  226. return $date->format("H:i");
  227. }
  228. catch (Exception $e) {
  229. return $value;
  230. }
  231. }
  232. }
  233. if(!function_exists('resolve_timezone')) {
  234. function resolve_timezone($value) {
  235. try {
  236. switch ($value) {
  237. case 'ALASKA': {
  238. return 'US/Alaska';
  239. }
  240. case 'CENTRAL': {
  241. return 'US/Central';
  242. }
  243. case 'EASTERN': {
  244. return 'US/Eastern';
  245. }
  246. case 'HAWAII': {
  247. return 'US/Hawaii';
  248. }
  249. case 'MOUNTAIN': {
  250. return 'US/Mountain';
  251. }
  252. case 'PACIFIC': {
  253. return 'US/Pacific';
  254. }
  255. case 'PUERTO_RICO': {
  256. return 'America/Puerto_Rico';
  257. }
  258. case 'UTC': {
  259. return 'UTC';
  260. }
  261. }
  262. }
  263. catch (Exception $e) {
  264. return $value;
  265. }
  266. }
  267. }
  268. // $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
  269. // echo $date->format('Y-m-d H:i:sP') . "\n";
  270. if(!function_exists('friendly_month')) {
  271. function friendly_month($value) {
  272. if(!$value || empty($value)) return "-";
  273. try {
  274. $result = strtotime($value);
  275. $result = date("M o", $result);
  276. return $result;
  277. }
  278. catch (Exception $e) {
  279. return $value;
  280. }
  281. }
  282. }
  283. if(!function_exists('friendly_money')){
  284. function friendly_money($value){
  285. return number_format((float)$value, 2, '.', '');
  286. }
  287. }
  288. if(!function_exists('time_in_hrminsec')) {
  289. function time_in_hrminsec($value, $default = '-') {
  290. if(!$value || empty($value)) return $default;
  291. $value = intval($value);
  292. $minutes = intval($value / 60);
  293. $seconds = $value % 60;
  294. $hours = 0;
  295. if($minutes >= 60) {
  296. $hours = intval($minutes / 60);
  297. $minutes = $minutes % 60;
  298. }
  299. $output = [];
  300. if($hours > 0) {
  301. $output[] = "{$hours}h";
  302. }
  303. if($minutes > 0) {
  304. $output[] = "{$minutes}m";
  305. }
  306. if($seconds > 0) {
  307. $output[] = "{$seconds}s";
  308. }
  309. return implode(" ", $output);
  310. }
  311. }
  312. if(!function_exists('sanitize_field_name')) {
  313. function sanitize_field_name($name) {
  314. $result = strtolower($name);
  315. return preg_replace("/[^0-9a-z]/i", "_", $result);
  316. }
  317. }
  318. if(!function_exists('renderNoteTemplate')) {
  319. function renderNoteTemplate($template, $topLevel)
  320. {
  321. echo
  322. '<div class="note-template-item" ' .
  323. 'template="' . (isset($template->template) ? $template->template : $template->text) . '" ' .
  324. 'type="' . (isset($template->type) ? $template->type : "value") . '" ' .
  325. '>' .
  326. '<div class="note-template-text d-flex align-items-center">' .
  327. '<span class="label">' .
  328. '<input type="checkbox" />' .
  329. '<span>' . $template->text . '</span>' .
  330. '</span>';
  331. if (isset($template->type) && $template->type === 'plus-minus') {
  332. echo '<div class="ml-auto mr-2 text-nowrap">';
  333. echo '<a href="#" class="plus-trigger"><i class="fa fa-plus-circle"></i></a>';
  334. echo '<a href="#" class="minus-trigger ml-1"><i class="fa fa-minus-circle"></i></a>';
  335. echo '</div>';
  336. }
  337. echo '</div>';
  338. if (isset($template->children) && count($template->children)) {
  339. echo '<i class="fa fa-chevron-right has-children"></i>';
  340. echo '<div class="note-template-children">';
  341. foreach ($template->children as $t) {
  342. renderNoteTemplate($t, false);
  343. }
  344. echo '</div>';
  345. } else if (isset($template->type) && $template->type !== 'plus-minus') {
  346. echo '<i class="fa fa-chevron-right has-children"></i>';
  347. echo '<div class="note-template-children">';
  348. if ($template->type === 'alpha') {
  349. echo '<textarea class="form-control form-control-sm"></textarea>';
  350. } else {
  351. echo '<input type="' . $template->type . '" class="form-control form-control-sm">';
  352. }
  353. echo '</div>';
  354. }
  355. echo '</div>';
  356. }
  357. }
  358. if(!function_exists('renderNoteTemplates')) {
  359. function renderNoteTemplates($path)
  360. {
  361. $templates = json_decode(file_get_contents($path));
  362. foreach ($templates->templates as $template) {
  363. renderNoteTemplate($template, true);
  364. }
  365. }
  366. }
  367. if(!function_exists('renderNoteExamTemplates')) {
  368. function renderNoteExamTemplates($parentPath, $childPath)
  369. {
  370. $templates = json_decode(file_get_contents($parentPath));
  371. $templates = $templates->templates;
  372. // override as needed with what is in template set
  373. if(file_exists($childPath)) {
  374. $orTemplates = json_decode(file_get_contents($parentPath));
  375. $orTemplates = $orTemplates->templates;
  376. for ($i = 0; $i < count($templates); $i++) {
  377. for ($j = 0; $j < count($orTemplates); $j++) {
  378. if($templates[$i]->text === $orTemplates[$j]->text) {
  379. $templates[$i] = $orTemplates[$j];
  380. }
  381. }
  382. }
  383. }
  384. foreach ($templates as $template) {
  385. renderNoteTemplate($template, true);
  386. }
  387. }
  388. }
  389. if(!function_exists('getVal')) {
  390. function getVal($object, $prop)
  391. {
  392. if (isset($object->$prop)) {
  393. return $object->$prop;
  394. } else {
  395. return '';
  396. }
  397. }
  398. }
  399. if(!function_exists('appTZtoPHPTZ')) {
  400. function appTZtoPHPTZ($_timezone)
  401. {
  402. switch ($_timezone) {
  403. case 'ALASKA':
  404. $timezone = "US/Alaska";
  405. break;
  406. case 'CENTRAL':
  407. $timezone = "US/Central";
  408. break;
  409. case 'HAWAII':
  410. $timezone = "US/Hawaii";
  411. break;
  412. case 'MOUNTAIN':
  413. $timezone = "US/Mountain";
  414. break;
  415. case 'PACIFIC':
  416. $timezone = "US/Pacific";
  417. break;
  418. case 'PUERTO_RICO':
  419. $timezone = "America/Puerto_Rico";
  420. break;
  421. default:
  422. $timezone = "US/Eastern";
  423. break;
  424. }
  425. return $timezone;
  426. }
  427. }
  428. if(!function_exists('convertToTimezone')) {
  429. function convertToTimezone($_dateTime, $_targetTimezone, $_sourceTimezone = 'UTC', $_returnRaw = false)
  430. {
  431. if (!$_dateTime) return $_dateTime;
  432. $date = new \DateTime($_dateTime, new \DateTimeZone($_sourceTimezone));
  433. $date->setTimezone(new \DateTimeZone(appTZtoPHPTZ($_targetTimezone)));
  434. return $_returnRaw ? $date : $date->format('Y-m-d H:i:s');
  435. }
  436. }
  437. if(!function_exists('minutes_to_hhmm')) {
  438. function minutes_to_hhmm($_minutes)
  439. {
  440. $h = intval(floor($_minutes / 60));
  441. $m = $_minutes;
  442. if($h > 0) {
  443. $m = $_minutes - $h * 60;
  444. }
  445. $h = ($h < 10 ? '0' : '') . $h;
  446. $m = ($m < 10 ? '0' : '') . $m;
  447. return $h . ':' . $m;
  448. }
  449. }