|
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
|
|
|
public function editingModel(Model $model) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->model = $model; |
|
45
|
|
|
|
|
46
|
52 |
|
return $this; |
|
47
|
|
|
} |
|
48
|
52 |
|
|
|
49
|
|
|
/** |
|
50
|
52 |
|
* 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
|
16 |
|
$model = array_first($params, function ($key, $value) { |
|
59
|
|
|
return $value instanceof Model; |
|
60
|
16 |
|
}); |
|
61
|
2 |
|
if ($model) { |
|
62
|
|
|
$this->editingModel($model); |
|
63
|
|
|
} |
|
64
|
16 |
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Whether or not the form is in editing of a model. |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public function isEditing() |
|
74
|
13 |
|
{ |
|
75
|
|
|
return $this->model instanceof Model; |
|
76
|
13 |
|
} |
|
77
|
|
|
|
|
78
|
13 |
|
/** |
|
79
|
|
|
* Return an instance of the model being edited. |
|
80
|
|
|
* |
|
81
|
|
|
* @return Model |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getModel() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->model; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns form type. |
|
90
|
26 |
|
* |
|
91
|
4 |
|
* @return string |
|
92
|
26 |
|
*/ |
|
93
|
26 |
|
public function openType() |
|
94
|
4 |
|
{ |
|
95
|
|
|
return 'open'; |
|
96
|
|
|
} |
|
97
|
26 |
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns an array of form actions. |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function actions() |
|
104
|
|
|
{ |
|
105
|
32 |
|
return []; |
|
106
|
|
|
} |
|
107
|
32 |
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns an array of form fields. |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
public function fields() |
|
114
|
|
|
{ |
|
115
|
50 |
|
return []; |
|
116
|
|
|
} |
|
117
|
50 |
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns an array form rules. |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
|
|
public function rules() |
|
124
|
|
|
{ |
|
125
|
41 |
|
return []; |
|
126
|
|
|
} |
|
127
|
41 |
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Returns the form redirect url on error. |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getRedirectUrl() |
|
134
|
|
|
{ |
|
135
|
14 |
|
return ''; |
|
136
|
|
|
} |
|
137
|
14 |
|
|
|
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
|
|
|
protected function projectUploadFields($name, ProjectModel $project, UserModel $user) |
|
148
|
|
|
{ |
|
149
|
|
|
return [ |
|
150
|
|
|
$name => [ |
|
151
|
|
|
'type' => 'FileUpload', |
|
152
|
|
|
'data_message_success' => trans('tinyissue.success_upload'), |
|
153
|
|
|
'data_message_failed' => trans('tinyissue.error_uploadfailed'), |
|
154
|
|
|
'multiple' => null, |
|
155
|
31 |
|
], |
|
156
|
|
|
$name . '_token' => [ |
|
157
|
31 |
|
'type' => 'hidden', |
|
158
|
|
|
'value' => md5($project->id . time() . $user->id . rand(1, 100)), |
|
159
|
|
|
], |
|
160
|
|
|
]; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|