helpers.php 11 KB

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