Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Add attributes mapped, is_association and data_source #914

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 165 additions & 4 deletions Datatable/Column/AbstractColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,39 @@ abstract class AbstractColumn implements ColumnInterface
* @var bool
*/
protected $sentInResponse;

/**
* If this column displays the total of its cells in its head
* Default: false.
*
* @var bool
*/
protected $computeTotal;

/**
* Contains the eventually computed total of the column.
*
* @var mixed
*/
protected $total;

/**
* If the column represents a real column in the database.
*
* @var bool
*/
protected $mapped;

/**
* Force the association property.
*/
protected $isAssociation;

/**
* The source of data, if different from title and no dql specified.
*/
protected $dataSource;

//-------------------------------------------------
// Options
//-------------------------------------------------
Expand Down Expand Up @@ -326,6 +359,10 @@ public function configureOptions(OptionsResolver $resolver)
'type_of_field' => null,
'responsive_priority' => null,
'sent_in_response' => true,
'compute_total' => false,
'mapped' => true,
'is_association' => false,
'data_source' => null,
]);

$resolver->setAllowedTypes('cell_type', ['null', 'string']);
Expand All @@ -347,6 +384,10 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setAllowedTypes('type_of_field', ['null', 'string']);
$resolver->setAllowedTypes('responsive_priority', ['null', 'int']);
$resolver->setAllowedTypes('sent_in_response', ['bool']);
$resolver->setAllowedTypes('compute_total', ['bool']);
$resolver->setAllowedTypes('mapped', ['bool']);
$resolver->setAllowedTypes('is_association', ['bool']);
$resolver->setAllowedTypes('data_source', ['string', 'null']);

$resolver->setAllowedValues('cell_type', [null, 'th', 'td']);
$resolver->setAllowedValues('join_type', [null, 'join', 'leftJoin', 'innerJoin']);
Expand Down Expand Up @@ -392,6 +433,10 @@ public function isAssociation()
*/
public function isToManyAssociation()
{
if ($this->isAssociation) {
return true;
}

if (true === $this->isAssociation() && null !== $this->typeOfAssociation) {
if (\in_array(ClassMetadataInfo::ONE_TO_MANY, $this->typeOfAssociation, true) || \in_array(ClassMetadataInfo::MANY_TO_MANY, $this->typeOfAssociation, true)) {
return true;
Expand Down Expand Up @@ -430,9 +475,9 @@ public function addDataToOutputArray(array &$row)
/**
* {@inheritdoc}
*/
public function renderCellContent(array &$row)
public function renderCellContent(array &$row, array &$resultRow)
{
$this->isToManyAssociation() ? $this->renderToMany($row) : $this->renderSingleField($row);
$this->isToManyAssociation() ? $this->renderToMany($row, $resultRow) : $this->renderSingleField($row, $resultRow);
}

/**
Expand Down Expand Up @@ -976,8 +1021,6 @@ public function setTypeOfAssociation($typeOfAssociation)
}

/**
* Add a typeOfAssociation.
*
* @param int $typeOfAssociation
*
* @return $this
Expand Down Expand Up @@ -1028,4 +1071,122 @@ public function setSentInResponse($sentInResponse)

return $this;
}

/**
* Get computeTotal.
*
* @return bool
*/
public function getComputeTotal()
{
return $this->computeTotal;
}

/**
* Set sentIntResponse.
*
* @param bool $computeTotal
*
* @return $this
*/
public function setComputeTotal($computeTotal)
{
$this->computeTotal = $computeTotal;

return $this;
}

/**
* Get total.
*/
public function getTotal()
{
return $this->total;
}

/**
* Set total.
*
* @param $total
*
* @return $this
*/
public function setTotal($total)
{
$this->total = $total;

return $this;
}

/**
* Get mapped.
*
* @return bool
*/
public function getMapped()
{
return $this->mapped;
}

/**
* Set mapped.
*
* @param bool $mapped
*
* @return $this
*/
public function setMapped($mapped)
{
$this->mapped = $mapped;

return $this;
}

/**
* Get isAssociation.
*
* @return bool
*/
public function getIsAssociation()
{
return $this->isAssociation;
}

/**
* Set isAssociation.
*
* @param bool $isAssociation
*
* @return $this
*/
public function setIsAssociation($isAssociation)
{
$this->isAssociation = $isAssociation;

return $this;
}

/**
* Get data source.
*
* @return string|null
*/
public function getDataSource()
{
return $this->isAssociation;
}

/**
* Set data source.
*
* @param string|null $dataSource
*
* @return $this
*/
public function setDataSource($dataSource)
{
$this->dataSource = $dataSource;

return $this;
}
}
6 changes: 3 additions & 3 deletions Datatable/Column/ActionColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function addDataToOutputArray(array &$row)
/**
* {@inheritdoc}
*/
public function renderSingleField(array &$row)
public function renderSingleField(array &$row, array &$resultRow)
{
$parameters = [];
$attributes = [];
Expand Down Expand Up @@ -132,7 +132,7 @@ public function renderSingleField(array &$row)
}
}

$row[$this->getIndex()] = $this->twig->render(
$resultRow[$this->getIndex()] = $this->twig->render(
$this->getCellContentTemplate(),
[
'actions' => $this->actions,
Expand All @@ -149,7 +149,7 @@ public function renderSingleField(array &$row)
/**
* {@inheritdoc}
*/
public function renderToMany(array &$row)
public function renderToMany(array &$row, array &$resultRow)
{
throw new Exception('ActionColumn::renderToMany(): This function should never be called.');
}
Expand Down
4 changes: 2 additions & 2 deletions Datatable/Column/ArrayColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class ArrayColumn extends Column
/**
* {@inheritdoc}
*/
public function renderSingleField(array &$row)
public function renderSingleField(array &$row, array &$resultRow)
{
$row[$this->data] = $this->arrayToString($row[$this->data] ?? []);

return parent::renderSingleField($row);
return parent::renderSingleField($row, $resultRow);
}

/**
Expand Down
20 changes: 13 additions & 7 deletions Datatable/Column/AttributeColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ class AttributeColumn extends AbstractColumn
/**
* {@inheritdoc}
*/
public function renderSingleField(array &$row)
public function renderSingleField(array &$row, array &$resultRow)
{
$renderAttributes = [];

$renderAttributes = \call_user_func($this->attributes, $row);

$path = Helper::getDataPropertyPath($this->data);
Expand All @@ -53,13 +51,13 @@ public function renderSingleField(array &$row)
]
);

$this->accessor->setValue($row, $path, $content);
$this->accessor->setValue($resultRow, $path, $content);
}

/**
* {@inheritdoc}
*/
public function renderToMany(array &$row)
public function renderToMany(array &$row, array &$resultRow)
{
$value = null;
$path = Helper::getDataPropertyPath($this->data, $value);
Expand All @@ -83,7 +81,7 @@ public function renderToMany(array &$row)
$currentObjectPath
);

$this->accessor->setValue($row, $currentPath, $content);
$this->accessor->setValue($resultRow, $currentPath, $content);
}
}
// no placeholder - leave this blank
Expand Down Expand Up @@ -122,6 +120,8 @@ public function getColumnType()
//-------------------------------------------------

/**
* Config options.
*
* @return $this
*/
public function configureOptions(OptionsResolver $resolver)
Expand All @@ -140,6 +140,8 @@ public function configureOptions(OptionsResolver $resolver)
}

/**
* Get attributes.
*
* @return Attributes[]
*/
public function getAttributes()
Expand All @@ -148,6 +150,8 @@ public function getAttributes()
}

/**
* Set attributes.
*
* @param Closure $attributes
*
* @throws Exception
Expand All @@ -168,9 +172,11 @@ public function setAttributes($attributes)
/**
* Render template.
*
* @param string|null $data
*
* @return mixed|string
*/
private function renderTemplate(?string $data)
private function renderTemplate($data)
{
return $this->twig->render(
$this->getCellContentTemplate(),
Expand Down
8 changes: 4 additions & 4 deletions Datatable/Column/BooleanColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class BooleanColumn extends AbstractColumn
/**
* {@inheritdoc}
*/
public function renderSingleField(array &$row)
public function renderSingleField(array &$row, array &$resultRow)
{
$path = Helper::getDataPropertyPath($this->data);

Expand All @@ -83,7 +83,7 @@ public function renderSingleField(array &$row)
$content = $this->renderTemplate($this->accessor->getValue($row, $path));
}

$this->accessor->setValue($row, $path, $content);
$this->accessor->setValue($resultRow, $path, $content);
}

return $this;
Expand All @@ -92,7 +92,7 @@ public function renderSingleField(array &$row)
/**
* {@inheritdoc}
*/
public function renderToMany(array &$row)
public function renderToMany(array &$row, array &$resultRow)
{
$value = null;
$path = Helper::getDataPropertyPath($this->data, $value);
Expand All @@ -115,7 +115,7 @@ public function renderToMany(array &$row)
$content = $this->renderTemplate($this->accessor->getValue($row, $currentPath));
}

$this->accessor->setValue($row, $currentPath, $content);
$this->accessor->setValue($resultRow, $currentPath, $content);
}
}
// no placeholder - leave this blank
Expand Down
Loading