Ei kuvausta
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.8KB

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