PageRenderTime 25ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Helpers/helpers.php

https://gitlab.com/ahmad.jamal/sally
PHP | 174 lines | 145 code | 15 blank | 14 comment | 24 complexity | d1a9c015142e211ae37ed6fc3c06a46a MD5 | raw file
  1. <?php // Code within app\Helpers\Helper.php
  2. namespace App\Helpers;
  3. use Config;
  4. use Illuminate\Support\Str;
  5. class Helper
  6. {
  7. public static function applClasses()
  8. {
  9. // Demo
  10. $fullURL = request()->fullurl();
  11. if (App()->environment() === 'production') {
  12. for ($i = 1; $i < 7; $i++) {
  13. $contains = Str::contains($fullURL, 'demo-' . $i);
  14. if ($contains === true) {
  15. $data = config('custom.' . 'demo-' . $i);
  16. }
  17. }
  18. } else {
  19. $data = config('custom.custom');
  20. }
  21. // default data array
  22. $DefaultData = [
  23. 'mainLayoutType' => 'vertical',
  24. 'theme' => 'light',
  25. 'sidebarCollapsed' => false,
  26. 'navbarColor' => '',
  27. 'horizontalMenuType' => 'floating',
  28. 'verticalMenuNavbarType' => 'floating',
  29. 'footerType' => 'static', //footer
  30. 'layoutWidth' => 'boxed',
  31. 'showMenu' => true,
  32. 'bodyClass' => '',
  33. 'pageClass' => '',
  34. 'pageHeader' => true,
  35. 'contentLayout' => 'default',
  36. 'blankPage' => false,
  37. 'defaultLanguage' => 'en',
  38. 'direction' => env('MIX_CONTENT_DIRECTION', 'ltr'),
  39. ];
  40. // if any key missing of array from custom.php file it will be merge and set a default value from dataDefault array and store in data variable
  41. $data = array_merge($DefaultData, $data);
  42. // All options available in the template
  43. $allOptions = [
  44. 'mainLayoutType' => array('vertical', 'horizontal'),
  45. 'theme' => array('light' => 'light', 'dark' => 'dark-layout', 'bordered' => 'bordered-layout', 'semi-dark' => 'semi-dark-layout'),
  46. 'sidebarCollapsed' => array(true, false),
  47. 'showMenu' => array(true, false),
  48. 'layoutWidth' => array('full', 'boxed'),
  49. 'navbarColor' => array('bg-primary', 'bg-info', 'bg-warning', 'bg-success', 'bg-danger', 'bg-dark'),
  50. 'horizontalMenuType' => array('floating' => 'navbar-floating', 'static' => 'navbar-static', 'sticky' => 'navbar-sticky'),
  51. 'horizontalMenuClass' => array('static' => '', 'sticky' => 'fixed-top', 'floating' => 'floating-nav'),
  52. 'verticalMenuNavbarType' => array('floating' => 'navbar-floating', 'static' => 'navbar-static', 'sticky' => 'navbar-sticky', 'hidden' => 'navbar-hidden'),
  53. 'navbarClass' => array('floating' => 'floating-nav', 'static' => 'navbar-static-top', 'sticky' => 'fixed-top', 'hidden' => 'd-none'),
  54. 'footerType' => array('static' => 'footer-static', 'sticky' => 'footer-fixed', 'hidden' => 'footer-hidden'),
  55. 'pageHeader' => array(true, false),
  56. 'contentLayout' => array('default', 'content-left-sidebar', 'content-right-sidebar', 'content-detached-left-sidebar', 'content-detached-right-sidebar'),
  57. 'blankPage' => array(false, true),
  58. 'sidebarPositionClass' => array('content-left-sidebar' => 'sidebar-left', 'content-right-sidebar' => 'sidebar-right', 'content-detached-left-sidebar' => 'sidebar-detached sidebar-left', 'content-detached-right-sidebar' => 'sidebar-detached sidebar-right', 'default' => 'default-sidebar-position'),
  59. 'contentsidebarClass' => array('content-left-sidebar' => 'content-right', 'content-right-sidebar' => 'content-left', 'content-detached-left-sidebar' => 'content-detached content-right', 'content-detached-right-sidebar' => 'content-detached content-left', 'default' => 'default-sidebar'),
  60. 'defaultLanguage' => array('en' => 'en', 'fr' => 'fr', 'de' => 'de', 'pt' => 'pt'),
  61. 'direction' => array('ltr', 'rtl'),
  62. ];
  63. //if mainLayoutType value empty or not match with default options in custom.php config file then set a default value
  64. foreach ($allOptions as $key => $value) {
  65. if (array_key_exists($key, $DefaultData)) {
  66. if (gettype($DefaultData[$key]) === gettype($data[$key])) {
  67. // data key should be string
  68. if (is_string($data[$key])) {
  69. // data key should not be empty
  70. if (isset($data[$key]) && $data[$key] !== null) {
  71. // data key should not be exist inside allOptions array's sub array
  72. if (!array_key_exists($data[$key], $value)) {
  73. // ensure that passed value should be match with any of allOptions array value
  74. $result = array_search($data[$key], $value, 'strict');
  75. if (empty($result) && $result !== 0) {
  76. $data[$key] = $DefaultData[$key];
  77. }
  78. }
  79. } else {
  80. // if data key not set or
  81. $data[$key] = $DefaultData[$key];
  82. }
  83. }
  84. } else {
  85. $data[$key] = $DefaultData[$key];
  86. }
  87. }
  88. }
  89. //layout classes
  90. $layoutClasses = [
  91. 'theme' => $data['theme'],
  92. 'layoutTheme' => $allOptions['theme'][$data['theme']],
  93. 'sidebarCollapsed' => $data['sidebarCollapsed'],
  94. 'showMenu' => $data['showMenu'],
  95. 'layoutWidth' => $data['layoutWidth'],
  96. 'verticalMenuNavbarType' => $allOptions['verticalMenuNavbarType'][$data['verticalMenuNavbarType']],
  97. 'navbarClass' => $allOptions['navbarClass'][$data['verticalMenuNavbarType']],
  98. 'navbarColor' => $data['navbarColor'],
  99. 'horizontalMenuType' => $allOptions['horizontalMenuType'][$data['horizontalMenuType']],
  100. 'horizontalMenuClass' => $allOptions['horizontalMenuClass'][$data['horizontalMenuType']],
  101. 'footerType' => $allOptions['footerType'][$data['footerType']],
  102. 'sidebarClass' => '',
  103. 'bodyClass' => $data['bodyClass'],
  104. 'pageClass' => $data['pageClass'],
  105. 'pageHeader' => $data['pageHeader'],
  106. 'blankPage' => $data['blankPage'],
  107. 'blankPageClass' => '',
  108. 'contentLayout' => $data['contentLayout'],
  109. 'sidebarPositionClass' => $allOptions['sidebarPositionClass'][$data['contentLayout']],
  110. 'contentsidebarClass' => $allOptions['contentsidebarClass'][$data['contentLayout']],
  111. 'mainLayoutType' => $data['mainLayoutType'],
  112. 'defaultLanguage' => $allOptions['defaultLanguage'][$data['defaultLanguage']],
  113. 'direction' => $data['direction'],
  114. ];
  115. // set default language if session hasn't locale value the set default language
  116. if (!session()->has('locale')) {
  117. app()->setLocale($layoutClasses['defaultLanguage']);
  118. }
  119. // sidebar Collapsed
  120. if ($layoutClasses['sidebarCollapsed'] == 'true') {
  121. $layoutClasses['sidebarClass'] = "menu-collapsed";
  122. }
  123. // blank page class
  124. if ($layoutClasses['blankPage'] == 'true') {
  125. $layoutClasses['blankPageClass'] = "blank-page";
  126. }
  127. return $layoutClasses;
  128. }
  129. public static function updatePageConfig($pageConfigs)
  130. {
  131. $demo = 'custom';
  132. $fullURL = request()->fullurl();
  133. if (App()->environment() === 'production') {
  134. for ($i = 1; $i < 7; $i++) {
  135. $contains = Str::contains($fullURL, 'demo-' . $i);
  136. if ($contains === true) {
  137. $demo = 'demo-' . $i;
  138. }
  139. }
  140. }
  141. if (isset($pageConfigs)) {
  142. if (count($pageConfigs) > 0) {
  143. foreach ($pageConfigs as $config => $val) {
  144. Config::set('custom.' . $demo . '.' . $config, $val);
  145. }
  146. }
  147. }
  148. }
  149. function renderStarRating($rating, $maxRating = 5)
  150. {
  151. $fullStar = "<img src=" . asset('front/imgs/icons/yellow_star.svg') . " alt=''>";
  152. $rating = $rating <= $maxRating ? $rating : $maxRating;
  153. $fullStarCount = (int)$rating;
  154. $halfStarCount = ceil($rating) - $fullStarCount;
  155. $emptyStarCount = $maxRating - $fullStarCount - $halfStarCount;
  156. $html = str_repeat($fullStar, $fullStarCount);
  157. return $html;
  158. }
  159. }