| Conditions | 18 |
| Paths | 19 |
| Total Lines | 76 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 11 | function init() { |
||
| 12 | $req = $this->owner->getRequest(); |
||
| 13 | |||
| 14 | // Ignore being called on LeftAndMain base class, |
||
| 15 | // which is the case when requests are first routed through AdminRootController |
||
| 16 | // as an intermediary rather than the endpoint controller |
||
| 17 | if(!$this->owner->stat('tree_class')) return; |
||
| 18 | |||
| 19 | // Locale" attribute is either explicitly added by LeftAndMain Javascript logic, |
||
| 20 | // or implied on a translated record (see {@link Translatable->updateCMSFields()}). |
||
| 21 | // $Lang serves as a "context" which can be inspected by Translatable - hence it |
||
| 22 | // has the same name as the database property on Translatable. |
||
| 23 | $id = $req->param('ID'); |
||
| 24 | if($req->requestVar("Locale")) { |
||
| 25 | $this->owner->Locale = $req->requestVar("Locale"); |
||
| 26 | } else if($id && is_numeric($id)) { |
||
| 27 | $record = DataObject::get_by_id($this->owner->stat('tree_class'), $id); |
||
| 28 | if($record && $record->Locale) $this->owner->Locale = $record->Locale; |
||
| 29 | } else { |
||
| 30 | $this->owner->Locale = Translatable::default_locale(); |
||
| 31 | if ($this->owner->class == 'CMSPagesController') { |
||
| 32 | // the CMSPagesController always needs to have the locale set, |
||
| 33 | // otherwise page editing will cause an extra |
||
| 34 | // ajax request which looks weird due to multiple "loading"-flashes |
||
| 35 | $getVars = $req->getVars(); |
||
| 36 | if(isset($getVars['url'])) unset($getVars['url']); |
||
| 37 | return $this->owner->redirect(Controller::join_links( |
||
| 38 | $this->owner->Link(), |
||
| 39 | $req->param('Action'), |
||
| 40 | $req->param('ID'), |
||
| 41 | $req->param('OtherID'), |
||
| 42 | ($query = http_build_query($getVars)) ? "?$query" : null |
||
| 43 | )); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | Translatable::set_current_locale($this->owner->Locale); |
||
| 47 | |||
| 48 | // If a locale is set, it needs to match to the current record |
||
| 49 | $requestLocale = $req->requestVar("Locale"); |
||
| 50 | $page = $this->owner->currentPage(); |
||
| 51 | if( |
||
| 52 | $req->httpMethod() == 'GET' // leave form submissions alone |
||
| 53 | && $requestLocale |
||
| 54 | && $page |
||
| 55 | && $page->hasExtension('Translatable') |
||
| 56 | && $page->Locale != $requestLocale |
||
| 57 | && $req->latestParam('Action') != 'EditorToolbar' |
||
| 58 | ) { |
||
| 59 | $transPage = $page->getTranslation($requestLocale); |
||
| 60 | if($transPage) { |
||
| 61 | Translatable::set_current_locale($transPage->Locale); |
||
| 62 | return $this->owner->redirect(Controller::join_links( |
||
| 63 | $this->owner->Link('show'), |
||
| 64 | $transPage->ID |
||
| 65 | // ?locale will automatically be added |
||
| 66 | )); |
||
| 67 | } else if ($this->owner->class != 'CMSPagesController') { |
||
| 68 | // If the record is not translated, redirect to pages overview |
||
| 69 | return $this->owner->redirect(Controller::join_links( |
||
| 70 | singleton('CMSPagesController')->Link(), |
||
| 71 | '?Locale=' . $requestLocale |
||
| 72 | )); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | // collect languages for TinyMCE spellchecker plugin. |
||
| 77 | // see http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/spellchecker |
||
| 78 | $langName = i18n::get_locale_name($this->owner->Locale); |
||
| 79 | HtmlEditorConfig::get('cms')->setOption( |
||
| 80 | 'spellchecker_languages', |
||
| 81 | "+{$langName}={$this->owner->Locale}" |
||
| 82 | ); |
||
| 83 | |||
| 84 | Requirements::javascript('translatable/javascript/CMSMain.Translatable.js'); |
||
| 85 | Requirements::css('translatable/css/CMSMain.Translatable.css'); |
||
| 86 | } |
||
| 87 | |||
| 233 |