index.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. if (cfr('TASKMANSEARCH')) {
  3. class TaskmanSearch {
  4. /**
  5. * Contains all available employee as employeeid=>name
  6. *
  7. * @var array
  8. */
  9. protected $allEmployee = array();
  10. /**
  11. * Contains all active employee as employeeid=>name
  12. *
  13. * @var array
  14. */
  15. protected $activeEmployee = array();
  16. /**
  17. * Contains available administrators as login=>employee name
  18. *
  19. * @var array
  20. */
  21. protected $allAdmins = array();
  22. /**
  23. * System alter.ini config stored as array key=>value
  24. *
  25. * @var array
  26. */
  27. protected $altCfg = array();
  28. /**
  29. * Available jobtypes as jobtypeid=>name
  30. *
  31. * @var array
  32. */
  33. protected $allJobtypes = array();
  34. const URL_TASKVIEW = '?module=taskman&edittask=';
  35. public function __construct() {
  36. $this->loadAllEmployee();
  37. $this->loadActiveEmployee();
  38. $this->loadAdmins();
  39. $this->loadAltcfg();
  40. $this->loadJobtypes();
  41. }
  42. /**
  43. * Loads all existing employees from database
  44. *
  45. * @return void
  46. */
  47. protected function loadAllEmployee() {
  48. $this->allEmployee = ts_GetAllEmployee();
  49. }
  50. /**
  51. * Loads available administrators
  52. *
  53. * @return void
  54. */
  55. protected function loadAdmins() {
  56. $all = rcms_scandir(USERS_PATH);
  57. @$employeeLogins = unserialize(ts_GetAllEmployeeLoginsCached());
  58. if (!empty($all)) {
  59. foreach ($all as $io => $each) {
  60. $adminName = (isset($employeeLogins[$each])) ? $employeeLogins[$each] : $each;
  61. $this->allAdmins[$each] = $adminName;
  62. }
  63. }
  64. }
  65. /**
  66. * Loads all existing employees from database
  67. *
  68. * @return void
  69. */
  70. protected function loadActiveEmployee() {
  71. $this->activeEmployee = ts_GetActiveEmployee();
  72. }
  73. /**
  74. * Loads system alter config
  75. *
  76. * @global object $ubillingConfig
  77. *
  78. * @return void
  79. */
  80. protected function loadAltcfg() {
  81. global $ubillingConfig;
  82. $this->altCfg = $ubillingConfig->getAlter();
  83. }
  84. /**
  85. * Loads available jobtypes from database
  86. *
  87. * @return void
  88. */
  89. protected function loadJobtypes() {
  90. $this->allJobtypes = ts_GetAllJobtypes();
  91. }
  92. /**
  93. * Renders search form. Deal with it.
  94. *
  95. * @return string
  96. */
  97. public function renderSearchForm() {
  98. $result = '';
  99. $datefromDefault = (wf_CheckPost(array('datefrom'))) ? $_POST['datefrom'] : curdate();
  100. $datetoDefault = (wf_CheckPost(array('dateto'))) ? $_POST['dateto'] : curdate();
  101. $inputs = __('Date') . ' ' . wf_DatePickerPreset('datefrom', $datefromDefault, true) . ' ' . __('From') . ' ' . wf_DatePickerPreset('dateto', $datetoDefault, true) . ' ' . __('To');
  102. $inputs .= wf_tag('br');
  103. $inputs .= wf_CheckInput('cb_admin', '', false, false);
  104. $inputs .= wf_Selector('admin', $this->allAdmins, __('Administrator'), '', true);
  105. $inputs .= wf_CheckInput('cb_id', '', false, false);
  106. $inputs .= wf_TextInput('taskid', __('ID'), '', true, 7);
  107. $inputs .= wf_CheckInput('cb_taskdays', '', false, false);
  108. $inputs .= wf_TextInput('taskdays', __('Implementation took more days'), '', true, 4);
  109. $inputs .= wf_CheckInput('cb_taskaddress', '', false, false);
  110. $inputs .= wf_TextInput('taskaddress', __('Address contains'), '', true, 20);
  111. $inputs .= wf_CheckInput('cb_taskphone', '', false, false);
  112. $inputs .= wf_TextInput('taskphone', __('Phone'), '', true, 20);
  113. $inputs .= wf_CheckInput('cb_tasknotes', '', false, false);
  114. $inputs .= wf_TextInput('tasknotes', __('Notes contains'), '', true, 20);
  115. $inputs .= wf_CheckInput('cb_taskjobcontain', '', false, false);
  116. $inputs .= wf_TextInput('taskjobcontain', __('Job type contains'), '', true, 20);
  117. $inputs .= wf_CheckInput('cb_taskjobtype', '', false, false);
  118. $inputs .= wf_Selector('taskjobtype', $this->allJobtypes, __('Job type'), '', true);
  119. $inputs .= wf_CheckInput('cb_employee', '', false, false);
  120. $inputs .= wf_Selector('employee', $this->activeEmployee, __('Who should do'), '', true);
  121. $inputs .= wf_CheckInput('cb_employeedone', '', false, false);
  122. $inputs .= wf_Selector('employeedone', $this->activeEmployee, __('Worker done'), '', true);
  123. $inputs .= wf_CheckInput('cb_duplicateaddress', __('Duplicate address'), true, false);
  124. $inputs .= wf_CheckInput('cb_showlate', __('Show late'), true, false);
  125. $inputs .= wf_CheckInput('cb_onlydone', __('Done tasks'), true, false);
  126. $inputs .= wf_CheckInput('cb_onlyundone', __('Undone tasks'), true, false);
  127. if ($this->altCfg['SALARY_ENABLED']) {
  128. $inputs .= wf_CheckInput('cb_nosalsaryjobs', __('Tasks without jobs'), true, false);
  129. }
  130. $inputs .= wf_Submit(__('Search'));
  131. $result = wf_Form('', 'POST', $inputs, 'glamour');
  132. $result .= wf_CleanDiv();
  133. return ($result);
  134. }
  135. /**
  136. * Do some search actions by captured POST request
  137. *
  138. * @return array
  139. */
  140. public function commonSearch() {
  141. $result = array();
  142. if (wf_CheckPost(array('datefrom', 'dateto'))) {
  143. $dateFrom = mysql_real_escape_string($_POST['datefrom']);
  144. $dateTo = mysql_real_escape_string($_POST['dateto']);
  145. $baseQuery = "SELECT `taskman`.*, `jobtypes`.`jobname` FROM `taskman`
  146. LEFT JOIN `jobtypes` ON `taskman`.`jobtype` = `jobtypes`.`id`
  147. WHERE `startdate` BETWEEN '" . $dateFrom . "' AND '" . $dateTo . "' ";
  148. $appendQuery = '';
  149. //task id
  150. if (wf_CheckPost(array('cb_id', 'taskid'))) {
  151. $taskid = vf($_POST['taskid'], 3);
  152. $appendQuery .= " AND `taskman`.`id`='" . $taskid . "' ";
  153. }
  154. //original task admin
  155. if (wf_CheckPost(array('cb_admin', 'admin'))) {
  156. $admin = mysql_real_escape_string($_POST['admin']);
  157. $appendQuery .= " AND `admin`='" . $admin . "' ";
  158. }
  159. //more than some days count
  160. if (wf_CheckPost(array('cb_taskdays', 'taskdays'))) {
  161. $taskdays = vf($_POST['taskdays'], 3);
  162. $appendQuery .= " AND DATEDIFF(`enddate`, `startdate`) > '" . $taskdays . "' ";
  163. }
  164. //task address non strict
  165. if (wf_CheckPost(array('cb_taskaddress', 'taskaddress'))) {
  166. $taskaddress = mysql_real_escape_string($_POST['taskaddress']);
  167. $appendQuery .= " AND `address` LIKE '%" . $taskaddress . "%' ";
  168. }
  169. //task phone non strict
  170. if (wf_CheckPost(array('cb_taskphone', 'taskphone'))) {
  171. $taskphone = mysql_real_escape_string($_POST['taskphone']);
  172. $appendQuery .= " AND `phone` LIKE '%" . $taskphone . "%' ";
  173. }
  174. //task note contains
  175. if (ubRouting::checkPost(array('cb_tasknotes', 'tasknotes'))) {
  176. $tasknotes = ubRouting::post('tasknotes', 'mres');
  177. $appendQuery .= " AND `jobnote` LIKE '%" . $tasknotes . "%'";
  178. }
  179. //task job type contains
  180. if (ubRouting::checkPost(array('cb_taskjobcontain', 'taskjobcontain'))) {
  181. $taskjobcontain = ubRouting::post('taskjobcontain', 'mres');
  182. $appendQuery .= " AND `jobname` LIKE '%" . $taskjobcontain . "%'";
  183. }
  184. //task job type
  185. if (wf_CheckPost(array('cb_taskjobtype', 'taskjobtype'))) {
  186. $taskjobtypeid = vf($_POST['taskjobtype'], 3);
  187. $appendQuery .= " AND `jobtype` LIKE '" . $taskjobtypeid . "' ";
  188. }
  189. //original task employee
  190. if (wf_CheckPost(array('cb_employee', 'employee'))) {
  191. $employee = mysql_real_escape_string($_POST['employee']);
  192. $appendQuery .= " AND `employee`='" . $employee . "' ";
  193. }
  194. //original task employeedone
  195. if (wf_CheckPost(array('cb_employeedone', 'employeedone'))) {
  196. $employeedone = mysql_real_escape_string($_POST['employeedone']);
  197. $appendQuery .= " AND `employeedone`='" . $employeedone . "' ";
  198. }
  199. //address duplicate search
  200. if (wf_CheckPost(array('cb_duplicateaddress'))) {
  201. // $appendQuery.=" AND `address` IN (SELECT `address` FROM `taskman` WHERE `startdate` BETWEEN '" . $dateFrom . "' AND '" . $dateTo . "' GROUP BY `address` HAVING COUNT(*) > 1) ";
  202. $baseQuery = "SELECT st1.*, st2.`address` FROM `taskman` st1 INNER JOIN taskman st2 ON (st2.`startdate` BETWEEN '" . $dateFrom . "' AND '" . $dateTo . "' AND st1.address = st2.address) GROUP BY st1.id HAVING COUNT(*) > 1 AND `startdate` BETWEEN '" . $dateFrom . "' AND '" . $dateTo . "' ";
  203. }
  204. //late jobs
  205. if (wf_CheckPost(array('cb_showlate'))) {
  206. $curdate = curdate();
  207. $appendQuery .= " AND `status`='0' AND `startdate`< '" . $curdate . "' ";
  208. }
  209. //only done jobs
  210. if (wf_CheckPost(array('cb_onlydone'))) {
  211. $appendQuery .= " AND `status`='1' ";
  212. }
  213. //only undone jobs
  214. if (wf_CheckPost(array('cb_onlyundone'))) {
  215. $appendQuery .= " AND `status`='0' ";
  216. }
  217. $query = $baseQuery . $appendQuery;
  218. // deb($query);
  219. $raw = simple_queryall($query);
  220. if (!empty($raw)) {
  221. foreach ($raw as $io => $each) {
  222. $result[$each['id']] = $each;
  223. }
  224. }
  225. //salary no jobs processing/excluding
  226. if (wf_CheckPost(array('cb_nosalsaryjobs'))) {
  227. $salaryTasks = array();
  228. $greed = new Avarice();
  229. $beggar = $greed->runtime('SALARY');
  230. if (!empty($beggar)) {
  231. $querySalaryJobs = "SELECT `id`,`taskid` from `salary_jobs`";
  232. $salaryJobsRaw = simple_queryall($querySalaryJobs);
  233. if (!empty($salaryJobsRaw)) {
  234. foreach ($salaryJobsRaw as $io => $each) {
  235. if (!empty($each['taskid'])) {
  236. $salaryTasks[$each['taskid']] = $each['id'];
  237. }
  238. }
  239. }
  240. if (!empty($salaryTasks)) {
  241. foreach ($salaryTasks as $jobTaskid => $eachJobId) {
  242. if (isset($result[$jobTaskid])) {
  243. unset($result[$jobTaskid]);
  244. }
  245. }
  246. }
  247. } else {
  248. show_error(__('No license key available'));
  249. }
  250. }
  251. }
  252. return ($result);
  253. }
  254. /**
  255. * Renders tasks list as human readable view
  256. *
  257. * @param array $tasksArray
  258. *
  259. * @return string
  260. */
  261. public function renderTasks($tasksArray) {
  262. $result = '';
  263. $totalCount = 0;
  264. if (!empty($tasksArray)) {
  265. $cells = wf_TableCell(__('ID'));
  266. $cells .= wf_TableCell(__('Address'));
  267. $cells .= wf_TableCell(__('Job type'));
  268. $cells .= wf_TableCell(__('Phone'));
  269. $cells .= wf_TableCell(__('Who should do'));
  270. $cells .= wf_TableCell(__('Worker done'));
  271. $cells .= wf_TableCell(__('Target date'));
  272. $cells .= wf_TableCell(__('Finish date'));
  273. $cells .= wf_TableCell(__('Status'));
  274. $cells .= wf_TableCell(__('Actions'));
  275. $rows = wf_TableRow($cells, 'row1');
  276. foreach ($tasksArray as $io => $each) {
  277. $cells = wf_TableCell($each['id']);
  278. $cells .= wf_TableCell($each['address']);
  279. $cells .= wf_TableCell(@$this->allJobtypes[$each['jobtype']]);
  280. $cells .= wf_TableCell($each['phone']);
  281. $cells .= wf_TableCell(@$this->allEmployee[$each['employee']]);
  282. $cells .= wf_TableCell(@$this->allEmployee[$each['employeedone']]);
  283. $cells .= wf_TableCell($each['startdate'] . ' ' . $each['starttime']);
  284. $cells .= wf_TableCell($each['enddate']);
  285. $cells .= wf_TableCell(web_bool_led($each['status']), '', '', 'sorttable_customkey="' . $each['status'] . '"');
  286. $actLinks = wf_Link(self::URL_TASKVIEW . $each['id'], web_edit_icon(), false);
  287. $cells .= wf_TableCell($actLinks);
  288. $rows .= wf_TableRow($cells, 'row3');
  289. $totalCount++;
  290. }
  291. $result = wf_TableBody($rows, '100%', 0, 'sortable');
  292. $result .= __('Total') . ': ' . $totalCount;
  293. } else {
  294. $messages = new UbillingMessageHelper();
  295. $result = $messages->getStyledMessage(__('Nothing found'), 'warning');
  296. }
  297. return ($result);
  298. }
  299. }
  300. $taskmanSearch = new TaskmanSearch();
  301. show_window(__('Tasks search'), $taskmanSearch->renderSearchForm());
  302. if (wf_CheckPost(array('datefrom', 'dateto'))) {
  303. $searchResults = $taskmanSearch->commonSearch();
  304. show_window(__('Search results'), $taskmanSearch->renderTasks($searchResults));
  305. }
  306. show_window('', wf_BackLink('?module=taskman'));
  307. } else {
  308. show_error(__('You cant control this module'));
  309. }
  310. ?>