Advertisement
Guest User

habib search mom controller

a guest
Jul 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.51 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Controller;
  4.  
  5. use \Norm\Controller\NormController;
  6. use Norm\Norm;
  7. use App\Library\Notification;
  8.  
  9. class MomController extends AppController
  10. {
  11. public function mapRoute()
  12. {
  13. parent::mapRoute();
  14.  
  15. $this->map('/null/by_ccd', 'mom_by_ccd')->via('GET', 'POST');
  16. $this->map('/null/by_date', 'mom_by_date')->via('GET', 'POST');
  17. $this->map('/null/dashboard', 'mom_dashboard')->via('GET', 'POST');
  18. $this->map('/null/dashboard_detail/:id', 'mom_dashboard_detail')->via('GET', 'POST');
  19. }
  20.  
  21. public function create () {
  22. $entry = $this->collection->newInstance()->set($this->getCriteria());
  23. $participant = Norm::factory('MomParticipant')->newInstance();
  24. $sys = Norm::factory('Sysparam')->find(array('groups' => 'Project Status'));
  25. $worksheet = Norm::factory('Worksheet');
  26.  
  27.  
  28. $this->data['entry'] = $entry;
  29. $this->data['participant'] = $participant;
  30. $this->data['sys'] = $sys;
  31.  
  32. if ($this->request->isPost()) {
  33. try {
  34. $post = $this->request->getBody();
  35.  
  36. $dataParticipant = array();
  37. foreach ($post['participant'] as $key => $participant) {
  38. $dataParticipant[$participant['name']] = $participant;
  39. }
  40.  
  41. foreach ($post['project'] as $key => $value) {
  42. $entrys = Norm::factory('Mom')->newInstance();
  43.  
  44. $getId = $worksheet->findOne(array('change_control_number' => $value['project']));
  45.  
  46. $dec = '';
  47. if (isset($value['decision'])) {
  48. $dec = $value['decision'];
  49. }
  50. $entrys->set('date', $post['date']);
  51. $entrys->set('factory', $post['factory']);
  52. $entrys->set('project', $value['project']);
  53. $entrys->set('id_worksheet', $getId['$id']);
  54. $entrys->set('notes', $value['notes']);
  55. $entrys->set('decision', $dec);
  56. $entrys->save();
  57. # code...
  58. foreach ($dataParticipant as $key => $value) {
  59. $dataSave = Norm::factory('MomParticipant')->newInstance();
  60. $position = Norm::factory('User')->findOne(array('id' => $value['name']));
  61. $dataSave->set('id_mom', $entrys->getId());
  62. $dataSave->set('role', $value['role']);
  63. $dataSave->set('name', $value['name']);
  64. $dataSave->set('position', $position['position']);
  65. $dataSave->save();
  66. }
  67. }
  68.  
  69.  
  70. h('notification.info', $this->clazz.' created.');
  71.  
  72. h('controller.create.success', array(
  73. 'model' => $entrys
  74. ));
  75. } catch (Stop $e) {
  76. throw $e;
  77. } catch (Exception $e) {
  78. // no more set notification.error since notificationmiddleware will
  79. // write this later
  80. // h('notification.error', $e);
  81.  
  82. h('controller.create.error', array(
  83. 'model' => $entry,
  84. 'error' => $e,
  85. ));
  86.  
  87. // rethrow error to make sure notificationmiddleware know what todo
  88. throw $e;
  89. }
  90. }
  91. }
  92.  
  93. public function update($id)
  94. {
  95. try {
  96. $entry = $this->collection->findOne($id);
  97. $participant = Norm::factory('MomParticipant')->find(array('id_mom' => $id));
  98. $sys = Norm::factory('Sysparam')->find(array('groups' => 'Project Status'));
  99. $role = Norm::factory('Role')->find();
  100. $user = Norm::factory('User');
  101. $ccm = Norm::factory('Worksheet');
  102.  
  103. } catch (Exception $e) {
  104. // noop
  105. }
  106.  
  107. if (is_null($entry)) {
  108. return $this->app->notFound();
  109. }
  110.  
  111. if ($this->request->isPost() || $this->request->isPut()) {
  112. try {
  113. $merged = array_merge(
  114. isset($entry) ? $entry->dump() : array(),
  115. $this->request->getBody() ?: array()
  116. );
  117.  
  118. $id_worksheet = $ccm->findOne(array('change_control_number' => $merged['project'][0]['project']));
  119.  
  120. $is_decision = '';
  121. if (isset($merged['project'][0]['decision'])) {
  122. $is_decision = $merged['project'][0]['decision'];
  123. }
  124.  
  125. $entry->set('date', $merged['date']);
  126. $entry->set('factory', $merged['factory']);
  127. $entry->set('project', $merged['project'][0]['project']);
  128. $entry->set('notes', $merged['project'][0]['notes']);
  129. $entry->set('decision', $is_decision);
  130. $entry->set('id_worksheet', $id_worksheet['$id']);
  131. $entry->save();
  132.  
  133. foreach ($participant as $key => $value) {
  134. $value->remove();
  135. }
  136.  
  137. foreach ($merged['participant'] as $key => $value) {
  138. $new_participant = Norm::factory('MomParticipant')->newInstance();
  139. $position = $user->findOne(array('id' => $value['name']));
  140. $new_participant->set('id_mom', $entry->getId());
  141. $new_participant->set('role', $value['role']);
  142. $new_participant->set('name', $value['name']);
  143. $new_participant->set('position', $position['position']);
  144. $new_participant->save();
  145. }
  146.  
  147.  
  148. h('notification.info', $this->clazz.' updated');
  149. $this->app->redirect('mom');
  150. h('controller.update.success', array(
  151. 'model' => $entry,
  152. ));
  153. } catch (Stop $e) {
  154. throw $e;
  155. } catch (Exception $e) {
  156. h('notification.error', $e);
  157.  
  158. if (empty($entry)) {
  159. $model = null;
  160. }
  161.  
  162. h('controller.update.error', array(
  163. 'error' => $e,
  164. 'model' => $entry,
  165. ));
  166. }
  167. }
  168.  
  169. $this->data['entry'] = $entry;
  170. $this->data['participant'] = $participant;
  171. $this->data['sys'] = $sys;
  172. $this->data['role'] = $role;
  173. $this->data['user'] = $user;
  174. }
  175.  
  176. public function delete($id)
  177. {
  178. $id = explode(',', $id);
  179.  
  180.  
  181. if ($this->request->isPost() || $this->request->isDelete()) {
  182. $single = false;
  183. if (count($id) === 1) {
  184. $single = true;
  185. }
  186.  
  187. try {
  188. $this->data['entries'] = array();
  189.  
  190. foreach ($id as $value) {
  191. $model = $this->collection->findOne($value);
  192.  
  193. if (is_null($model)) {
  194. if ($single) {
  195. $this->app->notFound();
  196. }
  197.  
  198. continue;
  199. }
  200.  
  201. $model->remove();
  202.  
  203. $data_participant = Norm::factory('MomParticipant')->find(array('id_mom' => $value));
  204. foreach ($data_participant as $key_rm => $value_rm) {
  205. $value_rm->remove();
  206. }
  207.  
  208. $this->data['entries'][] = $model;
  209. }
  210.  
  211. h('notification.info', $this->clazz.' deleted.');
  212.  
  213. h('controller.delete.success', array(
  214. 'models' => $this->data['entries'],
  215. ));
  216.  
  217. } catch (Stop $e) {
  218. throw $e;
  219. } catch (Exception $e) {
  220. h('notification.error', $e);
  221.  
  222. if (empty($model)) {
  223. $model = null;
  224. }
  225.  
  226. h('controller.delete.error', array(
  227. 'error' => $e,
  228. 'model' => $model,
  229. ));
  230. }
  231.  
  232. }
  233. }
  234.  
  235. public function mom_by_ccd () {
  236.  
  237. $criteria = $this->app->request->get();
  238. $group = ' GROUP BY m.project';
  239. $limit = ' LIMIT 10';
  240. $sort = ' ORDER BY m.id_worksheet DESC';
  241. $db = 'SELECT m.project FROM mom m';
  242.  
  243. if (isset($criteria['!search'])) {
  244. $data = $this->rowsArray($db . ' WHERE m.project LIKE ?' . $group . $sort .$limit, array('%'.$criteria['!search'].'%'));
  245. } else {
  246. $data = $this->rowsArray($db . $group . $sort . $limit);
  247. }
  248. $mom = Norm::factory('Mom');
  249. $mom_participant = Norm::factory('MomParticipant');
  250. $user = Norm::factory('User');
  251. $sysparam = Norm::factory('Sysparam');
  252. $worksheet = Norm::factory('Worksheet');
  253.  
  254. $all_data = array();
  255. foreach ($data as $key => $value) {
  256. $dt_array = array();
  257.  
  258. $stts = $worksheet->findOne(array('change_control_number' => $value['project']));
  259. $param = $sysparam->findOne(array('groups' => 'Approval Status', 'key' => $stts['approval_status']));
  260.  
  261. foreach ($value as $key_a => $value_a) {
  262. $dt_mom = $mom->find();
  263. foreach ($dt_mom as $key_mo => $value_mom) {
  264. if ($value_mom['project'] == $value_a) {
  265. $dc = $sysparam->findOne(array('groups' => 'Project Status', 'key' => $value_mom['decision']));
  266. $dt_array[$key_mo]['date'] = date('d-M-Y', strtotime($value_mom['date']));
  267. $dt_array[$key_mo]['mom'] = $value_mom['notes'];
  268. $dt_array[$key_mo]['decission'] = $dc['value'];
  269. $dt_array[$key_mo]['status'] = $param['value'];
  270.  
  271. $participant = $mom_participant->find(array('id_mom' => $value_mom['$id']));
  272. $dt_part = array();
  273. foreach ($participant as $key_part => $value_part) {
  274. $name = $user->findOne(array('id' => $value_part['name']));
  275. $dt_part[] = $name['first_name'].' '.$name['last_name'];
  276. $dt_array[$key_mo]['participant'] = $dt_part;
  277. }
  278. }
  279. }
  280. }
  281. $all_data[$value['project']] = $dt_array;
  282. }
  283.  
  284. $display_not_found = 'none';
  285. $display_found = '';
  286.  
  287. if (count($all_data) == 0) {
  288. $display_not_found = '';
  289. $display_found = 'none';
  290. }
  291.  
  292. $this->data['id_worksheet'] = $worksheet;
  293. $this->data['display_not_found'] = $display_not_found;
  294. $this->data['display_found'] = $display_found;
  295. $this->data['data'] = $all_data;
  296. $this->data['is_admin'] = $this->app->auth_ccm->isAdmin();
  297. $this->data['is_owner'] = $this->app->auth_ccm->isChangeOwner();
  298. $this->data['is_approver'] = $this->app->auth_ccm->isApprover();
  299. }
  300.  
  301. public function mom_by_date () {
  302.  
  303. $criteria = $this->app->request->get();
  304. $group = ' GROUP BY m.date';
  305. $sort = ' ORDER BY m.date DESC';
  306. $limit = ' LIMIT 5';
  307. $db = 'SELECT m.date, m.factory FROM mom m';
  308.  
  309. $cari = array();
  310. if (isset($criteria['factory']) || isset($criteria['date'])) {
  311. $search = ' WHERE m.factory LIKE ? AND m.date LIKE ?';
  312. $cr = array('%'.$criteria['factory'].'%', '%'.date('Y-m-d', strtotime($criteria['date'])).'%');
  313. if ($criteria['factory'] == '' && $criteria['date'] == '') {
  314. $data = $this->rowsArray($db . $group . $sort . $limit);
  315. } else if ($criteria['factory'] != '' && $criteria['date'] != ''){
  316. $cari = array('factory' => $criteria['factory'], 'date' => date('Y-m-d', strtotime($criteria['date'])));
  317. $data = $this->rowsArray($db . ' WHERE m.factory ='. $criteria['factory'] . $group . $sort . $limit);
  318. } else {
  319. $cr = array();
  320. if ($criteria['factory'] != '') {
  321. $search = ' WHERE m.factory LIKE ?';
  322. $cr[] = '%'.$criteria['factory'].'%';
  323. $cari = array('factory' => $criteria['factory']);
  324. }
  325. if ($criteria['date'] != '') {
  326. $search = ' WHERE m.date LIKE ?';
  327. $cr[] = '%'.date('Y-m-d', strtotime($criteria['date'])).'%';
  328. }
  329.  
  330. $data = $this->rowsArray($db . $search . $group . $sort . $limit, $cr);
  331. }
  332. } else {
  333. $data = $this->rowsArray($db . $group. $sort . $limit);
  334. }
  335.  
  336. $mom = Norm::factory('Mom');
  337. $mom_participant = Norm::factory('MomParticipant');
  338. $user = Norm::factory('User');
  339. $sysparam = Norm::factory('Sysparam');
  340. $factory = Norm::factory('Site')->find();
  341. $worksheet = Norm::factory('Worksheet');
  342.  
  343. $all_data = array();
  344. foreach ($data as $key => $value) {
  345. $dt_array = array();
  346.  
  347. foreach ($value as $key_a => $value_a) {
  348. $dt_mom = $mom->find($cari);
  349. foreach ($dt_mom as $key_mo => $value_mom) {
  350. if (date('Y-m-d', strtotime($value_mom['date'])) == date('Y-m-d', strtotime($value_a))) {
  351. $stts = $worksheet->findOne(array('change_control_number' => $value_mom['project']));
  352. $param = $sysparam->findOne(array('groups' => 'Approval Status', 'key' => $stts['approval_status']));
  353. $dc = $sysparam->findOne(array('groups' => 'Project Status', 'key' => $value_mom['decision']));
  354. $dt_array[$key_mo]['id'] = $value_mom['id_worksheet'];
  355. $dt_array[$key_mo]['ccd'] = $value_mom['project'];
  356. $dt_array[$key_mo]['date'] = date('d-M-Y', strtotime($value_mom['date']));
  357. $dt_array[$key_mo]['mom'] = $value_mom['notes'];
  358. $dt_array[$key_mo]['decission'] = $dc['value'];
  359. $dt_array[$key_mo]['status'] = $param['value'];
  360.  
  361. $participant = $mom_participant->find(array('id_mom' => $value_mom['$id']));
  362. $dt_part = array();
  363. foreach ($participant as $key_part => $value_part) {
  364. $name = $user->findOne(array('id' => $value_part['name']));
  365. $dt_part[] = $name['first_name'].' '.$name['last_name'];
  366. $dt_array[$key_mo]['participant'] = $dt_part;
  367. }
  368. }
  369. }
  370. }
  371. $all_data[date('Y-m-d', strtotime($value['date']))] = $dt_array;
  372. }
  373.  
  374. $display_not_found = 'none';
  375. $display_found = '';
  376. foreach ($all_data as $key => $value) {
  377. if (count($value) == 0) {
  378. $display_not_found = '';
  379. $display_found = 'none';
  380. }
  381. }
  382.  
  383. if (count($all_data) == 0) {
  384. $display_not_found = '';
  385. $display_found = 'none';
  386. }
  387.  
  388. $this->data['display_not_found'] = $display_not_found;
  389. $this->data['display_found'] = $display_found;
  390. $this->data['all_data'] = $all_data;
  391. $this->data['factory'] = $factory;
  392. }
  393.  
  394. public function mom_dashboard () {
  395. $mom = Norm::factory('Mom');
  396. $user = Norm::factory('User');
  397. $participant = Norm::factory('MomParticipant');
  398. $site = Norm::factory('Site')->find();
  399. $siteId = Norm::factory('Site');
  400. $dataMom = $mom->find()->limit(5)->sort(array('id_worksheet' => -1));
  401.  
  402. $allData = array();
  403. foreach ($site as $key => $value) {
  404. $get = $this->app->request->get();
  405. $criteria = array_filter($get);
  406.  
  407. if (empty($criteria)) {
  408. $criteria = array(
  409. 'factory' => $value['$id'],
  410. 'date!like' => date('Y')
  411. );
  412. } else {
  413. if (isset($criteria['start'])) {
  414. unset($criteria['start']);
  415. $criteria = array_merge($criteria, array('factory' => $value['$id'], 'date!gte' => date('Y-m-d', strtotime($get['start']))));
  416. }
  417.  
  418. if (isset($criteria['end'])) {
  419. $criteria = array_merge($criteria, array('factory' => $value['$id'], 'date!lte' => date('Y-m-d', strtotime($get['end']))));
  420. unset($criteria['end']);
  421. }
  422.  
  423. }
  424.  
  425. $data = $mom->find($criteria);
  426. $arr = array();
  427. foreach ($data as $k => $val) {
  428. $part = $participant->find(array('id_mom' => $val['$id'], 'role' => 3));
  429. foreach ($part as $key_part => $value_part) {
  430. $usr = $user->findOne(array('id' => $value_part['name']));
  431.  
  432. if ($usr) {
  433. $arr[$usr['$id']][] = $value_part['name'];
  434. }
  435. }
  436. }
  437. $allData[$value['name']] = $arr;
  438. }
  439.  
  440. $all = array();
  441. foreach ($allData as $key => $value) {
  442. $hasil = array();
  443. foreach ($value as $k => $val) {
  444. $hasil[$k] = count($val);
  445. }
  446. $all[$key] = $hasil;
  447. }
  448.  
  449. $allDataChart = array();
  450. foreach ($all as $key => $value) {
  451. $tes = array();
  452. foreach ($value as $k => $val) {
  453. $name = $user->findOne(array('id' => $k));
  454. $tes[$k]['name'] = '<b>' . $name['position'].'</b><br>'. $name['first_name'].' '.$name['last_name'];
  455. $tes[$k]['jml'] = $val;
  456. }
  457. $allDataChart[$key] = $tes;
  458. }
  459.  
  460. $this->data['mom'] = $dataMom;
  461. $this->data['site'] = $site;
  462. $this->data['siteId'] = $siteId;
  463. $this->data['dataChart'] = $allDataChart;
  464.  
  465. $this->data['is_admin'] = $this->app->auth_ccm->isAdmin();
  466. $this->data['is_pcm'] = $this->app->auth_ccm->isPCM();
  467. $this->data['is_owner'] = $this->app->auth_ccm->isChangeOwner();
  468. $this->data['is_approver'] = $this->app->auth_ccm->isApprover();
  469. }
  470.  
  471. public function mom_dashboard_detail ($id) {
  472. $mom = Norm::factory('Mom');
  473. $user = Norm::factory('User');
  474. $participant = Norm::factory('MomParticipant');
  475. $site = Norm::factory('Site')->findOne($id);
  476.  
  477. $colors = array(
  478. '1' => array('#54CDF2'),
  479. '2' => array('#F2A354'),
  480. '3' => array('#F254CD'),
  481. '4' => array('#54F254'),
  482. );
  483.  
  484. $criteria = array(
  485. 'factory' => $site['$id'],
  486. 'date!like' => date('Y')
  487. );
  488.  
  489. if (isset($_GET['start']) || isset($_GET['end'])) {
  490. $criteria = array(
  491. 'factory' => $site['$id'],
  492. 'date!gte' => date('Y-m-d', strtotime($_GET['start'])),
  493. 'date!lte' => date('Y-m-d', strtotime($_GET['end']))
  494. );
  495. }
  496.  
  497. $allData = array();
  498. $data = $mom->find($criteria);
  499. $arr = array();
  500. foreach ($data as $k => $val) {
  501. $part = $participant->find(array('id_mom' => $val['$id'], 'role' => 3));
  502. foreach ($part as $key_part => $value_part) {
  503. $usr = $user->findOne(array('id' => $value_part['name']));
  504.  
  505. if ($usr) {
  506. $arr[$usr['$id']][] = $value_part['name'];
  507. }
  508. }
  509. }
  510. $allData[$site['name']] = $arr;
  511.  
  512. $all = array();
  513. foreach ($allData as $key => $value) {
  514. $hasil = array();
  515. foreach ($value as $k => $val) {
  516. $hasil[$k] = count($val);
  517. }
  518. $all[$key] = $hasil;
  519. }
  520.  
  521. $allDataChart = array();
  522. foreach ($all as $key => $value) {
  523. $tes = array();
  524. foreach ($value as $k => $val) {
  525. $name = $user->findOne(array('id' => $k));
  526. $tes[$k]['name'] = $name['position'].'<br>'. $name['first_name'].' '.$name['last_name'];
  527. $tes[$k]['jml'] = $val;
  528. }
  529. $allDataChart[$key] = $tes;
  530. }
  531.  
  532. $this->data['site'] = $site;
  533. $this->data['colors'] = $colors[$id];
  534. $this->data['dataChart'] = $allDataChart;
  535. }
  536.  
  537. private function rowsArray($sql, $params = array())
  538. {
  539. $connection = \Norm::getConnection('mysql')->getRaw();
  540. $statement = $connection->prepare($sql);
  541.  
  542. if (empty($params)) {
  543. $statement->execute();
  544. } else {
  545. $statement->execute($params);
  546. }
  547.  
  548. $results = $statement->fetchAll(\PDO::FETCH_ASSOC);
  549.  
  550. return $results;
  551. }
  552.  
  553. private function rowArray($sql, $params = array())
  554. {
  555. $connection = \Norm::getConnection('mysql')->getRaw();
  556. $statement = $connection->prepare($sql);
  557.  
  558. if (empty($params)) {
  559. $statement->execute();
  560. } else {
  561. $statement->execute($params);
  562. }
  563.  
  564. $result = $statement->fetch(\PDO::FETCH_ASSOC);
  565.  
  566. return $result;
  567. }
  568. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement