helpers.php 12 KB

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