/application/controllers/FMController.php

https://github.com/binhtd/fms-portal-source · PHP · 147 lines · 118 code · 29 blank · 0 comment · 16 complexity · e36c495dcebbcd6596ddd425e5e9de00 MD5 · raw file

  1. <?php
  2. class FMController extends Zend_Controller_Action
  3. {
  4. public function indexAction()
  5. {
  6. if (0 <> strlen($this->_getParam("backbutton", ""))){
  7. $this->_redirect($this->_getParam("backurl","/ho/index"));
  8. return;
  9. }
  10. if (0 <> strlen($this->_getParam("Downloadall", ""))){
  11. $this->downloadAll();
  12. return;
  13. }
  14. $auth = Zend_Auth::getInstance();
  15. $this->view->backModuleName = $this->_getParam("download", "ho");
  16. if ($auth->getIdentity()->UserIsClient == "Y"){
  17. $this->view->backModuleName = "ho";
  18. }
  19. $fmMapper = new Application_Model_Mapper_FM();
  20. $files = array();
  21. $directoryPath = $fmMapper->getDirectoryPath($this->_getParam("handoffid", 0), $this->_getParam("download", "ho"));
  22. $handle = opendir($directoryPath);
  23. while ($file = readdir($handle)) {
  24. if ($file == "." || $file == "..") continue;
  25. $files[] = array( "fileinurl" => urlencode(stripslashes($file)), "handoffid" => $this->_getParam("handoffid", 0),
  26. "download" => $this->_getParam("download", "ho"), "filename" =>
  27. ( strlen(stripslashes($file)) > 43) ? substr(stripslashes($file), 0, 40) . '...' : stripslashes($file),
  28. "filesize" => round(filesize($directoryPath. $file)/1024) . " KB",
  29. "createdate" => date ("Y:m:d", filemtime($directoryPath .$file)),
  30. "allowDelete" => $fmMapper->isAllowDelete($this->_getParam("handoffid", 0), $this->_getParam("download", "ho"))
  31. );
  32. }
  33. $this->view->files = $files;
  34. closedir($handle);
  35. }
  36. public function deleteAction(){
  37. $activityMapper = new Application_Model_Mapper_Activity();
  38. $activity = new Application_Model_Activity();
  39. $auth = Zend_Auth::getInstance();
  40. $fmMapper = new Application_Model_Mapper_FM();
  41. if(!$fmMapper->isAllowDelete($this->_getParam("handoffid", 0), $this->_getParam("download", "ho"))){
  42. return $this->_helper->redirector->gotoRoute(array("controller"=> "fm", "action"=> "index", "handoffid" => $this->_getParam("handoffid", 0),
  43. "download" => $this->_getParam("download", "ho")));
  44. }
  45. $directoryPath = $fmMapper->getDirectoryPath($this->_getParam("handoffid", 0), $this->_getParam("download", "ho"));
  46. if (!file_exists($directoryPath . urldecode($this->_getParam("filename", "")))){
  47. throw new Exception("Please select correct file to delete");
  48. }
  49. unlink($directoryPath . urldecode($this->_getParam("filename", "")));
  50. $activity->setUserName($auth->getIdentity()->UserLoginName)
  51. ->setUserActivity("User Delete File " . $directoryPath . urldecode($this->_getParam("filename", "")))
  52. ->setUserActivityDateTime(date("Y-m-d : H:i:s", time()));
  53. $activityMapper->save($activity);
  54. if ( 0 == $fmMapper->updateFileCount($this->_getParam("handoffid", 0), $this->_getParam("download", "ho"))){
  55. return $this->_helper->redirector->gotoRoute(array("controller"=> $this->_getParam("download", "ho"), "action"=> "index"));
  56. }
  57. $this->_helper->redirector->gotoRoute(array("controller"=> "fm", "action"=> "index", "handoffid" => $this->_getParam("handoffid", 0),
  58. "download" => $this->_getParam("download", "ho")));
  59. }
  60. public function downloadAction(){
  61. $activityMapper = new Application_Model_Mapper_Activity();
  62. $activity = new Application_Model_Activity();
  63. $auth = Zend_Auth::getInstance();
  64. $fmMapper = new Application_Model_Mapper_FM();
  65. $directoryPath = $fmMapper->getDirectoryPath($this->_getParam("handoffid", 0), $this->_getParam("download", "ho"));
  66. if (!$fmMapper->isAllowDownload($this->_getParam("handoffid", 0), $this->_getParam("download", "ho"))){
  67. return $this->_helper->redirector->gotoRoute(array("controller"=> "fm", "action"=> "index", "handoffid" => $this->_getParam("handoffid", 0),
  68. "download" => $this->_getParam("download", "ho")));
  69. }
  70. if (!file_exists($directoryPath . $this->_getParam("filename", ""))){
  71. throw new Exception("Please select correct file to download");
  72. }
  73. header("Pragma: public");
  74. header("Expires: 0");
  75. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  76. header("Cache-Control: public");
  77. header("Content-Description: File Transfer");
  78. header("Content-type: application/octet-stream");
  79. header("Content-Transfer-Encoding: binary");
  80. header("Content-Length: " . filesize($directoryPath . $this->_getParam("filename", "")));
  81. header('Content-Disposition: attachment; filename="'.$this->_getParam("filename", "").'"');
  82. ob_end_flush();
  83. readfile($directoryPath . $this->_getParam("filename", ""));
  84. $activity->setUserName($auth->getIdentity()->UserLoginName)
  85. ->setUserActivity("User download File " . $directoryPath . $this->_getParam("filename", ""))
  86. ->setUserActivityDateTime(date("Y-m-d : H:i:s", time()));
  87. $activityMapper->save($activity);
  88. die();
  89. }
  90. private function downloadAll(){
  91. $zip = new ZipArchive();
  92. $fileName = sprintf("%s_ALL.zip", date("Y_m_d", time()));
  93. $fmMapper = new Application_Model_Mapper_FM();
  94. $directoryPath = $fmMapper->getDirectoryPath($this->_getParam("handoffid", 0), $this->_getParam("download", "ho"));
  95. $listFileNames = $fmMapper->getListFileInDirectory($directoryPath);
  96. if ($zip->open($directoryPath.$fileName, ZIPARCHIVE::CREATE )!==TRUE) {
  97. throw new Exception("cannot open zip file");
  98. }
  99. foreach($listFileNames as $files){
  100. $zip->addFile($directoryPath.$files, $files);
  101. }
  102. $zip->close();
  103. header("Content-Disposition: attachment; filename=\"" .$fileName."\"");
  104. header("Pragma: public");
  105. header("Expires: 0");
  106. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  107. header("Cache-Control: public");
  108. header("Content-Description: File Transfer");
  109. header("Content-type: application/octet-stream");
  110. header("Content-Transfer-Encoding: binary");
  111. header("Content-Length: ".filesize($directoryPath.$fileName));
  112. header("Accept-Ranges: ".filesize($directoryPath.$fileName));
  113. ob_end_flush();
  114. readfile($directoryPath.$fileName);
  115. unlink($directoryPath.$fileName);
  116. die();
  117. }
  118. }