From 21a3729356d81bad76d2b546f81468cf2950f42f Mon Sep 17 00:00:00 2001 From: Vladimir Prorok Date: Mon, 14 Aug 2017 12:11:44 +0200 Subject: [PATCH] fix column mapper --- src/mappers/ColumnValueMapper.php | 38 ++++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/mappers/ColumnValueMapper.php b/src/mappers/ColumnValueMapper.php index dc3bc99..81f54e9 100644 --- a/src/mappers/ColumnValueMapper.php +++ b/src/mappers/ColumnValueMapper.php @@ -10,6 +10,7 @@ namespace dosamigos\exportable\mappers; use yii\base\Model; +use yii\base\Object; use yii\db\ActiveRecordInterface; use yii\grid\ActionColumn; use yii\grid\CheckboxColumn; @@ -64,11 +65,9 @@ public function map($model, $index) ? $model->getPrimaryKey() : $model[$column->attribute]; - $value = $this->isHtml - ? $column->renderDataCell($model, $key, $index) - : ArrayHelper::getValue($model, $column->attribute); + $value = $this->getColumnValue($column, $model, $key, $index); - $header = $this->getColumnHeader($column, $model); + $header = $this->getColumnHeader($column); $row[$header] = $value; } } @@ -76,6 +75,16 @@ public function map($model, $index) return $row; } + protected function getColumnValue($column, $model, $key, $index) + { + $value = $column->renderDataCell($model, $key, $index); + if (!$this->isHtml) { + $value = strip_tags($value); + } + + return $value; + } + /** * Returns column headers * @@ -88,9 +97,9 @@ public function getHeaders($model) $headers = []; /** @var Column $column */ foreach ($this->columns as $column) { - $headers[] = $this->isHtml - ? $column->renderHeaderCell() - : $this->getColumnHeader($column, $model); + if ($this->isColumnExportable($column)) { + $headers[] = $this->getColumnHeader($column); + } } return $headers; @@ -105,7 +114,7 @@ public function getHeaders($model) */ protected function isColumnExportable($column) { - if (!($column instanceof DataColumn) || $column instanceof ActionColumn || $column instanceof CheckboxColumn) { + if ($column instanceof ActionColumn || $column instanceof CheckboxColumn) { return false; } @@ -124,16 +133,13 @@ protected function isColumnExportable($column) * * @return string */ - protected function getColumnHeader($column, $model) + protected function getColumnHeader($column) { - if (!($column instanceof DataColumn)) { - return $column->header; + $header = $column->renderHeaderCell(); + if (!$this->isHtml) { + $header = strip_tags($header); } - return $column->label !== null - ? $column->label - : (!empty($model) && $model instanceof Model - ? $model->getAttributeLabel($column->attribute) - : $column->attribute); + return $header; } }