| Conditions | 18 |
| Paths | 19 |
| Total Lines | 83 |
| Code Lines | 49 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 11 | use SilverStripe\Forms\Form; |
||
| 12 | use SilverStripe\Forms\FormAction; |
||
| 13 | use SilverStripe\Forms\HiddenField; |
||
| 14 | use SilverStripe\Forms\LiteralField; |
||
| 15 | use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig; |
||
| 16 | use SilverStripe\i18n\i18n; |
||
| 17 | use SilverStripe\ORM\DataObject; |
||
| 18 | use SilverStripe\Security\Member; |
||
| 19 | use SilverStripe\Security\Permission; |
||
| 20 | use SilverStripe\Security\SecurityToken; |
||
| 21 | use SilverStripe\SiteConfig\SiteConfig; |
||
| 22 | use SilverStripe\Translatable\Forms\LanguageDropdownField; |
||
| 23 | use SilverStripe\Translatable\Model\Translatable; |
||
| 24 | use SilverStripe\View\Requirements; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @package translatable |
||
| 28 | */ |
||
| 29 | class TranslatableCMSMainExtension extends Extension |
||
| 30 | { |
||
| 31 | private static $allowed_actions = array( |
||
| 32 | 'createtranslation', |
||
| 33 | ); |
||
| 34 | |||
| 35 | public function init() |
||
| 36 | { |
||
| 37 | $req = $this->owner->getRequest(); |
||
| 38 | |||
| 39 | // Ignore being called on LeftAndMain base class, |
||
| 40 | // which is the case when requests are first routed through AdminRootController |
||
| 41 | // as an intermediary rather than the endpoint controller |
||
| 42 | if (!$this->owner->stat('tree_class')) { |
||
| 43 | return; |
||
| 44 | } |
||
| 45 | |||
| 46 | // Locale" attribute is either explicitly added by LeftAndMain Javascript logic, |
||
| 47 | // or implied on a translated record (see {@link Translatable->updateCMSFields()}). |
||
| 48 | // $Lang serves as a "context" which can be inspected by Translatable - hence it |
||
| 49 | // has the same name as the database property on Translatable. |
||
| 50 | $id = $req->param('ID'); |
||
| 51 | if ($req->requestVar("Locale")) { |
||
| 52 | $this->owner->Locale = $req->requestVar("Locale"); |
||
| 53 | } elseif ($id && is_numeric($id)) { |
||
| 54 | $record = DataObject::get_by_id($this->owner->stat('tree_class'), $id); |
||
| 55 | if ($record && $record->Locale) { |
||
| 56 | $this->owner->Locale = $record->Locale; |
||
| 57 | } |
||
| 58 | } else { |
||
| 59 | $this->owner->Locale = Translatable::default_locale(); |
||
| 60 | if ($this->owner instanceof CMSPagesController) { |
||
| 61 | // the CMSPagesController always needs to have the locale set, |
||
| 62 | // otherwise page editing will cause an extra |
||
| 63 | // ajax request which looks weird due to multiple "loading"-flashes |
||
| 64 | $getVars = $req->getVars(); |
||
| 65 | if (isset($getVars['url'])) { |
||
| 66 | unset($getVars['url']); |
||
| 67 | } |
||
| 68 | return $this->owner->redirect(Controller::join_links( |
||
| 69 | $this->owner->Link(), |
||
| 70 | $req->param('Action'), |
||
| 71 | $req->param('ID'), |
||
| 72 | $req->param('OtherID'), |
||
| 73 | ($query = http_build_query($getVars)) ? "?$query" : null |
||
| 74 | )); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | Translatable::set_current_locale($this->owner->Locale); |
||
| 78 | |||
| 79 | // If a locale is set, it needs to match to the current record |
||
| 80 | $requestLocale = $req->requestVar("Locale"); |
||
| 81 | $page = $this->owner->currentPage(); |
||
| 82 | if ($req->httpMethod() == 'GET' // leave form submissions alone |
||
| 83 | && $requestLocale |
||
| 84 | && $page |
||
| 85 | && $page->hasExtension(Translatable::class) |
||
| 86 | && $page->Locale != $requestLocale |
||
| 87 | && $req->latestParam('Action') != 'EditorToolbar' |
||
| 88 | ) { |
||
| 89 | $transPage = $page->getTranslation($requestLocale); |
||
| 90 | if ($transPage) { |
||
| 91 | Translatable::set_current_locale($transPage->Locale); |
||
| 92 | return $this->owner->redirect(Controller::join_links( |
||
| 93 | $this->owner->Link('show'), |
||
| 94 | $transPage->ID |
||
| 269 |