Helpers.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. use Illuminate\Support\Facades\Cookie;
  3. if (!function_exists('toHumanReadable')) {
  4. function toHumanReadable($name)
  5. {
  6. return ucwords(preg_replace("/[^0-9a-z]/i", " ", $name));
  7. }
  8. }
  9. if(!function_exists('myPersonas')) {
  10. function myPersonas(\App\Models\User $user) {
  11. $user->refresh();
  12. $roles = [];
  13. if($user) { // -- will always be true!
  14. if($user->is_super_admin) $roles[] = 'Admin';
  15. if($user->is_agreed_as_manager) $roles[] = 'Manager';
  16. if($user->is_agreed_as_rep) $roles[] = 'Reseller';
  17. }
  18. return $roles;
  19. }
  20. }
  21. if(!function_exists('myPersona')) {
  22. function myPersona(\App\Models\User $user) {
  23. $user->refresh();
  24. $roles = [];
  25. if($user) { // -- will always be true!
  26. if($user->is_super_admin) $roles[] = 'Admin';
  27. if($user->is_agreed_as_manager) $roles[] = 'Manager';
  28. if($user->is_agreed_as_rep) $roles[] = 'Reseller';
  29. }
  30. $role = count($roles)?$roles[0]:null;
  31. if(!$role){
  32. return null;
  33. }else{
  34. return request()->session()->get('my_persona', $role);
  35. }
  36. }
  37. }
  38. if (!function_exists('toHumanReadable')) {
  39. function toHumanReadable($name)
  40. {
  41. return ucwords(preg_replace("/[^0-9a-z]/i", " ", $name));
  42. }
  43. }
  44. if(!function_exists('friendly_date_time')) {
  45. function friendly_date_time($value, $includeTime = true, $default = '-', $long_year=false) {
  46. if(!$value || empty($value)) return $default;
  47. try {
  48. if($includeTime) {
  49. $realTimezone = 'US/Eastern';
  50. $date = new DateTime($value);
  51. $date->setTimezone(new DateTimeZone($realTimezone));
  52. return $date->format("m/d/y" . ($includeTime ? ", h:ia" : "")) . ($includeTime ? ' EST' : '');
  53. }
  54. else {
  55. $result = strtotime($value);
  56. if($long_year){
  57. $result = date("m/d/Y" . ($includeTime ? ", h:ia" : ""), $result);
  58. }else{
  59. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  60. }
  61. return $result;
  62. }
  63. }
  64. catch (Exception $e) {
  65. return $value;
  66. }
  67. }
  68. }
  69. if(!function_exists('friendly_date')) {
  70. function friendly_date($value) {
  71. if(!$value || empty($value)) return '';
  72. try {
  73. $result = strtotime($value);
  74. $result = date("m/d/Y", $result);
  75. return $result;
  76. }
  77. catch (Exception $e) {
  78. return $value;
  79. }
  80. }
  81. }
  82. if (!function_exists('parseRender')) {
  83. function parseRender($_data)
  84. {
  85. $excludeWhole = [
  86. 'uid', 'id', 'password'
  87. ];
  88. $excludeEndsWith = [
  89. '_token', '_json', '_id', '_set_at'
  90. ];
  91. if ($_data) {
  92. $type = gettype($_data);
  93. if (is_string($_data) || is_numeric($_data)) {
  94. echo $_data;
  95. } else {
  96. echo "<table class='table table-sm table-bordered border w-100 mb-0'>";
  97. foreach ($_data as $k => $v) {
  98. if(in_array($k, $excludeWhole)) continue;
  99. if(\Illuminate\Support\Str::endsWith($k, $excludeEndsWith)) continue;
  100. echo "<tr>";
  101. echo "<td class='bg-light px-2'><b class='text-secondary'>" . toHumanReadable($k) . "</b></td>";
  102. echo "<td class='w-50 w-lg-75 px-2'>";
  103. if (is_object($v)) {
  104. parseRender($v);
  105. } elseif (is_array($v)) {
  106. foreach ($v as $k2 => $v2) {
  107. parseRender($v2);
  108. }
  109. } else {
  110. if(\Illuminate\Support\Str::endsWith($k, '_at')) {
  111. echo friendly_date_time($v);
  112. }
  113. elseif(\Illuminate\Support\Str::endsWith($k, 'date')) {
  114. echo friendly_date($v);
  115. }
  116. elseif($k == 'tracking_number') {
  117. echo "<a href='https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1={$v}'>{$v}</a>";
  118. }
  119. else {
  120. echo $v;
  121. }
  122. }
  123. echo "</td>";
  124. echo "</tr>";
  125. }
  126. echo "</table>";
  127. }
  128. }
  129. }
  130. }
  131. if(!function_exists('is_wanted_as_color_class')) {
  132. function is_wanted_as_color_class($status) {
  133. if($status == 'APPROVED') return 'bg-success';
  134. if($status == 'REJECTED') return 'bg-danger';
  135. if($status == 'UNDEFINED') return 'bg-white';
  136. }
  137. }
  138. if(!function_exists('getFirstSectionUID')) {
  139. function getFirstSectionUID($uid) {
  140. $sections = explode('-', $uid);
  141. return strtoupper(@$sections[0]);
  142. }
  143. }
  144. if (!function_exists('displayAmount')) {
  145. function displayAmount($prefix, $number)
  146. {
  147. return $prefix . '' . number_format($number, 2);
  148. }
  149. }
  150. if (!function_exists('is_future_date')) {
  151. function is_future_date($date)
  152. {
  153. $startDate = strtotime(date('Y-m-d', strtotime($date) ) );
  154. $currentDate = strtotime(date('Y-m-d'));
  155. if($startDate > $currentDate) {
  156. return true;
  157. }
  158. return false;
  159. }
  160. }
  161. if (!function_exists('days_until_this_date')) {
  162. function days_until_this_date($date)
  163. {
  164. $futureDate = $date;
  165. $d = new DateTime($futureDate);
  166. $days = $d->diff(new DateTime())->format('%a');
  167. if($days == 0) return 'Few hours';
  168. return $days . ' days';
  169. }
  170. }
  171. if(!function_exists('friendly_date_est')) {
  172. function friendly_date_est($value, $default = '-') {
  173. if(!$value || empty($value)) return $default;
  174. try {
  175. $realTimezone = resolve_timezone('EASTERN');
  176. $date = new DateTime($value);
  177. $date->setTimezone(new DateTimeZone($realTimezone));
  178. return $date->format("m/d/y");
  179. }
  180. catch (Exception $e) {
  181. return $e->getMessage();
  182. }
  183. }
  184. }
  185. if(!function_exists('resolve_timezone')) {
  186. function resolve_timezone($value) {
  187. try {
  188. switch ($value) {
  189. case 'ALASKA': {
  190. return 'US/Alaska';
  191. }
  192. case 'CENTRAL': {
  193. return 'US/Central';
  194. }
  195. case 'EASTERN': {
  196. return 'US/Eastern';
  197. }
  198. case 'HAWAII': {
  199. return 'US/Hawaii';
  200. }
  201. case 'MOUNTAIN': {
  202. return 'US/Mountain';
  203. }
  204. case 'PACIFIC': {
  205. return 'US/Pacific';
  206. }
  207. case 'PUERTO_RICO': {
  208. return 'America/Puerto_Rico';
  209. }
  210. case 'UTC': {
  211. return 'UTC';
  212. }
  213. }
  214. }
  215. catch (Exception $e) {
  216. return $value;
  217. }
  218. }
  219. }
  220. if(!function_exists('friendly_time_by_time_zone')) {
  221. function friendly_time_by_time_zone($value, $default = '-', $fromTz = 'UCT', $toTz = 'US/Eastern') {
  222. if(!$value || empty($value)) return $default;
  223. try {
  224. $date = new DateTime($value, new DateTimeZone($fromTz));
  225. $date->setTimezone(new DateTimeZone($toTz));
  226. return $date->format('h:i a') . ' ' . friendly_timezone($toTz);
  227. }
  228. catch (Exception $e) {
  229. return $value;
  230. }
  231. }
  232. }
  233. if(!function_exists('friendly_timezone')) {
  234. function friendly_timezone($value) {
  235. try {
  236. switch ($value) {
  237. case 'US/Eastern': {
  238. return 'EST';
  239. }
  240. }
  241. }catch(Exception $e){
  242. return $value;
  243. }
  244. }
  245. }
  246. ?>