Conditions | 13 |
Paths | 388 |
Total Lines | 171 |
Code Lines | 122 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
165 | public function getCMSFields() |
||
166 | { |
||
167 | $fields = parent::getCMSFields(); |
||
168 | |||
169 | $fields->removeFieldsFromTab('Root', [ |
||
170 | 'Pages', |
||
171 | 'Files', |
||
172 | 'ShowContentInMenu', |
||
173 | 'Name' |
||
174 | ]); |
||
175 | |||
176 | // Because we can't pass arrays to FieldList::insertBefore |
||
177 | foreach ([ |
||
178 | HeaderField::create('ProfileHeading', 'Migration Profile Configuration'), |
||
179 | LiteralField::create('ProfileIntro', '' |
||
180 | . '<p class="message notice">' |
||
181 | . 'This where the basics of your migration profile are configured.' |
||
182 | . '</p>' |
||
183 | )] as $introField) { |
||
184 | $fields->insertBefore('BaseUrl', $introField); |
||
185 | } |
||
186 | |||
187 | // Processing Options |
||
188 | $processingOptions = ['' => "No Processing"]; |
||
189 | |||
190 | foreach (ClassInfo::implementorsOf(StaticSiteUrlProcessor::class) as $processor) { |
||
191 | $processorObj = singleton($processor); |
||
192 | $processingOptions[$processor] = $processorObj->getName(); |
||
193 | } |
||
194 | |||
195 | $fields->addFieldsToTab( |
||
196 | 'Root.Main', [ |
||
197 | TextField::create("BaseUrl", "Base URL") |
||
198 | ->setDescription('The base URL of the site to be crawled and imported.'), |
||
199 | DropdownField::create("UrlProcessor", "URL Transformation", $processingOptions) |
||
200 | ->setDescription('Select the way in which crawled URLs should be transformed and cleaned-up.'), |
||
201 | CheckboxField::create("ParseCSS", "Fetch external CSS") |
||
202 | ->setDescription("Fetch images defined as CSS <strong>background-image</strong> which are not ordinarily reachable by crawling alone."), |
||
203 | CheckboxField::create("AutoRunTask", "Automatically rewrite links into Silverstripe-aware links") |
||
204 | ->setDescription("This will run a link-rewrite task automatically once an import has completed.") |
||
205 | ] |
||
206 | ); |
||
207 | $fields->fieldByName('Root.Main')->setTitle('Profile'); |
||
208 | $fields->insertBefore('BaseUrl', TextField::create('Name', 'Name') |
||
209 | ->setDescription('Allows you to differentiate between profiles.') |
||
210 | ); |
||
211 | |||
212 | // Schema Gridfield |
||
213 | $fields->addFieldToTab('Root.Main', HeaderField::create('ImportConfigHeader', 'Import Schema Configuration')); |
||
214 | $addNewButton = (new GridFieldAddNewButton('before'))->setButtonName("Add Schema"); |
||
215 | $importRules = $fields->dataFieldByName('Schemas'); |
||
216 | $importRules->getConfig()->removeComponentsByType(GridFieldAddNewButton::class); |
||
217 | $importRules->getConfig()->addComponent($addNewButton); |
||
218 | $fields->removeFieldFromTab("Root", "Schemas"); |
||
219 | $fields->addFieldToTab('Root.Main', LiteralField::create( |
||
220 | 'SchemaIntro', |
||
221 | '' |
||
222 | . '<p class="message notice">Schema map MIME-Types to Silverstripe content classes and' |
||
223 | . ' are related to one or more Import Rules. Each rule determines how content located at crawled URLs' |
||
224 | . ' should be imported into a content classes\' fields with the use of CSS selectors.' |
||
225 | . ' Where more than one schema exists for a field, they\'ll be processed in the order of Priority:' |
||
226 | . ' The first Schema to match a URI Pattern will be the one used for that field.</p>' |
||
227 | )); |
||
228 | $fields->addFieldToTab("Root.Main", $importRules); |
||
229 | |||
230 | switch ($this->urlList()->getSpiderStatus()) { |
||
231 | case StaticSiteUrlList::CRAWL_STATUS_NOTSTARTED: |
||
232 | $crawlButtonText = _t('StaticSiteContentSource.CRAWL_SITE', 'Crawl'); |
||
233 | break; |
||
234 | case StaticSiteUrlList::CRAWL_STATUS_PARTIAL: |
||
235 | $crawlButtonText = _t('StaticSiteContentSource.RESUME_CRAWLING', 'Resume Crawl'); |
||
236 | break; |
||
237 | case StaticSiteUrlList::CRAWL_STATUS_COMPLETE: |
||
238 | $crawlButtonText = _t('StaticSiteContentSource.RECRAWL_SITE', 'Re-Crawl'); |
||
239 | break; |
||
240 | default: |
||
241 | throw new \LogicException("Invalid getSpiderStatus() value '".$this->urlList()->getSpiderStatus().";"); |
||
242 | } |
||
243 | |||
244 | $crawlButton = FormAction::create('crawlsite', $crawlButtonText) |
||
245 | ->setAttribute('data-icon', 'arrow-circle-double') |
||
246 | ->setUseButtonTag(true) |
||
247 | ->addExtraClass('btn action btn btn-primary tool-button font-icon-plus'); |
||
248 | $crawlMsg = ''; |
||
249 | |||
250 | // Disable crawl-button if assets dir isn't writable |
||
251 | // TODO this will need to change if change the default location of crawl data. Like _why_ is it in assets? |
||
252 | if (!file_exists(ASSETS_PATH) || !is_writable(ASSETS_PATH)) { |
||
253 | $crawlMsg = '<p class="message warning">Warning: Assets directory is not writable.</p>'; |
||
254 | $crawlButton->setDisabled(true); |
||
255 | } |
||
256 | |||
257 | $fields->addFieldsToTab('Root.Crawl', [ |
||
258 | ReadonlyField::create("CrawlStatus", "Crawl Status", $this->urlList()->getSpiderStatus()), |
||
259 | ReadonlyField::create("NumURIs", "Number of URIs Crawled", $this->urlList()->getNumURIs()), |
||
260 | LiteralField::create( |
||
261 | 'CrawlActions', |
||
262 | $crawlMsg ? '<p class="message notice">' . $crawlMsg . '</p>' : '' |
||
263 | . '<div class="btn-toolbar">' . $crawlButton->forTemplate() . '</div>' |
||
264 | ) |
||
265 | ]); |
||
266 | |||
267 | // Because we can't pass arrays to FieldList::insertBefore |
||
268 | foreach ([ |
||
269 | HeaderField::create('CrawlHeading', 'Source Site Crawling'), |
||
270 | LiteralField::create('CrawlIntro', '' |
||
271 | . '<p class="message notice">' |
||
272 | . 'Before you can load any content into Silverstripe, all source URLs must first be crawled.' |
||
273 | . ' Select the button below to start or resume a crawl as applicable.' |
||
274 | . '</p>' |
||
275 | )] as $introField) { |
||
276 | $fields->insertBefore('CrawlStatus', $introField); |
||
277 | } |
||
278 | |||
279 | /* |
||
280 | * @todo use customise() and arrange this using an includes .ss template fragment |
||
281 | */ |
||
282 | if ($this->urlList()->getSpiderStatus() == StaticSiteUrlList::CRAWL_STATUS_COMPLETE) { |
||
283 | $fields->addFieldToTab( |
||
284 | 'Root.Crawl', |
||
285 | LiteralField::create( |
||
286 | 'CrawlURLListUIntro', |
||
287 | '<p class="mesage notice">Review the list of crawled URIs below. When you\'re happy with the import' |
||
288 | . ' you can proceed to the "Import" tab and follow the instructions there.</p>' |
||
289 | ), |
||
290 | LiteralField::create('CrawlURLList', $this->listofCrawledItems()) |
||
291 | ); |
||
292 | } |
||
293 | |||
294 | $fields->dataFieldByName("ExtraCrawlUrls") |
||
295 | ->setDescription("Add URIs that are not reachable via links when content scraping, eg: '/about/team'. One per line") |
||
296 | ->setTitle('Additional URIs'); |
||
297 | $fields->dataFieldByName("UrlExcludePatterns") |
||
298 | ->setDescription("URLs that should be excluded. (Supports regular expressions e.g. '/about/.*'). One per line") |
||
299 | ->setTitle('Excluded URLs'); |
||
300 | |||
301 | $hasImports = DataObject::get(StaticSiteImportDataObject::class); |
||
302 | $_source = []; |
||
303 | |||
304 | foreach ($hasImports as $import) { |
||
305 | $date = DBField::create_field(DBDatetime::class, $import->Created)->Time24(); |
||
306 | $_source[$import->ID] = $date . ' (Import #' . $import->ID . ')'; |
||
307 | } |
||
308 | |||
309 | $fields->addFieldsToTab('Root.Import', [ |
||
310 | HeaderField::create('ImportHeading', 'Source Site Import'), |
||
311 | LiteralField::create('ImportIntro', '' |
||
312 | . '<p class="message notice">' |
||
313 | . 'Use this area to configure where in the current IA imported page content should appear.' |
||
314 | . ' The same goes for imported files and images.' |
||
315 | . '</p>' |
||
316 | )]); |
||
317 | |||
318 | if ($importCount = $hasImports->count()) { |
||
319 | $clearImportButton = FormAction::create('clearimports', 'Clear selected imports') |
||
320 | ->setAttribute('data-icon', 'arrow-circle-double') |
||
321 | ->addExtraClass('btn action btn btn-primary tool-button font-icon-plus') |
||
322 | ->setUseButtonTag(true); |
||
323 | |||
324 | $clearImportField = ToggleCompositeField::create('ClearImports', 'Clear Import Metadata', [ |
||
325 | LiteralField::create('ImportCountText', '<p>Each time an import is run, some meta information is stored such as an import identifier and failed-link records.<br/><br/></p>'), |
||
326 | LiteralField::create('ImportCount', '<p>Total imports: ' . $importCount . '</p>'), |
||
327 | ListboxField::create('ShowImports', 'Select import(s) to clear:', $_source, '', null, true), |
||
328 | CheckboxField::create('ClearAllImports', 'Clear all import meta-data', 0), |
||
329 | LiteralField::create('ImportActions', '<div class="btn-toolbar">' . $clearImportButton->forTemplate() . '</div>') |
||
330 | ])->addExtraClass('clear-imports'); |
||
331 | |||
332 | $fields->addFieldToTab('Root.Import', $clearImportField); |
||
333 | } |
||
334 | |||
335 | return $fields; |
||
336 | } |
||
562 |