helpers.php 9.6 KB

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