api.sigmap.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * User signups mapping/location report
  4. */
  5. class SigMap {
  6. /**
  7. * Contains all available users data
  8. *
  9. * @var array
  10. */
  11. protected $allUserData = array();
  12. /**
  13. * Contains system maps configuration as key=>value
  14. *
  15. * @var array
  16. */
  17. protected $mapsCfg = array();
  18. /**
  19. * Contains selected year to show
  20. *
  21. * @var int
  22. */
  23. protected $showYear = '';
  24. /**
  25. * Contains selected month to show
  26. *
  27. * @var int
  28. */
  29. protected $showMonth = '';
  30. /**
  31. * Contains default signups data source table
  32. *
  33. * @var string
  34. */
  35. protected $dataTable = 'userreg';
  36. /**
  37. * User signups database abstraction layer placeholder
  38. *
  39. * @var object
  40. */
  41. protected $userSignups = '';
  42. /**
  43. * Contains count of users without build geo assigned
  44. *
  45. * @var int
  46. */
  47. protected $noGeoBuilds = 0;
  48. /**
  49. * Contains count of users whitch is not present currently in database
  50. *
  51. * @var int
  52. */
  53. protected $deletedUsers = 0;
  54. /**
  55. * Contains count of registered users by period
  56. *
  57. * @var int
  58. */
  59. protected $registeredUsers = 0;
  60. /**
  61. * Contains all users street data as login=>street
  62. *
  63. * @var array
  64. */
  65. protected $allUsersStreetData = array();
  66. /**
  67. * Contains all users cities data as login=>city
  68. *
  69. * @var array
  70. */
  71. protected $allUsersCityData = array();
  72. /**
  73. * Contains per-street signups data as cityname+streetname=>count
  74. *
  75. * @var array
  76. */
  77. protected $streetsSignups = array();
  78. /**
  79. * System message helper object placeholder
  80. *
  81. * @var object
  82. */
  83. protected $messages = '';
  84. /**
  85. * Creates new report instance
  86. */
  87. public function __construct() {
  88. $this->setDateData();
  89. $this->initMessages();
  90. $this->loadMapsConfig();
  91. $this->loadUsers();
  92. $this->initDataSource();
  93. }
  94. /**
  95. * Loads system maps configuration file
  96. *
  97. * @global object $ubillingConfig
  98. *
  99. * @return void
  100. */
  101. protected function loadMapsConfig() {
  102. global $ubillingConfig;
  103. $this->mapsCfg = $ubillingConfig->getYmaps();
  104. }
  105. /**
  106. * Inits system message helper object instance for further usage
  107. *
  108. * @return void
  109. */
  110. protected function initMessages() {
  111. $this->messages = new UbillingMessageHelper();
  112. }
  113. /**
  114. * Inits signups database abstraction layer
  115. *
  116. * @return void
  117. */
  118. protected function initDataSource() {
  119. $this->userSignups = new NyanORM($this->dataTable);
  120. }
  121. /**
  122. * Loads all users cached data
  123. *
  124. * @return void
  125. */
  126. protected function loadUsers() {
  127. $this->allUserData = zb_UserGetAllDataCache();
  128. $this->allUsersStreetData = zb_AddressGetStreetUsers();
  129. $this->allUsersCityData = zb_AddressGetCityUsers();
  130. }
  131. /**
  132. * Sets selected year/month properties of current as defaults
  133. *
  134. * @return void
  135. */
  136. protected function setDateData() {
  137. if (ubRouting::checkPost('showyear')) {
  138. $this->showYear = ubRouting::post('showyear', 'int');
  139. } else {
  140. $this->showYear = curyear();
  141. }
  142. if (ubRouting::checkPost('showmonth')) {
  143. $this->showMonth = ubRouting::post('showmonth', 'int');
  144. } else {
  145. $this->showMonth = date('m');
  146. }
  147. }
  148. /**
  149. * Returns array of user signups filtered by year/month
  150. *
  151. * @return array
  152. */
  153. protected function getRegisteredUsers() {
  154. $monthFilter = ($this->showMonth != '1488') ? $this->showMonth : '';
  155. $dateFilter = $this->showYear . "-" . $monthFilter . "%";
  156. $this->userSignups->where('date', 'LIKE', $dateFilter);
  157. $result = $this->userSignups->getAll();
  158. return ($result);
  159. }
  160. /**
  161. * Returns list of formatted placemarks for map rendering
  162. *
  163. * @param array $userSignups
  164. *
  165. * @return string
  166. */
  167. protected function getPlacemarks($userSignups) {
  168. $result = '';
  169. $buildsData = array();
  170. $buildsCounters = array();
  171. if (!empty($userSignups)) {
  172. foreach ($userSignups as $io => $each) {
  173. if (isset($this->allUserData[$each['login']])) {
  174. $userData = $this->allUserData[$each['login']];
  175. if (!empty($userData['geo'])) {
  176. $signupDate = date("Y-m-d", strtotime($each['date']));
  177. $userLink = $signupDate . ': ' . wf_Link('?module=userprofile&username=' . $each['login'], web_profile_icon() . ' ' . $userData['fulladress']);
  178. $userLink = trim($userLink);
  179. if (!isset($buildsData[$userData['geo']])) {
  180. $buildsData[$userData['geo']]['data'] = $userLink;
  181. $buildsData[$userData['geo']]['count'] = 1;
  182. } else {
  183. $buildsData[$userData['geo']]['data'] .= trim(wf_tag('br')) . $userLink;
  184. $buildsData[$userData['geo']]['count'] ++;
  185. }
  186. } else {
  187. $this->noGeoBuilds++;
  188. }
  189. } else {
  190. $this->deletedUsers++;
  191. }
  192. $userCity = (isset($this->allUsersCityData[$each['login']])) ? $this->allUsersCityData[$each['login']] : '';
  193. $userStreet = (isset($this->allUsersStreetData[$each['login']])) ? $this->allUsersStreetData[$each['login']] : '';
  194. $userStreetFull = $userCity . ' ' . $userStreet;
  195. if (!empty($userStreetFull)) {
  196. if (isset($this->streetsSignups[$userStreetFull])) {
  197. $this->streetsSignups[$userStreetFull] ++;
  198. } else {
  199. $this->streetsSignups[$userStreetFull] = 1;
  200. }
  201. }
  202. $this->registeredUsers++;
  203. }
  204. if (!empty($buildsData)) {
  205. foreach ($buildsData as $coords => $usersInside) {
  206. if ($usersInside['count'] > 1) {
  207. $placeMarkIcon = 'twirl#buildingsIcon';
  208. } else {
  209. $placeMarkIcon = 'twirl#houseIcon';
  210. }
  211. $result .= generic_mapAddMark($coords, $usersInside['data'], __('Users') . ': ' . $usersInside['count'], '', $placeMarkIcon, '', $this->mapsCfg['CANVAS_RENDER']);
  212. }
  213. }
  214. }
  215. return ($result);
  216. }
  217. /**
  218. * Returns year/month filtering form
  219. *
  220. * @return string
  221. */
  222. public function renderDateForm() {
  223. $result = '';
  224. $inputs = wf_YearSelectorPreset('showyear', __('Year'), false, $this->showYear) . ' ';
  225. $inputs .= wf_MonthSelector('showmonth', __('Month'), $this->showMonth, false, true) . ' ';
  226. $inputs .= wf_Submit(__('Show'));
  227. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  228. return ($result);
  229. }
  230. /**
  231. * Renders report as map
  232. *
  233. * @return string
  234. */
  235. public function renderMap() {
  236. $result = '';
  237. $allSignups = $this->getRegisteredUsers();
  238. $placemarks = $this->getPlacemarks($allSignups);
  239. $result .= generic_MapContainer();
  240. $result .= generic_MapInit($this->mapsCfg['CENTER'], $this->mapsCfg['ZOOM'], $this->mapsCfg['TYPE'], $placemarks, '', $this->mapsCfg['LANG'], 'ubmap');
  241. return ($result);
  242. }
  243. /**
  244. * Renders deleted users or unknown geo builds stats if they available
  245. *
  246. * @return string
  247. */
  248. public function renderStats() {
  249. $result = '';
  250. if ($this->registeredUsers) {
  251. $result .= $this->messages->getStyledMessage(__('Total users registered') . ': ' . $this->registeredUsers, 'success');
  252. }
  253. if ($this->registeredUsers AND $this->noGeoBuilds) {
  254. $result .= $this->messages->getStyledMessage(__('Users rendered on map') . ': ' . ($this->registeredUsers - $this->noGeoBuilds), 'info');
  255. }
  256. if ($this->noGeoBuilds) {
  257. $result .= $this->messages->getStyledMessage(__('Builds without geo location assigned') . ': ' . $this->noGeoBuilds, 'warning');
  258. }
  259. if ($this->deletedUsers) {
  260. $result .= $this->messages->getStyledMessage(__('Already deleted users') . ': ' . $this->deletedUsers, 'error');
  261. }
  262. if ($this->streetsSignups) {
  263. $cells = wf_TableCell(__('Street'));
  264. $cells .= wf_TableCell(__('Signups'));
  265. $rows = wf_TableRow($cells, 'row1');
  266. foreach ($this->streetsSignups as $streetName => $streetCount) {
  267. $cells = wf_TableCell($streetName);
  268. $cells .= wf_TableCell($streetCount);
  269. $rows .= wf_TableRow($cells, 'row5');
  270. }
  271. $result .= wf_delimiter();
  272. $result .= wf_TableBody($rows, '100%', 0, 'sortable');
  273. }
  274. return ($result);
  275. }
  276. }