Skip to content

Commit

Permalink
Tcpdf, Phpexcel, Bootstrap: Styling (row + column)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaDafinser committed Nov 5, 2013
1 parent d864558 commit 1f8d298
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 84 deletions.
1 change: 1 addition & 0 deletions src/ZfcDatagrid/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
43 changes: 27 additions & 16 deletions src/ZfcDatagrid/Examples/Controller/PersonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -91,19 +103,19 @@ 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);
}

{
$colType = new Type\Number();
$colType->addAttribute(\NumberFormatter::FRACTION_DIGITS, 2);
$colType->setSuffix(' kg');

$col = new Column\Select('weight');
$col->setLabel('Weight');
$col->setWidth(10);
Expand All @@ -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);
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down
20 changes: 20 additions & 0 deletions src/ZfcDatagrid/Renderer/AbstractRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ abstract class AbstractRenderer implements RendererInterface

protected $columns = array();

protected $rowStyles = array();

protected $sortConditions = null;

protected $filters = null;
Expand Down Expand Up @@ -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%
*
Expand Down
50 changes: 10 additions & 40 deletions src/ZfcDatagrid/Renderer/BootstrapTable/View/Helper/TableRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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':
Expand All @@ -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;
Expand Down Expand Up @@ -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()
);

Expand Down
14 changes: 11 additions & 3 deletions src/ZfcDatagrid/Renderer/PHPExcel/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -127,14 +127,22 @@ 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) . '"');

break;
}
}
}
}

$xColumn ++;
}
Expand Down
66 changes: 41 additions & 25 deletions src/ZfcDatagrid/Renderer/TCPDF/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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']);
}
}

0 comments on commit 1f8d298

Please sign in to comment.