attachments in redmine module

parent 2ca21ed0
...@@ -42,14 +42,6 @@ class DefaultController extends Controller ...@@ -42,14 +42,6 @@ class DefaultController extends Controller
]; ];
} }
public static function actionsTitles()
{
return [
'Login' => 'Авторизация',
'Logout' => 'Выход',
];
}
public function actionLogin() public function actionLogin()
{ {
$this->page_title = 'Техническая поддержка'; $this->page_title = 'Техническая поддержка';
......
...@@ -6,10 +6,12 @@ use Yii; ...@@ -6,10 +6,12 @@ use Yii;
use yii\filters\AccessControl; use yii\filters\AccessControl;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\web\Controller; use yii\web\Controller;
use yii\web\Response;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use common\models\Settings; use common\models\Settings;
use common\modules\support\models\redmine\Issue; use common\modules\support\models\redmine\Issue;
use common\modules\support\models\redmine\RedmineHelper;
/** /**
* Default controller for the `support` module * Default controller for the `support` module
...@@ -26,11 +28,11 @@ class SupportController extends Controller ...@@ -26,11 +28,11 @@ class SupportController extends Controller
'access' => [ 'access' => [
'class' => AccessControl::className(), 'class' => AccessControl::className(),
'user' => 'support', 'user' => 'support',
'only' => ['index'], 'only' => ['index', 'create', 'view', 'file'],
'rules' => [ 'rules' => [
[ [
'allow' => true, 'allow' => true,
'actions' => ['index'], 'actions' => ['index', 'create', 'view', 'file'],
'roles' => ['@'], 'roles' => ['@'],
], ],
], ],
...@@ -38,13 +40,6 @@ class SupportController extends Controller ...@@ -38,13 +40,6 @@ class SupportController extends Controller
]; ];
} }
public static function actionsTitles()
{
return [
'Index' => 'Техническая поддержка',
];
}
public function actionIndex() public function actionIndex()
{ {
$client = $this->getClient(); $client = $this->getClient();
...@@ -73,6 +68,8 @@ class SupportController extends Controller ...@@ -73,6 +68,8 @@ class SupportController extends Controller
public function actionCreate() public function actionCreate()
{ {
$this->view->registerJsFile('/js/support.js', ['position' => yii\web\View::POS_END ]);
$model = new Issue(); $model = new Issue();
$client = $this->getClient(); $client = $this->getClient();
...@@ -81,7 +78,7 @@ class SupportController extends Controller ...@@ -81,7 +78,7 @@ class SupportController extends Controller
if ($model->load(Yii::$app->request->post()) && $model->validate()) if ($model->load(Yii::$app->request->post()) && $model->validate())
{ {
$client->issue->create([ $params = [
'subject' => $model->subject, 'subject' => $model->subject,
'description' => $model->description, 'description' => $model->description,
'project_id' => $model->project_id, 'project_id' => $model->project_id,
...@@ -89,17 +86,79 @@ class SupportController extends Controller ...@@ -89,17 +86,79 @@ class SupportController extends Controller
'status_id' => $model->status_id, 'status_id' => $model->status_id,
'tracker_id' => $model->tracker_id, 'tracker_id' => $model->tracker_id,
'author_id' => $user['user']['id'] 'author_id' => $user['user']['id']
];
if($model->files)
{
$uploads = [];
foreach ($model->files as $file)
{
$uploads[] = [
'token' => $file['token'],
'filename' => $file['filename'],
'content_type' => $file['type'],
];
}
$params = ArrayHelper::merge($params, [
'uploads' => $uploads
]); ]);
}
$client->issue->create($params);
return $this->redirect(['/support']); return $this->redirect(['/support']);
} }
else
{
return $this->render('create', [ return $this->render('create', [
'model' => $model, 'model' => $model,
'user' => $user 'user' => $user
]); ]);
} }
public function actionFile()
{
if(Yii::$app->request->isAjax)
{
Yii::$app->response->format = Response::FORMAT_JSON;
try
{
if($_FILES['files'])
{
$html = '';
foreach ($_FILES['files']['name'] as $i => $name)
{
$client = $this->getClient();
$response = json_decode($client->attachment->upload(@file_get_contents($_FILES['files']['tmp_name'][$i])));
$html .= $this->renderPartial('_file', [
'name' => $name,
'size' => RedmineHelper::formatSize($_FILES['files']['size'][$i]),
'token' => $response->upload->token,
'type' => $_FILES['files']['type'][$i]
]);
}
return [
'html' => $html,
'success' => true
];
}
}
catch (Exception $e)
{
return ['success' => false];
}
return ['success' => false];
}
else
{
throw new NotFoundHttpException('Доступ запрещен!');
}
} }
public function actionView($id) public function actionView($id)
......
...@@ -14,6 +14,8 @@ class Issue extends yii\base\Model ...@@ -14,6 +14,8 @@ class Issue extends yii\base\Model
public $tracker_id = self::TRACKER_NONE; public $tracker_id = self::TRACKER_NONE;
public $status_id = self::STATUS_NEW; public $status_id = self::STATUS_NEW;
public $files = null;
const PRIORITY_SIMPLE = 2; const PRIORITY_SIMPLE = 2;
const PRIORITY_FIRE = 3; const PRIORITY_FIRE = 3;
...@@ -30,6 +32,7 @@ class Issue extends yii\base\Model ...@@ -30,6 +32,7 @@ class Issue extends yii\base\Model
[['tracker_id', 'project_id', 'status_id', 'priority_id', 'description'], 'required'], [['tracker_id', 'project_id', 'status_id', 'priority_id', 'description'], 'required'],
[['tracker_id', 'project_id', 'status_id', 'priority_id'], 'integer'], [['tracker_id', 'project_id', 'status_id', 'priority_id'], 'integer'],
[['description'], 'string'], [['description'], 'string'],
[['files'], 'safe'],
[['subject'], 'string', 'max' => 255], [['subject'], 'string', 'max' => 255],
]; ];
} }
......
...@@ -78,4 +78,17 @@ class RedmineHelper ...@@ -78,4 +78,17 @@ class RedmineHelper
return $memberships; return $memberships;
} }
public static function formatSize($value)
{
$units=array('B','KB','MB','GB','TB');
$base = 1024;
for($i = 0; $base <= $value; $i++)
{
$value=$value/$base;
}
return round($value, 2) . ' ' . $units[$i];
}
} }
\ No newline at end of file
<?php
$id = uniqid();
?>
<div class="file_box_wr">
<span class="glyphicon glyphicon-file"></span>
<span><?=$name?></span> &nbsp;
<span class="color_gray">&nbsp;<?=$size?>&nbsp;&nbsp;</span>
<a class="delete-file" href="#"><span class="glyphicon glyphicon-remove color_gray"></span></a>
<input type="hidden" name="Issue[files][<?=$id?>][token]" value="<?=$token?>">
<input type="hidden" name="Issue[files][<?=$id?>][filename]" value="<?=$name?>">
<input type="hidden" name="Issue[files][<?=$id?>][type]" value="<?=$type?>">
</div>
\ No newline at end of file
...@@ -139,7 +139,13 @@ use common\modules\support\models\redmine\RedmineHelper; ...@@ -139,7 +139,13 @@ use common\modules\support\models\redmine\RedmineHelper;
<div class="col-sm-12"> <div class="col-sm-12">
<button class="gray_button">Прикрепить файл<i class="glyphicon glyphicon-paperclip"></i></button> <div id="file-container">
</div>
<input type="file" id="file-attachment-input" style="display:none;" multiple="multiple">
<div class="gray_button attachment-button">Прикрепить файл<i class="glyphicon glyphicon-paperclip"></i></div>
<br> <br>
<?= Html::submitButton('Создать задачу', ['class' => 'btn btn-success-2']) ?> <?= Html::submitButton('Создать задачу', ['class' => 'btn btn-success-2']) ?>
......
...@@ -1800,3 +1800,8 @@ a.keys_test_btn { ...@@ -1800,3 +1800,8 @@ a.keys_test_btn {
.keys_block_small { .keys_block_small {
display: block; display: block;
} }
.attachment-button {
display: inline-block;
cursor: pointer;
}
\ No newline at end of file
$(function(){
$('.attachment-button').click(function(){
$('#file-attachment-input').trigger('click');
});
$('#file-attachment-input').change(function(){
var xhr = new XMLHttpRequest, data = new FormData(), files = $('#file-attachment-input')[0].files;
xhr.open("POST", '/support/file', true);
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
data.append("_csrf", $('meta[name="csrf-token"]').attr('content'));
$.each(files, function(i, file){
data.append("files[]", file);
});
xhr.send(data);
xhr.onreadystatechange = function()
{
if (xhr.readyState == 4)
{
try
{
var response = JSON.parse(xhr.responseText);
}
catch(e)
{
var response = xhr.responseText;
}
if(response.success)
{
$('#file-container').append(response.html);
}
}
}
$(this).val('');
});
$(document).delegate('.file_box_wr .delete-file', 'click', function(){
$(this).closest('.file_box_wr').remove();
return false;
});
});
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment