From 1f8d2988f1b370f8509d36b5c25637114ce94340 Mon Sep 17 00:00:00 2001 From: Martin Keckeis Date: Tue, 5 Nov 2013 09:42:11 +0100 Subject: [PATCH] Tcpdf, Phpexcel, Bootstrap: Styling (row + column) --- src/ZfcDatagrid/Datagrid.php | 1 + .../Examples/Controller/PersonController.php | 43 +++++++----- src/ZfcDatagrid/Renderer/AbstractRenderer.php | 20 ++++++ .../BootstrapTable/View/Helper/TableRow.php | 50 +++----------- .../Renderer/PHPExcel/Renderer.php | 14 +++- src/ZfcDatagrid/Renderer/TCPDF/Renderer.php | 66 ++++++++++++------- 6 files changed, 110 insertions(+), 84 deletions(-) diff --git a/src/ZfcDatagrid/Datagrid.php b/src/ZfcDatagrid/Datagrid.php index 60edc923..117645fe 100644 --- a/src/ZfcDatagrid/Datagrid.php +++ b/src/ZfcDatagrid/Datagrid.php @@ -833,6 +833,7 @@ public function getRenderer() $renderer->setTranslator($this->getTranslator()); $renderer->setTitle($this->getTitle()); $renderer->setColumns($this->getColumns()); + $renderer->setRowStyles($this->getRowStyles()); $renderer->setCacheId($this->getCacheId()); $renderer->setCacheData($this->getCache() ->getItem($this->getCacheId())); diff --git a/src/ZfcDatagrid/Examples/Controller/PersonController.php b/src/ZfcDatagrid/Examples/Controller/PersonController.php index ae508afa..65749a68 100644 --- a/src/ZfcDatagrid/Examples/Controller/PersonController.php +++ b/src/ZfcDatagrid/Examples/Controller/PersonController.php @@ -10,7 +10,8 @@ class PersonController extends AbstractActionController { - private function getGrid(){ + private function getGrid() + { /* @var $dataGrid \ZfcDatagrid\Datagrid */ $dataGrid = $this->getServiceLocator()->get('zfcDatagrid'); $dataGrid->setTitle('Persons'); @@ -45,7 +46,7 @@ private function getGrid(){ $dataPopulation = new Column\DataPopulation\Object(); $dataPopulation->setObject(new Column\DataPopulation\Object\Gravatar()); $dataPopulation->addObjectParameterColumn('email', $colEmail); - + $col = new Column\ExternalData('avatar'); $col->setLabel('Avatar'); $col->setDataPopulation($dataPopulation); @@ -71,7 +72,11 @@ private function getGrid(){ $col->setSortDefault(2, 'DESC'); $dataGrid->addColumn($col); - $style = new Style\BackgroundColor(Style\AbstractColor::$GREEN); + $style = new Style\BackgroundColor(array( + 200, + 200, + 200 + )); $style->setByValue($col, 'Martin'); $dataGrid->addRowStyle($style); @@ -82,6 +87,13 @@ private function getGrid(){ 'm' => 'male', 'f' => 'female' )); + $style = new Style\BackgroundColor(array( + 200, + 100, + 100 + )); + $style->setByValue($col, 'male'); + $col->addStyle($style); $col->setTranslationEnabled(true); $dataGrid->addColumn($col); @@ -91,11 +103,11 @@ private function getGrid(){ $col->setWidth(5); $col->setType(new Type\Number()); $col->setFilterDefaultValue('>=20'); - + $style = new Style\Color(Style\Color::$RED); $style->setByValue($col, 20); $col->addStyle($style); - + $dataGrid->addColumn($col); } @@ -103,7 +115,7 @@ private function getGrid(){ $colType = new Type\Number(); $colType->addAttribute(\NumberFormatter::FRACTION_DIGITS, 2); $colType->setSuffix(' kg'); - + $col = new Column\Select('weight'); $col->setLabel('Weight'); $col->setWidth(10); @@ -123,7 +135,7 @@ private function getGrid(){ $colType = new Type\DateTime('Y-m-d H:i:s', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::MEDIUM); $colType->setSourceTimezone('Europe/Vienna'); $colType->setOutputTimezone('UTC'); - + $col = new Column\Select('changeDate'); $col->setLabel('Last change'); $col->setWidth(15); @@ -133,7 +145,7 @@ private function getGrid(){ $action = new Column\Action\Button(); $action->setLabel('test'); - $action->setAttribute('href', '/someAction/id/'.$action->getRowIdPlaceholder()); + $action->setAttribute('href', '/someAction/id/' . $action->getRowIdPlaceholder()); $col = new Column\Action(); $col->setLabel('Actions'); @@ -144,29 +156,28 @@ private function getGrid(){ $dataGrid->setRowClickAction($action); return $dataGrid; - } - + /** * bootstrap table * * @return \ZfcDatagrid\Controller\ViewModel */ - public function bootstrapAction () + public function bootstrapAction() { $dataGrid = $this->getGrid(); - + $dataGrid->execute(); - + return $dataGrid->getResponse(); } - + /** * bootstrap table * * @return \ZfcDatagrid\Controller\ViewModel */ - public function jqgridAction () + public function jqgridAction() { $dataGrid = $this->getGrid(); $dataGrid->setRenderer('jqgrid'); @@ -183,7 +194,7 @@ public function jqgridAction () * * @return \Zend\Http\Response\Stream */ - public function consoleAction () + public function consoleAction() { /* @var $dataGrid \ZfcDatagrid\Datagrid */ $dataGrid = $this->getServiceLocator()->get('zfcDatagrid'); diff --git a/src/ZfcDatagrid/Renderer/AbstractRenderer.php b/src/ZfcDatagrid/Renderer/AbstractRenderer.php index bf61a6fb..4efd18e6 100644 --- a/src/ZfcDatagrid/Renderer/AbstractRenderer.php +++ b/src/ZfcDatagrid/Renderer/AbstractRenderer.php @@ -30,6 +30,8 @@ abstract class AbstractRenderer implements RendererInterface protected $columns = array(); + protected $rowStyles = array(); + protected $sortConditions = null; protected $filters = null; @@ -203,6 +205,24 @@ public function getColumns() return $this->columns; } + /** + * + * @param array $rowStyles + */ + public function setRowStyles($rowStyles = array()) + { + $this->rowStyles = $rowStyles; + } + + /** + * + * @return array + */ + public function getRowStyles() + { + return $this->rowStyles; + } + /** * Calculate the sum of the displayed column width to 100% * diff --git a/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php b/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php index 012b03b2..13e793e3 100644 --- a/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php +++ b/src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php @@ -51,7 +51,7 @@ public function __invoke($row, $columns, AbstractAction $rowClickAction = null, $value = $row[$column->getUniqueId()]; - $styles = array(); + $cssStyles = array(); $classes = array(); if ($column->isHidden() === true) { @@ -61,7 +61,7 @@ public function __invoke($row, $columns, AbstractAction $rowClickAction = null, switch (get_class($column->getType())) { case 'ZfcDatagrid\Column\Type\Number': - $styles[] = 'text-align: right'; + $cssStyle[] = 'text-align: right'; break; case 'ZfcDatagrid\Column\Type\PhpArray': @@ -83,58 +83,28 @@ public function __invoke($row, $columns, AbstractAction $rowClickAction = null, break; } - if ($column->hasStyles() === true) { - foreach ($column->getStyles() as $style) { - /* @var $style \ZfcDatagrid\Column\Style\AbstractStyle */ - if ($style->isApply($row) === true) { - - switch (get_class($style)) { - - case 'ZfcDatagrid\Column\Style\Bold': - $styles[] = 'font-weight: bold'; - break; - - case 'ZfcDatagrid\Column\Style\Italic': - $styles[] = 'font-style: italic'; - break; - - case 'ZfcDatagrid\Column\Style\BackgroundColor': - $styles[] = 'background-color: #' . $style->getRgbHexString(); - break; - - case 'ZfcDatagrid\Column\Style\Color': - $styles[] = 'color: #' . $style->getRgbHexString(); - break; - - default: - throw new \Exception('Not defined yet: "' . get_class($style) . '"'); - break; - } - } - } - } - - foreach ($rowStyles as $style) { + $styles = array_merge($rowStyles, $column->getStyles()); + foreach ($styles as $style) { /* @var $style \ZfcDatagrid\Column\Style\AbstractStyle */ if ($style->isApply($row) === true) { + switch (get_class($style)) { case 'ZfcDatagrid\Column\Style\Bold': - $styles[] = 'font-weight: bold'; + $cssStyles[] = 'font-weight: bold'; break; case 'ZfcDatagrid\Column\Style\Italic': - $styles[] = 'font-style: italic'; + $cssStyles[] = 'font-style: italic'; break; case 'ZfcDatagrid\Column\Style\Color': - $styles[] = 'color: #' . $style->getRgbHexString(); + $cssStyles[] = 'color: #' . $style->getRgbHexString(); break; case 'ZfcDatagrid\Column\Style\BackgroundColor': - $styles[] = 'background-color: #' . $style->getRgbHexString(); + $cssStyles[] = 'background-color: #' . $style->getRgbHexString(); break; - default: throw new \Exception('Not defined yet: "' . get_class($style) . '"'); break; @@ -163,7 +133,7 @@ public function __invoke($row, $columns, AbstractAction $rowClickAction = null, $attributes = array( 'class' => implode(',', $classes), - 'style' => implode(';', $styles), + 'style' => implode(';', $cssStyles), 'data-columnUniqueId' => $column->getUniqueId() ); diff --git a/src/ZfcDatagrid/Renderer/PHPExcel/Renderer.php b/src/ZfcDatagrid/Renderer/PHPExcel/Renderer.php index c58fd482..f06b2a47 100644 --- a/src/ZfcDatagrid/Renderer/PHPExcel/Renderer.php +++ b/src/ZfcDatagrid/Renderer/PHPExcel/Renderer.php @@ -107,8 +107,8 @@ public function execute() /* * Styles */ - if ($column->hasStyles() === true) { - foreach ($column->getStyles() as $style) { + $styles = array_merge($this->getRowStyles(), $column->getStyles()); + foreach ($styles as $style) { /* @var $style \ZfcDatagrid\Column\Style\AbstractStyle */ if ($style->isApply($row) === true) { switch (get_class($style)) { @@ -127,6 +127,15 @@ public function execute() ->setRGB($style->getRgbHexString()); break; + case 'ZfcDatagrid\Column\Style\BackgroundColor': + $columnStyle->getFill()->applyFromArray(array( + 'type' => \PHPExcel_Style_Fill::FILL_SOLID, + 'color' => array( + 'rgb' => $style->getRgbHexString() + ) + )); + break; + default: throw new \Exception('Not defined yet: "' . get_class($style) . '"'); @@ -134,7 +143,6 @@ public function execute() } } } - } $xColumn ++; } diff --git a/src/ZfcDatagrid/Renderer/TCPDF/Renderer.php b/src/ZfcDatagrid/Renderer/TCPDF/Renderer.php index b84cbee2..27abcc29 100644 --- a/src/ZfcDatagrid/Renderer/TCPDF/Renderer.php +++ b/src/ZfcDatagrid/Renderer/TCPDF/Renderer.php @@ -315,29 +315,35 @@ protected function printTableRow(array $row, $rowHeight) /* * Styles */ - if ($column->hasStyles() === true) { - foreach ($column->getStyles() as $style) { - /* @var $style \ZfcDatagrid\Column\Style\AbstractStyle */ - if ($style->isApply($row) === true) { - switch (get_class($style)) { - - case 'ZfcDatagrid\Column\Style\Bold': - $this->setBold(); - break; - - case 'ZfcDatagrid\Column\Style\Italic': - $this->setItalic(); - break; - - case 'ZfcDatagrid\Column\Style\Color': - $this->setColor($style->getRgbArray()); - break; + $backgroundColor = false; + + $styles = array_merge($this->getRowStyles(), $column->getStyles()); + foreach ($styles as $style) { + /* @var $style \ZfcDatagrid\Column\Style\AbstractStyle */ + if ($style->isApply($row) === true) { + switch (get_class($style)) { + + case 'ZfcDatagrid\Column\Style\Bold': + $this->setBold(); + break; + + case 'ZfcDatagrid\Column\Style\Italic': + $this->setItalic(); + break; + + case 'ZfcDatagrid\Column\Style\Color': + $this->setColor($style->getRgbArray()); + break; + + case 'ZfcDatagrid\Column\Style\BackgroundColor': + $this->setBackgroundColor($style->getRgbArray()); + $backgroundColor = true; + break; + + default: + throw new \Exception('Not defined yet: "' . get_class($style) . '"'); - default: - throw new \Exception('Not defined yet: "' . get_class($style) . '"'); - - break; - } + break; } } } @@ -367,7 +373,7 @@ protected function printTableRow(array $row, $rowHeight) if ($row[$column->getUniqueId()] != '') { $link = $row[$column->getUniqueId()]; - if(is_array($link)){ + if (is_array($link)) { $link = array_shift($link); } } @@ -381,12 +387,12 @@ protected function printTableRow(array $row, $rowHeight) break; } - if(is_array($text)){ + if (is_array($text)) { $text = implode(PHP_EOL, $text); } // MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false, $ln=1, $x='', $y='', $reseth=true, $stretch=0, $ishtml=false, $autopadding=true, $maxh=0, $valign='T', $fitcell=false) - $pdf->MultiCell($column->getWidth(), $rowHeight, $text, 1, 'L', false, 1, $x, $y, true, 0); + $pdf->MultiCell($column->getWidth(), $rowHeight, $text, 1, 'L', $backgroundColor, 1, $x, $y, true, 0); } } @@ -489,4 +495,14 @@ public function setColor(array $rgb) $pdf = $this->getPdf(); $pdf->SetTextColor($rgb['red'], $rgb['green'], $rgb['blue']); } + + /** + * + * @param array $rgb + */ + public function setBackgroundColor(array $rgb) + { + $pdf = $this->getPdf(); + $pdf->SetFillColor($rgb['red'], $rgb['green'], $rgb['blue']); + } }