helpers.php 12 KB

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