1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Tinyissue package. |
5
|
|
|
* |
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tinyissue\Form; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Model; |
15
|
|
|
use Tinyissue\Extensions\Auth\LoggedUser; |
16
|
|
|
use Tinyissue\Model\Project as ProjectModel; |
17
|
|
|
use Tinyissue\Model\User as UserModel; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* FormAbstract is an abstract class for Form classes. |
21
|
|
|
* |
22
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
abstract class FormAbstract implements FormInterface |
25
|
|
|
{ |
26
|
|
|
use LoggedUser; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* An instance of Model. |
30
|
|
|
* |
31
|
|
|
* @var Model |
32
|
|
|
*/ |
33
|
|
|
protected $model; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set an instance of model currently being edited. |
37
|
|
|
* |
38
|
|
|
* @param Model $model |
39
|
|
|
* |
40
|
|
|
* @return void|FormInterface |
41
|
|
|
*/ |
42
|
13 |
|
public function editingModel(Model $model) |
43
|
|
|
{ |
44
|
13 |
|
$this->model = $model; |
45
|
|
|
|
46
|
13 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Setup the object from the route parameters. |
51
|
|
|
* |
52
|
|
|
* @param array $params |
53
|
|
|
* |
54
|
|
|
* @return FormInterface |
55
|
|
|
*/ |
56
|
|
|
public function setup(array $params) |
57
|
|
|
{ |
58
|
26 |
|
$model = array_first($params, function ($key, $value) { |
59
|
4 |
|
return $value instanceof Model; |
60
|
26 |
|
}); |
61
|
26 |
|
if ($model) { |
62
|
4 |
|
$this->editingModel($model); |
63
|
|
|
} |
64
|
|
|
|
65
|
26 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Whether or not the form is in editing of a model. |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
32 |
|
public function isEditing() |
74
|
|
|
{ |
75
|
32 |
|
return $this->model instanceof Model; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return an instance of the model being edited. |
80
|
|
|
* |
81
|
|
|
* @return Model |
82
|
|
|
*/ |
83
|
50 |
|
public function getModel() |
84
|
|
|
{ |
85
|
50 |
|
return $this->model; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns form type. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
41 |
|
public function openType() |
94
|
|
|
{ |
95
|
41 |
|
return 'open'; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns an array of form actions. |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
14 |
|
public function actions() |
104
|
|
|
{ |
105
|
14 |
|
return []; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns an array of form fields. |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function fields() |
114
|
|
|
{ |
115
|
|
|
return []; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns an array form rules. |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
31 |
|
public function rules() |
124
|
|
|
{ |
125
|
31 |
|
return []; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the form redirect url on error. |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
public function getRedirectUrl() |
134
|
|
|
{ |
135
|
|
|
return ''; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns project upload fields. |
140
|
|
|
* |
141
|
|
|
* @param string $name |
142
|
|
|
* @param ProjectModel $project |
143
|
|
|
* @param UserModel $user |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
13 |
|
protected function projectUploadFields($name, ProjectModel $project, UserModel $user) |
148
|
|
|
{ |
149
|
|
|
return [ |
150
|
|
|
$name => [ |
151
|
13 |
|
'type' => 'FileUpload', |
152
|
13 |
|
'data_message_success' => trans('tinyissue.success_upload'), |
153
|
13 |
|
'data_message_failed' => trans('tinyissue.error_uploadfailed'), |
154
|
|
|
'multiple' => null, |
155
|
13 |
|
], |
156
|
13 |
|
$name . '_token' => [ |
157
|
13 |
|
'type' => 'hidden', |
158
|
13 |
|
'value' => md5($project->id . time() . $user->id . rand(1, 100)), |
159
|
|
|
], |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|