helpers.php 15 KB

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