AjaxResponse.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom;
  3. use MGModule\DNSManager2 as main;
  4. use MGModule\DNSManager2\mgLibs\lang;
  5. class AjaxResponse implements \ArrayAccess {
  6. private static $inst = false;
  7. private $data = array();
  8. /** @return AjaxResponse */
  9. public static function I() {
  10. if(self::$inst === false)
  11. self::$inst = new self();
  12. return self::$inst;
  13. }
  14. private function __construct() {
  15. $this->data['response'] = array();
  16. $this->data['errors'] = array();
  17. $this->data['infos'] = array();
  18. $this->data['warnings'] = array();
  19. }
  20. /**
  21. * Ustawia zmienną, którą można później użyć w callbacku w js
  22. * @param string $name
  23. * @param string $value
  24. */
  25. public function __set($name, $value) {
  26. $this->data['response'][$name] = $value;
  27. }
  28. /**
  29. * Dodaje error do wyświetlenia
  30. * @param string $error
  31. * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
  32. */
  33. public function addRawError($error) {
  34. $this->data['errors'][] = $error;
  35. return $this;
  36. }
  37. /**
  38. * Dodaje error z langów
  39. * @param string $error_lang
  40. * @param array $replace klucz => wartość, w langu :klucz: zostaje podmieniony na wartość
  41. * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
  42. */
  43. public function addError($error_lang, $replace = array()) {
  44. $this->data['errors'][] = str_replace(array_map(function($item){return ":$item:";},array_keys($replace)), array_values($replace), lang::T('errors', $error_lang));
  45. return $this;
  46. }
  47. /**
  48. * Dodaje info do wyświetlenia
  49. * @param string $info
  50. * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
  51. */
  52. public function addRawInfo($info) {
  53. $this->data['infos'][] = $info;
  54. return $this;
  55. }
  56. /**
  57. * Dodaje info z langów
  58. * @param string $info_lang
  59. * @param array $replace klucz => wartość, w langu :klucz: zostaje podmieniony na wartość
  60. * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
  61. */
  62. public function addInfo($info_lang, $replace = array()) {
  63. $this->data['infos'][] = str_replace(array_map(function($item){return ":$item:";},array_keys($replace)), array_values($replace), lang::T('infos', $info_lang));
  64. return $this;
  65. }
  66. /**
  67. * Dodaje warning z langów
  68. * @param string $info_lang
  69. * @param array $replace klucz => wartość, w langu :klucz: zostaje podmieniony na wartość
  70. * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
  71. */
  72. public function addWarning($info_lang, $replace = array()) {
  73. $this->data['warnings'][] = str_replace(array_map(function($item){return ":$item:";},array_keys($replace)), array_values($replace), lang::T('infos', $info_lang));
  74. return $this;
  75. }
  76. /**
  77. * Zwraca array, który jest wymagany przez framework Czecha
  78. * @return array
  79. */
  80. public function toArray() {
  81. return $this->data;
  82. }
  83. /**
  84. * Metoda generująca modala, który jest później wyświetlany
  85. * @param string $template nazwa templatki (bez tpl), która musi znajdować się w folderze /modal
  86. * @param array $vars zmienne, które zostaną przekazane do templatki
  87. * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
  88. */
  89. public function modal($template, $vars = array()) {
  90. $this->data['modal'] = main\mgLibs\smarty::I()->view($template,
  91. $vars,
  92. main\addon::getModuleTemplatesDir().DS.'pages' . DS . main\addon::I()->page . DS . 'modal');
  93. return $this;
  94. }
  95. /**
  96. * Powoduje przeładowanie strony
  97. * @param array $page_array taki sam array jak ten zwracany przez akcjaHTML(...)
  98. * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
  99. */
  100. public function refreshPage($page_array) {
  101. $this->data['refresh_html'] = main\mgLibs\smarty::I()->view($page_array['tpl'],
  102. $page_array['vars'],
  103. main\addon::getModuleTemplatesDir().DS.'pages' . DS . main\addon::I()->page);
  104. return $this;
  105. }
  106. public function offsetExists($offset)
  107. {
  108. return isset($this->data[$offset]);
  109. }
  110. public function offsetGet($offset)
  111. {
  112. return $this->data[$offset];
  113. }
  114. public function offsetSet($offset, $value)
  115. {
  116. $this->data[$offset] = $value;
  117. }
  118. public function offsetUnset($offset)
  119. {
  120. unset($this->data[$offset]);
  121. }
  122. }