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

Commit 593c3a2

Browse files
Add attributes mapped, is_association and data_source (#914)
* Add attributes mapped, is_association and data_source * allow to create column not mapped, and not defined with DQL * allow to use data from another column and transform it * add documentation
1 parent 4096b36 commit 593c3a2

13 files changed

+267
-93
lines changed

Datatable/Column/AbstractColumn.php

+165-4
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,39 @@ abstract class AbstractColumn implements ColumnInterface
296296
* @var bool
297297
*/
298298
protected $sentInResponse;
299+
300+
/**
301+
* If this column displays the total of its cells in its head
302+
* Default: false.
303+
*
304+
* @var bool
305+
*/
306+
protected $computeTotal;
307+
308+
/**
309+
* Contains the eventually computed total of the column.
310+
*
311+
* @var mixed
312+
*/
313+
protected $total;
314+
315+
/**
316+
* If the column represents a real column in the database.
317+
*
318+
* @var bool
319+
*/
320+
protected $mapped;
321+
322+
/**
323+
* Force the association property.
324+
*/
325+
protected $isAssociation;
326+
327+
/**
328+
* The source of data, if different from title and no dql specified.
329+
*/
330+
protected $dataSource;
331+
299332
//-------------------------------------------------
300333
// Options
301334
//-------------------------------------------------
@@ -326,6 +359,10 @@ public function configureOptions(OptionsResolver $resolver)
326359
'type_of_field' => null,
327360
'responsive_priority' => null,
328361
'sent_in_response' => true,
362+
'compute_total' => false,
363+
'mapped' => true,
364+
'is_association' => false,
365+
'data_source' => null,
329366
]);
330367

331368
$resolver->setAllowedTypes('cell_type', ['null', 'string']);
@@ -347,6 +384,10 @@ public function configureOptions(OptionsResolver $resolver)
347384
$resolver->setAllowedTypes('type_of_field', ['null', 'string']);
348385
$resolver->setAllowedTypes('responsive_priority', ['null', 'int']);
349386
$resolver->setAllowedTypes('sent_in_response', ['bool']);
387+
$resolver->setAllowedTypes('compute_total', ['bool']);
388+
$resolver->setAllowedTypes('mapped', ['bool']);
389+
$resolver->setAllowedTypes('is_association', ['bool']);
390+
$resolver->setAllowedTypes('data_source', ['string', 'null']);
350391

351392
$resolver->setAllowedValues('cell_type', [null, 'th', 'td']);
352393
$resolver->setAllowedValues('join_type', [null, 'join', 'leftJoin', 'innerJoin']);
@@ -392,6 +433,10 @@ public function isAssociation()
392433
*/
393434
public function isToManyAssociation()
394435
{
436+
if ($this->isAssociation) {
437+
return true;
438+
}
439+
395440
if (true === $this->isAssociation() && null !== $this->typeOfAssociation) {
396441
if (\in_array(ClassMetadataInfo::ONE_TO_MANY, $this->typeOfAssociation, true) || \in_array(ClassMetadataInfo::MANY_TO_MANY, $this->typeOfAssociation, true)) {
397442
return true;
@@ -430,9 +475,9 @@ public function addDataToOutputArray(array &$row)
430475
/**
431476
* {@inheritdoc}
432477
*/
433-
public function renderCellContent(array &$row)
478+
public function renderCellContent(array &$row, array &$resultRow)
434479
{
435-
$this->isToManyAssociation() ? $this->renderToMany($row) : $this->renderSingleField($row);
480+
$this->isToManyAssociation() ? $this->renderToMany($row, $resultRow) : $this->renderSingleField($row, $resultRow);
436481
}
437482

438483
/**
@@ -976,8 +1021,6 @@ public function setTypeOfAssociation($typeOfAssociation)
9761021
}
9771022

9781023
/**
979-
* Add a typeOfAssociation.
980-
*
9811024
* @param int $typeOfAssociation
9821025
*
9831026
* @return $this
@@ -1028,4 +1071,122 @@ public function setSentInResponse($sentInResponse)
10281071

10291072
return $this;
10301073
}
1074+
1075+
/**
1076+
* Get computeTotal.
1077+
*
1078+
* @return bool
1079+
*/
1080+
public function getComputeTotal()
1081+
{
1082+
return $this->computeTotal;
1083+
}
1084+
1085+
/**
1086+
* Set sentIntResponse.
1087+
*
1088+
* @param bool $computeTotal
1089+
*
1090+
* @return $this
1091+
*/
1092+
public function setComputeTotal($computeTotal)
1093+
{
1094+
$this->computeTotal = $computeTotal;
1095+
1096+
return $this;
1097+
}
1098+
1099+
/**
1100+
* Get total.
1101+
*/
1102+
public function getTotal()
1103+
{
1104+
return $this->total;
1105+
}
1106+
1107+
/**
1108+
* Set total.
1109+
*
1110+
* @param $total
1111+
*
1112+
* @return $this
1113+
*/
1114+
public function setTotal($total)
1115+
{
1116+
$this->total = $total;
1117+
1118+
return $this;
1119+
}
1120+
1121+
/**
1122+
* Get mapped.
1123+
*
1124+
* @return bool
1125+
*/
1126+
public function getMapped()
1127+
{
1128+
return $this->mapped;
1129+
}
1130+
1131+
/**
1132+
* Set mapped.
1133+
*
1134+
* @param bool $mapped
1135+
*
1136+
* @return $this
1137+
*/
1138+
public function setMapped($mapped)
1139+
{
1140+
$this->mapped = $mapped;
1141+
1142+
return $this;
1143+
}
1144+
1145+
/**
1146+
* Get isAssociation.
1147+
*
1148+
* @return bool
1149+
*/
1150+
public function getIsAssociation()
1151+
{
1152+
return $this->isAssociation;
1153+
}
1154+
1155+
/**
1156+
* Set isAssociation.
1157+
*
1158+
* @param bool $isAssociation
1159+
*
1160+
* @return $this
1161+
*/
1162+
public function setIsAssociation($isAssociation)
1163+
{
1164+
$this->isAssociation = $isAssociation;
1165+
1166+
return $this;
1167+
}
1168+
1169+
/**
1170+
* Get data source.
1171+
*
1172+
* @return string|null
1173+
*/
1174+
public function getDataSource()
1175+
{
1176+
return $this->isAssociation;
1177+
}
1178+
1179+
/**
1180+
* Set data source.
1181+
*
1182+
* @param string|null $dataSource
1183+
*
1184+
* @return $this
1185+
*/
1186+
public function setDataSource($dataSource)
1187+
{
1188+
$this->dataSource = $dataSource;
1189+
1190+
return $this;
1191+
}
10311192
}

Datatable/Column/ActionColumn.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function addDataToOutputArray(array &$row)
7272
/**
7373
* {@inheritdoc}
7474
*/
75-
public function renderSingleField(array &$row)
75+
public function renderSingleField(array &$row, array &$resultRow)
7676
{
7777
$parameters = [];
7878
$attributes = [];
@@ -132,7 +132,7 @@ public function renderSingleField(array &$row)
132132
}
133133
}
134134

135-
$row[$this->getIndex()] = $this->twig->render(
135+
$resultRow[$this->getIndex()] = $this->twig->render(
136136
$this->getCellContentTemplate(),
137137
[
138138
'actions' => $this->actions,
@@ -149,7 +149,7 @@ public function renderSingleField(array &$row)
149149
/**
150150
* {@inheritdoc}
151151
*/
152-
public function renderToMany(array &$row)
152+
public function renderToMany(array &$row, array &$resultRow)
153153
{
154154
throw new Exception('ActionColumn::renderToMany(): This function should never be called.');
155155
}

Datatable/Column/ArrayColumn.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class ArrayColumn extends Column
1616
/**
1717
* {@inheritdoc}
1818
*/
19-
public function renderSingleField(array &$row)
19+
public function renderSingleField(array &$row, array &$resultRow)
2020
{
2121
$row[$this->data] = $this->arrayToString($row[$this->data] ?? []);
2222

23-
return parent::renderSingleField($row);
23+
return parent::renderSingleField($row, $resultRow);
2424
}
2525

2626
/**

Datatable/Column/AttributeColumn.php

+13-7
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ class AttributeColumn extends AbstractColumn
3737
/**
3838
* {@inheritdoc}
3939
*/
40-
public function renderSingleField(array &$row)
40+
public function renderSingleField(array &$row, array &$resultRow)
4141
{
42-
$renderAttributes = [];
43-
4442
$renderAttributes = \call_user_func($this->attributes, $row);
4543

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

56-
$this->accessor->setValue($row, $path, $content);
54+
$this->accessor->setValue($resultRow, $path, $content);
5755
}
5856

5957
/**
6058
* {@inheritdoc}
6159
*/
62-
public function renderToMany(array &$row)
60+
public function renderToMany(array &$row, array &$resultRow)
6361
{
6462
$value = null;
6563
$path = Helper::getDataPropertyPath($this->data, $value);
@@ -83,7 +81,7 @@ public function renderToMany(array &$row)
8381
$currentObjectPath
8482
);
8583

86-
$this->accessor->setValue($row, $currentPath, $content);
84+
$this->accessor->setValue($resultRow, $currentPath, $content);
8785
}
8886
}
8987
// no placeholder - leave this blank
@@ -122,6 +120,8 @@ public function getColumnType()
122120
//-------------------------------------------------
123121

124122
/**
123+
* Config options.
124+
*
125125
* @return $this
126126
*/
127127
public function configureOptions(OptionsResolver $resolver)
@@ -140,6 +140,8 @@ public function configureOptions(OptionsResolver $resolver)
140140
}
141141

142142
/**
143+
* Get attributes.
144+
*
143145
* @return Attributes[]
144146
*/
145147
public function getAttributes()
@@ -148,6 +150,8 @@ public function getAttributes()
148150
}
149151

150152
/**
153+
* Set attributes.
154+
*
151155
* @param Closure $attributes
152156
*
153157
* @throws Exception
@@ -168,9 +172,11 @@ public function setAttributes($attributes)
168172
/**
169173
* Render template.
170174
*
175+
* @param string|null $data
176+
*
171177
* @return mixed|string
172178
*/
173-
private function renderTemplate(?string $data)
179+
private function renderTemplate($data)
174180
{
175181
return $this->twig->render(
176182
$this->getCellContentTemplate(),

Datatable/Column/BooleanColumn.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class BooleanColumn extends AbstractColumn
7272
/**
7373
* {@inheritdoc}
7474
*/
75-
public function renderSingleField(array &$row)
75+
public function renderSingleField(array &$row, array &$resultRow)
7676
{
7777
$path = Helper::getDataPropertyPath($this->data);
7878

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

86-
$this->accessor->setValue($row, $path, $content);
86+
$this->accessor->setValue($resultRow, $path, $content);
8787
}
8888

8989
return $this;
@@ -92,7 +92,7 @@ public function renderSingleField(array &$row)
9292
/**
9393
* {@inheritdoc}
9494
*/
95-
public function renderToMany(array &$row)
95+
public function renderToMany(array &$row, array &$resultRow)
9696
{
9797
$value = null;
9898
$path = Helper::getDataPropertyPath($this->data, $value);
@@ -115,7 +115,7 @@ public function renderToMany(array &$row)
115115
$content = $this->renderTemplate($this->accessor->getValue($row, $currentPath));
116116
}
117117

118-
$this->accessor->setValue($row, $currentPath, $content);
118+
$this->accessor->setValue($resultRow, $currentPath, $content);
119119
}
120120
}
121121
// no placeholder - leave this blank

0 commit comments

Comments
 (0)