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 | public function __construct( |
||
68 | FormFactory $formFactory, |
||
69 | AuthorizationCheckerInterface $checker, |
||
70 | Router $router, |
||
71 | FormatterInterface $formatter, |
||
72 | DataTableLayoutInterface $layout = null |
||
73 | ) { |
||
74 | $this->formFactory = $formFactory; |
||
75 | $this->router = $router; |
||
76 | $this->formatter = $formatter; |
||
|
|||
77 | $this->authorizationChecker = $checker; |
||
78 | $this->layout = null === $layout ? new Bootstrap() : $layout; |
||
79 | $this->optionsResolver = new OptionsResolver(); |
||
80 | //$this->initColumnsDefinitions(); |
||
81 | $this->setDefaultOptions($this->optionsResolver); |
||
82 | $this->configureOptions($this->optionsResolver); |
||
83 | $this->options = $this->optionsResolver->resolve(); |
||
84 | } |
||
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 | private final function setDefaultOptions(OptionsResolver $resolver) |
||
139 | /** |
||
140 | * Configure the table options |
||
141 | * |
||
142 | * @param OptionsResolver $resolver |
||
143 | */ |
||
144 | public function configureOptions(OptionsResolver $resolver){} |
||
145 | /** |
||
146 | * @return array |
||
147 | */ |
||
148 | public function setOptions(array $options) |
||
152 | /** |
||
153 | * @return array |
||
154 | */ |
||
155 | public function getOptions() |
||
159 | /** |
||
160 | * Build the filter form |
||
161 | * |
||
162 | * @param FormBuilder $builder |
||
163 | * |
||
164 | * @return FormBuilder |
||
165 | */ |
||
166 | public function buildFilterForm(FormBuilder $builder) |
||
170 | /** |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getAjaxAdditionalParameters() |
||
177 | /** |
||
178 | * @return string[] should return the content to insert in the rows key(colname) => value(string / html / any) |
||
179 | */ |
||
180 | public function getOutputRows($context = self::VIEW_CONTEXT) |
||
190 | |||
191 | /** |
||
192 | * @see getColumns() same as getColumns but filtered for datatable JS API |
||
193 | */ |
||
194 | public function getClientSideColumns($context = self::VIEW_CONTEXT) |
||
204 | /** |
||
205 | * @param Request $request |
||
206 | */ |
||
207 | public function handleRequest(Request $request) |
||
211 | /** |
||
212 | * @return PaginateRequest |
||
213 | */ |
||
214 | public function getCurrentRequest() |
||
218 | /** |
||
219 | * @return Form|\Symfony\Component\Form\Form |
||
220 | */ |
||
221 | public function getFilterForm() |
||
232 | /** |
||
233 | * Sets the formatter |
||
234 | * |
||
235 | * @param FormatterInterface $formatter |
||
236 | * |
||
237 | * @return void |
||
238 | */ |
||
239 | public function setFormatter(FormatterInterface $formatter) |
||
243 | /** |
||
244 | * @return string a table idenfitier that will be used for ajax requests |
||
245 | */ |
||
246 | public final function getTableId() |
||
250 | /** |
||
251 | * @return \CrossKnowledge\DataTableBundle\Table\Element\Column[] |
||
252 | */ |
||
253 | public function getColumns($context = self::VIEW_CONTEXT) |
||
260 | /** |
||
261 | * Builds the columns definition |
||
262 | */ |
||
263 | protected function initColumnsDefinitions($context = self::VIEW_CONTEXT) |
||
272 | /** |
||
273 | * Sets the table identifier |
||
274 | * |
||
275 | * @return null |
||
276 | */ |
||
277 | public final function setTableId($id) |
||
281 | } |
||
282 |
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: