|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hechoenlaravel\JarvisFoundation\Field\Date; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Styde\Html\Facades\Field; |
|
7
|
|
|
use Hechoenlaravel\JarvisFoundation\Field\FieldTypeInterface; |
|
8
|
|
|
use Hechoenlaravel\JarvisFoundation\Field\FieldTypeImplementationTrait; |
|
9
|
|
|
|
|
10
|
|
|
class DateFieldType implements FieldTypeInterface |
|
11
|
|
|
{ |
|
12
|
|
|
use FieldTypeImplementationTrait; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var |
|
16
|
|
|
*/ |
|
17
|
|
|
private $value; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $columnType = "date"; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The field type name |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
public $name = "Fecha"; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The field slug for the instance |
|
32
|
|
|
* @var |
|
33
|
|
|
*/ |
|
34
|
|
|
public $fieldSlug; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The field Name for the instance |
|
38
|
|
|
* @var |
|
39
|
|
|
*/ |
|
40
|
|
|
public $fieldName; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The field description for the instance |
|
44
|
|
|
* @var |
|
45
|
|
|
*/ |
|
46
|
|
|
public $fieldDescription; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The field Options for the Instance |
|
50
|
|
|
* @var |
|
51
|
|
|
*/ |
|
52
|
|
|
public $fieldOptions; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Validation rules for the field type |
|
56
|
|
|
* @var array |
|
57
|
|
|
*/ |
|
58
|
|
|
public $validationRules = ['date']; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* get the column type for this field type |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getColumnType() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->columnType; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set a value for this field; |
|
71
|
|
|
* @param $value |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setValue($value) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->value = $value; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* get the value for the field |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getValue() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->value; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* return the form view |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
|
|
public function present() |
|
93
|
|
|
{ |
|
94
|
|
|
$date = null; |
|
95
|
|
|
if(!empty($this->value)){ |
|
96
|
|
|
$date = Carbon::createFromTimestamp(strtotime($this->value)); |
|
97
|
|
|
} |
|
98
|
|
|
return Field::text($this->fieldSlug, empty($date) ? null : $date->format('m/d/Y'), ['label' => $this->fieldName, 'class' => 'datepicker']); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Que the form for the options of the field type |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getOptionsForm() |
|
106
|
|
|
{ |
|
107
|
|
|
return view('jarvisPlatform::field.types.date.optionsForm')->render(); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Take the value and capitalize the first letter. |
|
112
|
|
|
* @param $value |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function preSaveEvent($value) |
|
116
|
|
|
{ |
|
117
|
|
|
$date = Carbon::createFromTimestamp(strtotime($value)); |
|
118
|
|
|
return $date->format('Y-m-d'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return mixed |
|
123
|
|
|
*/ |
|
124
|
|
|
public function presentFront() |
|
125
|
|
|
{ |
|
126
|
|
|
$date = Carbon::createFromTimestamp(strtotime($this->value)); |
|
127
|
|
|
return $date->format('m/d/Y'); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: