helpers.php 12 KB

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