Skip to content

Commit

Permalink
Fixed bugs with ajax cruds. More flexible file field
Browse files Browse the repository at this point in the history
  • Loading branch information
execut committed Jun 28, 2018
1 parent d4a3339 commit 594b614
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
28 changes: 21 additions & 7 deletions action/adapter/File.php
Expand Up @@ -24,6 +24,8 @@ class File extends Adapter
public $model = null;
public $dataAttribute = 'data';
public $nameAttribute = 'name';
public $mimeTypeAttribute = 'mime_type';
public $extensionAttribute = 'extension';
public $extensionIsRequired = true;
protected function _run() {
$attributes = $this->actionParams->get;
Expand All @@ -41,27 +43,39 @@ protected function _run() {
$dataAttribute = $this->dataAttribute;
}

$selectedAttributes = array_merge([
$extensionAttribute = $this->extensionAttribute;
$selectedAttributes = [
$this->nameAttribute,
$dataAttribute,
'mime_type',
], array_keys($attributes));
$extensionAttribute,
$this->mimeTypeAttribute,
];

$selectedAttributes = array_filter($selectedAttributes);
$attributes = [
$extensionAttribute => $attributes['extension'],
'id' => $attributes['id'],
];
$result = $class::find()->select($selectedAttributes)->andWhere($attributes)->one();
if (!$result) {
throw new NotFoundHttpException('File by url "' . \yii::$app->request->getUrl() . '" not found');
}

if ($this->extensionIsRequired && strtolower($result->extension) !== $attributes['extension']) {
if ($this->extensionIsRequired && strtolower($result->$extensionAttribute) !== $attributes[$extensionAttribute]) {
throw new NotFoundHttpException('File extension is wrong');
}

$this->model = $result;

$response = \Yii::$app->getResponse();
if (strpos($result->mime_type, 'image/') === 0) {
$response->headers->set('Content-Type', $result->mime_type);
if ($this->mimeTypeAttribute) {
if (strpos($result->mime_type, 'image/') === 0) {
$response->headers->set('Content-Type', $result->mime_type);
} else {
$response->setDownloadHeaders($result->{$this->nameAttribute}, $result->mime_type);
}
} else {
$response->setDownloadHeaders($result->{$this->nameAttribute}, $result->mime_type);
$response->headers->set('Content-Type', 'image/jpeg');
}

$response = $this->getResponse([
Expand Down
1 change: 1 addition & 0 deletions widgets/EditDialog.php
Expand Up @@ -10,6 +10,7 @@


use execut\yii\jui\Widget;
use function Leafo\ScssPhp\Tests\_dump;
use yii\bootstrap\Modal;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
Expand Down
26 changes: 20 additions & 6 deletions widgets/GridView.php
Expand Up @@ -58,7 +58,6 @@ protected function getUpdateUrl() {
*/
protected function registerWidget($name = null, $id = null)
{

if ($name === null) {
$name = $this->getDefaultJsWidgetName();
}
Expand Down Expand Up @@ -147,10 +146,7 @@ public function getToolbarConfig(): array
public function beginPjax() {
parent::beginPjax();
if ($this->isAjaxCrud) {
$model = $this->formModel;
if (is_callable($model)) {
$model = $model();
}
$model = $this->getFormModel();

if ($this->pjax) {
$gridId = $this->id . '-pjax';
Expand Down Expand Up @@ -193,12 +189,30 @@ public function renderAlertBlock() {
protected function renderAddButton()
{
$title = \yii::t('execut.actions', 'Add') . ' ' . $this->title;
return Html::a($title, Url::to($this->addButtonUrl), [
$url = $this->addButtonUrl;
if (!is_array($url)) {
$url = [$url];
}


return Html::a($title, Url::to($url), [
'id' => $this->id . '-edit-add-button',
'type' => 'button',
'data-pjax' => 0,
'title' => $title,
'class' => 'btn btn-success'
]);
}

/**
* @return null
*/
protected function getFormModel()
{
$model = $this->formModel;
if (is_callable($model)) {
$model = $model();
}
return $model;
}
}
13 changes: 12 additions & 1 deletion widgets/assets/EditDialog.js
Expand Up @@ -9,6 +9,11 @@
var t = this;
t._initElements();
t._initEvents();
// $.ajaxSetup({
// cache: false,
// contentType: false,
// processData: false
// });
},
_initElements: function () {
var t = this;
Expand Down Expand Up @@ -78,7 +83,13 @@

var realAction = t._sourceAction;
if (typeof attributes.id !== 'undefined') {
realAction += '?id=' + attributes.id;
if (realAction.search('\\?') !== 0) {
realAction += '&'
} else {
realAction += '?'
}

realAction += 'id=' + attributes.id;
}

t.formEl.attr('action', realAction)
Expand Down
4 changes: 4 additions & 0 deletions widgets/assets/GridView.css
Expand Up @@ -4,3 +4,7 @@
.kv-grid-table tr.link-row:hover td {
color: #289939;
}
.btn-group,
.summary {
padding: 10px;
}
6 changes: 5 additions & 1 deletion widgets/assets/GridView.less
Expand Up @@ -5,4 +5,8 @@
color: #289939;
}
}
}
}

.btn-group, .summary {
padding: 10px;
}

0 comments on commit 594b614

Please sign in to comment.