Last active
December 29, 2015 08:09
-
-
Save ThaDafinser/7641719 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ZfcDatagrid\Column; | |
use ZfcDatagrid\Filter; | |
use ZfcDatagrid\Column\Formatter\AbstractFormatter; | |
abstract class AbstractColumn | |
{ | |
protected $label; | |
protected $uniqueId; | |
protected $selectPart1; | |
protected $selectPart2 = null; | |
/** | |
* | |
* @var Type\AbstractType | |
*/ | |
protected $type = null; | |
protected $styles = array(); | |
protected $width = 5; | |
protected $isHidden = false; | |
protected $isIdentity = false; | |
protected $userSortEnabled = true; | |
protected $sortDefault = array(); | |
protected $sortActive = null; | |
protected $filterDefaultValue = null; | |
protected $filterDefaultOperation = null; | |
/** | |
* | |
* @var null array | |
*/ | |
protected $filterSelectOptions; | |
protected $filterActive = null; | |
protected $filterActiveValue = ''; | |
protected $userFilterEnabled = true; | |
protected $translationEnabled = false; | |
protected $replaceValues = array(); | |
protected $notReplacedGetEmpty = true; | |
protected $rowClickEnabled = true; | |
protected $rendererParameter = array(); | |
protected $formatter; | |
public function setLabel($name) | |
{ | |
$this->label = (string) $name; | |
} | |
/** | |
* Get the label | |
* | |
* @return string null | |
*/ | |
public function getLabel() | |
{ | |
return $this->label; | |
} | |
public function setUniqueId($id) | |
{ | |
$this->uniqueId = $id; | |
} | |
public function getUniqueId() | |
{ | |
return $this->uniqueId; | |
} | |
public function setSelect($part1, $part2 = null) | |
{ | |
$this->selectPart1 = $part1; | |
$this->selectPart2 = $part2; | |
} | |
public function getSelectPart1() | |
{ | |
return $this->selectPart1; | |
} | |
public function getSelectPart2() | |
{ | |
return $this->selectPart2; | |
} | |
/** | |
* Set the width in "percent" | |
* It will be calculated to 100% dependend on what is displayed | |
* If it's a different output mode like Excel it's dependend on the papersize/orientation | |
* | |
* @param number $percent | |
*/ | |
public function setWidth($percent) | |
{ | |
$this->width = (float) $percent; | |
} | |
/** | |
* Get the width | |
* | |
* @return number | |
*/ | |
public function getWidth() | |
{ | |
return $this->width; | |
} | |
public function setHidden($mode = true) | |
{ | |
$this->isHidden = (bool) $mode; | |
} | |
public function isHidden() | |
{ | |
return (bool) $this->isHidden; | |
} | |
public function setIdentity($mode = true) | |
{ | |
$this->isIdentity = (bool) $mode; | |
// Because IDs are normally hidden | |
$this->setHidden($mode); | |
} | |
public function isIdentity() | |
{ | |
return (bool) $this->isIdentity; | |
} | |
/** | |
* Set the column type | |
* | |
* @param Type\AbstractType $type | |
*/ | |
public function setType(Type\AbstractType $type) | |
{ | |
$this->type = $type; | |
} | |
/** | |
* | |
* @return Type\AbstractType | |
*/ | |
public function getType() | |
{ | |
if ($this->type === null) { | |
$this->type = new Type\String(); | |
} | |
return $this->type; | |
} | |
public function addStyle(Style\AbstractStyle $style) | |
{ | |
$this->styles[] = $style; | |
} | |
public function getStyles() | |
{ | |
return $this->styles; | |
} | |
public function hasStyles() | |
{ | |
if (count($this->styles) > 0) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Is the user allowed to do sort on this column? | |
* | |
* @param boolean $mode | |
*/ | |
public function setUserSortDisabled($mode = true) | |
{ | |
$this->userSortEnabled = (bool) ! $mode; | |
} | |
/** | |
* Is user sort enabled? | |
* | |
* @return boolean | |
*/ | |
public function isUserSortEnabled() | |
{ | |
return (bool) $this->userSortEnabled; | |
} | |
/** | |
* The data will get sorted by this column (by default) | |
* If will be changed by the user per request (POST,GET....) | |
* | |
* @param integer $priority | |
* @param string $direction | |
*/ | |
public function setSortDefault($priority = 1, $direction = 'ASC') | |
{ | |
$this->sortDefault = array( | |
'priority' => $priority, | |
'sortDirection' => $direction | |
); | |
} | |
/** | |
* Get the sort defaults | |
* | |
* @return array | |
*/ | |
public function getSortDefault() | |
{ | |
return $this->sortDefault; | |
} | |
/** | |
* Does this column has sort defaults? | |
* | |
* @return boolean | |
*/ | |
public function hasSortDefault() | |
{ | |
if (count($this->sortDefault) > 0) { | |
return true; | |
} | |
return false; | |
} | |
public function setSortActive($direction = 'ASC') | |
{ | |
$this->sortActive = $direction; | |
} | |
public function isSortActive() | |
{ | |
if ($this->sortActive !== null) { | |
return true; | |
} | |
return false; | |
} | |
public function getSortActiveDirection() | |
{ | |
return $this->sortActive; | |
} | |
/** | |
* | |
* @param boolean $mode | |
*/ | |
public function setUserFilterDisabled($mode = true) | |
{ | |
$this->userFilterEnabled = (bool) ! $mode; | |
} | |
/** | |
* Set the default filterung value (used as long no user filtering getting applied) | |
* Examples | |
* $grid->setFilterDefaultValue('something'); | |
* $grid->setFilterDefaultValue('>20'); | |
* | |
* OPERATORS are ALLOWED (like for the user) | |
* | |
* @param string $value | |
*/ | |
public function setFilterDefaultValue($value = null) | |
{ | |
if ($value != '') { | |
$this->filterDefaultValue = (string) $value; | |
} | |
} | |
public function getFilterDefaultValue() | |
{ | |
return $this->filterDefaultValue; | |
} | |
public function hasFilterDefaultValue() | |
{ | |
if ($this->filterDefaultValue != '') { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public function setFilterDefaultOperation($operation = Filter::LIKE) | |
{ | |
$this->filterDefaultOperation = $operation; | |
} | |
public function getFilterDefaultOperation() | |
{ | |
if ($this->filterDefaultOperation != '') { | |
return $this->filterDefaultOperation; | |
} | |
return $this->getType()->getFilterDefaultOperation(); | |
} | |
public function setFilterSelectOptions(array $options = null, $noSelect = true) | |
{ | |
if ($noSelect === true) { | |
$nothing = array( | |
'' => '-' | |
); | |
$options = array_merge($nothing, $options); | |
} | |
$this->filterSelectOptions = $options; | |
} | |
/** | |
* Unset the filter select options (normal search) | |
*/ | |
public function unsetFilterSelectOptions() | |
{ | |
$this->filterSelectOptions = null; | |
} | |
public function getFilterSelectOptions() | |
{ | |
return $this->filterSelectOptions; | |
} | |
public function hasFilterSelectOptions() | |
{ | |
if (is_array($this->filterSelectOptions)) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* | |
* @param boolean $mode | |
*/ | |
public function setFilterActive($value = '') | |
{ | |
$this->filterActive = (bool) true; | |
$this->filterActiveValue = $value; | |
} | |
/** | |
* | |
* @return boolean | |
*/ | |
public function isFilterActive() | |
{ | |
return $this->filterActive; | |
} | |
public function getFilterActiveValue() | |
{ | |
return $this->filterActiveValue; | |
} | |
/** | |
* | |
* @return boolean | |
*/ | |
public function isUserFilterEnabled() | |
{ | |
return (bool) $this->userFilterEnabled; | |
} | |
/** | |
* Enable data translation | |
* | |
* @param boolean $mode | |
*/ | |
public function setTranslationEnabled($mode = true) | |
{ | |
$this->translationEnabled = (bool) $mode; | |
} | |
/** | |
* Is data translation enabled? | |
* | |
* @return boolean | |
*/ | |
public function isTranslationEnabled() | |
{ | |
return (bool) $this->translationEnabled; | |
} | |
/** | |
* Replace the column values with the applied values | |
* | |
* @param array $values | |
* @param boolean $notReplacedGetEmpty | |
*/ | |
public function setReplaceValues(array $values, $notReplacedGetEmpty = true) | |
{ | |
$this->replaceValues = $values; | |
$this->notReplacedGetEmpty = (bool) $notReplacedGetEmpty; | |
$this->setFilterDefaultOperation(Filter::EQUAL); | |
$this->setFilterSelectOptions($values); | |
} | |
/** | |
* | |
* @return boolean | |
*/ | |
public function hasReplaceValues() | |
{ | |
if (count($this->replaceValues) > 0) | |
return true; | |
return false; | |
} | |
/** | |
* | |
* @return array | |
*/ | |
public function getReplaceValues() | |
{ | |
return $this->replaceValues; | |
} | |
/** | |
* | |
* @return boolean | |
*/ | |
public function notReplacedGetEmpty() | |
{ | |
return $this->notReplacedGetEmpty; | |
} | |
/** | |
* Set parameter for a specific renderer (currently only supported for jqgrid) | |
* | |
* @param string $name | |
* @param mixed $value | |
* @param string $rendererType | |
*/ | |
public function setRendererParameter($name, $value, $rendererType = 'jqgrid') | |
{ | |
if (! isset($this->rendererParameter[$rendererType])) { | |
$this->rendererParameter[$rendererType] = array(); | |
} | |
$parameters = $this->rendererParameter[$rendererType]; | |
$parameters[$name] = $value; | |
$this->rendererParameter[$rendererType] = $parameters; | |
} | |
/** | |
* | |
* @param string $rendererType | |
* @return array | |
*/ | |
public function getRendererParameters($rendererName = 'jqgrid') | |
{ | |
if (! isset($this->rendererParameter[$rendererName])) { | |
$this->rendererParameter[$rendererName] = array(); | |
} | |
return $this->rendererParameter[$rendererName]; | |
} | |
/** | |
* Set a a template formatter | |
* | |
* @param AbstractFormatter $formatter | |
*/ | |
public function setFormatter(AbstractFormatter $formatter) | |
{ | |
$this->formatter = $formatter; | |
} | |
/** | |
* | |
* @param string $rendererName | |
* @return NULL AbstractFormatter | |
*/ | |
public function getFormatter() | |
{ | |
return $this->formatter; | |
} | |
/** | |
* | |
* @param string $rendererType | |
* @return boolean | |
*/ | |
public function hasFormatter() | |
{ | |
if ($this->formatter !== null) { | |
return true; | |
} | |
return false; | |
} | |
public function setRowClickDisabled($mode = true) | |
{ | |
$this->rowClickEnabled = (bool) ! $mode; | |
} | |
public function isRowClickEnabled() | |
{ | |
return $this->rowClickEnabled; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ZfcDatagrid\Column\Formatter; | |
use ZfcDatagrid\Column\AbstractColumn; | |
abstract class AbstractFormatter | |
{ | |
private $columns = array(); | |
private $data = array(); | |
private $rendererName; | |
protected $validRenderers = array(); | |
public function setColumns(array $columns) | |
{ | |
$this->columns = $columns; | |
} | |
/** | |
* | |
* @return array | |
*/ | |
public function getColumns() | |
{ | |
return $this->columns; | |
} | |
public function setRowData(array $data) | |
{ | |
$this->data = $data; | |
} | |
/** | |
* | |
* @return array | |
*/ | |
public function getRowData() | |
{ | |
return $this->data; | |
} | |
public function setRenderer($name = null) | |
{ | |
$this->rendererName = $name; | |
} | |
public function getRendererName() | |
{ | |
return $this->rendererName; | |
} | |
public function isApply() | |
{ | |
if (in_array($this->getRendererName(), $this->validRenderers)) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* | |
* @param AbstractColumn $column | |
* @return string | |
*/ | |
public function format(AbstractColumn $column) | |
{ | |
$data = $this->getRowData(); | |
if ($this->isApply() === true) { | |
return $this->getFormattedValue($data[$column->getUniqueId()], $column->getUniqueId()); | |
} | |
return $data[$column->getUniqueId()]; | |
} | |
/** | |
* | |
* @param string $value | |
* @param string $columnUniqueId | |
* | |
* @return string | |
*/ | |
abstract public function getFormattedValue($value, $columnUniqueId); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ZfcDatagrid\Column\Formatter; | |
use ZfcDatagrid\Column\AbstractColumn; | |
class Email extends AbstractFormatter | |
{ | |
protected $validRenderers = array( | |
'jqgrid', | |
'bootstrapTable' | |
); | |
public function getFormattedValue($value, $columnUniqueId) | |
{ | |
return '<a href="mailto:' . $value . '">' . $value . '</a>'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$col = new Column\Select('displayName'); | |
$col->setLabel('Displayname'); | |
$col->setWidth(25); | |
$col->setSortDefault(1, 'ASC'); | |
$col->setFormatter(new \ZfcDatagrid\Column\Formatter\Email()); | |
$col->addStyle(new Style\Bold()); | |
$dataGrid->addColumn($col); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace ZfcDatagrid; | |
use ZfcDatagrid\Column; | |
use Zend\I18n\Translator\Translator; | |
class PrepareData | |
{ | |
/** | |
* | |
* @var array | |
*/ | |
private $columns = array(); | |
/** | |
* | |
* @var array | |
*/ | |
private $data = array(); | |
/** | |
* | |
* @var array null | |
*/ | |
private $dataPrepared; | |
private $rendererName; | |
/** | |
* | |
* @var Translator | |
*/ | |
private $translator; | |
/** | |
* | |
* @param array $data | |
* @param array $columns | |
*/ | |
public function __construct(array $data, array $columns) | |
{ | |
$this->setData($data); | |
$this->setColumns($columns); | |
} | |
/** | |
* | |
* @param array $columns | |
*/ | |
public function setColumns(array $columns) | |
{ | |
$this->columns = $columns; | |
} | |
/** | |
* | |
* @return array | |
*/ | |
public function getColumns() | |
{ | |
return $this->columns; | |
} | |
/** | |
* | |
* @param array $data | |
*/ | |
public function setData(array $data) | |
{ | |
$this->data = $data; | |
} | |
/** | |
* | |
* @param boolean $raw | |
* @return array | |
*/ | |
public function getData($raw = false) | |
{ | |
if ($raw === true) { | |
return $this->data; | |
} | |
$this->prepare(); | |
return $this->dataPrepared; | |
} | |
public function setRenderer($name = null) | |
{ | |
$this->rendererName = $name; | |
} | |
public function getRendererName(){ | |
return $this->rendererName; | |
} | |
/** | |
* | |
* @param Translator $translator | |
*/ | |
public function setTranslator(Translator $translator) | |
{ | |
$this->translator = $translator; | |
} | |
/** | |
* | |
* @return \Zend\I18n\Translator\Translator | |
*/ | |
public function getTranslator() | |
{ | |
return $this->translator; | |
} | |
/** | |
* | |
* @throws \Exception | |
* @return void | |
*/ | |
public function prepare() | |
{ | |
if (is_array($this->dataPrepared)) { | |
return; | |
} | |
$data = $this->data; | |
foreach ($data as $key => &$row) { | |
$ids = array(); | |
foreach ($this->getColumns() as $column) { | |
/* @var $column \ZfcDatagrid\Column\AbstractColumn */ | |
if (isset($row[$column->getUniqueId()]) && $column->isIdentity() === true) { | |
$ids[] = $row[$column->getUniqueId()]; | |
} | |
/** | |
* Maybe the data come not from another DataSource? | |
*/ | |
if ($column instanceof Column\ExternalData) { | |
// @todo improve the interface... | |
$dataPopulation = $column->getDataPopulation(); | |
foreach ($dataPopulation->getParameters() as $parameter) { | |
$dataPopulation->setParameterValue($parameter['objectParameterName'], $row[$parameter['column']->getUniqueId()]); | |
} | |
$row[$column->getUniqueId()] = $dataPopulation->toString(); | |
} | |
if (! isset($row[$column->getUniqueId()])) { | |
$row[$column->getUniqueId()] = ''; | |
} | |
/** | |
* Replace | |
*/ | |
if ($column->hasReplaceValues() === true) { | |
$replaceValues = $column->getReplaceValues(); | |
if (is_array($row[$column->getUniqueId()])) { | |
foreach ($row[$column->getUniqueId()] as &$value) { | |
if (isset($replaceValues[$value])) { | |
$value = $replaceValues[$value]; | |
} elseif ($column->notReplacedGetEmpty() === true) { | |
$value = ''; | |
} | |
} | |
} else { | |
if (isset($replaceValues[$row[$column->getUniqueId()]])) { | |
$row[$column->getUniqueId()] = $replaceValues[$row[$column->getUniqueId()]]; | |
} elseif ($column->notReplacedGetEmpty() === true) { | |
$row[$column->getUniqueId()] = ''; | |
} | |
} | |
} | |
/* | |
* Type converting | |
*/ | |
$row[$column->getUniqueId()] = $column->getType()->getUserValue($row[$column->getUniqueId()]); | |
/** | |
* Translate (nach typ convertierung -> PhpArray...) | |
*/ | |
if ($column->isTranslationEnabled() === true) { | |
if (is_array($row[$column->getUniqueId()])) { | |
foreach ($row[$column->getUniqueId()] as &$value) { | |
$value = $this->getTranslator()->translate($value); | |
} | |
} else { | |
$row[$column->getUniqueId()] = $this->getTranslator()->translate($row[$column->getUniqueId()]); | |
} | |
} | |
/* | |
* Trim the values | |
*/ | |
if (is_array($row[$column->getUniqueId()])) { | |
array_walk_recursive($row[$column->getUniqueId()], function (&$value) | |
{ | |
$value = trim($value); | |
}); | |
} else { | |
$row[$column->getUniqueId()] = trim($row[$column->getUniqueId()]); | |
} | |
/* | |
* Custom formatter? | |
*/ | |
if($column->hasFormatter($this->getRendererName()) === true){ | |
/* @var $formatter \ZfcDatagrid\Column\Formatter\AbstractFormatter */ | |
$formatter = $column->getFormatter($this->getRendererName()); | |
$formatter->setColumns($this->getColumns()); | |
$formatter->setRowData($row); | |
$formatter->setRenderer($this->getRendererName()); | |
$row[$column->getUniqueId()] = $formatter->format($column); | |
} | |
} | |
// Concat all identity columns | |
if (count($ids) > 0) { | |
$data[$key]['idConcated'] = implode('~', $ids); | |
} | |
} | |
$this->dataPrepared = $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment