LaravelRUS /
SleepingOwlAdmin
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace SleepingOwl\Admin\Display\Column\Editable; |
||||||
| 4 | |||||||
| 5 | use Carbon\Carbon; |
||||||
| 6 | use Illuminate\Http\Request; |
||||||
| 7 | use SleepingOwl\Admin\Contracts\Display\ColumnEditableInterface; |
||||||
| 8 | use SleepingOwl\Admin\Form\FormDefault; |
||||||
| 9 | use SleepingOwl\Admin\Traits\DateFormat; |
||||||
| 10 | use SleepingOwl\Admin\Traits\DatePicker; |
||||||
| 11 | |||||||
| 12 | class DateTime extends EditableColumn implements ColumnEditableInterface |
||||||
| 13 | { |
||||||
| 14 | use DatePicker, DateFormat; |
||||||
| 15 | |||||||
| 16 | /** |
||||||
| 17 | * @var string |
||||||
| 18 | */ |
||||||
| 19 | protected $defaultValue; |
||||||
| 20 | |||||||
| 21 | /** |
||||||
| 22 | * @var string |
||||||
| 23 | */ |
||||||
| 24 | protected $format = 'Y-m-d H:i:s'; |
||||||
| 25 | |||||||
| 26 | /** |
||||||
| 27 | * @var string |
||||||
| 28 | */ |
||||||
| 29 | protected $type = 'combodate'; |
||||||
| 30 | |||||||
| 31 | /** |
||||||
| 32 | * @var string |
||||||
| 33 | */ |
||||||
| 34 | protected $timezone; |
||||||
| 35 | |||||||
| 36 | /** |
||||||
| 37 | * @var bool |
||||||
| 38 | */ |
||||||
| 39 | protected $seconds = false; |
||||||
| 40 | |||||||
| 41 | /** |
||||||
| 42 | * @var string |
||||||
| 43 | */ |
||||||
| 44 | protected $view = 'column.editable.datetime'; |
||||||
| 45 | |||||||
| 46 | /** |
||||||
| 47 | * @var string |
||||||
| 48 | */ |
||||||
| 49 | protected $combodateValue = '{}'; |
||||||
| 50 | |||||||
| 51 | /** |
||||||
| 52 | * Text constructor. |
||||||
| 53 | * |
||||||
| 54 | * @param $name |
||||||
| 55 | * @param $label |
||||||
| 56 | */ |
||||||
| 57 | public function __construct($name, $label = null) |
||||||
| 58 | { |
||||||
| 59 | parent::__construct($name, $label); |
||||||
| 60 | |||||||
| 61 | $this->setFormat(config('sleeping_owl.datetimeFormat')); |
||||||
| 62 | $this->setCombodateValue(['maxYear' => now()->addYears(10)->format('Y')]); |
||||||
| 63 | } |
||||||
| 64 | |||||||
| 65 | /** |
||||||
| 66 | * @return string |
||||||
| 67 | */ |
||||||
| 68 | public function getCombodateValue() |
||||||
| 69 | { |
||||||
| 70 | return $this->combodateValue; |
||||||
| 71 | } |
||||||
| 72 | |||||||
| 73 | /** |
||||||
| 74 | * @param array $maxYear |
||||||
| 75 | * @return $this |
||||||
| 76 | */ |
||||||
| 77 | public function setCombodateValue(array $value) |
||||||
| 78 | { |
||||||
| 79 | $this->combodateValue = json_encode($value); |
||||||
| 80 | |||||||
| 81 | return $this; |
||||||
| 82 | } |
||||||
| 83 | |||||||
| 84 | /** |
||||||
| 85 | * @return mixed |
||||||
| 86 | */ |
||||||
| 87 | public function getModifierValue() |
||||||
| 88 | { |
||||||
| 89 | if (is_callable($this->modifier)) { |
||||||
| 90 | return call_user_func($this->modifier, $this); |
||||||
| 91 | } |
||||||
| 92 | |||||||
| 93 | if (is_null($this->modifier)) { |
||||||
| 94 | return $this->getFormatedDate($this->getModelValue()); |
||||||
| 95 | } |
||||||
| 96 | |||||||
| 97 | return $this->modifier; |
||||||
| 98 | } |
||||||
| 99 | |||||||
| 100 | /** |
||||||
| 101 | * @return array |
||||||
| 102 | */ |
||||||
| 103 | public function toArray() |
||||||
| 104 | { |
||||||
| 105 | $value = $this->getModelValue(); |
||||||
| 106 | |||||||
| 107 | return array_merge(parent::toArray(), [ |
||||||
| 108 | 'id' => $this->getModel()->getKey(), |
||||||
| 109 | 'value' => $this->getFormatedDate($value), |
||||||
| 110 | 'isEditable' => $this->getModelConfiguration()->isEditable($this->getModel()), |
||||||
| 111 | 'url' => $this->getUrl(), |
||||||
| 112 | |||||||
| 113 | 'format' => $this->getJsPickerFormat(), |
||||||
| 114 | 'viewformat' => $this->getJsPickerFormat(), |
||||||
| 115 | 'data-date-pickdate' => 'true', |
||||||
| 116 | 'data-date-picktime' => 'false', |
||||||
| 117 | 'data-date-useseconds' => $this->hasSeconds() ? 'true' : 'false', |
||||||
| 118 | 'type' => $this->type, |
||||||
| 119 | |||||||
| 120 | 'text' => $this->getModifierValue(), |
||||||
| 121 | 'combodateValue' => $this->getCombodateValue(), |
||||||
| 122 | ]); |
||||||
| 123 | } |
||||||
| 124 | |||||||
| 125 | /** |
||||||
| 126 | * @param string $date |
||||||
| 127 | * |
||||||
| 128 | * @return null|string |
||||||
| 129 | */ |
||||||
| 130 | protected function getFormatedDate($date) |
||||||
| 131 | { |
||||||
| 132 | if (! is_null($date)) { |
||||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||||
| 133 | if (! $date instanceof Carbon) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 134 | $date = Carbon::parse($date); |
||||||
| 135 | } |
||||||
| 136 | |||||||
| 137 | $date = $date->timezone($this->getTimezone())->format($this->getFormat()); |
||||||
| 138 | } |
||||||
| 139 | |||||||
| 140 | return $date; |
||||||
| 141 | } |
||||||
| 142 | |||||||
| 143 | /** |
||||||
| 144 | * @return null |
||||||
| 145 | */ |
||||||
| 146 | public function getValueFromModel() |
||||||
| 147 | { |
||||||
| 148 | $value = parent::getModelValue(); |
||||||
| 149 | if (! empty($value)) { |
||||||
| 150 | return $this->parseValue($value); |
||||||
|
0 ignored issues
–
show
The method
parseValue() does not exist on SleepingOwl\Admin\Display\Column\Editable\DateTime.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 151 | } |
||||||
| 152 | } |
||||||
| 153 | |||||||
| 154 | /** |
||||||
| 155 | * @return bool |
||||||
| 156 | */ |
||||||
| 157 | public function hasSeconds() |
||||||
| 158 | { |
||||||
| 159 | return (bool) $this->seconds; |
||||||
| 160 | } |
||||||
| 161 | |||||||
| 162 | /** |
||||||
| 163 | * @param bool $seconds |
||||||
| 164 | * |
||||||
| 165 | * @return $this |
||||||
| 166 | */ |
||||||
| 167 | public function setSeconds($seconds) |
||||||
| 168 | { |
||||||
| 169 | $this->seconds = $seconds; |
||||||
| 170 | |||||||
| 171 | return $this; |
||||||
| 172 | } |
||||||
| 173 | |||||||
| 174 | /** |
||||||
| 175 | * @param mixed $value |
||||||
| 176 | * |
||||||
| 177 | * @return void |
||||||
| 178 | */ |
||||||
| 179 | public function setModelAttribute($value) |
||||||
| 180 | { |
||||||
| 181 | $value = ! empty($value) |
||||||
| 182 | ? Carbon::createFromFormat($this->getPickerFormat(), $value, $this->getTimezone()) |
||||||
| 183 | ->timezone(config('app.timezone'))->format($this->getFormat()) |
||||||
| 184 | : null; |
||||||
| 185 | |||||||
| 186 | parent::setModelAttribute($value); |
||||||
|
0 ignored issues
–
show
The method
setModelAttribute() does not exist on SleepingOwl\Admin\Displa...Editable\EditableColumn. Did you maybe mean setModel()?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 187 | } |
||||||
| 188 | |||||||
| 189 | /** |
||||||
| 190 | * @param Request $request |
||||||
| 191 | * @throws \SleepingOwl\Admin\Exceptions\Form\FormElementException |
||||||
| 192 | * @throws \SleepingOwl\Admin\Exceptions\Form\FormException |
||||||
| 193 | */ |
||||||
| 194 | public function save(Request $request) |
||||||
| 195 | { |
||||||
| 196 | $form = new FormDefault([ |
||||||
| 197 | new \SleepingOwl\Admin\Form\Element\Text( |
||||||
| 198 | $this->getName() |
||||||
| 199 | ), |
||||||
| 200 | ]); |
||||||
| 201 | |||||||
| 202 | $model = $this->getModel(); |
||||||
| 203 | |||||||
| 204 | $value = Carbon::createFromFormat($this->format, $request->input('value'), $this->getTimezone()); |
||||||
| 205 | $request->offsetSet($this->getName(), $value); |
||||||
| 206 | |||||||
| 207 | $form->setModelClass(get_class($model)); |
||||||
| 208 | $form->initialize(); |
||||||
| 209 | $form->setId($model->getKey()); |
||||||
| 210 | |||||||
| 211 | $form->saveForm($request); |
||||||
| 212 | } |
||||||
| 213 | |||||||
| 214 | /** |
||||||
| 215 | * @return string |
||||||
| 216 | */ |
||||||
| 217 | public function getPickerFormat() |
||||||
| 218 | { |
||||||
| 219 | return $this->pickerFormat ?: config('sleeping_owl.datetimeFormat'); |
||||||
| 220 | } |
||||||
| 221 | |||||||
| 222 | /** |
||||||
| 223 | * @return $this |
||||||
| 224 | * |
||||||
| 225 | * SMELLS This function does more than it says. |
||||||
| 226 | */ |
||||||
| 227 | public function setCurrentDate() |
||||||
| 228 | { |
||||||
| 229 | $this->defaultValue = Carbon::now()->timezone($this->getTimezone())->format($this->getFormat()); |
||||||
| 230 | |||||||
| 231 | return $this; |
||||||
| 232 | } |
||||||
| 233 | } |
||||||
| 234 |