| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom;
- use MGModule\DNSManager2 as main;
- use MGModule\DNSManager2\mgLibs\lang;
- class AjaxResponse implements \ArrayAccess {
- private static $inst = false;
- private $data = array();
-
- /** @return AjaxResponse */
- public static function I() {
- if(self::$inst === false)
- self::$inst = new self();
- return self::$inst;
- }
-
- private function __construct() {
- $this->data['response'] = array();
- $this->data['errors'] = array();
- $this->data['infos'] = array();
- $this->data['warnings'] = array();
- }
-
- /**
- * Ustawia zmienną, którą można później użyć w callbacku w js
- * @param string $name
- * @param string $value
- */
- public function __set($name, $value) {
- $this->data['response'][$name] = $value;
- }
-
- /**
- * Dodaje error do wyświetlenia
- * @param string $error
- * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
- */
- public function addRawError($error) {
- $this->data['errors'][] = $error;
- return $this;
- }
-
- /**
- * Dodaje error z langów
- * @param string $error_lang
- * @param array $replace klucz => wartość, w langu :klucz: zostaje podmieniony na wartość
- * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
- */
- public function addError($error_lang, $replace = array()) {
- $this->data['errors'][] = str_replace(array_map(function($item){return ":$item:";},array_keys($replace)), array_values($replace), lang::T('errors', $error_lang));
- return $this;
- }
-
- /**
- * Dodaje info do wyświetlenia
- * @param string $info
- * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
- */
- public function addRawInfo($info) {
- $this->data['infos'][] = $info;
- return $this;
- }
-
- /**
- * Dodaje info z langów
- * @param string $info_lang
- * @param array $replace klucz => wartość, w langu :klucz: zostaje podmieniony na wartość
- * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
- */
- public function addInfo($info_lang, $replace = array()) {
- $this->data['infos'][] = str_replace(array_map(function($item){return ":$item:";},array_keys($replace)), array_values($replace), lang::T('infos', $info_lang));
- return $this;
- }
-
-
- /**
- * Dodaje warning z langów
- * @param string $info_lang
- * @param array $replace klucz => wartość, w langu :klucz: zostaje podmieniony na wartość
- * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
- */
- public function addWarning($info_lang, $replace = array()) {
- $this->data['warnings'][] = str_replace(array_map(function($item){return ":$item:";},array_keys($replace)), array_values($replace), lang::T('infos', $info_lang));
- return $this;
- }
-
- /**
- * Zwraca array, który jest wymagany przez framework Czecha
- * @return array
- */
- public function toArray() {
- return $this->data;
- }
-
- /**
- * Metoda generująca modala, który jest później wyświetlany
- * @param string $template nazwa templatki (bez tpl), która musi znajdować się w folderze /modal
- * @param array $vars zmienne, które zostaną przekazane do templatki
- * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
- */
- public function modal($template, $vars = array()) {
- $this->data['modal'] = main\mgLibs\smarty::I()->view($template,
- $vars,
- main\addon::getModuleTemplatesDir().DS.'pages' . DS . main\addon::I()->page . DS . 'modal');
- return $this;
- }
-
- /**
- * Powoduje przeładowanie strony
- * @param array $page_array taki sam array jak ten zwracany przez akcjaHTML(...)
- * @return \MGModule\DNSManager2\mgLibs\custom\AjaxResponse
- */
- public function refreshPage($page_array) {
- $this->data['refresh_html'] = main\mgLibs\smarty::I()->view($page_array['tpl'],
- $page_array['vars'],
- main\addon::getModuleTemplatesDir().DS.'pages' . DS . main\addon::I()->page);
- return $this;
- }
- public function offsetExists($offset)
- {
- return isset($this->data[$offset]);
- }
- public function offsetGet($offset)
- {
- return $this->data[$offset];
- }
- public function offsetSet($offset, $value)
- {
- $this->data[$offset] = $value;
- }
- public function offsetUnset($offset)
- {
- unset($this->data[$offset]);
- }
-
- }
|