1 | <?php |
||
18 | abstract class AbstractTable |
||
19 | { |
||
20 | const VIEW_CONTEXT = 'view'; |
||
21 | |||
22 | /** |
||
23 | * @var Router |
||
24 | */ |
||
25 | protected $router; |
||
26 | /** |
||
27 | * @var PaginateRequest |
||
28 | */ |
||
29 | protected $currentRequest; |
||
30 | /** |
||
31 | * @var FormFactory |
||
32 | */ |
||
33 | protected $formFactory; |
||
34 | /** |
||
35 | * @var Form |
||
36 | */ |
||
37 | protected $filterForm; |
||
38 | /** |
||
39 | * @var AuthorizationChecker |
||
40 | */ |
||
41 | protected $authorizationChecker; |
||
42 | |||
43 | /** |
||
44 | * @var Column[] |
||
45 | */ |
||
46 | protected $columns = array(); |
||
47 | protected $columnsInitialized = array(); |
||
48 | |||
49 | /** |
||
50 | * @var OptionsResolver |
||
51 | */ |
||
52 | protected $optionsResolver; |
||
53 | |||
54 | /** |
||
55 | * @var array Key value array of options |
||
56 | */ |
||
57 | protected $options = []; |
||
58 | |||
59 | /** |
||
60 | * @var DataTableLayoutInterface |
||
61 | */ |
||
62 | protected $layout; |
||
63 | /** |
||
64 | * @param FormFactory $formFactory |
||
65 | * @param Router $router |
||
66 | */ |
||
67 | 4 | public function __construct( |
|
85 | /** |
||
86 | * |
||
87 | * Example implementation |
||
88 | |||
89 | public function buildColumns(ColumnBuilder $builder) |
||
90 | { |
||
91 | $builder->add('Learner.FirstName', new Column('First name title', ['width' => '20%'])) |
||
92 | ->add('Learner.Name', new Column('Last name')); |
||
93 | } |
||
94 | * |
||
95 | * @return array key must be the column field name, |
||
96 | * value must be an array of options for https://datatables.net/reference/option/columns |
||
97 | */ |
||
98 | abstract public function buildColumns(ColumnBuilder $builder, $context = self::VIEW_CONTEXT); |
||
99 | /** |
||
100 | * Must return a \Traversable a traversable element that must contain for each element an ArrayAccess such as |
||
101 | * key(colname) => value(db value) |
||
102 | * |
||
103 | * The filter should be used there. |
||
104 | * |
||
105 | * Example of the expected return |
||
106 | return [ |
||
107 | 'first_name' => 'John', |
||
108 | 'last_name' => 'Doe' |
||
109 | ]; |
||
110 | * Example: |
||
111 | return new \PropelCollection(); |
||
112 | * |
||
113 | * @return \Traversable |
||
114 | */ |
||
115 | abstract public function getDataIterator(); |
||
116 | /** |
||
117 | * @return int the total number of rows regardless of filters |
||
118 | */ |
||
119 | abstract public function getUnfilteredCount(); |
||
120 | /** |
||
121 | * @return int|false if there is no such count |
||
122 | */ |
||
123 | abstract public function getFilteredCount(); |
||
124 | |||
125 | 4 | private final function setDefaultOptions(OptionsResolver $resolver) |
|
126 | { |
||
127 | 4 | $resolver->setDefaults([ |
|
128 | 4 | 'layout' => $this->layout, |
|
129 | 4 | 'client_side_filtering' => false, |
|
130 | 4 | 'filter_reload_table_on_change' => false, |
|
131 | 4 | 'template' => 'CrossKnowledgeDataTableBundle::default_table.html.twig', |
|
132 | 4 | 'data_table_custom_options' => [], |
|
133 | 'has_filter_form' => function() { |
||
134 | return $this->getFilterForm()->count()>1; |
||
135 | } |
||
136 | 4 | ]); |
|
137 | 4 | } |
|
138 | /** |
||
139 | * Configure the table options |
||
140 | * |
||
141 | * @param OptionsResolver $resolver |
||
142 | */ |
||
143 | public function configureOptions(OptionsResolver $resolver){} |
||
144 | /** |
||
145 | * @return array |
||
146 | */ |
||
147 | 2 | public function setOptions(array $options) |
|
151 | /** |
||
152 | * @return array |
||
153 | */ |
||
154 | 2 | public function getOptions() |
|
158 | /** |
||
159 | * Build the filter form |
||
160 | * |
||
161 | * @param FormBuilder $builder |
||
162 | * |
||
163 | * @return FormBuilder |
||
164 | */ |
||
165 | public function buildFilterForm(FormBuilder $builder) |
||
169 | /** |
||
170 | * @return string |
||
171 | */ |
||
172 | public function getAjaxAdditionalParameters() |
||
176 | /** |
||
177 | * @return string[] should return the content to insert in the rows key(colname) => value(string / html / any) |
||
178 | */ |
||
179 | 3 | public function getOutputRows($context = self::VIEW_CONTEXT) |
|
189 | |||
190 | /** |
||
191 | * @see getColumns() same as getColumns but filtered for datatable JS API |
||
192 | */ |
||
193 | 2 | public function getClientSideColumns($context = self::VIEW_CONTEXT) |
|
203 | /** |
||
204 | * @param Request $request |
||
205 | */ |
||
206 | 1 | public function handleRequest(Request $request) |
|
210 | /** |
||
211 | * @return PaginateRequest |
||
212 | */ |
||
213 | 1 | public function getCurrentRequest() |
|
217 | /** |
||
218 | * @return Form|\Symfony\Component\Form\Form |
||
219 | */ |
||
220 | public function getFilterForm() |
||
231 | /** |
||
232 | * @param bool $loadData |
||
233 | * @return array key value of variables accessible for renderers. |
||
234 | */ |
||
235 | 2 | public function buildView($loadData = true) |
|
236 | { |
||
237 | $viewParameters = [ |
||
254 | |||
255 | /** |
||
256 | * Sets the formatter |
||
257 | * |
||
258 | * @param FormatterInterface $formatter |
||
259 | * |
||
260 | * @return void |
||
261 | */ |
||
262 | public function setFormatter(FormatterInterface $formatter) |
||
266 | /** |
||
267 | * @return string a table idenfitier that will be used for ajax requests |
||
268 | */ |
||
269 | public final function getTableId() |
||
273 | /** |
||
274 | * @return \CrossKnowledge\DataTableBundle\Table\Element\Column[] |
||
275 | */ |
||
276 | public function getColumns($context = self::VIEW_CONTEXT) |
||
283 | /** |
||
284 | * Builds the columns definition |
||
285 | */ |
||
286 | protected function initColumnsDefinitions($context = self::VIEW_CONTEXT) |
||
295 | /** |
||
296 | * Sets the table identifier |
||
297 | * |
||
298 | * @return null |
||
299 | */ |
||
300 | 4 | public final function setTableId($id) |
|
304 | } |
||
305 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: