ButtonSwitchAjax.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Buttons;
  3. use \ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AjaxElementInterface;
  4. use \ModulesGarden\Servers\ProxmoxVps\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. protected function afterInitContent()
  59. {
  60. }
  61. public function getValue()
  62. {
  63. return $this->value;
  64. }
  65. public function setValue($value)
  66. {
  67. return $this->value;
  68. }
  69. public function setSwitchTarget($model = null, $columnName = null)
  70. {
  71. if ($model instanceof \ModulesGarden\Servers\ProxmoxVps\Core\Models\ExtendedEloquentModel || $model instanceof \Illuminate\Database\Eloquent\Model)
  72. {
  73. $this->switchModel = $model;
  74. }
  75. if ($columnName !== null && is_string($columnName))
  76. {
  77. $this->switchColumn = $columnName;
  78. }
  79. return $this;
  80. }
  81. public function getSwitchColumnName()
  82. {
  83. return $this->switchColumn;
  84. }
  85. public function setSwitchOnValue($value)
  86. {
  87. if (is_string($value) || is_numeric($value))
  88. {
  89. $this->switchOnValue = $value;
  90. }
  91. return $this;
  92. }
  93. public function getSwitchOnValue()
  94. {
  95. return $this->switchOnValue;
  96. }
  97. public function setActionIdColumn($colName)
  98. {
  99. if ($colName !== '' && is_string($colName))
  100. {
  101. $this->actionIdColumn = $colName;
  102. }
  103. return $this;
  104. }
  105. }