Response.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates;
  3. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper;
  4. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\ServiceLocator;
  5. /**
  6. * Abstract Ajax Response Model
  7. */
  8. abstract class Response
  9. {
  10. const STATUS_SUCCESS = 'success';
  11. const STATUS_ERROR = 'error';
  12. protected $status = self::STATUS_SUCCESS;
  13. protected $data = [];
  14. protected $message = null;
  15. protected $dataType = 'data';
  16. protected $callBackFunction = null;
  17. protected $refreshTargetId = [];
  18. protected $customParams = [];
  19. protected $lang = null;
  20. public function __construct($data = [])
  21. {
  22. $this->data = $data;
  23. }
  24. public function setStatusSuccess()
  25. {
  26. $this->status = self::STATUS_SUCCESS;
  27. return $this;
  28. }
  29. public function setRefreshTargetIds(array $targetIds = [])
  30. {
  31. $this->refreshTargetId = $targetIds;
  32. return $this;
  33. }
  34. public function addRefreshTargetId($targetId)
  35. {
  36. if (in_array($targetId, $this->refreshTargetId, true) === false)
  37. {
  38. $this->refreshTargetId[] = $targetId;
  39. }
  40. return $this;
  41. }
  42. public function setStatusError()
  43. {
  44. $this->status = self::STATUS_ERROR;
  45. return $this;
  46. }
  47. public function setData($data)
  48. {
  49. $this->data = $data;
  50. return $this;
  51. }
  52. public function addData($key, $data)
  53. {
  54. $this->data[$key] = $data;
  55. return $this;
  56. }
  57. public function setCallBackFunction($name)
  58. {
  59. $this->callBackFunction = $name;
  60. return $this;
  61. }
  62. public function disableCallBackFunction()
  63. {
  64. $this->callBackFunction = null;
  65. return $this;
  66. }
  67. public function getData()
  68. {
  69. $return = [
  70. 'status' => $this->status,
  71. 'message' => $this->message,
  72. $this->dataType => $this->data,
  73. 'refreshIds' => $this->refreshTargetId,
  74. 'customParams' => $this->customParams
  75. ];
  76. if ($this->callBackFunction)
  77. {
  78. $return['callBackFunction'] = $this->callBackFunction;
  79. }
  80. return $return;
  81. }
  82. public function getFormatedResponse()
  83. {
  84. return Helper\json($this->getData())->setStatusCode(200);
  85. }
  86. public function setMessage($message)
  87. {
  88. $this->message = $message;
  89. return $this;
  90. }
  91. protected function loadLang()
  92. {
  93. if($this->lang === null)
  94. {
  95. $this->lang = ServiceLocator::call('lang');
  96. }
  97. }
  98. public function setMessageAndTranslate($message)
  99. {
  100. $this->loadLang();
  101. $this->message = $this->lang->absoluteT($message);
  102. return $this;
  103. }
  104. public function setCustomParam($key, $value)
  105. {
  106. $this->customParams[$key] = $value;
  107. return $this;
  108. }
  109. }