helpers.php 14 KB

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