helpers.php 11 KB

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