Conditions | 16 |
Paths | 577 |
Total Lines | 138 |
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 |
||
77 | public function updateCMSFields(FieldList $fields) |
||
78 | { |
||
79 | if ($this->classHasAdvertisements($this->owner->ClassName) && $this->owner->exists()) { |
||
80 | $tabName = $this->MyTabName(); |
||
81 | //advertisements shown... |
||
82 | $where = '1 = 1'; |
||
83 | if ($this->owner->AdvertisementsFolderID) { |
||
84 | $images = Image::get()->filter("ParentID", $this->owner->AdvertisementsFolderID); |
||
85 | if ($images->count()) { |
||
86 | $where = "\"AdvertisementImageID\" IN (".implode(",", $images->column("ID")).")"; |
||
87 | } else { |
||
88 | $where = " 1 = 2"; |
||
89 | } |
||
90 | } |
||
91 | |||
92 | $source = Advertisement::get(); |
||
93 | if ($source && $source->count()) { |
||
94 | $newSource = []; |
||
95 | foreach ($source as $ad) { |
||
96 | $newSource[$ad->ID] = $ad->getFullTitle('width: 75px!important;'); |
||
97 | } |
||
98 | $fields->addFieldToTab($tabName, CheckboxSetField::create( |
||
99 | 'Advertisements', |
||
100 | 'Select '.Config::inst()->get("Advertisement", "plural_name"), |
||
101 | $newSource |
||
102 | )); |
||
103 | } |
||
104 | |||
105 | //$advertisementsCount = DB::query("SELECT COUNT(ID) FROM \"Advertisement\" $whereDB ;")->value(); |
||
106 | $advertisements = $this->owner->Advertisements()->where($where); |
||
107 | $txt = sprintf(_t("AdvertisementDecorator.ACTUAL", 'Current %1$s Shown'), Config::inst()->get("Advertisement", "plural_name")); |
||
108 | $fields->addFieldToTab($tabName, $this->MyHeaderField($txt)); |
||
109 | $txt = sprintf(_t("AdvertisementDecorator.SELECT", 'Select %1$s to show ... '), Config::inst()->get("Advertisement", "plural_name")); |
||
110 | $advertisementsGridField = new GridField('AdvertisementsList', $txt, $this->owner->Advertisements(), GridFieldConfig_RelationEditor::create()); |
||
111 | $fields->addFieldToTab($tabName, $advertisementsGridField); |
||
112 | if (Config::inst()->get("Advertisement", "resize_images") == 'no') { |
||
113 | $totalSize = 0; |
||
114 | foreach ($this->owner->Advertisements() as $advertisement) { |
||
115 | $totalSize += $advertisement->AdvertisementImage()->getAbsoluteSize(); |
||
116 | } |
||
117 | $seconds = round(($totalSize + 1) / 524288) ; |
||
118 | $fields->addFieldToTab($tabName, new LiteralField("TotalSize", '<p><em>Total download size: '.File::format_size($totalSize).', good reception 3G network download time less than ~'.($seconds+1).' seconds.</em></p>')); |
||
119 | } |
||
120 | if (class_exists("DataObjectSorterController")) { |
||
121 | $shownAdvertisements = $this->owner->getManyManyComponents('Advertisements'); |
||
122 | if ($shownAdvertisements) { |
||
123 | $array = $shownAdvertisements->column("ID"); |
||
124 | $idString = implode(",", $array); |
||
125 | $link = DataObjectSorterController::popup_link("Advertisement", $filterField = "ID", $filterValue = $idString, $linkText = "sort ".Config::inst()->get("Advertisement", "plural_name"), $titleField = "FullTitle"); |
||
126 | $fields->addFieldToTab($tabName, new LiteralField("AdvertisementsSorter", $link)); |
||
127 | } |
||
128 | } |
||
129 | if ($advertisements->count()) { |
||
130 | } else { |
||
131 | $txt = sprintf( |
||
132 | _t("AdvertisementDecorator.CREATE", '<p>Please <a href="admin/%1$s/">create %2$s</a> on the <a href="admin/%1$s/">%3$s tab</a> first, or see below on how to create %2$s from a folder.</p>'), |
||
133 | Config::inst()->get("AdvertisementAdmin", "url_segment"), |
||
134 | Config::inst()->get("Advertisement", "plural_name"), |
||
135 | Config::inst()->get("AdvertisementAdmin", "menu_title") |
||
136 | ); |
||
137 | $fields->addFieldToTab($tabName, new LiteralField("AdvertisementsHowToCreate", $txt)); |
||
138 | } |
||
139 | if ($parent = $this->advertisementParent()) { |
||
140 | $txt = sprintf(_t("AdvertisementDecorator.ORUSE", 'OR ... use %1$s from <i>%2$s</i>.'), Config::inst()->get("Advertisement", "plural_name"), $parent->Title); |
||
141 | $fields->addFieldToTab($tabName, new CheckboxField("UseParentAdvertisements", $txt)); |
||
142 | } |
||
143 | |||
144 | |||
145 | $txt = _t('AdvertisementDecorator.ADVANCED', 'Advanced'); |
||
146 | $fields->addFieldToTab($tabName, $this->MyHeaderFieldLarge($txt)); |
||
147 | |||
148 | //create new advertisements |
||
149 | $txt = sprintf(_t("AdvertisementDecorator.CREATE", 'Create new %1$s'), Config::inst()->get("Advertisement", "plural_name")); |
||
150 | $fields->addFieldToTab($tabName, $this->MyHeaderField($txt)); |
||
151 | $txt = sprintf( |
||
152 | _t( |
||
153 | "AdvertisementDecorator.CREATENEWFROMFOLDER_EXPLANATION", |
||
154 | 'Create New %1$s from images in the folder selected - each image in the folder will be used to create a %3$s. %2$s' |
||
155 | ), |
||
156 | Config::inst()->get("Advertisement", "plural_name"), |
||
157 | Advertisement::recommended_image_size_statement(), |
||
158 | Config::inst()->get("Advertisement", "singular_name") |
||
159 | ); |
||
160 | if (Folder::get()->count()) { |
||
161 | $fields->addFieldToTab( |
||
162 | $tabName, |
||
163 | $treeDropdownField = new TreeDropdownField( |
||
164 | 'AdvertisementsFolderID', |
||
165 | _t("AdvertisementDecorator.CREATENEWFROMFOLDER", "Create from folder"), |
||
166 | 'Folder' |
||
167 | ) |
||
168 | ); |
||
169 | $treeDropdownField->setRightTitle($txt); |
||
170 | } |
||
171 | |||
172 | $styles = AdvertisementStyle::get(); |
||
173 | if ($styles->count()) { |
||
174 | $fields->addFieldToTab($tabName, $this->MyHeaderField("Style")); |
||
175 | $list = $styles->map("ID", "Title", $emptyString = _t("AdvertisementDecorator.SELECTSTYLE", "--select style--"), $sortByTitle = true); |
||
176 | $fields->addFieldToTab( |
||
177 | $tabName, |
||
178 | $selectStyleField = new DropdownField( |
||
179 | "AdvertisementStyleID", |
||
180 | _t("AdvertisementDecorator.STYLECREATED", "Select style"), |
||
181 | $list |
||
182 | ) |
||
183 | ); |
||
184 | $selectStyleField->setRightTitle(_t("AdvertisementDecorator.STYLECREATED_EXPLANATION", "Styles are created by your developer")); |
||
185 | } |
||
186 | |||
187 | |||
188 | $txt = sprintf(_t("AdvertisementDecorator.EDIT", 'Edit %1$s'), Config::inst()->get("Advertisement", "plural_name")); |
||
189 | $txt = sprintf( |
||
190 | _t("AdvertisementDecorator.PLEASEMANAGEEXISTING", '<p>Please manage existing %1$s on the <a href="admin/%2$s/">%3$s tab</a>.</p>'), |
||
191 | Config::inst()->get("Advertisement", "plural_name"), |
||
192 | Config::inst()->get("AdvertisementAdmin", "url_segment"), |
||
193 | Config::inst()->get("AdvertisementAdmin", "menu_title") |
||
194 | ); |
||
195 | $fields->addFieldToTab($tabName, new LiteralField("ManageAdvertisements", $txt)); |
||
196 | $txt = sprintf(_t("AdvertisementDecorator.DELETE", 'Delete %1$s'), Config::inst()->get("Advertisement", "plural_name")); |
||
197 | $fields->addFieldToTab($tabName, $this->MyHeaderField($txt)); |
||
198 | $page = SiteTree::get()->byID($this->owner->ID); |
||
199 | |||
200 | $txtRemove = sprintf(_t("AdvertisementDecorator.REMOVE", 'Remove all %1$s from this page (%1$s will not be deleted but are not longer associated with this page)'), Config::inst()->get("Advertisement", "plural_name")); |
||
201 | $txtConfirmRemove = sprintf(_t("AdvertisementDecorator.CONFIRMREMOVE", 'Are you sure you want to remove all %1$s from this page?'), Config::inst()->get("Advertisement", "plural_name")); |
||
202 | $removeallLink = 'advertisements/removealladvertisements/'.$this->owner->ID.'/'; |
||
203 | $jquery = 'if(confirm(\''.$txtConfirmRemove.'\')) {jQuery(\'#removealladvertisements\').load(\''.$removeallLink.'\');} return false;'; |
||
204 | $fields->addFieldToTab($tabName, new LiteralField("removealladvertisements", '<p class="message warning"><a href="'.$removeallLink.'" onclick="'.$jquery.'" id="removealladvertisements" class="ss-ui-button">'.$txtRemove.'</a></p>')); |
||
205 | |||
206 | $txtDelete = sprintf(_t("AdvertisementDecorator.DELETE", 'Delete all %1$s from this website (but not the images associated with them)'), Config::inst()->get("Advertisement", "plural_name")); |
||
207 | $txtConfirmDelete = sprintf(_t("AdvertisementDecorator.CONFIRMDELETE", 'Are you sure you want to delete all %1$s - there is no UNDO?'), Config::inst()->get("Advertisement", "plural_name")); |
||
208 | $deleteallLink = 'advertisements/deletealladvertisements/'.$this->owner->ID.'/'; |
||
209 | $jquery = 'if(confirm(\''.$txtConfirmDelete.'\')) {jQuery(\'#deletealladvertisements\').load(\''.$deleteallLink.'\');} return false;'; |
||
210 | $fields->addFieldToTab($tabName, new LiteralField("deletealladvertisements", '<p class="message bad"><a href="'.$deleteallLink.'" onclick="'.$jquery.'" id="deletealladvertisements" class="ss-ui-button">'.$txtDelete.'</a></p>')); |
||
211 | } |
||
212 | |||
213 | return $fields; |
||
214 | } |
||
215 | |||
400 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.