Total Complexity | 40 |
Total Lines | 372 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ModelAdminPlus often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ModelAdminPlus, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | abstract class ModelAdminPlus extends ModelAdmin |
||
34 | { |
||
35 | const EXPORT_FIELDS = "export_fields"; |
||
36 | |||
37 | /** |
||
38 | * Automatically convert date fields on gridfields |
||
39 | * to use `Date.Nice`. |
||
40 | * |
||
41 | * @var boolean |
||
42 | */ |
||
43 | private static $auto_convert_dates = true; |
||
44 | |||
45 | |||
46 | /** |
||
47 | * Automatically convert DB text fields to AutoComplete fields |
||
48 | * |
||
49 | * @var boolean |
||
50 | */ |
||
51 | private static $convert_to_autocomplete = true; |
||
52 | |||
53 | private static $allowed_actions = [ |
||
|
|||
54 | "SearchForm" |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * List of currently registered ModelAdminSnippets, that is represented as |
||
59 | * a list of classnames. |
||
60 | * |
||
61 | * These snippets are then setup when ModelAdminPlus is initilised and |
||
62 | * rendered into the ModelAdminPlus content template. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | private static $registered_snippets = []; |
||
67 | |||
68 | /** |
||
69 | * Setup |
||
70 | */ |
||
71 | public function getSnippets() |
||
72 | { |
||
73 | $snippets = ArrayList::create(); |
||
74 | |||
75 | // Setup any model admin plus snippets |
||
76 | foreach ($this->config()->registered_snippets as $snippet) { |
||
77 | $snippet = Injector::inst()->create($snippet); |
||
78 | $snippet->setParent($this); |
||
79 | $snippets->add($snippet); |
||
80 | } |
||
81 | |||
82 | $snippets = $snippets->sort("Order", "DESC"); |
||
83 | |||
84 | $this->extend("updateSnippets", $snippets); |
||
85 | |||
86 | return $snippets; |
||
87 | } |
||
88 | |||
89 | public function init() |
||
90 | { |
||
91 | parent::init(); |
||
92 | |||
93 | // Require additional CSS |
||
94 | Requirements::css("i-lateral/silverstripe-modeladminplus:client/dist/css/admin.css"); |
||
95 | |||
96 | $clear = $this->getRequest()->getVar("clear"); |
||
97 | |||
98 | if (isset($clear) && $clear == 1) { |
||
99 | $this->clearSearchSession(); |
||
100 | // Remove clear flag |
||
101 | return $this->redirect( |
||
102 | $this->Link( |
||
103 | $this->sanitiseClassName($this->modelClass) |
||
104 | ) |
||
105 | ); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Get the default export fields for the current model. |
||
111 | * |
||
112 | * First this checks if there is an `export_fields` config variable set on |
||
113 | * the model class, if not, it reverts to the default behaviour. |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | public function getExportFields() |
||
118 | { |
||
119 | $export_fields = Config::inst()->get( |
||
120 | $this->modelClass, |
||
121 | self::EXPORT_FIELDS |
||
122 | ); |
||
123 | |||
124 | if (isset($export_fields) && is_array($export_fields)) { |
||
125 | $fields = $export_fields; |
||
126 | } else { |
||
127 | $fields = parent::getExportFields(); |
||
128 | } |
||
129 | |||
130 | $this->extend("updateExportFields", $fields); |
||
131 | |||
132 | return $fields; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Get the name of the session to be useed by this model admin's search |
||
137 | * form. |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | public function getSearchSessionName() |
||
142 | { |
||
143 | $curr = $this->sanitiseClassName(self::class); |
||
144 | $model = $this->sanitiseClassName($this->modelClass); |
||
145 | return $curr . "." . $model; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Empty the current search session |
||
150 | * |
||
151 | * @return Session |
||
152 | */ |
||
153 | public function clearSearchSession() |
||
154 | { |
||
155 | $session = $this->getRequest()->getSession(); |
||
156 | return $session->clear($this->getSearchSessionName()); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get the current search session |
||
161 | * |
||
162 | * @return Session |
||
163 | */ |
||
164 | public function getSearchSession() |
||
165 | { |
||
166 | $session = $this->getRequest()->getSession(); |
||
167 | return $session->get($this->getSearchSessionName()); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Set some data to a search session. This needs to be an array of |
||
172 | * data (like the data submitted by a form). |
||
173 | * |
||
174 | * @param array $data An array of data to store in the session |
||
175 | * |
||
176 | * @return self |
||
177 | */ |
||
178 | public function setSearchSession($data) |
||
179 | { |
||
180 | $session = $this->getRequest()->getSession(); |
||
181 | return $session->set($this->getSearchSessionName(), $data); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get the current search results, combined with any saved |
||
186 | * search results and resturn (as an array). |
||
187 | * |
||
188 | * @return array |
||
189 | */ |
||
190 | public function getSearchData() |
||
191 | { |
||
192 | $data = $this->getSearchSession(); |
||
193 | |||
194 | if (!$data || $data && !is_array($data)) { |
||
195 | $data = []; |
||
196 | } |
||
197 | |||
198 | return $data; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Overwrite the default search list to account for the new session based |
||
203 | * search data. |
||
204 | * |
||
205 | * You can override how ModelAdmin returns DataObjects by either overloading this method, |
||
206 | * or defining an extension to ModelAdmin that implements the `updateList` method |
||
207 | * (and takes a {@link \SilverStripe\ORM\DataList} as the |
||
208 | * first argument). |
||
209 | * |
||
210 | * @return \SilverStripe\ORM\DataList |
||
211 | */ |
||
212 | public function getList() |
||
213 | { |
||
214 | $context = $this->getSearchContext(); |
||
215 | $params = $this->getSearchData(); |
||
216 | |||
217 | if (is_array($params)) { |
||
218 | $params = ArrayLib::array_map_recursive('trim', $params); |
||
219 | |||
220 | // Parse all DateFields to handle user input non ISO 8601 dates |
||
221 | foreach ($context->getFields() as $field) { |
||
222 | if ($field instanceof DatetimeField && !empty($params[$field->getName()])) { |
||
223 | $params[$field->getName()] = date( |
||
224 | 'Y-m-d', |
||
225 | strtotime($params[$field->getName()]) |
||
226 | ); |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | |||
231 | $list = $context->getResults($params); |
||
232 | |||
233 | $this->extend('updateList', $list); |
||
234 | |||
235 | return $list; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * Gets a list of fields that have been searched |
||
240 | * |
||
241 | * @return SilverStripe\ORM\ArrayList |
||
242 | */ |
||
243 | public function SearchSummary() |
||
244 | { |
||
245 | $context = $this->getSearchContext(); |
||
246 | $params = $this->getSearchData(); |
||
247 | $context->setSearchParams($params); |
||
248 | |||
249 | return $context->getSummary(); |
||
250 | } |
||
251 | |||
252 | |||
253 | /** |
||
254 | * Add bulk editor to Edit Form |
||
255 | * |
||
256 | * @param int|null $id |
||
257 | * @param FieldList $fields |
||
258 | * |
||
259 | * @return Form A Form object |
||
260 | */ |
||
261 | public function getEditForm($id = null, $fields = null) |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Overwrite default search form |
||
288 | * |
||
289 | * @return Form |
||
290 | */ |
||
291 | public function SearchForm() |
||
292 | { |
||
293 | $form = parent::SearchForm(); |
||
294 | $fields = $form->Fields(); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Set the session from the submitted form data (and redirect back) |
||
380 | * |
||
381 | * @param array $data Submitted form |
||
382 | * @param Form $form The current form |
||
383 | * |
||
384 | * @return HTTPResponse |
||
385 | */ |
||
386 | public function search($data, $form) |
||
407 |