runInitContentProcess(); } public function returnAjaxData() { $actionId = $this->getRequestValue('actionElementId', false); $switchValue = $this->getRequestValue('value', false); if (!$this->switchModel || !$this->switchColumn || !$this->switchOnValue || !$actionId || !$switchValue) { return (new ResponseTemplates\DataJsonResponse())->setStatusError()->setMessageAndTranslate('SavingError')->setCallBackFunction($this->callBackFunction)->setRefreshTargetIds($this->refreshActionIds); } try { $model = new $this->switchModel(); $query = $model->newQuery(); $query->where($this->actionIdColumn, '=', $actionId); if ($query->count() > 0) { $query = $model->newQuery(); $query->where($this->actionIdColumn, '=', $actionId) ->update([$this->switchColumn => ($switchValue == 'on' ? $this->switchOnValue : $this->switchOffValue)]); } else { $model->{$this->actionIdColumn} = $actionId; $model->{$this->switchColumn} = ($switchValue == 'on' ? $this->switchOnValue : $this->switchOffValue); $model->save(); } } catch (\Exception $exc) { return (new ResponseTemplates\DataJsonResponse())->setStatusError()->setMessage($exc->getMessage())->setCallBackFunction($this->callBackFunction)->addRefreshTargetId($this->refreshActionIds); } return (new ResponseTemplates\DataJsonResponse())->setMessageAndTranslate('changesSaved')->setCallBackFunction($this->callBackFunction)->addRefreshTargetId($this->refreshActionIds); } public function initContent() { $this->htmlAttributes['@change'] = 'onOffSwitch($event, \'' . $this->id . '\')'; } protected function afterInitContent() { } public function getValue() { return $this->value; } public function setValue($value) { return $this->value; } public function setSwitchTarget($model = null, $columnName = null) { if ($model instanceof \ModulesGarden\Servers\ProxmoxVps\Core\Models\ExtendedEloquentModel || $model instanceof \Illuminate\Database\Eloquent\Model) { $this->switchModel = $model; } if ($columnName !== null && is_string($columnName)) { $this->switchColumn = $columnName; } return $this; } public function getSwitchColumnName() { return $this->switchColumn; } public function setSwitchOnValue($value) { if (is_string($value) || is_numeric($value)) { $this->switchOnValue = $value; } return $this; } public function getSwitchOnValue() { return $this->switchOnValue; } public function setActionIdColumn($colName) { if ($colName !== '' && is_string($colName)) { $this->actionIdColumn = $colName; } return $this; } }