helpers.php 5.8 KB

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