Skip to content

Commit 1f912bb

Browse files
author
phpnt
committed
0.0.1
0 parents  commit 1f912bb

23 files changed

+25813
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Composer
2+
vendor/
3+
composer.lock
4+
composer.phar

ExportFile.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: phpNT - http://phpnt.com
5+
* Date: 05.07.2016
6+
* Time: 21:24
7+
*/
8+
9+
namespace phpnt\exportFile;
10+
11+
use yii\base\Widget;
12+
use yii\helpers\Json;
13+
14+
class ExportFile extends Widget
15+
{
16+
public $model;
17+
public $searchAttributes;
18+
public $sort;
19+
public $page;
20+
public $getAll = false;
21+
22+
public $title = false;
23+
24+
public $csvCharset = 'UTF-8';
25+
26+
public $buttonClass = 'btn btn-primary';
27+
public $blockClass = 'pull-left';
28+
public $blockStyle = 'padding: 5px;';
29+
30+
public $xls = true;
31+
public $xlsButtonName = false;
32+
public $csv = true;
33+
public $csvButtonName = false;
34+
public $word = true;
35+
public $wordButtonName = false;
36+
public $html = true;
37+
public $htmlButtonName = false;
38+
public $pdf = true;
39+
public $pdfButtonName = false;
40+
41+
public function init()
42+
{
43+
$this->searchAttributes = Json::encode($this->searchAttributes);
44+
$this->sort = \Yii::$app->request->get('sort');
45+
$this->page = \Yii::$app->request->get('page');
46+
$this->xlsButtonName = $this->xlsButtonName ? $this->xlsButtonName : \Yii::t('app', 'MS Excel');
47+
$this->csvButtonName = $this->csvButtonName ? $this->csvButtonName : \Yii::t('app', 'CSV');
48+
$this->wordButtonName = $this->wordButtonName ? $this->wordButtonName : \Yii::t('app', 'MS Word');
49+
$this->htmlButtonName = $this->htmlButtonName ? $this->htmlButtonName : \Yii::t('app', 'HTML');
50+
$this->pdfButtonName = $this->pdfButtonName ? $this->pdfButtonName : \Yii::t('app', 'PDF');
51+
parent::init();
52+
}
53+
54+
public function run()
55+
{
56+
return $this->render(
57+
'view',
58+
[
59+
'widget' => $this
60+
]
61+
);
62+
}
63+
}

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 HimikLab
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
phpNT - Export to file
2+
================================
3+
[![Latest Stable Version](https://poser.pugx.org/phpnt/yii2-export/v/stable)](https://packagist.org/packages/phpnt/yii2-export) [![Total Downloads](https://poser.pugx.org/phpnt/yii2-export/downloads)](https://packagist.org/packages/phpnt/yii2-export) [![Latest Unstable Version](https://poser.pugx.org/phpnt/yii2-export/v/unstable)](https://packagist.org/packages/phpnt/yii2-export) [![License](https://poser.pugx.org/phpnt/yii2-export/license)](https://packagist.org/packages/phpnt/yii2-export)
4+
### Описание:
5+
#### Сохраняет данные в xls, csv, word, html, pdf файлы. Если, в представлении, модель Search использовалась вместе с DataProvider для вывода GridView и применялся фильтр, то к сохраняемым данным будет также применен этот фильтр.
6+
#### Для CSV файлов предусмотрен выбор кодировок 'UTF-8' (по умолчанию) и 'Windows-1251'.
7+
#### Инструкция для русификации PDF файлов находится в файле README, в папке /dompdf_ru.
8+
### [DEMO](http://phpnt.com/widget/export-file)
9+
10+
------------
11+
[![Donate button](https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif)](http://phpnt.com/donate/index)
12+
------------
13+
14+
### Социальные сети:
15+
- [Канал YouTube](https://www.youtube.com/c/phpnt)
16+
- [Группа VK](https://vk.com/phpnt)
17+
- [Группа facebook](https://www.facebook.com/Phpnt-595851240515413/)
18+
19+
------------
20+
21+
Установка:
22+
23+
------------
24+
25+
```
26+
php composer.phar require "phpnt/yii2-export" "*"
27+
```
28+
или
29+
30+
```
31+
composer require phpnt/yii2-export
32+
```
33+
34+
или добавить в composer.json файл
35+
36+
```
37+
"phpnt/yii2-export": "*"
38+
```
39+
## Использование:
40+
### Подключение:
41+
------------
42+
```php
43+
// в файле настройки приложения (main.php - Advanced или web.php - Basic) добавляется класс в controllerMap
44+
...
45+
'controllerMap' => [
46+
'export' => 'phpnt\exportFile\controllers\ExportController'
47+
],
48+
'components' => [
49+
...
50+
],
51+
```
52+
### В любой модели Search:
53+
------------
54+
```php
55+
...
56+
class GeoCitySearch extends GeoCity
57+
{
58+
...
59+
// указываются свойства, которые нужно выводить в файлы
60+
public function exportFields()
61+
{
62+
return [
63+
'id' => function ($model) {
64+
/* @var $model User */
65+
return $model->id;
66+
},
67+
'name_ru',
68+
'region_id' => function ($model) {
69+
/* @var $model GeoCity */
70+
if (isset($model->region->name_ru)) {
71+
return $model->region->name_ru;
72+
}
73+
return false;
74+
},
75+
'lat',
76+
'lon'
77+
];
78+
}
79+
...
80+
}
81+
```
82+
### Контроллер:
83+
------------
84+
```php
85+
...
86+
// cоздается стандартное действие для вывода данных
87+
public function actionExportFile()
88+
{
89+
$searchModel = new GeoCitySearch();
90+
$dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
91+
92+
return $this->render('export-file', [
93+
'searchModel' => $searchModel,
94+
'dataProvider' => $dataProvider,
95+
]);
96+
}
97+
...
98+
```
99+
100+
### Представление 1:
101+
------------
102+
```php
103+
use phpnt\exportFile\ExportFile;
104+
use yii\grid\GridView;
105+
/* @var $searchModel \common\models\GeoCitySearch */
106+
/* @var $dataProvider yii\data\ActiveDataProvider */
107+
108+
// минимальные настройки
109+
echo ExportFile::widget([
110+
'model' => 'common\models\GeoCitySearch', // путь к модели
111+
'searchAttributes' => $searchModel, // фильтр
112+
]) ?>
113+
<?= GridView::widget([
114+
'dataProvider' => $dataProvider,
115+
'filterModel' => $searchModel,
116+
'columns' => [
117+
...
118+
]
119+
]);
120+
?>
121+
```
122+
### Представление 2:
123+
------------
124+
```php
125+
use phpnt\exportFile\ExportFile;
126+
use yii\grid\GridView;
127+
/* @var $searchModel \common\models\GeoCitySearch */
128+
/* @var $dataProvider yii\data\ActiveDataProvider */
129+
130+
// максимальные настройки
131+
echo ExportFile::widget([
132+
'model' => 'common\models\GeoCitySearch', // путь к модели
133+
'searchAttributes' => $searchModel, // фильтр
134+
'title' => 'Заголовок документа',
135+
136+
'getAll' => false, // все записи - true, учитывать пагинацию - false
137+
'csvCharset' => 'Windows-1251', // кодировка csv файла: 'UTF-8' (по умолчанию) или 'Windows-1251'
138+
139+
'buttonClass' => 'btn btn-primary', // класс кнопки
140+
'blockClass' => 'pull-left', // класс блока в котором кнопка
141+
'blockStyle' => 'padding: 5px;', // стиль блока в котором кнопка
142+
143+
// экспорт в следующие файлы (true - разрешить, false - запретить)
144+
'xls' => true,
145+
'csv' => true,
146+
'word' => true,
147+
'html' => true,
148+
'pdf' => true,
149+
150+
// шаблоны кнопок
151+
'xlsButtonName' => '\Yii::t('app', 'MS Excel'),
152+
'csvButtonName' => '\Yii::t('app', 'CSV'),
153+
'wordButtonName' => '\Yii::t('app', 'MS Word'),
154+
'htmlButtonName' => '\Yii::t('app', 'HTML'),
155+
'pdfButtonName' => '\Yii::t('app', 'PDF')
156+
]) ?>
157+
<?= GridView::widget([
158+
'dataProvider' => $dataProvider,
159+
'filterModel' => $searchModel,
160+
'columns' => [
161+
...
162+
]
163+
]);
164+
?>
165+
```
166+
# Документация (примеры):
167+
## [PHPExcel](https://phpexcel.codeplex.com/)
168+
## [PHPWord](https://phpword.readthedocs.io/en/latest/)
169+
## [dompdf](https://github.com/dompdf/dompdf)
170+
------------
171+
### Версия:
172+
### 0.0.1
173+
------------
174+
### Лицензия:
175+
### [MIT](https://ru.wikipedia.org/wiki/%D0%9B%D0%B8%D1%86%D0%B5%D0%BD%D0%B7%D0%B8%D1%8F_MIT)
176+
------------

composer.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "phpnt/yii2-export",
3+
"description": "Yii2 It saves data in xls, csv, word, html, pdf files.",
4+
"keywords": ["yii2", "export", "xls", "excel", "csv", "word", "html", "pdf", "widget", "extension", "phpexcel", "phpword", "dompdf"],
5+
"type": "yii2-extension",
6+
"license": "MIT",
7+
"minimum-stability" : "dev",
8+
"prefer-stable": true,
9+
"support": {
10+
"source": "https://github.com/phpnt/yii2-export"
11+
},
12+
"authors": [
13+
{
14+
"name": "phpNT",
15+
"email": "[email protected]",
16+
"homepage": "http://phpnt.com"
17+
}
18+
],
19+
"require": {
20+
"php": ">=5.4.0",
21+
"yiisoft/yii2": "*",
22+
"phpoffice/phpexcel": ">=1.8.1",
23+
"phpoffice/phpword": ">=v0.12.1 ",
24+
"dompdf/dompdf": ">=v0.7.0"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"phpnt\\exportFile\\": ""
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)