api.insurance.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * Insurance prototype
  4. */
  5. class Insurance {
  6. /**
  7. * Contains system alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Home insurance database abstraction layer placeholder
  14. *
  15. * @var object
  16. */
  17. protected $hinsDb = '';
  18. /**
  19. * Contains all of available home insurance requests as id=>reqdata
  20. *
  21. * @var array
  22. */
  23. protected $allHinsReq = array();
  24. /**
  25. * System messages helper placeholder
  26. *
  27. * @var object
  28. */
  29. protected $messages = '';
  30. /**
  31. * Contains all available user data as login=>userdata
  32. *
  33. * @var array
  34. */
  35. protected $allUserData = array();
  36. /**
  37. * Contains additional comments
  38. *
  39. * @var object
  40. */
  41. protected $adComments = '';
  42. /**
  43. * Predefined routes, URLs etc..
  44. */
  45. const ADSCOPE_HINS = 'HINS';
  46. const HINS_TABLE = 'ins_homereq';
  47. const URL_ME = '?module=insurance';
  48. const ROUTE_AJHINSLIST = 'ajhinslist';
  49. const ROUTE_VIEWHINSREQ = 'viewrequest';
  50. const ROUTE_HINSDONE = 'sethinsdone';
  51. const ROUTE_HINSUNDONE = 'sethinsundone';
  52. /**
  53. * Creates new zastrahuy bratuhu zastrahuy
  54. *
  55. * @param bool $loadAllData
  56. */
  57. public function __construct($loadAllData = true) {
  58. $this->initDatabaseLayers();
  59. if ($loadAllData) {
  60. $this->initMessages();
  61. $this->loadConfigs();
  62. $this->loadUserData();
  63. $this->loadHinsRequests();
  64. $this->initAdComments();
  65. }
  66. }
  67. /**
  68. * Preloads system configs for further usage
  69. *
  70. * @global object $ubillingConfig
  71. *
  72. * @return void
  73. */
  74. protected function loadConfigs() {
  75. global $ubillingConfig;
  76. $this->altCfg = $ubillingConfig->getAlter();
  77. }
  78. /**
  79. * Inits additional comments instance
  80. *
  81. * @return void
  82. */
  83. protected function initAdComments() {
  84. if ($this->altCfg['ADCOMMENTS_ENABLED']) {
  85. $this->adComments = new ADcomments(self::ADSCOPE_HINS);
  86. }
  87. }
  88. /**
  89. * Loads existing users data into protected prop
  90. *
  91. * @return void
  92. */
  93. protected function loadUserData() {
  94. $this->allUserData = zb_UserGetAllDataCache();
  95. }
  96. /**
  97. * Inits all of required database abstraction layers
  98. *
  99. * @return void
  100. */
  101. protected function initDatabaseLayers() {
  102. $this->hinsDb = new NyanORM(self::HINS_TABLE);
  103. }
  104. /**
  105. * Loads all home insurance requests
  106. *
  107. * @return void
  108. */
  109. protected function loadHinsRequests() {
  110. $this->allHinsReq = $this->hinsDb->getAll('id');
  111. }
  112. /**
  113. * Inits system messages helper instance
  114. *
  115. * @return void
  116. */
  117. protected function initMessages() {
  118. $this->messages = new UbillingMessageHelper();
  119. }
  120. /**
  121. * Renders available home insurance requests
  122. *
  123. * @return string
  124. */
  125. public function renderHinsRequestsList() {
  126. $result = '';
  127. $columns = array('ID', 'Date', 'User', 'Address', 'Real Name', 'Mobile', 'Email', 'Processed', 'Actions');
  128. $opts = '"order": [[ 0, "desc" ]]';
  129. $result .= wf_JqDtLoader($columns, self::URL_ME . '&' . self::ROUTE_AJHINSLIST . '=true', false, __('Request'), 100, $opts);
  130. return($result);
  131. }
  132. /**
  133. * Renders available home insurance requests json backend
  134. *
  135. * @return void
  136. */
  137. public function ajHinsList() {
  138. $json = new wf_JqDtHelper();
  139. if (!empty($this->allHinsReq)) {
  140. foreach ($this->allHinsReq as $io => $each) {
  141. $data[] = $each['id'];
  142. $data[] = $each['date'];
  143. $userAddress = @$this->allUserData[$each['login']]['fulladress'];
  144. $userLink = wf_Link(UserProfile::URL_PROFILE . $each['login'], web_profile_icon() . ' ' . $userAddress);
  145. $data[] = $userLink;
  146. $data[] = $each['address'];
  147. $data[] = $each['realname'];
  148. $data[] = $each['mobile'];
  149. $data[] = $each['email'];
  150. $adcIndicator = '';
  151. if ($this->adComments) {
  152. $adcIndicator = ' ' . $this->adComments->getCommentsIndicator($each['id']);
  153. }
  154. $data[] = web_bool_led($each['state']) . $adcIndicator;
  155. $reqControls = wf_Link(self::URL_ME . '&' . self::ROUTE_VIEWHINSREQ . '=' . $each['id'], web_icon_search() . ' ' . __('Show'));
  156. $data[] = $reqControls;
  157. $json->addRow($data);
  158. unset($data);
  159. }
  160. }
  161. $json->getJson();
  162. }
  163. /**
  164. * Renders home insurance request controls
  165. *
  166. * @param int $requestId
  167. *
  168. * @return string
  169. */
  170. protected function renderHinsControls($requestId) {
  171. $result = '';
  172. $requestId = ubRouting::filters($requestId, 'int');
  173. $result .= wf_BackLink(self::URL_ME);
  174. if (isset($this->allHinsReq[$requestId])) {
  175. $requestData = $this->allHinsReq[$requestId];
  176. if ($requestData['state']) {
  177. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_HINSUNDONE . '=' . $requestId, wf_img_sized('skins/icon_inactive.gif', '', '10') . ' ' . __('Open'), false, 'ubButton');
  178. } else {
  179. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_HINSDONE . '=' . $requestId, wf_img_sized('skins/icon_active.gif', '', '10') . ' ' . __('Close'), false, 'ubButton');
  180. }
  181. }
  182. return($result);
  183. }
  184. /**
  185. * Renders some home insurance request body
  186. *
  187. * @param int $requestId
  188. *
  189. * @return string
  190. */
  191. public function renderHinsRequest($requestId) {
  192. $requestId = ubRouting::filters($requestId, 'int');
  193. $result = '';
  194. if (isset($this->allHinsReq[$requestId])) {
  195. $reqData = $this->allHinsReq[$requestId];
  196. $cells = wf_TableCell(__('ID'), '', 'row2');
  197. $cells .= wf_TableCell($reqData['id']);
  198. $rows = wf_TableRow($cells, 'row3');
  199. $cells = wf_TableCell(__('Date'), '', 'row2');
  200. $cells .= wf_TableCell($reqData['date']);
  201. $rows .= wf_TableRow($cells, 'row3');
  202. $userAddress = @$this->allUserData[$reqData['login']]['fulladress'];
  203. $userLink = wf_Link(UserProfile::URL_PROFILE . $reqData['login'], web_profile_icon() . ' ' . $userAddress);
  204. $cells = wf_TableCell(__('User'), '', 'row2');
  205. $cells .= wf_TableCell($userLink);
  206. $rows .= wf_TableRow($cells, 'row3');
  207. $cells = wf_TableCell(__('Address'), '', 'row2');
  208. $cells .= wf_TableCell($reqData['address']);
  209. $rows .= wf_TableRow($cells, 'row3');
  210. $cells = wf_TableCell(__('Real Name'), '', 'row2');
  211. $cells .= wf_TableCell($reqData['realname']);
  212. $rows .= wf_TableRow($cells, 'row3');
  213. $cells = wf_TableCell(__('Mobile'), '', 'row2');
  214. $cells .= wf_TableCell($reqData['mobile']);
  215. $rows .= wf_TableRow($cells, 'row3');
  216. $cells = wf_TableCell(__('Email'), '', 'row2');
  217. $cells .= wf_TableCell($reqData['email']);
  218. $rows .= wf_TableRow($cells, 'row3');
  219. $cells = wf_TableCell(__('Processed'), '', 'row2');
  220. $cells .= wf_TableCell(web_bool_led($reqData['state']));
  221. $rows .= wf_TableRow($cells, 'row3');
  222. $result .= wf_TableBody($rows, '100%', 0, '');
  223. //request controls
  224. $result .= $this->renderHinsControls($requestId);
  225. //adcomments here
  226. if ($this->adComments) {
  227. $result .= wf_delimiter();
  228. $result .= $this->adComments->renderComments($requestId);
  229. }
  230. } else {
  231. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': EX_WRONG_ID', 'error');
  232. }
  233. return($result);
  234. }
  235. /**
  236. * Sets home insurance request as done
  237. *
  238. * @param int $requestId
  239. *
  240. * @return void
  241. */
  242. public function setHinsDone($requestId) {
  243. $requestId = ubRouting::filters($requestId, 'int');
  244. if (isset($this->allHinsReq[$requestId])) {
  245. $this->hinsDb->data('state', 1);
  246. $this->hinsDb->where('id', '=', $requestId);
  247. $this->hinsDb->save();
  248. $darkVoid = new DarkVoid();
  249. $darkVoid->flushCache();
  250. log_register('INSURANCE HINS [' . $requestId . '] DONE');
  251. }
  252. }
  253. /**
  254. * Sets home insurance request as undone
  255. *
  256. * @param int $requestId
  257. *
  258. * @return void
  259. */
  260. public function setHinsUnDone($requestId) {
  261. $requestId = ubRouting::filters($requestId, 'int');
  262. if (isset($this->allHinsReq[$requestId])) {
  263. $this->hinsDb->data('state', 0);
  264. $this->hinsDb->where('id', '=', $requestId);
  265. $this->hinsDb->save();
  266. $darkVoid = new DarkVoid();
  267. $darkVoid->flushCache();
  268. log_register('INSURANCE HINS [' . $requestId . '] UNDONE');
  269. }
  270. }
  271. /**
  272. * Returns count of unprocessed home insurance requests
  273. *
  274. * @return int
  275. */
  276. public function getUnprocessedHinsReqCount() {
  277. $this->hinsDb->where('state', '=', 0);
  278. $result = $this->hinsDb->getFieldsCount();
  279. return($result);
  280. }
  281. }