From a151393ecda32e63214d8093ce6800998a686652 Mon Sep 17 00:00:00 2001 From: andyv4 Date: Sun, 11 Jul 2021 16:00:36 +0700 Subject: [PATCH] v0.9.006 --- src/Http/Controllers/ActionableController.php | 10 ++ src/Http/Controllers/TableView1Controller.php | 117 ++++++++++++------ src/Responses/HTMLResponse.php | 10 +- src/helpers.php | 2 +- src/views/tableview1.blade.php | 4 +- 5 files changed, 100 insertions(+), 43 deletions(-) diff --git a/src/Http/Controllers/ActionableController.php b/src/Http/Controllers/ActionableController.php index 0d9d4ad..847e63d 100644 --- a/src/Http/Controllers/ActionableController.php +++ b/src/Http/Controllers/ActionableController.php @@ -5,6 +5,7 @@ use Andiwijaya\WebApp\Exceptions\KnownException; use Illuminate\Http\Request; use Illuminate\Routing\Controller as BaseController; +use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\View; use function foo\func; @@ -81,4 +82,13 @@ public function alertRequest(Request $request){ return htmlresponse()->alert(json_encode($request->all(), JSON_PRETTY_PRINT)); } + + public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = []) + { + $validator = Validator::make($request->all(), $rules, $messages, $customAttributes); + + if($validator->fails()){ + exc(implode("
\n", $validator->errors()->all())); + } + } } \ No newline at end of file diff --git a/src/Http/Controllers/TableView1Controller.php b/src/Http/Controllers/TableView1Controller.php index d8ea1b7..bc39212 100644 --- a/src/Http/Controllers/TableView1Controller.php +++ b/src/Http/Controllers/TableView1Controller.php @@ -12,6 +12,7 @@ class TableView1Controller extends ActionableController protected $extends; protected $title; protected $model; + protected $id; protected $columns = [ [ 'text'=>'', 'name'=>'options', 'width'=>50 ], [ 'text'=>'Name', 'name'=>'name', 'width'=>260, 'filterable'=>[ 'type'=>'string' ], 'sortable'=>true ], @@ -35,6 +36,7 @@ public function view(Request $request) View::share([ 'extends'=>$this->extends, 'title'=>$this->title, + 'id'=>$this->id, 'column_html'=>$this->renderHeader() ]); @@ -47,51 +49,18 @@ public function load(Request $request) $html = []; foreach($data as $obj){ - - $id = $obj['id'] ?? ''; - - $tag = ""; - foreach($this->columns as $column){ - - $name = $column['name'] ?? ''; - $text = $obj[$name] ?? ''; - $datatype = $column['datatype'] ?? 'text'; - $align = $column['align'] ?? ''; - $class = $column['class'] ?? ''; - - switch($datatype){ - case 'number': - $text = number_format(doubleval($text)); - if(!$align) $align = 'align-right'; - break; - - case 'datetime': - $text = date('j M Y H:i', strtotime($text)); - break; - } - - $tag .= ""; - if(method_exists($this, ($method = 'column' . ucwords(Str::camel($name))))){ - $tag .= $this->$method($obj, $column); - } - else{ - $tag .= ""; - } - $tag .= ""; - } - $tag .= ""; - - $html[] = $tag; + + $html[] = $this->renderItem($obj); } $html = implode('', $html); $response = htmlresponse(); if($page <= 1) - $response->value('#tableview1', $html, [ 'next_page'=>$next ]) - ->html('#tableview1 .table-foot', $this->renderFooter($builder)); + $response->value("#{$this->id}", $html, [ 'next_page'=>$next ]) + ->html("#{$this->id} .table-foot", $this->renderFooter($builder)); else - $response->append('#tableview1', $html, [ 'next_page'=>$next ]); + $response->append("#{$this->id}", $html, [ 'next_page'=>$next ]); return $response; } @@ -235,6 +204,12 @@ protected function renderHeader() $sortable = $column['sortable'] ?? false; switch($datatype){ + + case 'bool': + case 'boolean': + if(!$align) $align = 'align-center'; + break; + case 'number': if(!$align) $align = 'align-right'; break; @@ -251,6 +226,65 @@ protected function renderHeader() return implode('', $html); } + + protected function renderItem($obj){ + + $id = $obj['id'] ?? ''; + $tag = ""; + foreach($this->columns as $column){ + + $name = $column['name'] ?? ''; + $text = $obj[$name] ?? ''; + $datatype = $column['datatype'] ?? 'text'; + $align = $column['align'] ?? ''; + $class = $column['class'] ?? ''; + + switch($datatype){ + + case 'bool': + case 'boolean': + if(!$align) $align = 'align-center'; + break; + + case 'number': + $text = number_format(doubleval($text)); + if(!$align) $align = 'align-right'; + break; + + case 'date': + $text = date('j M Y', strtotime($text)); + break; + + case 'datetime': + $text = date('j M Y H:i', strtotime($text)); + break; + } + + $tag .= ""; + switch($datatype){ + + case 'bool': + case 'boolean': + if($text) + $tag .= ""; + else + $tag .= ""; + break; + + default: + if(method_exists($this, ($method = 'column' . ucwords(Str::camel($name))))){ + $tag .= $this->$method($obj, $column); + } + else{ + $tag .= ""; + } + } + $tag .= ""; + } + $tag .= ""; + + return $tag; + } protected function renderFooter($builder) { @@ -363,4 +397,11 @@ protected function addFilter(Request $request) // { name: "or|contains|123", "|between|2012-04-01,2021-04-22" }} } + + public function __construct() + { + if(!$this->id) $this->id = 'tableview1'; + + parent::__construct(); + } } \ No newline at end of file diff --git a/src/Responses/HTMLResponse.php b/src/Responses/HTMLResponse.php index 0511056..9b2650b 100644 --- a/src/Responses/HTMLResponse.php +++ b/src/Responses/HTMLResponse.php @@ -32,6 +32,12 @@ public function prepend($target, $html, $options = []){ return $this; } + public function close($target, array $params = []){ + + $this->data[] = [ '_type'=>'method', 'method'=>'close', 'target'=>$target, 'params'=>$params ]; + return $this; + } + public function html($target, $html, $options = []){ $this->data[] = [ '_type'=>'html', 'html'=>$html, 'target'=>$target, 'options'=>$options ]; @@ -44,13 +50,13 @@ public function replace($target, $html){ return $this; } - public function replaceOrAppend($target, $html, $options) + public function replaceOrAppend($target, $html, $options = []) { $this->data[] = [ '_type'=>'html', 'html'=>$html, 'mode'=>'replace-or-append', 'target'=>$target, 'options'=>$options ]; return $this; } - public function replaceOrPrepend($target, $html, $options) + public function replaceOrPrepend($target, $html, $options = []) { $this->data[] = [ '_type'=>'html', 'html'=>$html, 'mode'=>'replace-or-prepend', 'target'=>$target, 'options'=>$options ]; return $this; diff --git a/src/helpers.php b/src/helpers.php index fab95ec..333d1ad 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -747,7 +747,7 @@ function in_array_all($needles, $haystack) { function save_image($image, $disk = 'images', $dir = '', $urlprefix = ''){ $file_md5 = ''; - + if(filter_var($image, FILTER_VALIDATE_URL)){ $image_params = getimagesize($image); diff --git a/src/views/tableview1.blade.php b/src/views/tableview1.blade.php index 75d86eb..9d7c71e 100644 --- a/src/views/tableview1.blade.php +++ b/src/views/tableview1.blade.php @@ -50,7 +50,7 @@
-
+
@@ -61,7 +61,7 @@
-
+