ButtonSwitchAjax.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Buttons;
  3. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\AjaxElementInterface;
  4. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates;
  5. class ButtonSwitchAjax extends ButtonCustomAction implements AjaxElementInterface
  6. {
  7. protected $id = 'ButtonSwitchAjax';
  8. protected $class = ['lu-btn lu-btn-circle lu-btn-outline lu-btn-inverse lu-btn-success lu-btn-icon-only'];
  9. protected $icon = 'lu-zmdi lu-zmdi-plus';
  10. protected $title = 'ButtonSwitchAjax';
  11. protected $htmlAttributes = [];
  12. protected $switchModel = null;
  13. protected $switchColumn = 'enable_value';
  14. protected $switchOnValue = 'on';
  15. protected $switchOffValue = 'off';
  16. protected $actionIdColumn = 'id';
  17. public function __construct($baseId = null)
  18. {
  19. parent::__construct($baseId);
  20. $this->runInitContentProcess();
  21. }
  22. public function returnAjaxData()
  23. {
  24. $actionId = $this->getRequestValue('actionElementId', false);
  25. $switchValue = $this->getRequestValue('value', false);
  26. if (!$this->switchModel || !$this->switchColumn || !$this->switchOnValue || !$actionId || !$switchValue)
  27. {
  28. return (new ResponseTemplates\DataJsonResponse())->setStatusError()->setMessageAndTranslate('SavingError')->setCallBackFunction($this->callBackFunction)->setRefreshTargetIds($this->refreshActionIds);
  29. }
  30. try
  31. {
  32. $model = new $this->switchModel();
  33. $query = $model->newQuery();
  34. $query->where($this->actionIdColumn, '=', $actionId);
  35. if ($query->count() > 0)
  36. {
  37. $query = $model->newQuery();
  38. $query->where($this->actionIdColumn, '=', $actionId)
  39. ->update([$this->switchColumn => ($switchValue == 'on' ? $this->switchOnValue : $this->switchOffValue)]);
  40. }
  41. else
  42. {
  43. $model->{$this->actionIdColumn} = $actionId;
  44. $model->{$this->switchColumn} = ($switchValue == 'on' ? $this->switchOnValue : $this->switchOffValue);
  45. $model->save();
  46. }
  47. }
  48. catch (\Exception $exc)
  49. {
  50. return (new ResponseTemplates\DataJsonResponse())->setStatusError()->setMessage($exc->getMessage())->setCallBackFunction($this->callBackFunction)->addRefreshTargetId($this->refreshActionIds);
  51. }
  52. return (new ResponseTemplates\DataJsonResponse())->setMessageAndTranslate('changesSaved')->setCallBackFunction($this->callBackFunction)->addRefreshTargetId($this->refreshActionIds);
  53. }
  54. public function initContent()
  55. {
  56. $this->htmlAttributes['@change'] = 'onOffSwitch($event, \'' . $this->id . '\')';
  57. }
  58. public function getValue()
  59. {
  60. return $this->value;
  61. }
  62. public function setValue($value)
  63. {
  64. return $this->value;
  65. }
  66. public function setSwitchTarget($model = null, $columnName = null)
  67. {
  68. if ($model instanceof \ModulesGarden\Servers\ProxmoxCloudVps\Core\Models\ExtendedEloquentModel || $model instanceof \Illuminate\Database\Eloquent\Model)
  69. {
  70. $this->switchModel = $model;
  71. }
  72. if ($columnName !== null && is_string($columnName))
  73. {
  74. $this->switchColumn = $columnName;
  75. }
  76. return $this;
  77. }
  78. public function getSwitchColumnName()
  79. {
  80. return $this->switchColumn;
  81. }
  82. public function setSwitchOnValue($value)
  83. {
  84. if (is_string($value) || is_numeric($value))
  85. {
  86. $this->switchOnValue = $value;
  87. }
  88. return $this;
  89. }
  90. public function getSwitchOnValue()
  91. {
  92. return $this->switchOnValue;
  93. }
  94. public function setActionIdColumn($colName)
  95. {
  96. if ($colName !== '' && is_string($colName))
  97. {
  98. $this->actionIdColumn = $colName;
  99. }
  100. return $this;
  101. }
  102. }