Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Helper.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. namespace App\Helpers;
  3. use Auth;
  4. use ReflectionClass;
  5. use File;
  6. use URL;
  7. use Spatie\Permission\Models\Permission;
  8. use Spatie\Permission\Models\Role;
  9. use Carbon\Carbon;
  10. class Helper
  11. {
  12. public static function success_alert($message)
  13. {
  14. $string = '';
  15. if (is_array($message)) {
  16. $string = '<ul>';
  17. foreach ($message as $key => $value) {
  18. $string .= '<li>' . ucfirst($value) . '</li>';
  19. }
  20. $string .= '</li>';
  21. } else {
  22. $string = ucfirst($message);
  23. }
  24. $alert = '<div class="alert alert-success alert-dismissible text-left fade show" role="alert">
  25. <strong>Success !</strong>
  26. ' . $string . '
  27. <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  28. <span aria-hidden="true">&times;</span>
  29. </button>
  30. </div>';
  31. return $alert;
  32. }
  33. /**summernote */
  34. public static function input_summernote($value)
  35. {
  36. libxml_use_internal_errors(true);
  37. $dom = new \DomDocument();
  38. $dom->loadHtml($value, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  39. $images = $dom->getElementsByTagName('img');
  40. if (!empty($images)) {
  41. foreach ($images as $k => $img) {
  42. $data = $img->getAttribute('src');
  43. list($type, $data) = explode(';', $data);
  44. list(, $data) = explode(',', $data);
  45. $data = base64_decode($data);
  46. $image_name = "/summernote/images/" . Carbon::parse(NOW())->format('d-m-Y-His') . $k . '.png';
  47. $path = public_path() . $image_name;
  48. file_put_contents($path, $data);
  49. $img->removeAttribute('src');
  50. $img->setAttribute('src', $image_name);
  51. }
  52. }
  53. $value = $dom->saveHTML();
  54. return $value;
  55. }
  56. public static function parsing_alert($message)
  57. {
  58. $string = '';
  59. if (is_array($message)) {
  60. foreach ($message as $key => $value) {
  61. $string .= ucfirst($value) . '<br>';
  62. }
  63. } else {
  64. $string = ucfirst($message);
  65. }
  66. return $string;
  67. }
  68. public static function warning_alert($message)
  69. {
  70. $string = '';
  71. if (is_array($message)) {
  72. foreach ($message as $key => $value) {
  73. $string .= '<li>' . ucfirst($value) . '</li>';
  74. }
  75. $string .= '</li>';
  76. } else {
  77. $string = ucfirst($message);
  78. }
  79. $alert = '<div class="alert alert-warning alert-dismissible fade show" role="alert">
  80. <strong>Warning !</strong> ' . $string . '
  81. <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  82. <span aria-hidden="true">&times;</span>
  83. </button>
  84. </div>';
  85. return $alert;
  86. }
  87. public static function failed_alert($message)
  88. {
  89. $string = '';
  90. if (is_array($message)) {
  91. $string = '<ul class="m-0">';
  92. foreach ($message as $key => $value) {
  93. $string .= '<li>' . ucfirst($value) . '</li>';
  94. }
  95. $string .= '</li>';
  96. } else {
  97. $string = ucfirst($message);
  98. }
  99. $alert = '<div class="alert alert-danger alert-dismissible text-left fade show" role="alert">
  100. ' . $string . '
  101. <button type="button" class="close" data-dismiss="alert" aria-label="Close">
  102. <span aria-hidden="true">&times;</span>
  103. </button>
  104. </div>';
  105. return $alert;
  106. }
  107. public static function alert_fail($message)
  108. {
  109. $string = '';
  110. if (is_array($message)) {
  111. foreach ($message as $key => $value) {
  112. $string .= ucfirst($value) . '<br>';
  113. }
  114. } else {
  115. $string = ucfirst($message);
  116. }
  117. return $string;
  118. }
  119. public static function short_text($phrase, $max_words, $tanda_baca = null)
  120. {
  121. if ($tanda_baca != null) {
  122. $phrase_array = explode($tanda_baca, $phrase);
  123. if (count($phrase_array) > $max_words && $max_words > 0) {
  124. $phrase = implode($tanda_baca, array_slice($phrase_array, 0, $max_words)) . '...';
  125. }
  126. } else {
  127. $phrase_array = explode(' ', $phrase);
  128. if (count($phrase_array) > $max_words && $max_words > 0) {
  129. $phrase = implode(' ', array_slice($phrase_array, 0, $max_words)) . '...';
  130. }
  131. }
  132. return $phrase;
  133. }
  134. /**
  135. * cek jika dia super admin jangan dikasih user + permission
  136. */
  137. public static function get_permission_by_role()
  138. {
  139. $get_roles_user = Auth::user()->roles->pluck('id')->toArray();
  140. /**1 = super admin */
  141. if (in_array(1, $get_roles_user)) {
  142. $permission = Permission::get();
  143. } else {
  144. $not_in = [
  145. 5, //permission-create
  146. 6, //permission-delete
  147. 7, //permission-update
  148. 8, //permission-list
  149. 9, //user-create
  150. 10, //user-delete
  151. 11, //user-update
  152. 12, //user-list
  153. ];
  154. $permission = Permission::whereNotIn('id', $not_in)
  155. ->get();
  156. }
  157. return $permission;
  158. }
  159. public static function get_roles()
  160. {
  161. $get_roles_user = Auth::user()->roles->pluck('id')->toArray();
  162. /**1 = super admin */
  163. if (in_array(1, $get_roles_user)) {
  164. $permission = Role::get();
  165. } else {
  166. $not_in = [1];
  167. $permission = Role::whereNotIn('id', $not_in)
  168. ->get();
  169. }
  170. return $permission;
  171. }
  172. public static function is_super_admin()
  173. {
  174. return Auth::user()->roles->where('id', 1)->first();
  175. }
  176. public static function format_date($date, $format)
  177. {
  178. $newDate = date($format, strtotime($date));
  179. return $newDate;
  180. }
  181. public static function swal()
  182. {
  183. if (session('success')) {
  184. alert()->html('', session('success'), 'success');
  185. }
  186. if (session('error')) {
  187. alert()->html('', session('error'), 'error');
  188. }
  189. if (session('warning')) {
  190. alert()->html('', session('warning'), 'warning');
  191. }
  192. }
  193. /**Helper Enum */
  194. public static function enum($value)
  195. {
  196. if ($value == 0) {
  197. return 'Tidak';
  198. } else if ($value == 1) {
  199. return 'Ya';
  200. }
  201. }
  202. /**end enum */
  203. public static function declare_route()
  204. {
  205. $loader = require base_path('vendor/autoload.php');
  206. $methods = [];
  207. $route = [];
  208. $classes = [];
  209. // dd($loader->getClassMap());
  210. foreach ($loader->getClassMap() as $class => $file) {
  211. if (preg_match('/[a-z]+Controller$/', $class)) {
  212. $reflection = new ReflectionClass($class);
  213. // dd($reflection);
  214. // exclude inherited methods
  215. foreach ($reflection->getMethods() as $method)
  216. if ($method->class == $reflection->getName()) {
  217. $classes[] = $class;
  218. if ($method->name == 'route') {
  219. $methods[] = $method->name;
  220. $route[] = app(get_class(new $class))->route();
  221. }
  222. }
  223. }
  224. }
  225. return $route;
  226. }
  227. public static function get_ip_user(){
  228. $ipaddress = '';
  229. if (getenv('HTTP_CLIENT_IP'))
  230. $ipaddress = getenv('HTTP_CLIENT_IP');
  231. else if(getenv('HTTP_X_FORWARDED_FOR'))
  232. $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
  233. else if(getenv('HTTP_X_FORWARDED'))
  234. $ipaddress = getenv('HTTP_X_FORWARDED');
  235. else if(getenv('HTTP_FORWARDED_FOR'))
  236. $ipaddress = getenv('HTTP_FORWARDED_FOR');
  237. else if(getenv('HTTP_FORWARDED'))
  238. $ipaddress = getenv('HTTP_FORWARDED');
  239. else if(getenv('REMOTE_ADDR'))
  240. $ipaddress = getenv('REMOTE_ADDR');
  241. else
  242. $ipaddress = 'UNKNOWN';
  243. return $ipaddress;
  244. }
  245. public static function Hash($value, $type='encode', $return_json=false)
  246. {
  247. $secret = getenv('HASH');
  248. if ($type == 'encode') {
  249. $return = base64_encode($value.$secret);
  250. }elseif ('decode') {
  251. $return = str_replace($secret, '', base64_decode($value));
  252. }else{
  253. $return = 'Wrong type hashing';
  254. }
  255. if (!empty($return_json)) {
  256. echo json_encode($return);
  257. }else{
  258. return $return;
  259. }
  260. }
  261. public static function base64_to_image($string, $nama_module, $is_json=false)
  262. {
  263. $image = $string; // your base64 encoded
  264. $image = str_replace('data:image/png;base64,', '', $image);
  265. $image = str_replace(' ', '+', $image);
  266. $imageName = str_random(10).'.'.'png';
  267. \File::put(storage_path(). '/images/'. $nama_module . '/' . $imageName, base64_decode($image));
  268. $return = URL::to('storage/images/'. $nama_module . '/' . $imageName);
  269. if (!empty($return_json)) {
  270. echo json_encode($return);
  271. }else{
  272. return $return;
  273. }
  274. }
  275. }