Created
July 30, 2020 04:28
-
-
Save ManojKiranA/bb21b57f93dc953ba8374ebd22a03a1a to your computer and use it in GitHub Desktop.
Laravel Macros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Illuminate\Support\Str; | |
| use App\Libraries\RDStationApi; | |
| use Illuminate\Support\Debug\Dumper; | |
| Form::macro('dateField', function ($name, $label = NULL, $value = NULL, $attributes = []) { | |
| $element = '<div class="datepicker-input input-group date">'; | |
| $element .= Form::text($name, $value ? $value : old($name), field_attributes($name, array_merge($attributes, ['data-input' => 'date']))); | |
| $element .= '<span class="input-group-addon">'; | |
| $element .= '<i class="fa fa-calendar"></i>'; | |
| $element .= '</span>'; | |
| $element .= '</div>'; | |
| return field_wrapper($name, $label, $element, $attributes); | |
| }); | |
| Form::macro('currencyField', function ($name, $label = NULL, $value = NULL, $attributes = []) { | |
| $addon = '<span class="input-group-addon">'; | |
| $addon .= 'R$'; | |
| $addon .= "</span>"; | |
| $element = Form::text($name, old($name, $value), field_attributes($name, $attributes)); | |
| $out = '<div class="input-group">'; | |
| $out .= $addon; | |
| $out .= $element; | |
| $out .= '</div>'; | |
| return field_wrapper($name, $label, $out, $attributes); | |
| }); | |
| Form::macro('currency', function ($name, $label = NULL, $value = NULL, $attributes = [], $prefix = 'R$') { | |
| $addon = '<span class="input-group-addon">'; | |
| $addon .= $prefix; | |
| $addon .= "</span>"; | |
| $element = Form::text($name, old($name, $value), field_attributes($name, $attributes)); | |
| $out = '<div class="input-group">'; | |
| $out .= $addon; | |
| $out .= $element; | |
| $out .= '</div>'; | |
| return field_wrapper($name, $label, $out, $attributes); | |
| }); | |
| Form::macro('percent', function ($name, $label = NULL, $value = NULL, $attributes = []) { | |
| $addon = '<span class="input-group-addon">'; | |
| $addon .= '%'; | |
| $addon .= "</span>"; | |
| $element = Form::text($name, old($name, $value), field_attributes($name, $attributes)); | |
| $out = '<div class="input-group">'; | |
| $out .= $element; | |
| $out .= $addon; | |
| $out .= '</div>'; | |
| return field_wrapper($name, $label, $out, $attributes); | |
| }); | |
| Form::macro('inputGroupNumber', function ($name, $label, $end = false, $icon, $content, array $attributes = [], $value = NULL) { | |
| $addon = '<span class="input-group-addon">'; | |
| $addon .= $icon ? '<span class="fa fa-' . $content . '"></span>' : $content; | |
| $addon .= "</span>"; | |
| $element = Form::number($name, $value ? $value : old($name), field_attributes($name, $attributes)); | |
| $out = '<div class="input-group">'; | |
| $out .= $end ? '' : $addon; | |
| $out .= $element; | |
| $out .= $end ? $addon : ''; | |
| $out .= '</div>'; | |
| return field_wrapper($name, $label, $out, $attributes); | |
| }); | |
| Form::macro('textFieldClean', function ($name, $label = NULL, $value = NULL, $attributes = []) { | |
| $attributes = array_merge($attributes, ['placeholder' => $label]); | |
| $element = Form::text($name, $value ? $value : old($name), field_attributes($name, $attributes)); | |
| return form_group($element, $name); | |
| }); | |
| Form::macro('textField', function ($name, $label = NULL, $value = NULL, $attributes = []) { | |
| $element = Form::text($name, $value ? $value : old($name), field_attributes($name, $attributes)); | |
| return field_wrapper($name, $label, $element, $attributes); | |
| }); | |
| Form::macro('emailField', function ($name, $label = NULL, $value = NULL, $attributes = []) { | |
| $element = Form::email($name, $value ? $value : old($name), field_attributes($name, $attributes)); | |
| return field_wrapper($name, $label, $element, $attributes); | |
| }); | |
| Form::macro('emailFieldClean', function ($name, $label = NULL, $value = NULL, $attributes = []) { | |
| $attributes = array_merge($attributes, ['placeholder' => $label]); | |
| $element = Form::email($name, $value ? $value : old($name), field_attributes($name, $attributes)); | |
| return form_group($element, $name); | |
| }); | |
| Form::macro('passwordField', function ($name, $label = NULL, $attributes = []) { | |
| $element = Form::password($name, field_attributes($name, $attributes)); | |
| return field_wrapper($name, $label, $element, $attributes); | |
| }); | |
| Form::macro('passwordFieldClean', function ($name, $label = NULL, $value = NULL, $attributes = []) { | |
| $attributes = array_merge($attributes, ['placeholder' => $label]); | |
| $element = Form::password($name, field_attributes($name, $attributes)); | |
| return form_group($element, $name); | |
| }); | |
| Form::macro('textareaField', function ($name, $label = NULL, $value = NULL, $attributes = []) { | |
| $attributes = array_merge($attributes, ['rows' => 4]); | |
| $element = Form::textarea($name, $value ? $value : old($name), field_attributes($name, $attributes)); | |
| return field_wrapper($name, $label, $element, $attributes); | |
| }); | |
| Form::macro('fileField', function ($name, $label = NULL, $attributes = []) { | |
| $element = '<p>' . Form::file($name, field_attributes($name, $attributes, true)) . '</p>'; | |
| return field_wrapper($name, $label, $element, $attributes); | |
| }); | |
| Form::macro('textareaFieldClean', function ($name, $label = NULL, $value = NULL, $attributes = []) { | |
| $attributes = array_merge($attributes, ['placeholder' => $label, 'rows' => 4]); | |
| $element = Form::textarea($name, $value ? $value : old($name), field_attributes($name, $attributes)); | |
| return form_group($element, $name); | |
| }); | |
| Form::macro('selectFieldClean', function ($name, $label = NULL, $options = [], $value = NULL, $attributes = []) { | |
| $attributes = array_merge($attributes, ['placeholder' => $label]); | |
| $element = Form::select($name, $options, old($name, $value), field_attributes($name, $attributes)); | |
| return form_group($element, $name); | |
| }); | |
| Form::macro('selectField', function ($name, $label = NULL, $options = [], $attributes = [], $value = NULL) { | |
| $element = Form::select($name, $options, old($name, $value), field_attributes($name, $attributes)); | |
| return field_wrapper($name, $label, $element, $attributes); | |
| }); | |
| Form::macro('imagePickerField', function ($name, $images, $value = NULL) { | |
| $value = $value ? $value : old($name); | |
| $element = '<select name="' . $name . '" class="image-picker show-html">'; | |
| $element .= '<option ' . selected(!$value) . ' value=""></option>'; | |
| foreach($images as $image) { | |
| $element .= '<option ' . selected($value == $image->id) . ' data-img-src="' . $image->url . '" value="' . $image->id . '"></option>'; | |
| } | |
| $element .= '</select>'; | |
| return form_group($element, $name); | |
| }); | |
| Form::macro('submitBtn', function ($name = 'Enviar') { | |
| $btn = '<input type="submit" class="btn btn-success" value="' . $name . '" />'; | |
| return form_group($btn); | |
| }); | |
| Form::macro('selectMultipleField', function ($name, $label = NULL, $options, $attributes = [], $value = NULL) { | |
| $attributes = array_merge($attributes, ['multiple' => true]); | |
| $value = $value === '*' ? array_keys($options) : $value; | |
| $element = Form::select($name . '[]', $options, $value ? $value : old($name), field_attributes($name, $attributes)); | |
| return field_wrapper($name, $label, $element, $attributes); | |
| }); | |
| Form::macro('selectMultipleFieldClean', function ($name, $label = NULL, $options, $value = NULL, $attributes = []) { | |
| $attributes = array_merge($attributes, ['placeholder' => $label]); | |
| $attributes = array_merge($attributes, ['multiple' => true]); | |
| $value = $value === '*' ? array_keys($options) : $value; | |
| $element = Form::select($name . '[]', $options, !is_null($value) ? $value : old($name), field_attributes($name, $attributes)); | |
| return form_group($element, $name); | |
| }); | |
| Form::macro('checkboxField', function ($name, $label = NULL, $value = 1, $checked = NULL, $attributes = []) { | |
| $attributes = array_merge(['id' => $name], $attributes); | |
| $out = '<div class="checkbox'; | |
| $out .= field_error($name) . '">'; | |
| $out .= '<label>'; | |
| $out .= Form::checkbox($name, $value, $checked ? $checked : old($name), $attributes) . $label; | |
| $out .= '</label>'; | |
| $out .= errors_msg($name); | |
| $out .= '</div>'; | |
| return $out; | |
| }); | |
| Form::macro('checkboxWithErrorsField', function ($name, $label = NULL, $value = 1, $checked = NULL, $attributes = []) { | |
| $attributes = array_merge(['id' => $name], $attributes); | |
| $out = '<div class="form-group'; | |
| $out .= field_error($name) . '">'; | |
| $out .= Form::checkbox($name, $value, $checked ? $checked : old($name), $attributes); | |
| $out .= '<strong>' . $label . '</strong>'; | |
| //$out .= field_label($name, $label); | |
| $out .= errors_msg($name); | |
| $out .= '</div>'; | |
| return $out; | |
| }); | |
| Form::macro('radioInline', function ($name, $label = NULL, array $options, $checked = NULL, $attributes = []) { | |
| $out = $label ? '<label for="' . $name . '">' . $label . '</label>' : ''; | |
| $values = array_keys($options); | |
| $out .= '<div class="radio">'; | |
| foreach($values as $value) { | |
| $isChecked = old($name, $checked) == $value; | |
| $out .= '<label class="radio-inline">'; | |
| $out .= Form::radio($name, $value, $isChecked, $attributes) . $options[$value]; | |
| $out .= '</label>'; | |
| } | |
| $out .= '</div>'; | |
| return form_group($out, $name); | |
| }); | |
| HTML::macro('edit', function ($route, $id, $attributes = []) { | |
| $defaults = [ | |
| 'tooltip' => 'Editar', | |
| 'text' => 'Editar', | |
| 'icon' => true, | |
| 'size' => 'sm' | |
| ]; | |
| extract(array_merge($defaults, $attributes)); | |
| $out = HTML::linkRoute($route . '.edit', $text, $id, [ | |
| 'class' => "btn btn-success btn-{$size}" . ( $icon ? " btn-labeled icon-{$size} fa fa-pencil" : '' ), | |
| 'data-toggle' => 'tooltip', | |
| 'data-placement' => 'top', | |
| 'data-original-title' => $tooltip, | |
| ]); | |
| return $out; | |
| }); | |
| HTML::macro('delete', function ($route, $id, $attributes = []) { | |
| $defaults = [ | |
| 'title' => 'Deletar', | |
| 'text' => 'Deletar', | |
| 'message' => '', | |
| 'tooltip' => 'Deletar', | |
| 'icon' => true, | |
| 'size' => 'sm', | |
| 'type' => 'deletion', | |
| 'back' => false | |
| ]; | |
| extract(array_merge($defaults, $attributes)); | |
| $out = Form::open(['route' => [$route . '.destroy', $id], 'method' => 'DELETE', 'data-id' => $id, 'style' => 'display: inline-block']); | |
| if($back) { | |
| $out .= Form::hidden('back', 1); | |
| } | |
| $out .= Form::button($text, [ | |
| 'class' => "btn btn-danger btn-{$size}" . ( $icon ? " btn-labeled icon-{$size} fa fa-times" : '' ), | |
| 'data-id' => $id, | |
| 'data-type' => $type, | |
| 'data-title' => $title, | |
| 'data-toggle' => 'tooltip', | |
| 'data-message' => $message, | |
| 'data-placement' => 'top', | |
| 'data-original-title' => $tooltip, | |
| ]); | |
| $out .= Form::close(); | |
| return $out; | |
| }); | |
| Form::macro('delete', function ($route, $id, $text = '', $tooltip = false, $icon = true) { | |
| $model = explode('.', $route); | |
| $model = ucfirst(substr($model[1], 0, -1)); | |
| $tooltip = $tooltip ? $tooltip : 'Deletar ' . $model; | |
| $out = Form::open(['route' => [$route . '.destroy', $id], 'method' => 'DELETE', 'data-id' => $id, 'style' => 'display: inline-block']); | |
| $out .= '<button data-toggle="tooltip" data-placement="top" data-original-title="' . $tooltip . '" type="submit" data-id="' . $id . '" class="btn btn-danger btn-sm ' . ($icon ? 'btn-fw' : '') . '">'; | |
| $out .= $icon ? '<i class="fa fa-times"></i> ' . $text : $text; | |
| $out .= '</button>'; | |
| $out .= Form::close(); | |
| return $out; | |
| }); | |
| Form::macro('undelete', function ($route, $id, $text = '', $tooltip = false) { | |
| $model = explode('.', $route); | |
| $model = ucfirst(substr($model[1], 0, -1)); | |
| $tooltip = $tooltip ? $tooltip : 'Reativar ' . $model; | |
| $out = Form::open(['route' => [$route . '.undestroy', $id], 'method' => 'PUT', 'data-confirm' => $id . '-ativar', 'style' => 'display: inline-block']); | |
| $out .= '<button data-toggle="tooltip" data-placement="top" | |
| data-original-title="' . $tooltip . '" type="submit" data-confirm="' . $id . '-ativar" class="btn btn-warning btn-fw btn-sm">'; | |
| $out .= '<i class="fa fa-undo"></i> ' . $text; | |
| $out .= '</button>'; | |
| $out .= Form::close(); | |
| return $out; | |
| }); | |
| HTML::macro('undelete', function ($route, $id, $attributes = []) { | |
| $defaults = [ | |
| 'title' => 'Restaurar', | |
| 'text' => 'Restaurar', | |
| 'message' => '', | |
| 'tooltip' => 'Restaurar', | |
| 'icon' => true, | |
| 'size' => 'sm', | |
| 'type' => 'confirmation', | |
| 'back' => false | |
| ]; | |
| extract(array_merge($defaults, $attributes)); | |
| $out = Form::open(['route' => [$route . '.undestroy', $id], 'method' => 'PUT', 'data-id' => $id, 'style' => 'display: inline-block']); | |
| $out .= Form::hidden('ativo', '1'); | |
| if($back) { | |
| $out .= Form::hidden('back', 1); | |
| } | |
| $out .= Form::button($text, [ | |
| 'class' => "btn btn-warning btn-{$size}" . ( $icon ? " btn-labeled icon-{$size} fa fa-undo" : '' ), | |
| 'data-id' => $id, | |
| 'data-type' => $type, | |
| 'data-title' => $title, | |
| 'data-toggle' => 'tooltip', | |
| 'data-message' => $message, | |
| 'data-placement' => 'top', | |
| 'data-original-title' => $tooltip, | |
| ]); | |
| $out .= Form::close(); | |
| return $out; | |
| }); | |
| Form::macro('textFieldBtn', function ($name, $label, $content = 'pencil', $btnClass = 'success', $end = true, array $attributes = [], $value = NULL) { | |
| $addon = '<div class="input-group-btn">'; | |
| $addon .= '<button id="' . $name . '_btn" class="btn btn-' . $btnClass . '" type="button">'; | |
| $addon .= '<span class="fa fa-' . $content . '"></span>'; | |
| $addon .= '</button>'; | |
| $addon .= "</div>"; | |
| $element = Form::text($name, $value ? $value : old($name), field_attributes($name, $attributes)); | |
| $out = '<div class="input-group">'; | |
| $out .= $end ? '' : $addon; | |
| $out .= $element; | |
| $out .= $end ? $addon : ''; | |
| $out .= '</div>'; | |
| return field_wrapper($name, $label, $out, $attributes); | |
| }); | |
| Form::macro('selectFieldBtnClean', function ($name, $label = null, $options = [], $content = 'pencil', $btnClass = 'success', $end = true, array $attributes = [], $value = NULL) { | |
| $attributes = array_merge($attributes, ['placeholder' => $label]); | |
| $addon = '<div class="input-group-btn">'; | |
| $addon .= '<button id="' . $name . '_btn" class="btn btn-' . $btnClass . '" type="button">'; | |
| $addon .= '<span class="fa fa-' . $content . '"></span>'; | |
| $addon .= '</button>'; | |
| $addon .= "</div>"; | |
| $element = Form::select($name, $options, $value ? $value : old($name), field_attributes($name, $attributes)); | |
| $out = '<div class="input-group">'; | |
| $out .= $end ? '' : $addon; | |
| $out .= $element; | |
| $out .= $end ? $addon : ''; | |
| $out .= '</div>'; | |
| return form_group($out, $name); | |
| }); | |
| Form::macro('edit', function ($route, $id, $text = '', $tooltip = false) { | |
| $model = explode('.', $route); | |
| $model = ucfirst(substr($model[1], 0, -1)); | |
| $tooltip = $tooltip ? $tooltip : 'Editar ' . $model; | |
| $out = '<a data-toggle="tooltip" data-placement="top" data-original-title="' . $tooltip . '" href="' . route($route . '.edit', $id) . '" class="btn btn-fw btn-success btn-sm">'; | |
| $out .= '<i class="fa fa-pencil"></i> ' . $text; | |
| $out .= '</a>'; | |
| return $out; | |
| }); | |
| HTML::macro('dataBr', function ($data_from_bd) { | |
| return Carbon\Carbon::createFromFormat('Y-m-d', $data_from_bd)->format('d/m/Y'); | |
| }); | |
| HTML::macro('cancel', function ($route, $title = 'Cancelar', $tooltip = '') { | |
| $out = '<a href="' . route($route . '.index') . '" class="btn btn-info btn-sm">'; | |
| $out .= $title; | |
| $out .= '</a>'; | |
| return $out; | |
| }); | |
| HTML::macro('details', function ($route, $id, $title = '', $tooltip = false) { | |
| $model = explode('.', $route); | |
| $model = ucfirst(substr($model[1], 0, -1)); | |
| $tooltip = $tooltip ? $tooltip : 'Ver ' . $model; | |
| $out = '<a href="' . route($route . '.show', [$id]) . '" class="btn btn-fw btn-info btn-sm" data-toggle="tooltip" data-original-title="' . $tooltip . '">'; | |
| $out .= '<i class="fa fa-info-circle"></i> ' . $title; | |
| $out .= '</a>'; | |
| return $out; | |
| }); | |
| HTML::macro('pageHeader', function ($name) { | |
| $out = '<div class="page-header">'; | |
| $out .= '<h1>' . $name . '</h1>'; | |
| $out .= '</div>'; | |
| return $out; | |
| }); | |
| HTML::macro('currency', function ($number, $prefix = 'R$ ') { | |
| return $prefix . number_format($number, 2, ',', '.'); | |
| }); | |
| HTML::macro('modal', function ($id, $title, $msg, $confirm = 'Sim', $close = 'Fechar') { | |
| $out = '<div class="modal fade" id="' . $id . '"> | |
| <div class="modal-dialog modal-sm"> | |
| <div class="modal-content"> | |
| <div class="modal-header"> | |
| <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> | |
| <h4 class="modal-title">' . $title . '</h4> | |
| </div> | |
| <div class="modal-body"> | |
| <p> ' . $msg . '</p> | |
| </div> | |
| <div class="modal-footer"> | |
| <button type="button" class="btn btn-info" data-dismiss="modal">' . $close . '</button> | |
| <button type="button" class="btn btn-warning btn-confirmation">' . $confirm . '</button> | |
| </div> | |
| </div><!-- /.modal-content --> | |
| </div><!-- /.modal-dialog --> | |
| </div><!-- /.modal --> '; | |
| return $out; | |
| }); | |
| HTML::macro('modalDelete', function ($model, $msg = NULL, $confirmation = 'Deletar', $close = 'Fechar') { | |
| $msg = $msg ? $msg : "Tem certeza que deseja excluir este dado?"; | |
| $out = '<div class="modal fade" id="modal-delete"> | |
| <div class="modal-dialog modal-sm"> | |
| <div class="modal-content"> | |
| <div class="modal-header"> | |
| <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> | |
| <h4 class="modal-title">' . $confirmation . ' ' . $model . '</h4> | |
| </div> | |
| <div class="modal-body"> | |
| <p> ' . $msg . '</p> | |
| </div> | |
| <div class="modal-footer"> | |
| <button type="button" class="btn btn-info" data-dismiss="modal">' . $close . '</button> | |
| <button type="button" class="btn btn-danger btn-delete">' . $confirmation . '</button> | |
| </div> | |
| </div><!-- /.modal-content --> | |
| </div><!-- /.modal-dialog --> | |
| </div><!-- /.modal --> '; | |
| return $out; | |
| }); | |
| HTML::macro('table', function ($fields = [], $data = [], $msg, $resource, $showView = true, $showEdit = true, $showDelete = true) { | |
| if(!$data->count()) return '<p>' . $msg . '</p>'; | |
| $table = '<table class="table dt-responsive table-condensed" cellspacing="0" width="100%">'; | |
| $table .= '<thead>'; | |
| $table .= '<tr>'; | |
| foreach(array_keys($fields) as $field) { | |
| $table .= '<th>' . Str::title($field) . '</th>'; | |
| } | |
| if($showEdit || $showDelete || $showView) | |
| $table .= '<th>Gerenciar</th>'; | |
| $table .= '</thead>'; | |
| $table .= '</tr>'; | |
| $table .= '<tbody>'; | |
| foreach($data as $d) { | |
| $table .= '<tr>'; | |
| foreach(array_values($fields) as $key) { | |
| $table .= '<td>' . $d->$key . '</td>'; | |
| } | |
| if($showEdit || $showDelete || $showView) { | |
| $table .= '<td>'; | |
| if($showView) | |
| $table .= HTML::details($resource, $d->id, 'Detalhes'); | |
| if($showEdit) | |
| $table .= Form::edit($resource, $d->id, 'Editar'); | |
| if($showDelete) | |
| $table .= Form::delete($resource, $d->id, 'Deletar'); | |
| $table .= '</td>'; | |
| } | |
| $table .= '</tr>'; | |
| } | |
| $table .= '</tbody>'; | |
| $table .= '</table>'; | |
| return $table; | |
| }); | |
| HTML::macro('tableIcon', function ($fields = [], $data = [], $msg, $resource, $showView = true, $showEdit = true, $showDelete = true) { | |
| if(!$data->count()) return '<p>' . $msg . '</p>'; | |
| $table = '<table class="table table-responsive table-condensed ">'; | |
| $table .= '<thead>'; | |
| $table .= '<tr>'; | |
| foreach(array_keys($fields) as $field) { | |
| $table .= '<th>' . Str::title($field) . '</th>'; | |
| } | |
| if($showEdit || $showDelete || $showView) | |
| $table .= '<th>Gerenciar</th>'; | |
| $table .= '</thead>'; | |
| $table .= '</tr>'; | |
| $table .= '<tbody>'; | |
| foreach($data as $d) { | |
| $table .= '<tr>'; | |
| foreach(array_values($fields) as $key) { | |
| $table .= '<td>' . $d->$key . '</td>'; | |
| } | |
| if($showEdit || $showDelete || $showView) { | |
| $table .= '<td>'; | |
| if($showView) | |
| $table .= HTML::details($resource, $d->id); | |
| if($showEdit) | |
| $table .= Form::edit($resource, $d->id); | |
| if($showDelete) | |
| $table .= Form::delete($resource, $d->id); | |
| $table .= '</td>'; | |
| } | |
| $table .= '</tr>'; | |
| } | |
| $table .= '</tbody>'; | |
| $table .= '</table>'; | |
| return $table; | |
| }); | |
| HTML::macro('alert', function ($show = false, $msg, $class = 'success') { | |
| $class = empty($class) ? 'success' : $class; | |
| $out = '<div class="alert alert-' . $class . ' alert-dismissible" role="alert">'; | |
| $out .= '<button type="button" class="close" data-dismiss="alert" aria-label="Close">'; | |
| $out .= '<i class="fa fa-times-circle"></i>'; | |
| $out .= '</button>'; | |
| $out .= $msg; | |
| $out .= '</div>'; | |
| return $show ? $out : ''; | |
| }); | |
| HTML::macro('flash', function () { | |
| // <div class="media-body"><h4 class="alert-title"></h4><p class="alert-message">Lorem ipsum dolor sit amet, consectetuer adipiscing elit</p></div> | |
| $class = session('flashClass') ? session('flashClass') : 'success'; | |
| $out = '<div class="alert-wrap in" id="flash" role="alert">'; $out .= '<div class="alert alert-' . $class . ' alert-dismissible">'; | |
| $out .= '<button data-dismiss="alert" class="close" type="button"><i class="fa fa-times-circle"></i></button>'; | |
| $out .= '<div class="media">'; | |
| $out .= '<div class="media-body">'; | |
| if(session('flashTitle')) { | |
| $out .= '<h4 class="alert-title">'; | |
| $out .= session('flashTitle'); | |
| $out .= '</h4>'; | |
| } | |
| $out .= '<p class="alert-message">'; | |
| $out .= session('flashMsg'); | |
| $out .= '</p>'; | |
| $out .= '</div>'; | |
| $out .= '</div>'; | |
| $out .= '</div>'; | |
| $out .= '</div>'; | |
| return session()->has('flashMsg') ? $out : ''; | |
| }); | |
| HTML::macro('simpleFlash', function () { | |
| $class = session('flashClass') ? session('flashClass') : 'success'; | |
| $out = '<div class="alert-wrap in" id="flash" role="alert">'; | |
| $out .= '<div class="alert alert-' . $class . ' alert-dismissible">'; | |
| $out .= '<button data-dismiss="alert" class="close" type="button"><i class="fa fa-times-circle"></i></button>'; | |
| $out .= session('flashMsg'); | |
| $out .= '</div>'; | |
| $out .= '</div>'; | |
| return session()->has('flashMsg') ? $out : ''; | |
| }); | |
| HTML::macro('openTable', function () { | |
| $table = '<table class="table table-striped data-tables ">'; | |
| return $table; | |
| }); | |
| HTML::macro('tableOpen', function () { | |
| $table = '<table class="table table-striped data-tables ">'; | |
| return $table; | |
| }); | |
| HTML::macro('tableHeader', function ($headers = []) { | |
| $table = '<thead>'; | |
| $table .= '<tr>'; | |
| foreach($headers as $header) { | |
| $table .= '<th>' . $header . '</th>'; | |
| } | |
| $table .= '</tr>'; | |
| $table .= '</thead>'; | |
| return $table; | |
| }); | |
| HTML::macro('tableBody', function (array $keys, array $data, $resource = NULL, $showView = true, $showEdit = true, $showDelete = true) { | |
| $table = '<tbody>'; | |
| foreach($data as $d) { | |
| $table .= '<tr>'; | |
| foreach($keys as $key) { | |
| $table .= '<td>' . $d->$key . '</td>'; | |
| } | |
| if($showEdit || $showDelete || $showView) { | |
| $table .= '<td>'; | |
| if($showView) | |
| $table .= HTML::details($resource, $d->id); | |
| if($showEdit) | |
| $table .= Form::edit($resource, $d->id); | |
| if($showDelete) | |
| $table .= Form::delete($resource, $d->id); | |
| $table .= '</td>'; | |
| } | |
| $table .= '</tr>'; | |
| } | |
| $table = '</tbody>'; | |
| return $table; | |
| }); | |
| HTML::macro('tableClose', function () { | |
| $table = '</table>'; | |
| return $table; | |
| }); | |
| HTML::macro('closeTable', function () { | |
| $table = '</table>'; | |
| return $table; | |
| }); | |
| HTML::macro('notification', function($msg = "", $url = "javascript:void", $class = "warning", $icon = "fa-exclamation") { | |
| $out = "<li>"; | |
| $out .= "<a href=\"{$url}\" class=\"media\">"; | |
| $out .= "<div class=\"media-left\">"; | |
| $out .= "<span class=\"icon-wrap icon-circle bg-{$class}\">"; | |
| $out .= "<i class=\"fa fa-{$icon} fa-lg\"></i>"; | |
| $out .= "</span>"; | |
| $out .= "</div>"; | |
| $out .= "<div class=\"media-body\">"; | |
| $out .= "<div class=\"text-dark\">{$msg}</div>"; | |
| $out .= "</div>"; | |
| $out .= "</a>"; | |
| $out .= "</li>"; | |
| return $out; | |
| }); | |
| function active($route, $active = 'active') { | |
| return Request::is($route) ? $active : ''; | |
| } | |
| function active_if($condition, $active = 'active') { | |
| return $condition ? $active : ''; | |
| } | |
| function checked_if($condition, $checked = 'checked') { | |
| return $condition ? $checked : ''; | |
| } | |
| function active_route($route) { | |
| return URL::route($route) == Request::url() ? 'active' : ''; | |
| } | |
| function selected($condition) { | |
| return $condition ? 'selected' : ''; | |
| } | |
| function disabled($condition) { | |
| return $condition ? 'disabled' : ''; | |
| } | |
| function errors_msg($field) { | |
| $errors = session('errors'); | |
| if($errors && $errors->has($field)) { | |
| $msg = $errors->first($field); | |
| return '<p class="help-block js-laravel-error-message">' . $msg . '</p>'; | |
| } | |
| return ''; | |
| } | |
| function field_error($field) { | |
| $error = ''; | |
| if($errors = session('errors')) { | |
| $error = $errors->first($field) ? ' has-error' : ''; | |
| } | |
| return $error; | |
| } | |
| function field_label($name, $label) { | |
| if(is_null($label)) return ''; | |
| $name = str_replace('[]', '', $name); | |
| $out = '<label for="' . $name . '" class="control-label">'; | |
| $out .= $label . '</label>'; | |
| return $out; | |
| } | |
| function field_attributes($name, $attributes = [], $noClass = false) { | |
| $name = str_replace('[]', '', $name); | |
| if(isset($attributes['class']) && !$noClass) { | |
| $attributes['class'] .= ' form-control'; | |
| } elseif(!$noClass) { | |
| $attributes['class'] = 'form-control'; | |
| } else { | |
| $attributes['class'] = ''; | |
| } | |
| $attributes['id'] = isset($attributes['id']) ? $attributes['id'] : $name; | |
| return $attributes; | |
| } | |
| function ternary($condition, $else = '') { | |
| return $condition ? $condition : $else; | |
| } | |
| function format_float($string) { | |
| $string = str_replace('R$ ', '', $string); | |
| $string = str_replace('.', '', $string); | |
| $string = str_replace(',', '.', $string); | |
| return floatval($string); | |
| } | |
| function format_currency ($number, $prefix = 'R$ ') { | |
| return $prefix . number_format($number, 2, ',', '.'); | |
| } | |
| function field_wrapper($name, $label, $element, $attributes) { | |
| $out = '<div class="form-group'; | |
| $out .= field_error($name) . '">'; | |
| $out .= field_label($name, $label); | |
| $out .= $element; | |
| $out .= errors_msg($name); | |
| $out .= isset($attributes['help']) ? '<span class="help-block">' . $attributes['help'] . '</span>' : ''; | |
| $out .= '</div>'; | |
| return $out; | |
| } | |
| function form_group($element, $name = '') { | |
| $out = '<div class="form-group'; | |
| $out .= field_error($name) . '">'; | |
| $out .= $element; | |
| $out .= errors_msg($name); | |
| $out .= '</div>'; | |
| return $out; | |
| } | |
| function errors() { | |
| $out = ""; | |
| if(session()->has('errors')) { | |
| foreach(session('errors') as $error) { | |
| $out .= '<p class="help-block">' . $error . '</p>'; | |
| } | |
| } | |
| return $out; | |
| } | |
| /** | |
| * Returns a string with the actual Method and Path names of the request | |
| * | |
| * @return string | |
| */ | |
| function get_method_path() { | |
| return Request::method() . ' /' . Request::path(); | |
| } | |
| function change_error_bag($name) { | |
| $errors = session('errors'); | |
| if(!is_null($errors) && $errors instanceof Illuminate\Support\ViewErrorBag && $errors->{$name}->any()) { | |
| session()->set('errors', $errors->{$name}); | |
| } elseif($errors instanceof Illuminate\Support\MessageBag) { | |
| session()->forget('errors'); | |
| Request::flush(); | |
| } | |
| } | |
| /** | |
| * Equivalent to the toFixed method of Javascript Numbers | |
| * @param float $number | |
| * @param int $decimals = 2 | |
| * | |
| * @return string | |
| */ | |
| function to_fixed($number, $decimals = 2) { | |
| return number_format((float) $number, $decimals, '.', ''); | |
| } | |
| /** | |
| * Validate money in US and other patterns without the prefix or sufix. | |
| * Only validates numbers with commas and dots. | |
| * Ex: 100,00 // is valid | |
| * Ex: 100.00 // is valid | |
| * Ex: 100a00 // is invalid | |
| * Ex: 1,000.0 // is valid | |
| * Ex: 1.000,0 // is valid | |
| * @param string $number | |
| * | |
| * @return bool | |
| */ | |
| function is_money($number) { | |
| return preg_match("/^[0-9]{1,3}(,?[0-9]{3})*(\.[0-9]{1,2})?$/", $number) || preg_match("/^[0-9]{1,3}(\.?[0-9]{3})*(,[0-9]{1,2})?$/", $number); | |
| } | |
| /** | |
| * | |
| */ | |
| function money_to_float ($number) { | |
| if (preg_match("/^[0-9]{1,3}(,?[0-9]{3})*(\.[0-9]{1,2})?$/", $number)) { | |
| return (float) str_replace(',', '', $number); | |
| } elseif(preg_match("/^[0-9]{1,3}(\.?[0-9]{3})*(,[0-9]{1,2})?$/", $number)) { | |
| return (float) str_replace(',', '.', str_replace('.', '', $number)); | |
| } else { | |
| throw new Exception('The parameter is not a valid money string.'); | |
| } | |
| } | |
| Validator::extend('money', function($attribute, $value, $parameters) { | |
| return is_money($value); | |
| }); | |
| Validator::extend('expense_limit', function($attribute, $value, $parameters, $validator) { | |
| $idDespesaTipo = key_exists('idDespesaTipo', $validator->getData()) | |
| ? $validator->getData()['idDespesaTipo'] | |
| : null; | |
| $politica = user('limitePolitica'); | |
| $value = money_to_float($value); | |
| if(!empty($idDespesaTipo) && !is_null($politica) && ($idDespesaTipo != user('empresa')->idTipoPercurso)) { | |
| $tipo = $politica->tipos->whereLoose('idDespesaTipo', $idDespesaTipo)->first(); | |
| if(!is_null($tipo) && !is_null($tipo->valorLimitePorDespesaItem) && ($value > $tipo->valorLimitePorDespesaItem)) { | |
| if($tipo->permiteUltrapassarLimite) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| } | |
| return true; | |
| }); | |
| use App\Repositories\Contracts\PercursoRepository; | |
| Validator::extend('percurso_permitealterarvalorkm', function($attribute, $value, $parameters, $validator) { | |
| if(!isset($validator->getData()['valorKm'])) { | |
| return true; | |
| } | |
| $permiteAlterarValorKm = user('empresa')->parameters('empresa', 'permiteAlterarValorKm'); | |
| $permiteAlterarValorKm = !is_null($permiteAlterarValorKm) ? $permiteAlterarValorKm : 1; | |
| if($permiteAlterarValorKm === "0") { | |
| $data = $validator->getData(); | |
| if($data['valorKm'] != user('empresa')->valorKm) { | |
| return false; | |
| } | |
| if(!empty($data['idPercurso'])) { | |
| $percurso = app(PercursoRepository::class)->find($data['idPercurso']); | |
| $data['kilometragem'] = $percurso->kilometragem; | |
| } | |
| if($data['valor'] != round($data['valorKm'] * $data['kilometragem'], 2)) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| }); | |
| Validator::extend('receipt_needed', function($attribute, $value, $parameters, $validator) { | |
| $politica = user('limitePolitica'); | |
| $files = $validator->getFiles(); | |
| $hasFile = key_exists('_hasFile', $validator->getData()); | |
| if(!empty($value) && !is_null($politica) && ($value != user('empresa')->idTipoPercurso)) { | |
| $tipo = $politica->tipos->whereLoose('idDespesaTipo', $value)->first(); | |
| if(!is_null($tipo) && $tipo->exigeRecibo) { | |
| return isset($files['pdf']) || isset($files['imagem']) || $hasFile; | |
| } | |
| } | |
| return true; | |
| }); | |
| /** | |
| * Verifica se a etapa é valida, onde todos os grupos e etapas passadas | |
| * tenham um usuario inserido | |
| */ | |
| Validator::extend('valid_step', function($attribute, $value, $parameters, $validator) { | |
| $inputs = $validator->getData(); | |
| foreach($inputs['condicaoFilho'] as $keyEtapa => $condicaoEtapa) { | |
| // Faz mapeamento dentre os grupos de usuarios pertencentes a esta etapa | |
| $usuariosEtapa = array_map(function ($grupo) { | |
| // Reduz o array a false dentre os usuarios desse grupo e etapa se não for | |
| // encontrado nenhum usuario, true se for encontrado | |
| return array_reduce($grupo, function ($carry, $usuario) { | |
| return $carry ?: $usuario !== ''; | |
| }); | |
| }, $inputs['usuarios'][$keyEtapa]); | |
| if(array_search(false, $usuariosEtapa) !== false) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| }); | |
| /** | |
| * Não permite auto aprovação | |
| */ | |
| use App\Repositories\Contracts\UsuarioRepository; | |
| use App\Repositories\Criteria\FromAuthCompanyCriteria; | |
| Validator::extend('no_auto_approval', function($attribute, $value, $parameters, $validator) { | |
| $request = request(); | |
| if($request->method() === 'POST') { | |
| return true; | |
| } | |
| $fluxo = (int) $request->route('fluxos_aprovacao'); | |
| $approvers = collect($value)->values()->flatten()->values()->all(); | |
| return !app(UsuarioRepository::class) | |
| ->pushCriteria(new FromAuthCompanyCriteria) | |
| ->findWhereIn('id', $approvers) | |
| ->where('idAprovacaoFluxo', $fluxo, false) | |
| ->count(); | |
| }); | |
| /** | |
| * Verifica se não há repetição de ids de usuarios passados no fluxo de aprovação | |
| */ | |
| Validator::extend('no_repeat', function($attribute, $value, $parameters, $validator) { | |
| $usuarios = collect($value)->collapse()->collapse()->filter(); | |
| return $usuarios->count() === $usuarios->unique()->count(); | |
| }); | |
| /** | |
| * Bool for humans. Just to see the world burn! | |
| * @param bool | |
| * | |
| * @return string | |
| */ | |
| function bool_for_humans($condition) { | |
| return $condition ? "Sim" : "Não"; | |
| } | |
| function make_rdstation_default_lead($user, $identifier = 'CADASTRO') { | |
| $rdAPI = new RDStationAPI("54a8c6985dbd1c109eb6deb1dfd47cfa", "b4e1a1d73d026bac3a94068f9758d524"); | |
| $rdAPI->sendNewLead($user->email, [ | |
| 'nome' => $user->nome, | |
| 'vex-fonte' => $user->origem, | |
| 'identificador' => $identifier, | |
| 'vex-tamanho-equipe' => 0, | |
| 'vex-alterou-dados-empresa' => 'Não', | |
| 'vex-confirmou-cadastro' => 'Não', | |
| 'vex-frequencia-utilizacao' => 0, | |
| 'vex-total-despesas-inseridas' => 0, | |
| 'vex-data-ultima-despesa-inserida' => 0, | |
| 'vex-diferenca-de-dias-entre-cadastro-ultima-despesa' => 0 | |
| ]); | |
| } | |
| function decode_hash($hash) { | |
| $id = Hashids::decode($hash); | |
| if(empty($id)) { | |
| return null; | |
| } | |
| return count($id) > 1 ? $id : $id[0]; | |
| } | |
| function list_errors($messages) { | |
| return '<ul class="list-unstyled">' . implode('', $messages->all('<li>:message</li>')) . '</ul>'; | |
| }; | |
| function versioned_asset($path) | |
| { | |
| return asset($path) . '?v=' . filemtime(public_path($path)); | |
| } | |
| function get_domain() { | |
| $domain = str_replace(['http://', 'https://', 'dev.', 'app.', 'api.', 'intelliscan.', 'admin.'], '', Request::root()); | |
| return $domain; | |
| } | |
| function str_title($str) { | |
| return Str::title($str); | |
| } | |
| function user($property = null) { | |
| if(auth()->check()) { | |
| return $property ? auth()->user()->{$property} : auth()->user(); | |
| } | |
| return null; | |
| } | |
| function rdstation() { | |
| return new RDStationApi( | |
| "54a8c6985dbd1c109eb6deb1dfd47cfa", | |
| "b4e1a1d73d026bac3a94068f9758d524" | |
| ); | |
| } | |
| function get_modal_name($name, $prefix = 'app.partials.modal-') { | |
| return str_replace($prefix, '', $name); | |
| }; | |
| use Moip\Moip; | |
| use Moip\MoipBasicAuth; | |
| function moip() { | |
| $endpoint = app()->environment('production') | |
| ? Moip::ENDPOINT_PRODUCTION | |
| : Moip::ENDPOINT_SANDBOX; | |
| return new Moip(new MoipBasicAuth(env('MOIP_TOKEN'), env('MOIP_KEY')), $endpoint); | |
| } | |
| /** | |
| * Tests if the string matches a CPF document | |
| * @param string $cpf | |
| * | |
| * @return bool | |
| */ | |
| function is_cpf($cpf) { | |
| return preg_match('/^\d{3}\.\d{3}\.\d{3}\-\d{2}$/', $cpf) || strlen($cpf) === 11; | |
| } | |
| /** | |
| * Tests if the string matches a CNPJ document | |
| * @param string $cnpj | |
| * | |
| * @return bool | |
| */ | |
| function is_cnpj($cnpj) { | |
| return preg_match('/^\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}$/', $cnpj) || strlen($cnpj) === 14; | |
| } | |
| use Illuminate\Support\Collection; | |
| use Illuminate\Database\Eloquent\Collection as E_Collection; | |
| E_Collection::macro('update', function(array $update) { | |
| return $this->each(function($item) use($update) { | |
| $item->update($update); | |
| }); | |
| }); | |
| E_Collection::macro('updateAll', function(array $update) { | |
| return $this->each(function($item) use($update) { | |
| $item->update($update); | |
| }); | |
| }); | |
| E_Collection::macro('deleteAll', function() { | |
| return $this->each(function($item) { | |
| $item->delete(); | |
| }); | |
| }); | |
| E_Collection::macro('lists', function($value, $key = null) { | |
| return $this->pluck($value, $key); | |
| }); | |
| Collection::macro('dd', function() { | |
| dd($this); | |
| }); | |
| Collection::macro('lists', function($value, $key = null) { | |
| return $this->pluck($value, $key); | |
| }); | |
| Collection::macro('ddArray', function() { | |
| dd($this->toArray()); | |
| }); | |
| Collection::macro('find', function($key, $value, $strict = true) { | |
| return $this->where($key, $value, $strict)->first(); | |
| }); | |
| if(!function_exists('uses_trait')) { | |
| /** | |
| * Returns if the passed class uses or not a Trait | |
| * | |
| * @return bool | |
| */ | |
| function uses_trait($class, $trait, $strict = false) | |
| { | |
| $traits = array_values(class_uses($class)); | |
| if($strict) { | |
| return in_array($trait, $traits); | |
| } | |
| $traits = array_map('class_basename', $traits); | |
| return in_array($trait, $traits); | |
| } | |
| } | |
| if(!function_exists('dd_error')) { | |
| /** | |
| * Die and dump with HTTP:500 | |
| * | |
| * @return void | |
| */ | |
| function dd_error() | |
| { | |
| http_response_code($fixture); | |
| array_map(function ($x) { | |
| (new Dumper)->dump($x); | |
| }, func_get_args()); | |
| die(1); | |
| } | |
| } | |
| if(!function_exists('round_up')) { | |
| /** | |
| * Rounds up a number | |
| * | |
| * @return float | |
| */ | |
| function round_up($number, $precision = 2) | |
| { | |
| return round($number, $precision); | |
| } | |
| } | |
| if(!function_exists('round_down')) { | |
| /** | |
| * Rounds down a number | |
| * | |
| * @return float | |
| */ | |
| function round_down($number, $precision = 2) | |
| { | |
| $mult = pow(10, $precision); | |
| return floor($number * $mult) / $mult; | |
| } | |
| } | |
| if(!function_exists('svg_icon')) { | |
| /** | |
| * Get inline svg by icon name | |
| * | |
| * @return string | |
| */ | |
| function svg_icon($icon) | |
| { | |
| return file_get_contents( | |
| public_path("assets/icons/{$icon}.svg") | |
| ); | |
| } | |
| } | |
| if(!function_exists('filter_null')) { | |
| /** | |
| * Transforms an invalid string in null | |
| * | |
| * @return string|null | |
| */ | |
| function filter_null($value) | |
| { | |
| $value = trim($value); | |
| if(!strlen($value) || in_array($value, ['(null)', '<null>', 'null'])) { | |
| return null; | |
| } | |
| return $value; | |
| } | |
| } | |
| use Illuminate\Database\Query\Builder; | |
| Builder::macro('lists', function($value, $key = null) { | |
| return $this->pluck($value, $key); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment