|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DoctrineDatatable; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
6
|
|
|
use DoctrineDatatable\Exception\MissingData; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Editortable. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Editortable extends Datatable |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
private $rootClass; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* {@inheritdoc} |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(QueryBuilder $query, string $identifier, array $columns, ?int $resultPerPage = self::RESULT_PER_PAGE) |
|
22
|
|
|
{ |
|
23
|
|
|
parent::__construct($query, $identifier, $columns, $resultPerPage); |
|
24
|
|
|
$this->rootClass = $query->getRootEntities()[0]; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* PRIVATE METHODS. |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @author Mathieu Petrini <[email protected]> |
|
33
|
|
|
* |
|
34
|
|
|
* @param object $entity |
|
35
|
|
|
* @param string $field |
|
36
|
|
|
* @param mixed $value |
|
37
|
|
|
* |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
private function processValue(object $entity, string $field, $value): array |
|
41
|
|
|
{ |
|
42
|
|
|
$ccField = self::toUpperCamelCase($field); |
|
43
|
|
|
|
|
44
|
|
|
$field = property_exists($entity, $ccField) ? $ccField : $field; |
|
45
|
|
|
if (property_exists($entity, $field)) { |
|
46
|
|
|
$associations = $this->query->getEntityManager()->getClassMetadata($this->rootClass)->associationMappings; |
|
47
|
|
|
if (isset($associations[$field])) { |
|
48
|
|
|
$value = $this->query->getEntityManager()->getRepository( |
|
49
|
|
|
$associations[$field]['targetEntity'] |
|
50
|
|
|
)->find($value); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return array( |
|
55
|
|
|
'field' => $field, |
|
56
|
|
|
'setter' => 'set'.$ccField, |
|
57
|
|
|
'value' => $value, |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @author Mathieu Petrini <[email protected]> |
|
63
|
|
|
* |
|
64
|
|
|
* @param object $entity |
|
65
|
|
|
* @param array $data |
|
66
|
|
|
*/ |
|
67
|
|
|
private function processRowEditing(object $entity, array $data): void |
|
68
|
|
|
{ |
|
69
|
|
|
foreach ($data as $field => $value) { |
|
70
|
|
|
$property = $this->processValue($entity, $field, $value); |
|
71
|
|
|
|
|
72
|
|
|
if (isset($entity->{$property['field']})) { |
|
73
|
|
|
$entity->{$property['field']} = $property['value']; |
|
74
|
|
|
} elseif (method_exists($entity, $property['setter'])) { |
|
75
|
|
|
$entity->{$property['setter']}($property['value']); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @author Mathieu Petrini <[email protected]> |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $params |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
private function processEditing(array $params): array |
|
88
|
|
|
{ |
|
89
|
|
|
$entities = array(); |
|
90
|
|
|
$repository = $this->query->getEntityManager()->getRepository($this->rootClass); |
|
91
|
|
|
foreach ($params['data'] as $id => $row) { |
|
92
|
|
|
$entity = $repository->find($id); |
|
93
|
|
|
if (\is_object($entity)) { |
|
94
|
|
|
$this->processRowEditing($entity, $row); |
|
95
|
|
|
$entities[] = $entity; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $entities; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* PUBLIC METHODS. |
|
104
|
|
|
*/ |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @author Mathieu Petrini <[email protected]> |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $params |
|
110
|
|
|
* |
|
111
|
|
|
* @return array |
|
112
|
|
|
* |
|
113
|
|
|
* @throws MissingData |
|
114
|
|
|
*/ |
|
115
|
|
|
public function edit(array $params): array |
|
116
|
|
|
{ |
|
117
|
|
|
if (!isset($params['data'])) { |
|
118
|
|
|
throw new MissingData(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $this->processEditing($params); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $field |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public static function toUpperCamelCase(string $field): string |
|
130
|
|
|
{ |
|
131
|
|
|
return preg_replace_callback( |
|
132
|
|
|
'/_(.?)/', |
|
133
|
|
|
function (array $matches): string { |
|
134
|
|
|
return strtoupper($matches[1]); |
|
135
|
|
|
}, |
|
136
|
|
|
ucfirst($field) |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|