Conditions | 18 |
Paths | 19 |
Total Lines | 82 |
Code Lines | 48 |
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 |
||
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 |
||
95 | // ?locale will automatically be added |
||
96 | )); |
||
97 | } elseif (!($this->owner instanceof CMSPagesController)) { |
||
98 | // If the record is not translated, redirect to pages overview |
||
99 | return $this->owner->redirect(Controller::join_links( |
||
100 | singleton(CMSPagesController::class)->Link(), |
||
101 | '?Locale=' . $requestLocale |
||
102 | )); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | // collect languages for TinyMCE spellchecker plugin. |
||
107 | // see http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker |
||
108 | $langName = i18n::getData()->localeName($this->owner->Locale); |
||
109 | HtmlEditorConfig::get('cms')->setOption( |
||
110 | 'spellchecker_languages', |
||
111 | "+{$langName}={$this->owner->Locale}" |
||
112 | ); |
||
113 | |||
114 | Requirements::javascript('translatable/javascript/CMSMain.Translatable.js'); |
||
115 | Requirements::css('translatable/css/CMSMain.Translatable.css'); |
||
116 | } |
||
117 | |||
269 |