helpers.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 o" . ($includeTime ? ", h:ia" : ""), $result);
  20. return $result;
  21. }
  22. catch (Exception $e) {
  23. return $value;
  24. }
  25. }
  26. }
  27. if(!function_exists('friendly_time')) {
  28. function friendly_time($value, $default = '-') {
  29. if(!$value || empty($value)) return $default;
  30. try {
  31. $result = strtotime($value);
  32. $result = date("h:ia", $result);
  33. return $result;
  34. }
  35. catch (Exception $e) {
  36. return $value;
  37. }
  38. }
  39. }
  40. if(!function_exists('friendly_month')) {
  41. function friendly_month($value) {
  42. if(!$value || empty($value)) return "-";
  43. try {
  44. $result = strtotime($value);
  45. $result = date("M o", $result);
  46. return $result;
  47. }
  48. catch (Exception $e) {
  49. return $value;
  50. }
  51. }
  52. }
  53. if(!function_exists('time_in_hrminsec')) {
  54. function time_in_hrminsec($value, $default = '-') {
  55. if(!$value || empty($value)) return $default;
  56. $value = intval($value);
  57. $minutes = intval($value / 60);
  58. $seconds = $value % 60;
  59. $hours = 0;
  60. if($minutes >= 60) {
  61. $hours = intval($minutes / 60);
  62. $minutes = $minutes % 60;
  63. }
  64. $output = [];
  65. if($hours > 0) {
  66. $output[] = "{$hours}h";
  67. }
  68. if($minutes > 0) {
  69. $output[] = "{$minutes}m";
  70. }
  71. if($seconds > 0) {
  72. $output[] = "{$seconds}s";
  73. }
  74. return implode(" ", $output);
  75. }
  76. }
  77. if(!function_exists('sanitize_field_name')) {
  78. function sanitize_field_name($name) {
  79. $result = strtolower($name);
  80. return preg_replace("/[^0-9a-z]/i", "_", $result);
  81. }
  82. }
  83. if(!function_exists('renderNoteTemplate')) {
  84. function renderNoteTemplate($template, $topLevel)
  85. {
  86. echo
  87. '<div class="note-template-item" ' .
  88. 'template="' . (isset($template->template) ? $template->template : $template->text) . '" ' .
  89. 'type="' . (isset($template->type) ? $template->type : "value") . '" ' .
  90. '>' .
  91. '<div class="note-template-text d-flex align-items-center">' .
  92. '<span class="label">' .
  93. '<input type="checkbox" />' .
  94. '<span>' . $template->text . '</span>' .
  95. '</span>';
  96. if (isset($template->type) && $template->type === 'plus-minus') {
  97. echo '<div class="ml-auto mr-2 text-nowrap">';
  98. echo '<a href="#" class="plus-trigger"><i class="fa fa-plus-circle"></i></a>';
  99. echo '<a href="#" class="minus-trigger ml-1"><i class="fa fa-minus-circle"></i></a>';
  100. echo '</div>';
  101. }
  102. echo '</div>';
  103. if (isset($template->children) && count($template->children)) {
  104. echo '<i class="fa fa-chevron-right has-children"></i>';
  105. echo '<div class="note-template-children">';
  106. foreach ($template->children as $t) {
  107. renderNoteTemplate($t, false);
  108. }
  109. echo '</div>';
  110. } else if (isset($template->type) && $template->type !== 'plus-minus') {
  111. echo '<i class="fa fa-chevron-right has-children"></i>';
  112. echo '<div class="note-template-children">';
  113. if ($template->type === 'alpha') {
  114. echo '<textarea class="form-control form-control-sm"></textarea>';
  115. } else {
  116. echo '<input type="' . $template->type . '" class="form-control form-control-sm">';
  117. }
  118. echo '</div>';
  119. }
  120. echo '</div>';
  121. }
  122. }
  123. if(!function_exists('renderNoteTemplates')) {
  124. function renderNoteTemplates($path)
  125. {
  126. $templates = json_decode(file_get_contents($path));
  127. foreach ($templates->templates as $template) {
  128. renderNoteTemplate($template, true);
  129. }
  130. }
  131. }
  132. if(!function_exists('renderNoteExamTemplates')) {
  133. function renderNoteExamTemplates($parentPath, $childPath)
  134. {
  135. $templates = json_decode(file_get_contents($parentPath));
  136. $templates = $templates->templates;
  137. // override as needed with what is in template set
  138. if(file_exists($childPath)) {
  139. $orTemplates = json_decode(file_get_contents($parentPath));
  140. $orTemplates = $orTemplates->templates;
  141. for ($i = 0; $i < count($templates); $i++) {
  142. for ($j = 0; $j < count($orTemplates); $j++) {
  143. if($templates[$i]->text === $orTemplates[$j]->text) {
  144. $templates[$i] = $orTemplates[$j];
  145. }
  146. }
  147. }
  148. }
  149. foreach ($templates as $template) {
  150. renderNoteTemplate($template, true);
  151. }
  152. }
  153. }
  154. if(!function_exists('getVal')) {
  155. function getVal($object, $prop)
  156. {
  157. if (isset($object->$prop)) {
  158. return $object->$prop;
  159. } else {
  160. return '';
  161. }
  162. }
  163. }