Skip to content

Commit

Permalink
Pielikta iespēja ielādēt PDF attachmentus modal logā no ikonas
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jul 12, 2019
1 parent 3416761 commit 8c9329b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 5 deletions.
14 changes: 10 additions & 4 deletions components/D3FilesColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ class D3FilesColumn extends DataColumn
public $model;
public $modelClass;
public $listBoxOptions = [];
public $listTemplate;

private $dataProviderIds = [];
private $recordsWithFiles = [];
private $controllerRoute = false;

public const TEMPLATE_DROPDOWN_LIST = 'dropdown-list';
public const TEMPLATE_UL_LIST = 'list';
public const TEMPLATE_FILES = 'files';
public const TEMPLATE_ATTACHMENT_ICON = 'attachment-icon';

/**
* Set the initial properties on class init
*/
Expand All @@ -42,6 +48,8 @@ public function init(): void
'prompt' => \Yii::t('d3files', 'Filter by Attachment')
];

$this->listTemplate = self::TEMPLATE_FILES;

parent::init();
}

Expand Down Expand Up @@ -86,15 +94,13 @@ public function renderDataCellContent($model, $key, $index): string
$search = Yii::$app->request->get('RkInvoiceSearch');

$files = !empty($model->filesList) ? json_decode($model->filesList, true) : [];

$filesCount = count($files);


$params = [
'model' => $this->modelClass,
'model_id' => $model->id,
'readOnly' => true,
'viewByFancyBox' => true,
'template' => $filesCount > 1 ? 'dropdown-list' : 'list',
'template' => self::TEMPLATE_ATTACHMENT_ICON,
'fileList' => $files,
];

Expand Down
46 changes: 46 additions & 0 deletions views/d3files/attachment-icon_readonly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use d3yii2\d3files\widgets\D3FilesWidget;
use eaBlankonThema\widget\ThModal;
use yii\helpers\Html;

\d3yii2\d3files\D3FilesAsset::register($this);
/**
* @var string $url_prefix
* @var string $viewByFancyBox
* @var array $fileList
*/

if (empty($fileList)) {
return;
}

$pdfFile = false;

foreach ($fileList as $i => $file) {
if (strstr($file['file_name'], '.pdf')) {
$file['file_url'] = [$url_prefix . 'd3filesopen', 'id' => $file['file_model_id']];
$pdfFile = $file;
break;
}
}

if (!$pdfFile) {
return;
}

$dataAttributes = D3FilesWidget::getModalLoadAttributes(
$pdfFile['file_url'],
$pdfFile,
'#' . ThModal::MODAL_ID,
'.' . ThModal::MODAL_CONTENT_CLASS
);

$dataAttributesStr = '';

foreach ($dataAttributes as $key => $val) {
$dataAttributesStr .= ' ' . $key . '="' . $val . '"';
}
?>

<a href="javascript:;" title="Atvērt pielikumus"<?= $dataAttributesStr ?> class="d3files-attachment-load" data-files-list='<?= \yii\helpers\Json::encode($fileList) ?>'><i class="fa fa-file-text-o"></i></a>
46 changes: 46 additions & 0 deletions views/d3files/dropdown-list_readonly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

use d3yii2\d3files\widgets\D3FilesWidget;
use eaBlankonThema\widget\ThModal;
use yii\helpers\Html;

\d3yii2\d3files\D3FilesAsset::register($this);
/**
* @var string $url_prefix
* @var string $viewByFancyBox
* @var array $fileList
*/

if (empty($fileList)) {
return;
} ?>
<select class="form-control d3files-list th-dropdown-load">
<option value="default" selected="selected"><?= Yii::t('d3files', 'Select attachment') ?></option>
<?php

foreach ($fileList as $row) {
$ext = strtolower(pathinfo($row['file_name'], PATHINFO_EXTENSION));
if ($viewByFancyBox && in_array($ext, $viewByFancyBoxExtensions, true)) {
$fileUrl = $fileUrl = [
$url_prefix . 'd3filesopen',
'id' => $row['file_model_id']
];

$dataAttributes = D3FilesWidget::getModalLoadAttributes(
$fileUrl,
$row,
'#' . ThModal::MODAL_ID,
'.' . ThModal::MODAL_CONTENT_CLASS
);

$dataAttributesStr = '';

foreach ($dataAttributes as $key => $val) {
$dataAttributesStr .= ' ' . $key . '="' . $val . '"';
} ?>
<option value="<?= $row['file_name'] ?>"<?= $dataAttributesStr ?>><?= $row['file_name'] ?></option>
<?php
}
}
?>
</select>
2 changes: 1 addition & 1 deletion widgets/D3FilesWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static function getModalLoadAttributes(array $attachmentUrl, array $file,

$attrs = [
'data-toggle' => 'modal',
'data-src' => Url::to($attachmentUrl),
'data-src' => Url::to($attachmentUrl, true),
'data-target' => $modalSelector,
'data-content-target' => $modalContentSelector,
];
Expand Down

0 comments on commit 8c9329b

Please sign in to comment.