api.reportbuilds.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * Report for filtering and display basic builds info
  4. */
  5. class ReportBuilds {
  6. /**
  7. * Contains alter config as key=>value
  8. *
  9. * @var array
  10. */
  11. protected $altCfg = array();
  12. /**
  13. * Contains basic city data as cityid=>name
  14. *
  15. * @var array
  16. */
  17. protected $allCities = array();
  18. /**
  19. * Contains full streets data as streetid=>streetdata
  20. *
  21. * @var array
  22. */
  23. protected $allStreets = array();
  24. /**
  25. * Contains all streets names array as streetid=>streetname
  26. *
  27. * @var array
  28. */
  29. protected $allStreetNames = array();
  30. /**
  31. * Contains full builds data as id=>builddata
  32. *
  33. * @var array
  34. */
  35. protected $allBuilds = array();
  36. /**
  37. * Contains array of build apartments as buildid=>aptsData
  38. *
  39. * @var array
  40. */
  41. protected $allApts = array();
  42. /**
  43. * Just BUILD_EXTENDED option based flag
  44. *
  45. * @var bool
  46. */
  47. protected $buildPassportsFlag = false;
  48. /**
  49. * Is ADcomments enabled flag?
  50. *
  51. * @var bool
  52. */
  53. protected $adCommentsFlag = false;
  54. /**
  55. * Build passports instance placeholder
  56. *
  57. * @var object
  58. */
  59. protected $buildPassports = '';
  60. /**
  61. * System messages helper instance placeholder
  62. *
  63. * @var object
  64. */
  65. protected $messages = '';
  66. /**
  67. * Some routes, urls, defines etc
  68. */
  69. const URL_ME = '?module=report_builds';
  70. const ROUTE_AJLIST = 'ajaxbuildslist';
  71. const ROUTE_EXPORTS = 'exportcontrols';
  72. const PROUTE_FILTERS = 'applynewfilters';
  73. const PROUTE_FILTERCITY = 'filtercityid';
  74. const PROUTE_FILTERSTREET = 'filterstreetid';
  75. public function __construct() {
  76. $this->loadConfigs();
  77. $this->initMessages();
  78. $this->loadCities();
  79. $this->loadStreets();
  80. $this->loadBuilds();
  81. $this->loadApartments();
  82. $this->initBuildPassports();
  83. }
  84. /**
  85. * Preloads some required configs and sores it in protected properties
  86. *
  87. * @global object $ubillingConfig
  88. *
  89. * @return vod
  90. */
  91. protected function loadConfigs() {
  92. global $ubillingConfig;
  93. $this->altCfg = $ubillingConfig->getAlter();
  94. if (@$this->altCfg['BUILD_EXTENDED']) {
  95. $this->buildPassportsFlag = true;
  96. }
  97. if (@$this->altCfg['ADCOMMENTS_ENABLED']) {
  98. $this->adCommentsFlag = true;
  99. }
  100. }
  101. /**
  102. * Inits message helper for further usage
  103. *
  104. * @return void
  105. */
  106. protected function initMessages() {
  107. $this->messages = new UbillingMessageHelper();
  108. }
  109. /**
  110. * Loads city data from database
  111. *
  112. * @return void
  113. */
  114. protected function loadCities() {
  115. $this->allCities = zb_AddressGetFullCityNames();
  116. }
  117. /**
  118. * Loads streets data from database
  119. *
  120. * @return void
  121. */
  122. protected function loadStreets() {
  123. $this->allStreets = zb_AddressGetStreetsDataAssoc('ORDER BY `streetname` ASC');
  124. if (!empty($this->allStreets)) {
  125. foreach ($this->allStreets as $io => $each) {
  126. $this->allStreetNames[$each['id']] = $each['streetname'];
  127. }
  128. }
  129. }
  130. /**
  131. * Loads builds data from database
  132. *
  133. * @return void
  134. */
  135. protected function loadBuilds() {
  136. $this->allBuilds = zb_AddressGetBuildAllDataAssoc();
  137. }
  138. /**
  139. * Loads apartments data from database
  140. *
  141. * @return void
  142. */
  143. protected function loadApartments() {
  144. $aptTmp = zb_AddressGetAptAllData();
  145. if (!empty($aptTmp)) {
  146. foreach ($aptTmp as $io => $each) {
  147. $this->allApts[$each['buildid']][] = $each;
  148. }
  149. }
  150. }
  151. /**
  152. * Inits build passports object for further usage
  153. *
  154. * @return void
  155. */
  156. protected function initBuildPassports() {
  157. if ($this->buildPassportsFlag) {
  158. $this->buildPassports = new BuildPassport();
  159. }
  160. }
  161. /**
  162. * Returns city id of build
  163. *
  164. * @param int $buildId
  165. *
  166. * @return int
  167. */
  168. protected function getCityOfBuild($buildId) {
  169. $result = 0;
  170. $streetId = $this->getStreetOfBuild($buildId);
  171. if ($streetId) {
  172. if (isset($this->allStreets[$streetId])) {
  173. $streetData = $this->allStreets[$streetId];
  174. $result = $streetData['cityid'];
  175. }
  176. }
  177. return($result);
  178. }
  179. /**
  180. * Returns street id of build
  181. *
  182. * @param int $buildId
  183. *
  184. * @return int
  185. */
  186. protected function getStreetOfBuild($buildId) {
  187. $result = 0;
  188. if (isset($this->allBuilds[$buildId])) {
  189. $result = $this->allBuilds[$buildId]['streetid'];
  190. }
  191. return($result);
  192. }
  193. /**
  194. * Returns apartments count in some build
  195. *
  196. * @param int $buildId
  197. *
  198. * @return int
  199. */
  200. protected function getAptCount($buildId) {
  201. $result = 0;
  202. if (isset($this->allApts[$buildId])) {
  203. $result = sizeof($this->allApts[$buildId]);
  204. }
  205. return($result);
  206. }
  207. /**
  208. * Renders report container
  209. *
  210. * @return string
  211. */
  212. public function renderBuilds() {
  213. $result = '';
  214. if (!empty($this->allBuilds)) {
  215. $columns = array(
  216. 'City',
  217. 'Street',
  218. 'Building number',
  219. 'Users',
  220. 'Actions'
  221. );
  222. if ($this->buildPassportsFlag) {
  223. $columns = array(
  224. 'City',
  225. 'Street',
  226. 'Building number',
  227. 'Owner',
  228. 'Phone',
  229. 'Type',
  230. 'Floors',
  231. 'Entrances',
  232. 'Apartments',
  233. 'Users',
  234. '%',
  235. 'Access',
  236. 'Actions'
  237. );
  238. }
  239. $opts = '"order": [[ 1, "asc" ]]';
  240. //optional ID column
  241. if (cfr('ROOT')) {
  242. $columns = array_merge(array('ID'), $columns);
  243. $opts = '"order": [[ 2, "asc" ]]';
  244. }
  245. //optional export options
  246. if (ubRouting::checkGet(self::ROUTE_EXPORTS)) {
  247. $opts .= ', "dom": \'<"F"lfB>rti<"F"ps>\', buttons: [\'csv\', \'excel\', \'pdf\']';
  248. }
  249. $filters = '';
  250. if (ubRouting::checkPost(self::PROUTE_FILTERS)) {
  251. //filters form catched?
  252. if (ubRouting::checkPost(self::PROUTE_FILTERCITY)) {
  253. $filters .= '&' . self::PROUTE_FILTERCITY . '=' . ubRouting::post(self::PROUTE_FILTERCITY);
  254. }
  255. if (ubRouting::checkPost(self::PROUTE_FILTERSTREET)) {
  256. $filters .= '&' . self::PROUTE_FILTERSTREET . '=' . ubRouting::post(self::PROUTE_FILTERSTREET);
  257. }
  258. }
  259. $ajaxSource = self::URL_ME . '&' . self::ROUTE_AJLIST . '=true' . $filters;
  260. $result .= wf_JqDtLoader($columns, $ajaxSource, false, __('Builds'), 100, $opts);
  261. } else {
  262. $result .= $this->messages->getStyledMessage(__('Nothing to show'), 'warning');
  263. }
  264. return($result);
  265. }
  266. /**
  267. * Renders filters form
  268. *
  269. * @return string
  270. */
  271. public function renderFiltersForm() {
  272. $result = '';
  273. if (!empty($this->allCities) AND ! empty($this->allStreets)) {
  274. $cityArr = array('' => __('Any'));
  275. $cityArr += $this->allCities;
  276. $streetArr = array('' => __('Any'));
  277. if (ubRouting::checkPost(self::PROUTE_FILTERCITY)) {
  278. //filter streets by some selected city
  279. $filterCityId = ubRouting::post(self::PROUTE_FILTERCITY);
  280. foreach ($this->allStreets as $io => $each) {
  281. if ($each['cityid'] == $filterCityId) {
  282. $streetArr[$each['id']] = $each['streetname'];
  283. }
  284. }
  285. } else {
  286. //full streets list
  287. $streetArr += $this->allStreetNames;
  288. }
  289. $inputs = wf_HiddenInput(self::PROUTE_FILTERS, 'true');
  290. $inputs .= wf_SelectorAC(self::PROUTE_FILTERCITY, $cityArr, __('City'), ubRouting::post(self::PROUTE_FILTERCITY), false) . ' ';
  291. $inputs .= wf_SelectorAC(self::PROUTE_FILTERSTREET, $streetArr, __('Street'), ubRouting::post(self::PROUTE_FILTERSTREET), false) . ' ';
  292. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  293. }
  294. return($result);
  295. }
  296. /**
  297. * Renders json build data array
  298. *
  299. * @return void
  300. */
  301. public function renderAjBuildList() {
  302. $json = new wf_JqDtHelper();
  303. $cityFilter = ubRouting::get(self::PROUTE_FILTERCITY, 'int');
  304. $streetFilter = ubRouting::get(self::PROUTE_FILTERSTREET, 'int');
  305. $backUrl = '&back=' . base64_encode('report_builds');
  306. $passportUrl = BuildPassport::URL_PASSPORT . $backUrl . '&' . BuildPassport::ROUTE_BUILD . '=';
  307. if ($this->adCommentsFlag) {
  308. $adComments = new ADcomments('BUILDS');
  309. }
  310. $actBoxStyle = wf_tag('div', false, '', 'style="width:60px;"');
  311. $actBoxStyleEnd = wf_tag('div', true);
  312. if (!empty($this->allBuilds)) {
  313. foreach ($this->allBuilds as $io => $each) {
  314. $filtersPassed = true;
  315. $buildId = $each['id'];
  316. $buildCity = $this->getCityOfBuild($buildId);
  317. $buildStreet = $this->getStreetOfBuild($buildId);
  318. $cityName = (isset($this->allCities[$buildCity])) ? $this->allCities[$buildCity] : __('Missed');
  319. $streetName = (isset($this->allStreets[$buildStreet])) ? $this->allStreets[$buildStreet]['streetname'] : __('Missed');
  320. $userCount = $this->getAptCount($buildId);
  321. //some optional filtering here
  322. if ($cityFilter) {
  323. if ($buildCity != $cityFilter) {
  324. $filtersPassed = false;
  325. }
  326. }
  327. if ($streetFilter) {
  328. if ($buildStreet != $streetFilter) {
  329. $filtersPassed = false;
  330. }
  331. }
  332. if ($filtersPassed) {
  333. if (cfr('ROOT')) {
  334. $data[] = $each['id'];
  335. }
  336. $data[] = $cityName;
  337. $data[] = $streetName;
  338. $data[] = $each['buildnum'];
  339. if ($this->buildPassportsFlag) {
  340. $buildPassport = $this->buildPassports->getPassportData($buildId);
  341. if (!empty($buildPassport)) {
  342. //some passport data available
  343. $ownerLabel = $buildPassport['owner'] . ' ' . $buildPassport['ownername'] . ' ' . $buildPassport['ownercontact'];
  344. $ownerPhone = $buildPassport['ownerphone'];
  345. $floors = $buildPassport['floors'];
  346. $type = ($buildPassport['anthill']) ? wf_img('skins/ymaps/build.png', __('Apartment house')) : wf_img('skins/ymaps/home.png');
  347. $entrances = $buildPassport['entrances'];
  348. $apts = $buildPassport['apts'];
  349. $accessNotices = $buildPassport['accessnotices'];
  350. } else {
  351. $ownerLabel = '';
  352. $ownerPhone = '';
  353. $type = wf_img('skins/ymaps/home.png');
  354. $floors = '';
  355. $entrances = '';
  356. $apts = '';
  357. $accessNotices = '';
  358. }
  359. $data[] = $ownerLabel;
  360. $data[] = $ownerPhone;
  361. $data[] = $type;
  362. $data[] = $floors;
  363. $data[] = $entrances;
  364. $data[] = $apts;
  365. }
  366. $data[] = $userCount;
  367. if ($this->buildPassportsFlag) {
  368. $signupsPercent = '';
  369. if (($apts > 0)) {
  370. $signupsPercent = zb_PercentValue($apts, $userCount);
  371. }
  372. $data[] = $signupsPercent;
  373. $data[] = $accessNotices;
  374. }
  375. $actionLinks = '';
  376. if ($this->buildPassportsFlag) {
  377. if ($this->adCommentsFlag) {
  378. $actionLinks .= $adComments->getCommentsIndicator($each['id']) . ' ';
  379. }
  380. $actionLinks .= wf_Link($passportUrl . $each['id'], wf_img('skins/icon_buildpassport.png', __('Build passport'))) . ' ';
  381. }
  382. if (!empty($each['geo'])) {
  383. $actionLinks .= wf_Link("?module=usersmap&findbuild=" . $each['geo'], wf_img('skins/icon_search_small.gif', __('Find on map')), false) . ' ';
  384. } else {
  385. if (cfr('BUILDS')) {
  386. $actionLinks .= wf_Link('?module=usersmap&locfinder=true&placebld=' . $each['id'], wf_img('skins/ymaps/target.png', __('Place on map')), false, '') . ' ';
  387. }
  388. }
  389. $data[] = $actBoxStyle . $actionLinks . $actBoxStyleEnd;
  390. $json->addRow($data);
  391. unset($data);
  392. }
  393. }
  394. }
  395. $json->getJson();
  396. }
  397. }