/** * This method saves uploaded file to its persistent location. * @throws Exception */ public function saveFileAttachment() { // Set filename attribute $this->filename = basename($this->fileAttachment->getName()); // Set filesize attribute $this->filesize = $this->fileAttachment->getSize(); // Get bug model this attachment belongs to. $bugChange = BugChange::model()->findByPk($this->bug_change_id); $bug = Bug::model()->findByPk($bugChange->bug_id); // Check project quota if ($bug->project->bug_attachment_files_disc_quota > 0) { $totalFileSize = 0; $percentOfDiskQuota = 0; $bug->project->getBugAttachmentCount($totalFileSize, $percentOfDiskQuota); if ($bug->project->bug_attachment_files_disc_quota * 1024 * 1024 - $totalFileSize - $this->filesize < 0) { $this->addError('fileAttachment', 'Bug attachment quota for this project has exceeded.'); throw new Exception('Bug attachment quota constrained.'); } } // Calc md5 hash and save it as model attribute $this->md5 = md5_file($this->fileAttachment->getTempName()); // Create the directory where we will place the uploaded file $subDir1 = substr($this->md5, 0, 3); $subDir2 = substr($this->md5, 3, 3); $dirName = Yii::app()->getBasePath() . "/data/bugAttachments/" . $subDir1 . "/" . $subDir2; if (!@is_dir($dirName)) { if (False == @mkdir($dirName, 0777, TRUE)) { $error = error_get_last(); $this->addError("fileAttachment", "Couldn't make directory for file attachment."); throw new Exception('Could not make directory for file attachment.'); } } // Move uploaded file to an appropriate directory and delete temp file $fileName = $dirName . "/" . $this->md5; if (!$this->fileAttachment->saveAs($fileName, true) && !$this->ignoreFileAttachmentErrors) { $this->addError("fileAttachment", "Couldn't save file attachment"); throw new Exception('Could not save file attachment'); } }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function loadModel($id) { $model = Bug::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }
/** * Generates a crash report version distribution graph for currently * selected project and dumps it to stdout. * @param type $w Desired image width. * @param type $h Desired image height. * @throws CHttpException * @return void */ public static function generateBugStatusDistributionGraph($w, $h, $file = null) { if (!is_numeric($w) || $w <= 0 || $w > 1024) { throw new CHttpException(403, 'Invalid parameter'); } if (!is_numeric($h) || $h <= 0 || $h > 960) { throw new CHttpException(403, 'Invalid parameter'); } // Get current project info $curProjectId = Yii::app()->user->getCurProjectId(); if ($curProjectId == false) { throw new CHttpException(403, 'Invalid parameter'); } $curVer = Yii::app()->user->getCurProjectVer(); // Prepare data $criteria = new CDbCriteria(); $criteria->select = 'status, COUNT(*) as cnt'; $criteria->group = 'status'; $criteria->compare('project_id', $curProjectId); $criteria->order = 'status DESC'; if ($curVer != -1) { $criteria->compare('appversion_id', $curVer == 0 ? null : $curVer); } $data = array(); $models = Bug::model()->findAll($criteria); $totalCount = 0; $curVerStr = ''; foreach ($models as $model) { $totalCount += $model->cnt; $data[Lookup::item('BugStatus', $model->status)] = $model->cnt; } // Check the case when $data is empty. if (count($data) == 0) { // Open out file if ($file != null) { $fout = @fopen($file, 'w'); if ($fout == false) { @unlink($tmpfile); throw new CHttpException(403, 'Invalid file.'); } } // No data available $fileName = Yii::app()->basePath . '/../images/no_data_available.png'; if ($fd = @fopen($fileName, "r")) { $fsize = filesize($fileName); // Write HTTP headers header("Content-type: image/png"); //header("Content-Disposition: filename=\"".$fileName."\""); header("Content-length: {$fsize}"); header("Cache-control: private"); //use this to open files directly // Write file content while (!feof($fd)) { $buffer = fread($fd, 2048); if ($file == null) { echo $buffer; } else { fwrite($fout, $buffer); } } if ($file != null) { fclose($fout); } fclose($fd); } return; } $graph = new ezcGraphPieChart(); $graph->palette = new ezcGraphPaletteEzRed(); $graph->data['Versions'] = new ezcGraphArrayDataSet($data); $graph->legend = true; $graph->legend->position = ezcGraph::RIGHT; $graph->options->font->name = 'Tahoma'; $graph->options->sum = $totalCount; $graph->options->summarizeCaption = 'Others'; $graph->renderer->options->pieChartGleam = 0.3; $graph->renderer->options->pieChartGleamColor = '#FFFFFF'; $graph->renderer->options->pieChartGleamBorder = 2; $graph->renderer = new ezcGraphRenderer3d(); $graph->renderer->options->pieChartRotation = 0.8; $graph->renderer->options->pieChartShadowSize = 10; if ($file === null) { $graph->renderToOutput($w, $h); } else { $graph->render($w, $h, $file); } }
public function testDelete() { // Create new bug $bug = new Bug(); $bug->summary = "Bug on Crash Report #3"; $bug->description = "Some description"; $bug->assigned_to = 1; $bug->status = Bug::STATUS_ACCEPTED; $bug->priority = Bug::PRIORITY_HIGH; $bug->reproducability = Bug::REPRO_NOT_TRIED; $bug->crashreports = "3"; // Login as root $model = new LoginForm('RegularLogin'); $model->username = "******"; $model->password = "******"; $this->assertTrue($model->login()); // Apply changes $opened = $bug->open(); // Should succeed $this->assertTrue($opened); // Find the bug $bug = Bug::model()->findByAttributes(array('summary' => 'Bug on Crash Report #3')); $this->assertTrue($bug != null); // Delete the bug $this->assertTrue($bug->delete()); }