helpers.php 6.0 KB

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