Пример #1
0
 public function problemsAction()
 {
     if (!$this->checkLogin()) {
         return;
     }
     if ($this->request->isPost() && $this->request->hasPost("type") && $this->security->checkToken()) {
         $this->session->set("changeOccurred", true);
         $this->session->set("changeSuccessful", true);
         $this->session->set("teamsGenerated", false);
         switch ($this->request->getPost("type")) {
             case 'update':
                 $problem = Problems::findFirst(intval($this->request->getPost("id")));
                 if ($problem) {
                     $problem->setName($this->request->getPost("name"));
                     $problem->setRuntime($this->request->getPost("runtime"));
                     if ($this->request->hasPost("hasDataFile")) {
                         if ($this->request->hasFiles()) {
                             // Adding data file
                             foreach ($this->request->getUploadedFiles() as $file) {
                                 if ($file->getKey() == "dataFile") {
                                     unlink($problem->getDataFile());
                                     $dataFileLocation = "../app/data/" . Phalcon\Text::random(Phalcon\Text::RANDOM_ALNUM, rand(13, 17));
                                     $problem->setDataFile($dataFileLocation);
                                     $problem->setDataFileFriendly($file->getName());
                                     $file->moveTo($dataFileLocation);
                                 }
                             }
                         }
                     } else {
                         if ($problem->getDataFile() != null) {
                             // Removing data file
                             unlink($problem->getDataFile());
                             $problem->setDataFile(null);
                             $problem->setDataFileFriendly(null);
                         }
                     }
                     if ($this->request->hasPost("hasJudgeFile")) {
                         if ($this->request->hasFiles()) {
                             // Adding judge file
                             foreach ($this->request->getUploadedFiles() as $file) {
                                 if ($file->getKey() == "judgeFile") {
                                     unlink($problem->getJudgeFile());
                                     $judgeFileLocation = "../app/data/" . Phalcon\Text::random(Phalcon\Text::RANDOM_ALNUM, rand(13, 17));
                                     $problem->setJudgeFile($judgeFileLocation);
                                     $problem->setJudgeFileFriendly($file->getName());
                                     $file->moveTo($judgeFileLocation);
                                 }
                             }
                         }
                     } else {
                         // Removing judge file
                         if ($problem->getJudgeFile() != null) {
                             unlink($problem->getJudgeFile());
                             $problem->setJudgeFile(null);
                             $problem->setJudgeFileFriendly(null);
                         }
                     }
                     if (!$problem->save()) {
                         $this->session->set("changeSuccessful", false);
                     }
                 } else {
                     $this->session->set("changeSuccessful", false);
                 }
                 break;
             case 'create':
                 $error = false;
                 $problem = new Problems();
                 $problem->setName($this->request->getPost("name"));
                 $problem->setRuntime($this->request->getPost("runtime"));
                 if ($this->request->hasFiles()) {
                     foreach ($this->request->getUploadedFiles() as $file) {
                         if ($file->getKey() == "dataFile" && $this->request->hasPost("hasDataFile")) {
                             $dataFileLocation = "../app/data/" . Phalcon\Text::random(Phalcon\Text::RANDOM_ALNUM, rand(13, 17));
                             $problem->setDataFile($dataFileLocation);
                             $problem->setDataFileFriendly($file->getName());
                             $file->moveTo($dataFileLocation);
                         }
                         if ($file->getKey() == "judgeFile" && $this->request->hasPost("hasJudgeFile")) {
                             $judgeFileLocation = "../app/data/" . Phalcon\Text::random(Phalcon\Text::RANDOM_ALNUM, rand(13, 17));
                             $problem->setJudgeFile($judgeFileLocation);
                             $problem->setJudgeFileFriendly($file->getName());
                             $file->moveTo($judgeFileLocation);
                         }
                     }
                 }
                 if ($error || !$problem->save()) {
                     $this->session->set("changeSuccessful", false);
                 }
                 break;
             case 'delete':
                 $problem = Problems::findFirst(intval($this->request->getPost("id")));
                 $dataLocation = $problem->getDataFile();
                 $judgeLocation = $problem->getJudgeFile();
                 if ($problem->delete()) {
                     unlink($dataLocation);
                     unlink($judgeLocation);
                 } else {
                     $this->session->set("changeSuccessful", false);
                 }
                 break;
             default:
                 $this->session->set("changeSuccessful", false);
                 break;
         }
         return $this->response->redirect("/admin/problems");
     } else {
         if ($this->session->has("changeOccurred")) {
             $this->view->changeOccurred = $this->session->get("changeOccurred");
             $this->session->remove("changeOccurred");
         }
         if ($this->session->has("changeSuccessful")) {
             $this->view->changeSuccessful = $this->session->get("changeSuccessful");
             $this->session->remove("changeSuccessful");
         }
     }
     $this->view->problems = Problems::find();
 }