| Conditions | 20 |
| Paths | 768 |
| Total Lines | 140 |
| Lines | 9 |
| Ratio | 6.43 % |
| 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 |
||
| 171 | public function getCMSFields() |
||
| 172 | { |
||
| 173 | $fields = parent::getCMSFields(); |
||
| 174 | $fields->addFieldToTab("Root.Images", new CheckboxField("HasImages", _t("ProductQuestion.HAS_IMAGES", "This question makes use of images"))); |
||
| 175 | $fields->addFieldToTab("Root.Products", new CheckboxField("ApplyToAllProducts", _t("ProductQuestion.APPLY_TO_ALL_PRODUCTS", "Apply to all products ..."))); |
||
| 176 | if (!$this->HasImages) { |
||
| 177 | $fields->addFieldToTab( |
||
| 178 | "Root.DefaultFormField", |
||
| 179 | new OptionSetField( |
||
| 180 | "DefaultFormField", |
||
| 181 | _t("ProductQuestion.DEFAULTFORMFIELD", "Field type to use"), |
||
| 182 | $this->Options ? $this->Config()->get("available_form_fields_list") : $this->Config()->get("available_form_fields_free") |
||
| 183 | ) |
||
| 184 | ); |
||
| 185 | } else { |
||
| 186 | $fields->removeFieldFromTab("Root.Main", "DefaultFormField"); |
||
| 187 | } |
||
| 188 | $productFieldTitle = _t("ProductQuestion.PRODUCTS", "Products showing this question"); |
||
| 189 | if ($this->ApplyToAllProducts) { |
||
| 190 | $productField = new HiddenField("Products"); |
||
| 191 | } elseif (Product::get()->count() < $this->Config()->get("max_products_for_checkbox_set_field")) { |
||
| 192 | $productField = new CheckboxSetField("Products", $productFieldTitle, Product::get()->map("ID", "FullName")->toArray()); |
||
| 193 | } else { |
||
| 194 | $productField = new GridField( |
||
| 195 | 'Products', |
||
| 196 | _t("ProductQuestion.PRODUCTS", "Products showing this question"), |
||
| 197 | $this->Products(), |
||
| 198 | GridFieldEditOriginalPageConfigWithDelete::create() |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | $fields->addFieldToTab("Root.Products", $productField); |
||
| 202 | if (!$this->ApplyToAllProducts) { |
||
| 203 | View Code Duplication | foreach ($this->Products() as $product) { |
|
| 204 | $fields->addFieldToTab( |
||
| 205 | "Root.Products", |
||
| 206 | new LiteralField( |
||
| 207 | "Product".$product->ID, |
||
| 208 | "<h5><a href=\"".$product->CMSEditLink()."\">"._t("ProductQuestion.BACK_TO", "Edit ")." ".$product->Title."</a></h5>" |
||
| 209 | ) |
||
| 210 | ); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | if ($this->HasImages) { |
||
| 214 | $folders = Folder::get(); |
||
| 215 | if ($folders->count()) { |
||
| 216 | $folderMap = $folders->map("ID", "Title")->toArray(); |
||
| 217 | $folders = null; |
||
| 218 | $fields->removeFieldFromTab("Root.Main", "Folder"); |
||
| 219 | $fields->addFieldToTab( |
||
| 220 | "Root.Images", |
||
| 221 | $treeDropdownField = new TreeDropdownField("FolderID", _t("ProductQuestion.FOLDER", "Folder"), "Folder") |
||
| 222 | ); |
||
| 223 | $treeDropdownFieldRightTitle = _t( |
||
| 224 | "ProductQuestion.FOLDER_ID", |
||
| 225 | "Select the folder in which the images live. |
||
| 226 | <br /> |
||
| 227 | <strong> |
||
| 228 | The images need to have the exact same file name as the options listed. |
||
| 229 | For example, if one of your options is 'red' then there should be a file in your folder called 'red.png' or 'red.jpg' or 'red.gif', |
||
| 230 | the following filenames would not work: 'Red.png', 'red1.jpg', 'RED.gif', etc... |
||
| 231 | </strong>" |
||
| 232 | ); |
||
| 233 | $folder = Folder::get()->byID($this->FolderID); |
||
| 234 | if ($folder) { |
||
| 235 | $treeDropdownFieldRightTitle .= " |
||
| 236 | <br /><a href=\"admin/assets/show/".$folder->ID."/\">"._t("ProductQuestion.OPEN", "Open")." ".$folder->Title."</a> |
||
| 237 | <br /><a href=\"admin/assets/add/?ID".$folder->ID."/\">"._t("ProductQuestion.ADD_IMAGES_TO", "Add images to")." ".$folder->Title."</a>"; |
||
| 238 | } |
||
| 239 | $treeDropdownField->setRightTitle($treeDropdownFieldRightTitle); |
||
| 240 | } |
||
| 241 | if ($this->FolderID) { |
||
| 242 | $imagesInFolder = Image::get()->filter(array("ParentID" => $this->FolderID)); |
||
| 243 | if ($imagesInFolder->count()) { |
||
| 244 | $imagesInFolderArray = $imagesInFolder->map("ID", "Name")->toArray(); |
||
| 245 | $options = explode(",", $this->Options); |
||
| 246 | $imagesInFolderField = new ReadonlyField("ImagesInFolder", _t("ProductQuestion.NO_IMAGES", "Images in folder"), implode("<br />", $imagesInFolderArray)); |
||
| 247 | $imagesInFolderField->dontEscape = true; |
||
| 248 | $fields->addFieldToTab("Root.Images", $imagesInFolderField); |
||
| 249 | //matches |
||
| 250 | if ($this->exists()) { |
||
| 251 | $matchesInFolderArray = array(); |
||
| 252 | $nonMatchesInFolderArray = array(); |
||
| 253 | $options = explode(",", $this->Options); |
||
| 254 | if (count($options)) { |
||
| 255 | foreach ($options as $option) { |
||
| 256 | $fileNameArray = self::create_file_array_from_option($option); |
||
| 257 | foreach ($fileNameArray as $fileName) { |
||
| 258 | if (in_array($fileName, $imagesInFolderArray)) { |
||
| 259 | $matchesInFolderArray[$option] = "<strong>".$option."</strong>: ".$fileName; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | if (!isset($matchesInFolderArray[$option])) { |
||
| 263 | $nonMatchesInFolderArray[$option] = "<strong>".$option." - add one these files: </strong>".implode(",", $fileNameArray); |
||
| 264 | } |
||
| 265 | } |
||
| 266 | } |
||
| 267 | $matchesInFolderField = new ReadonlyField("MatchesInFolder", _t("ProductQuestion.MATCHES_IN_FOLDER", "Matches in folder"), implode("<br />", $matchesInFolderArray)); |
||
| 268 | $matchesInFolderField->dontEscape = true; |
||
| 269 | $fields->addFieldToTab("Root.Images", $matchesInFolderField); |
||
| 270 | $nonMatchesInFolderField = new ReadonlyField("NonMatchesInFolder", _t("ProductQuestion.NON_MATCHES_IN_FOLDER", "NON Matches in folder"), implode("<br />", $nonMatchesInFolderArray)); |
||
| 271 | $nonMatchesInFolderField->dontEscape = true; |
||
| 272 | $fields->addFieldToTab("Root.Images", $nonMatchesInFolderField); |
||
| 273 | } |
||
| 274 | } else { |
||
| 275 | $imagesInFolderField = new ReadonlyField("ImagesInFolder", "Images in folder", _t("ProductQuestion.NO_IMAGES", "There are no images in this folder")); |
||
| 276 | $fields->addFieldToTab("Root.Main", $imagesInFolderField); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | } else { |
||
| 280 | $fields->removeByName("Folder"); |
||
| 281 | } |
||
| 282 | if ($this->Products()->count()) { |
||
| 283 | $randomProduct = $this->Products()->First(); |
||
| 284 | $fields->addFieldToTab("Root.Example", $this->getFieldForProduct($randomProduct)); |
||
| 285 | } |
||
| 286 | $folderExplanation = _t( |
||
| 287 | "ProductQuestion.FOLDER_EXPLANATION", |
||
| 288 | "Tick the box to link each option to an image (e.g. useful if you have colour swatches). |
||
| 289 | Once ticked and the question is saved you will be able to select a folder from where to select the images." |
||
| 290 | ); |
||
| 291 | $field = $fields->dataFieldByName("HasImages"); |
||
| 292 | if ($field) { |
||
| 293 | $field->setDescription($folderExplanation); |
||
| 294 | } |
||
| 295 | |||
| 296 | $this->extend('updateCMSFields', $fields); |
||
| 297 | |||
| 298 | //custom field labels |
||
| 299 | $internalCodeField = $fields->dataFieldByName("InternalCode"); |
||
| 300 | $internalCodeField->setRightTitle(_t("ProductQuestion.INTERNALCODE", "Code used to identify question (not shown to customers)")); |
||
| 301 | $questionField = $fields->dataFieldByName("Question"); |
||
| 302 | $questionField->setRightTitle(_t("ProductQuestion.QUESTION", "Question (e.g. what configuration do you prefer?)")); |
||
| 303 | $labelField = $fields->dataFieldByName("Label"); |
||
| 304 | $labelField->setRightTitle(_t("ProductQuestion.LABEL", "Label (e.g. Your selected configuration)")); |
||
| 305 | $defaultAnswerField = $fields->dataFieldByName("DefaultAnswer"); |
||
| 306 | $defaultAnswerField->setRightTitle(_t("ProductQuestion.DEFAULT_ANSWER", "Default Answer if no Answer has been provided. Can be blank or, for example, tba.")); |
||
| 307 | $optionsField = $fields->dataFieldByName("Options"); |
||
| 308 | $optionsField->setRightTitle(_t("ProductQuestion.OPTIONS", "Predefined Options (leave blank for any option). These must be comma separated (e.g. red, blue, yellow, orange)")); |
||
| 309 | return $fields; |
||
| 310 | } |
||
| 311 | |||
| 432 |
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.