diff --git a/controllers/StatisticsController.php b/controllers/StatisticsController.php index d1050e9..666a23b 100755 --- a/controllers/StatisticsController.php +++ b/controllers/StatisticsController.php @@ -7,6 +7,7 @@ use common\models\User; use yii\data\ActiveDataProvider; use yii\helpers\ArrayHelper; + use yii\helpers\Html; use yii\helpers\VarDumper; use yii\web\Controller; use yii\web\Response; @@ -186,7 +187,8 @@ public function actionExport($date_range = NULL, $label = NULL, $manager = NULL) { - \Yii::$app->response = Response::FORMAT_JSON; + \Yii::$app->response->format = Response::FORMAT_JSON; + $formatter = \Yii::$app->formatter; /** * Get export options @@ -203,19 +205,19 @@ } else { $dateFilter = []; } - + if (!empty($label)) { $labelFilter = [ 'label' => $label ]; } else { $labelFilter = []; } - + if (!empty($manager)) { $managerFilter = [ 'manager_id' => $manager ]; } else { $managerFilter = []; } - + $orders = Order::find() ->with( [ @@ -227,10 +229,9 @@ ->filterWhere($dateFilter) ->andFilterWhere($labelFilter) ->andFilterWhere($managerFilter) - ->limit(10) ->all(); - $file = fopen(\Yii::getAlias('@backend') . '/web/export/' . time() . '_export.csv', 'a'); + $file = fopen(\Yii::getAlias('@frontend/web/') . 'statistics_export.csv', 'w'); foreach ($orders as $order) { $line = []; /** @@ -238,35 +239,40 @@ */ $line[] = $formatter->asDecimal($order->id); $line[] = $formatter->asDatetime($order->created_at); - $line[] = $formatter->asText($order->name); + $line[] = (string) $order->name; if (empty($order->products)) { $line[] = ''; } else { $i = 0; $products = ''; foreach ($order->products as $product) { + $i ++; $products .= $product->sku; if (count($order->products) != $i) { - $products .= ' ;'; + $products .= ' ,'; } } $line[] = $products; } - $line[] = $formatter->asText($order->city); - $line[] = $formatter->asText($order->orderLabel->label); - $line[] = $formatter->asDecimal($order->total); - $line[] = empty($order->reason) ? '' : $formatter->asText(Order::REASONS[$order->reason]); + $line[] = (string) $order->city; + $line[] = (string) $order->orderLabel->label; + $line[] = (string) $order->total; + $line[] = empty($order->reason) ? '' : $formatter->asText(Order::REASONS[ $order->reason ]); $line[] = empty($order->manager) ? '' : $formatter->asText($order->manager->username); - $line[] = $formatter->asText($order->body); + $line[] = (string) $order->body; fputcsv($file, $line); } fclose($file); - - /** - * Start here - * @todo Ajax download button generation - * change ->asText - it generates html - */ + + return [ + 'message' => 'Файл успешно сгенерирован', + 'button' => Html::a( + Html::tag('i', '', [ + 'class' => 'glyphicon glyphicon-download-alt' + ]) . ' Скачать', '/statistics_export.csv', [ + 'class' => 'btn bg-olive', + ]) + ]; } } diff --git a/views/statistics/index.php b/views/statistics/index.php index 54a1d93..110d0d1 100644 --- a/views/statistics/index.php +++ b/views/statistics/index.php @@ -10,6 +10,7 @@ use yii\helpers\ArrayHelper; use yii\helpers\Html; use yii\helpers\StringHelper; + use yii\helpers\Url; use yii\web\View; use yiier\chartjs\ChartJs; @@ -29,11 +30,11 @@ * @var int | boolean $dataLabel * @var int | boolean $dataManager */ - - $js = <<< JS -$('[data-toggle="popover"]').popover(); -JS; - $this->registerJs($js, View::POS_READY); + + $this->registerJsFile('/admin/js/statistics.js', [ + 'position' => View::POS_END, + 'depends' => 'yii\web\JqueryAsset' + ]); ?> @@ -360,10 +361,26 @@ JS; - +
+ 'glyphicon glyphicon-cog', + ]) . ' Создать выгрузку', [ + 'class' => 'btn bg-navy', + 'id' => 'generate-button', + 'data-link' => Url::to([ + 'export', + 'date_range' => $dateValue, + 'label' => $dataLabel, + 'manager' => $dataManager, + ]) + ]) +?> +