1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: shanmaseen |
5
|
|
|
* Date: 09/04/19 |
6
|
|
|
* Time: 02:44 م |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Shamaseen\Repository\Generator\Forms; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use DB; |
13
|
|
|
use Doctrine\DBAL\Schema\Column; |
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
|
16
|
|
|
class FormGenerator |
17
|
|
|
{ |
18
|
|
|
private $inputs; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Column $column |
22
|
|
|
* @return string |
23
|
|
|
*/ |
24
|
|
|
function generateFormInput($column) { |
25
|
|
|
$fileInput = $this->getFormInputClass($column); |
26
|
|
|
return $fileInput->template(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Column $column |
31
|
|
|
* @return Input|TextArea |
32
|
|
|
*/ |
33
|
|
|
function getFormInputClass($column) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
switch ($column->getType()->getName()) |
36
|
|
|
{ |
37
|
|
|
case "integer": |
38
|
|
|
case "int": |
39
|
|
|
case "mediumint": |
40
|
|
|
case "bigint": |
41
|
|
|
case "decimal": |
42
|
|
|
case "float": |
43
|
|
|
case "double": |
44
|
|
|
return new Input($column); |
45
|
|
|
case "enum": |
46
|
|
|
return new Input($column); |
47
|
|
|
case "date": |
48
|
|
|
case "datetime": |
49
|
|
|
case "timestamp": |
50
|
|
|
case "time": |
51
|
|
|
case "year": |
52
|
|
|
return new Input($column); |
53
|
|
|
case "text": |
54
|
|
|
return new TextArea($column); |
55
|
|
|
case "boolean": |
56
|
|
|
case "bool": |
57
|
|
|
return new Input($column); |
58
|
|
|
case "varchat": |
59
|
|
|
default: |
60
|
|
|
return new Input($column); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Model $entity |
66
|
|
|
* @param string $method |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
function generateForm($entity,$method = 'post') |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$html = '<form method="post" action="#"> |
72
|
|
|
<input type="hidden" name="__method" value="'.$method.'">'; |
73
|
|
|
$html .= $this->getInputs($entity); |
74
|
|
|
$html .= '</form>'; |
75
|
|
|
return $html; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Model $entity |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
function getFillables($entity) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
if(!empty($entity->getFillable())) |
85
|
|
|
return $entity->getFillable(); |
86
|
|
|
|
87
|
|
|
$columns = \Schema::getColumnListing($entity->getTable()); |
88
|
|
|
|
89
|
|
|
foreach ($entity->getGuarded() as $guarded) |
90
|
|
|
{ |
91
|
|
|
if (($key = array_search($guarded, $columns)) !== false) { |
92
|
|
|
unset($columns[$key]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $columns; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param Model $entity |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
function getInputs($entity) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
if($this->inputs) |
106
|
|
|
return $this->inputs; |
107
|
|
|
|
108
|
|
|
return $this->generateInputs($entity); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param Model $entity |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
function generateInputs($entity) |
|
|
|
|
116
|
|
|
{ |
117
|
|
|
$html = ''; |
118
|
|
|
foreach ($this->getFillables($entity) as $fillable) |
119
|
|
|
{ |
120
|
|
|
$column = DB::connection()->getDoctrineColumn($entity->getTable(),$fillable); |
121
|
|
|
|
122
|
|
|
$html .= $this->generateFormInput($column); |
123
|
|
|
} |
124
|
|
|
$html .= '<button type="submit" class="btn btn-primary">Submit</button>'; |
125
|
|
|
$this->inputs = $html; |
126
|
|
|
|
127
|
|
|
return $html; |
128
|
|
|
} |
129
|
|
|
} |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.