api.tasksmap.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * Tasks manager map-view rendering class
  4. */
  5. class TasksMap {
  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 system alter config as key=>value
  20. *
  21. * @var array
  22. */
  23. protected $altCfg = array();
  24. /**
  25. * Contains selected year to show
  26. *
  27. * @var int
  28. */
  29. protected $showYear = '';
  30. /**
  31. * Contains selected month to show
  32. *
  33. * @var int
  34. */
  35. protected $showMonth = '';
  36. /**
  37. * Contains selected jobtype to show or empty for all jobs
  38. *
  39. * @var int/void
  40. */
  41. protected $showJobType = '';
  42. /**
  43. * Contains default tasks data source table
  44. *
  45. * @var string
  46. */
  47. protected $dataTable = 'taskman';
  48. /**
  49. * Contains count of users without build geo assigned
  50. *
  51. * @var int
  52. */
  53. protected $noGeoBuilds = 0;
  54. /**
  55. * Contains count of users whitch is not present currently in database or tasks without login
  56. *
  57. * @var int
  58. */
  59. protected $deletedUsers = 0;
  60. /**
  61. * Contains count of tasks by period
  62. *
  63. * @var int
  64. */
  65. protected $tasksExtracted = 0;
  66. /**
  67. * Contains available jobtypes
  68. *
  69. * @var array
  70. */
  71. protected $allJobTypes = array();
  72. /**
  73. * System message helper object placeholder
  74. *
  75. * @var object
  76. */
  77. protected $messages = '';
  78. /**
  79. * Tasks source database abstraction layer pleceholder
  80. *
  81. * @var object
  82. */
  83. protected $tasksDb = '';
  84. /**
  85. * Creates new report instance
  86. */
  87. public function __construct() {
  88. $this->setFiltersData();
  89. $this->initMessages();
  90. $this->initDatasource();
  91. $this->loadConfigs();
  92. $this->loadUsers();
  93. $this->loadJobTypes();
  94. }
  95. /**
  96. * Loads system maps and alter configuration files
  97. *
  98. * @global object $ubillingConfig
  99. *
  100. * @return void
  101. */
  102. protected function loadConfigs() {
  103. global $ubillingConfig;
  104. $this->mapsCfg = $ubillingConfig->getYmaps();
  105. $this->altCfg = $ubillingConfig->getAlter();
  106. }
  107. /**
  108. * Inits system message helper object instance for further usage
  109. *
  110. * @return void
  111. */
  112. protected function initMessages() {
  113. $this->messages = new UbillingMessageHelper();
  114. }
  115. /**
  116. * Loads all available jobtypes into protected property
  117. *
  118. * @return void
  119. */
  120. protected function loadJobTypes() {
  121. $this->allJobTypes = ts_GetAllJobtypes();
  122. }
  123. /**
  124. * Inits data source database abstraction layer
  125. *
  126. * @return void
  127. */
  128. protected function initDatasource() {
  129. $this->tasksDb = new NyanORM($this->dataTable);
  130. }
  131. /**
  132. * Loads all users cached data
  133. *
  134. * @return void
  135. */
  136. protected function loadUsers() {
  137. $this->allUserData = zb_UserGetAllDataCache();
  138. }
  139. /**
  140. * Sets selected year/month properties of current as defaults
  141. *
  142. * @return void
  143. */
  144. protected function setFiltersData() {
  145. if (ubRouting::checkPost('showyear')) {
  146. $this->showYear = ubRouting::post('showyear', 'int');
  147. } else {
  148. $this->showYear = curyear();
  149. }
  150. if (ubRouting::checkPost('showmonth')) {
  151. $this->showMonth = ubRouting::post('showmonth', 'int');
  152. } else {
  153. $this->showMonth = date('m');
  154. }
  155. if (ubRouting::checkPost('showjobtype')) {
  156. $this->showJobType = ubRouting::post('showjobtype', 'int');
  157. }
  158. }
  159. /**
  160. * Returns array of tasks filtered by year/month
  161. *
  162. * @return array
  163. */
  164. protected function getPlannedTasks() {
  165. $monthFilter = ($this->showMonth != '1488') ? $this->showMonth : '';
  166. $this->tasksDb->where('startdate', 'LIKE', $this->showYear . "-" . $monthFilter . "%");
  167. if ($this->showJobType) {
  168. $this->tasksDb->where('jobtype', '=', $this->showJobType);
  169. }
  170. $result = $this->tasksDb->getAll();
  171. return ($result);
  172. }
  173. /**
  174. * Returns array of tasks planned for current day
  175. *
  176. * @param int $employeeId optional employee ID to get tasks for
  177. *
  178. * @return array
  179. */
  180. public function getTodayTasks($employeeId = '') {
  181. $employeeId = ubRouting::filters($employeeId, 'int');
  182. $this->tasksDb->where('startdate', 'LIKE', curdate() . '%');
  183. if ($employeeId) {
  184. $this->tasksDb->where('employee', '=', $employeeId);
  185. }
  186. $result = $this->tasksDb->getAll();
  187. return($result);
  188. }
  189. /**
  190. * Returns list of formatted placemarks for map rendering
  191. *
  192. * @param array $userTasks
  193. *
  194. * @return string
  195. */
  196. public function getPlacemarks($userTasks) {
  197. $result = '';
  198. $buildsData = array();
  199. $buildsCounters = array();
  200. if (!empty($userTasks)) {
  201. foreach ($userTasks as $io => $each) {
  202. if (isset($this->allUserData[$each['login']])) {
  203. $userData = $this->allUserData[$each['login']];
  204. if (!empty($userData['geo'])) {
  205. $taskDate = $each['startdate'];
  206. $userLink = '';
  207. $userLink .= $taskDate . ': ' . wf_Link('?module=taskman&edittask=' . $each['id'], web_bool_led($each['status']) . ' ' . @$this->allJobTypes[$each['jobtype']]);
  208. $userLink = trim($userLink) . ', ';
  209. $userLink .= wf_Link('?module=userprofile&username=' . $each['login'], web_profile_icon() . ' ' . $userData['fulladress']);
  210. $userLink = trim($userLink);
  211. if (!isset($buildsData[$userData['geo']])) {
  212. $buildsData[$userData['geo']]['data'] = $userLink;
  213. $buildsData[$userData['geo']]['count'] = 1;
  214. } else {
  215. $buildsData[$userData['geo']]['data'] .= trim(wf_tag('br')) . $userLink;
  216. $buildsData[$userData['geo']]['count'] ++;
  217. }
  218. } else {
  219. $this->noGeoBuilds++;
  220. }
  221. } else {
  222. $this->deletedUsers++;
  223. }
  224. $this->tasksExtracted++;
  225. }
  226. if (!empty($buildsData)) {
  227. foreach ($buildsData as $coords => $usersInside) {
  228. if ($usersInside['count'] > 1) {
  229. if ($usersInside['count'] > 3) {
  230. $placeMarkIcon = 'twirl#redIcon';
  231. } else {
  232. $placeMarkIcon = 'twirl#yellowIcon';
  233. }
  234. } else {
  235. $placeMarkIcon = 'twirl#lightblueIcon';
  236. }
  237. $result .= generic_mapAddMark($coords, $usersInside['data'], __('Tasks') . ': ' . $usersInside['count'], '', $placeMarkIcon, '', $this->mapsCfg['CANVAS_RENDER']);
  238. }
  239. }
  240. }
  241. return ($result);
  242. }
  243. /**
  244. * Returns year/month filtering form
  245. *
  246. * @return string
  247. */
  248. public function renderFiltersForm() {
  249. $result = '';
  250. $inputs = wf_YearSelectorPreset('showyear', __('Year'), false, $this->showYear) . ' ';
  251. $inputs .= wf_MonthSelector('showmonth', __('Month'), $this->showMonth, false, true) . ' ';
  252. $jobTypes = array('' => __('Any'));
  253. $jobTypes += $this->allJobTypes;
  254. $inputs .= wf_Selector('showjobtype', $jobTypes, __('Job type'), $this->showJobType, false) . ' ';
  255. $inputs .= wf_Submit(__('Show'));
  256. $result .= wf_Form('', 'POST', $inputs, 'glamour');
  257. return ($result);
  258. }
  259. /**
  260. * Renders report as map
  261. *
  262. * @return string
  263. */
  264. public function renderMap() {
  265. $result = '';
  266. $allTasks = $this->getPlannedTasks();
  267. $placemarks = $this->getPlacemarks($allTasks);
  268. $result .= generic_MapContainer();
  269. $result .= generic_MapInit($this->mapsCfg['CENTER'], $this->mapsCfg['ZOOM'], $this->mapsCfg['TYPE'], $placemarks, '', $this->mapsCfg['LANG'], 'ubmap');
  270. return ($result);
  271. }
  272. /**
  273. * Renders deleted users or unknown geo builds stats if they available
  274. *
  275. * @return string
  276. */
  277. public function renderStats() {
  278. $result = '';
  279. if ($this->tasksExtracted) {
  280. $result .= $this->messages->getStyledMessage(__('Total tasks') . ': ' . $this->tasksExtracted, 'success');
  281. }
  282. if ($this->tasksExtracted AND $this->noGeoBuilds) {
  283. $result .= $this->messages->getStyledMessage(__('Tasks rendered on map') . ': ' . ($this->tasksExtracted - $this->noGeoBuilds - $this->deletedUsers), 'info');
  284. }
  285. if ($this->noGeoBuilds) {
  286. $result .= $this->messages->getStyledMessage(__('Builds without geo location assigned') . ': ' . $this->noGeoBuilds, 'warning');
  287. }
  288. if ($this->deletedUsers) {
  289. $result .= $this->messages->getStyledMessage(__('Already deleted users or tasks without user') . ': ' . $this->deletedUsers, 'error');
  290. }
  291. return ($result);
  292. }
  293. }