PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/module/Indicator/src/Indicator/Controller/IndicatorController.php

https://gitlab.com/mnomansheikh/ampuz
PHP | 309 lines | 235 code | 43 blank | 31 comment | 34 complexity | 407c52184e8b84f2e448a41a01f3fcd9 MD5 | raw file
  1. <?php
  2. namespace Indicator\Controller;
  3. use Zend\Mvc\Controller\AbstractActionController;
  4. use Zend\View\Model\ViewModel;
  5. use Indicator\Model\IndicatorTable;
  6. use Zend\Db\ResultSet\ResultSet;
  7. use Zend\Db\TableGateway\TableGateway;
  8. use Indicator\Model\Indicator;
  9. use Indicator\Form\IndicatorForm;
  10. use Zend\Validator\File\Size;
  11. use Zend\Session\Container;
  12. class IndicatorController extends AbstractActionController
  13. {
  14. protected $indicatorTable;
  15. protected $authservice;
  16. protected $IndicatorTable;
  17. public function getIndicatorTable()
  18. {
  19. if (!$this->IndicatorTable) {
  20. $sm = $this->getServiceLocator();
  21. $this->IndicatorTable = $sm->get('Indicator\Model\IndicatorTable');
  22. }
  23. return $this->IndicatorTable;
  24. }
  25. public function getAuthService()
  26. {
  27. if (!$this->authservice) {
  28. $this->authservice = $this->getServiceLocator()
  29. ->get('AuthService');
  30. }
  31. return $this->authservice;
  32. }
  33. public function indexAction()
  34. {
  35. //Check user is logged in
  36. if ($this->getAuthService()->hasIdentity()) {
  37. $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
  38. $form = new IndicatorForm($dbAdapter);
  39. $form->get('submit')->setValue('Request Indicator');
  40. $userSession = new Container('user');
  41. // echo 'Logged in as ' . $userSession->user_type;
  42. //For admin user
  43. $_SESSION['category'] = "community";
  44. $_SESSION['area'] = "indicator";
  45. if ($userSession->user_type == _ADMIN_) {
  46. // grab the paginator from the CommunityTable
  47. $paginator = $this->getIndicatorTable()->fetchAll_admin($dbAdapter, true);
  48. // set the current page to what has been passed in query string, or to 1 if none set
  49. $paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page', 1));
  50. // set the number of items per page to 10
  51. $paginator->setItemCountPerPage(10);
  52. $view = new ViewModel(array(
  53. 'paginator' => $paginator,
  54. 'form' => $form
  55. ));
  56. $view->setTemplate('indicator/index');
  57. return $view;
  58. }
  59. } else {
  60. return $this->redirect()->toRoute('login');
  61. }
  62. }
  63. public function addAction()
  64. {
  65. //Check user is logged in
  66. if ($this->getAuthService()->hasIdentity()) {
  67. $userSession = new Container('user');
  68. // echo 'Logged in as ' . $userSession->user_type;
  69. //For admin user
  70. $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
  71. if ($userSession->user_type == _ADMIN_) {
  72. $form = new IndicatorForm($dbAdapter);
  73. $form->get('submit')->setValue('Save');
  74. $form->get('cancel')->setValue('Cancel');
  75. $request = $this->getRequest();
  76. if ($request->isPost()) {
  77. $indicator = new Indicator();
  78. $form->setInputFilter($indicator->getInputFilter());
  79. $data = array_merge_recursive(
  80. $this->getRequest()->getPost()->toArray(),
  81. $this->getRequest()->getFiles()->toArray()
  82. );
  83. foreach($data['field_name'] AS $fieldname){
  84. $indicator->field_name[] = (!empty($fieldname)) ? $fieldname : null;
  85. }
  86. $form->setData($request->getPost());
  87. $form->setData($data);
  88. if ($form->isValid()) {
  89. $indicator->exchangeArray($form->getData());
  90. $this->getIndicatorTable()->saveIndicator($indicator, $dbAdapter);
  91. // Redirect to list of albums
  92. return $this->redirect()->toRoute('indicator', array(
  93. 'action' => 'index'
  94. ));
  95. }
  96. }
  97. //return array('form' => $form, 'indicator' => $this->getIndicatorTable()->fetchAll($dbAdapter));
  98. $view = new ViewModel(array(
  99. 'form' => $form, 'indicator' => $this->getIndicatorTable()->fetchAll($dbAdapter)
  100. ));
  101. $view->setTemplate('indicator/add');
  102. return $view;
  103. }
  104. if ($userSession->user_type == _CHAMPION_) {
  105. return $this->redirect()->toRoute('application');
  106. }
  107. } else {
  108. return $this->redirect()->toRoute('login');
  109. }
  110. }
  111. public function editAction()
  112. {
  113. //Check user is logged in
  114. if ($this->getAuthService()->hasIdentity()) {
  115. $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
  116. $id = (int)$this->params()->fromRoute('id', 0);
  117. try {
  118. $indicator = $this->getIndicatorTable()->getIndicator($id);
  119. } catch (\Exception $ex) {
  120. return $this->redirect()->toRoute('indicator', array(
  121. 'action' => 'index'
  122. ));
  123. }
  124. $form = new IndicatorForm($dbAdapter);
  125. $form->bind($indicator);
  126. //for session varibles
  127. $userSession = new Container('user');
  128. // echo 'Logged in as ' . $userSession->user_type;
  129. //For admin user
  130. $form->get('cancel')->setValue('Cancel');
  131. if ($userSession->user_type == _ADMIN_) {
  132. $form->get('submit')->setAttribute('value', 'Update & Approval');
  133. }
  134. if ($userSession->user_type == _CHAMPION_) {
  135. $form->get('submit')->setAttribute('value', 'Update');
  136. }
  137. $request = $this->getRequest();
  138. if ($request->isPost()) {
  139. $form->setInputFilter($indicator->getInputFilter());
  140. $data = array_merge_recursive(
  141. $this->getRequest()->getPost()->toArray(),
  142. $this->getRequest()->getFiles()->toArray()
  143. );
  144. foreach($data['field_name'] AS $fieldname){
  145. $indicator->field_name[] = (!empty($fieldname)) ? $fieldname : null;
  146. }
  147. $form->setData($request->getPost());
  148. $form->setData($data);
  149. if ($form->isValid()) {
  150. $this->getIndicatorTable()->saveIndicator($indicator, $dbAdapter);
  151. // Redirect to list of Community
  152. return $this->redirect()->toRoute('indicator');
  153. }
  154. }
  155. $indicator_details = $this->getIndicatorTable()->fetchAll($dbAdapter, $id);
  156. $indicator = $this->getIndicatorTable()->fetchAll($dbAdapter, $id);
  157. return array(
  158. 'id' => $id,
  159. 'form' => $form,
  160. 'indicator_details' => $indicator_details,
  161. 'indicator' => $indicator,
  162. );
  163. } else {
  164. return $this->redirect()->toRoute('login');
  165. }
  166. }
  167. public function deleteAction()
  168. {
  169. $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
  170. $id = (int)$this->params()->fromRoute('id', 0);
  171. if (!$id) {
  172. return $this->redirect()->toRoute('indicator');
  173. }
  174. if ($id) {
  175. $this->getIndicatorTable()->deleteIndicator($id, $dbAdapter);
  176. return $this->redirect()->toRoute('indicator');
  177. }
  178. }
  179. public function downloadAction()
  180. {
  181. $str = $this->params()->fromRoute('str', 0);
  182. $id = $this->params()->fromRoute('id', 0);
  183. $str = trim($str);
  184. if ($id == 0) {
  185. $file = 'sampleformat/'.$str;
  186. } else {
  187. $file = 'sampleformat/'.$str;
  188. }
  189. $response = new \Zend\Http\Response\Stream();
  190. $response->setStream(fopen($file, 'r'));
  191. $response->setStatusCode(200);
  192. $response->setStreamName(basename($file));
  193. $headers = new \Zend\Http\Headers();
  194. $headers->addHeaders(array(
  195. 'Content-Disposition' => 'attachment; filename="' . basename($file) .'"',
  196. 'Content-Type' => 'application/octet-stream',
  197. 'Content-Length' => filesize($file),
  198. 'Expires' => '@0', // @0, because zf2 parses date as string to \DateTime() object
  199. 'Cache-Control' => 'must-revalidate',
  200. 'Pragma' => 'public'
  201. ));
  202. $response->setHeaders($headers);
  203. return $response;
  204. }
  205. public function Graph1Action()
  206. {
  207. $fromyear = '';
  208. $toyear = '';
  209. $indicator_desc = '';
  210. $indicator = '';
  211. $company_id = '';
  212. if ($_REQUEST) {
  213. extract($_REQUEST);
  214. $fromyear;
  215. $toyear;
  216. $indicator;
  217. $company_id;
  218. }
  219. //Check user is logged in
  220. if ($this->getAuthService()->hasIdentity()) {
  221. //for session varibles
  222. $userSession = new Container('user');
  223. // echo 'Logged in as ' . $userSession->user_type;
  224. //For admin user
  225. $dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
  226. if ($userSession->user_type == _ADMIN_) {
  227. // grab the paginator from the CommunityTable
  228. $paginator = $this->getIndicatorTable()->fetchAll_admin($dbAdapter, true);
  229. // set the current page to what has been passed in query string, or to 1 if none set
  230. $paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page', 1));
  231. // set the number of items per page to 10
  232. $paginator->setItemCountPerPage(10);
  233. $GData = $this->getIndicatorTable()->fetchGraphData($dbAdapter, $fromyear, $toyear, $company_id, $indicator_desc);
  234. //------------------------------ BAR Chart --------------------------------------
  235. // grab the paginator from the CommunityTable
  236. $principle = $this->getIndicatorTable()->fetchAll_principle($dbAdapter, true);
  237. // set the current page to what has been passed in query string, or to 1 if none set
  238. $principle->setCurrentPageNumber((int)$this->params()->fromQuery('page', 1));
  239. // set the number of items per page to 10
  240. $principle->setItemCountPerPage(10);
  241. //------------------------------ PIE Chart --------------------------------------
  242. // grab the paginator from the CommunityTable
  243. $principle_pie = $this->getIndicatorTable()->fetchAll_principle($dbAdapter, true);
  244. // set the current page to what has been passed in query string, or to 1 if none set
  245. $principle_pie->setCurrentPageNumber((int)$this->params()->fromQuery('page', 1));
  246. // set the number of items per page to 10
  247. $principle_pie->setItemCountPerPage(10);
  248. $view = new ViewModel(array(
  249. 'GData' => $GData,
  250. 'indicator' => $indicator,
  251. 'paginator' => $paginator,
  252. 'indicator_desc' => $indicator_desc,
  253. 'principle' => $principle,
  254. 'principle_pie' => $principle_pie,
  255. ));
  256. $view->setTemplate('indicator/analytic');
  257. return $view;
  258. }
  259. if ($userSession->user_type == _CHAMPION_) {
  260. return $this->redirect()->toRoute('application');
  261. }
  262. } else {
  263. return $this->redirect()->toRoute('login');
  264. }
  265. }
  266. }