helpers.php 12 KB

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