Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 12 | class DataObjectSorterController extends DataObjectSortBaseClass |
||
| 13 | { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * standard SS variable |
||
| 17 | * |
||
| 18 | */ |
||
| 19 | private static $allowed_actions = array( |
||
|
|
|||
| 20 | "sort" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION', |
||
| 21 | "dosort" => 'DATA_OBJECT_SORT_AND_EDIT_PERMISSION' |
||
| 22 | ); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * |
||
| 26 | * make sure to also change in routes if you change this link |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private static $url_segment = 'dataobjectsorter'; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * returns a link for sorting objects. You can use this in the CMS like this.... |
||
| 34 | * <code> |
||
| 35 | * if(class_exists("DataObjectSorterController")) { |
||
| 36 | * $fields->addFieldToTab("Root.Position", new LiteralField("AdvertisementsSorter", DataObjectSorterController::popup_link("Advertisement", $filterField = "", $filterValue = "", $linkText = "sort ".Advertisement::$plural_name, $titleField = "FullTitle"))); |
||
| 37 | * } |
||
| 38 | * else { |
||
| 39 | * $fields->addFieldToTab("Root.Position", new NumericField($name = "Sort", "Sort index number (the lower the number, the earlier it shows up")); |
||
| 40 | * } |
||
| 41 | * </code> |
||
| 42 | * |
||
| 43 | * @param String $className - DataObject Class Name you want to sort |
||
| 44 | * @param String | Int $filterField - Field you want to filter for OR ParentID number (i.e. you are sorting children of Parent with ID = $filterField) |
||
| 45 | * @param String $filterValue - filter field should be equal to this integer OR string. You can provide a list of IDs like this: 1,2,3,4 where the filterFiel is probably equal to ID or MyRelationID |
||
| 46 | * @param String $linkText - text to show on the link |
||
| 47 | * @param String $titleField - field to show in the sort list. This defaults to the DataObject method "getTitle", but you can use "name" or something like that. |
||
| 48 | * |
||
| 49 | * @return String - html |
||
| 50 | */ |
||
| 51 | public static function popup_link_only($className, $filterField = "", $filterValue = "", $titleField = "") |
||
| 66 | /** |
||
| 67 | * returns a link for sorting objects. You can use this in the CMS like this.... |
||
| 68 | * <code> |
||
| 69 | * if(class_exists("DataObjectSorterController")) { |
||
| 70 | * $fields->addFieldToTab("Root.Position", new LiteralField("AdvertisementsSorter", DataObjectSorterController::popup_link("Advertisement", $filterField = "", $filterValue = "", $linkText = "sort ".Advertisement::$plural_name, $titleField = "FullTitle"))); |
||
| 71 | * } |
||
| 72 | * else { |
||
| 73 | * $fields->addFieldToTab("Root.Position", new NumericField($name = "Sort", "Sort index number (the lower the number, the earlier it shows up")); |
||
| 74 | * } |
||
| 75 | * </code> |
||
| 76 | * |
||
| 77 | * @param String $className - DataObject Class Name you want to sort |
||
| 78 | * @param String | Int $filterField - Field you want to filter for OR ParentID number (i.e. you are sorting children of Parent with ID = $filterField) |
||
| 79 | * @param String $filterValue - filter field should be equal to this integer OR string. You can provide a list of IDs like this: 1,2,3,4 where the filterFiel is probably equal to ID or MyRelationID |
||
| 80 | * @param String $linkText - text to show on the link |
||
| 81 | * @param String $titleField - field to show in the sort list. This defaults to the DataObject method "getTitle", but you can use "name" or something like that. |
||
| 82 | * |
||
| 83 | * @return String - html |
||
| 84 | */ |
||
| 85 | View Code Duplication | public static function popup_link($className, $filterField = "", $filterValue = "", $linkText = "sort this list", $titleField = "") |
|
| 93 | |||
| 94 | public function init() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * the standard action... |
||
| 106 | * no need to add anything here now |
||
| 107 | */ |
||
| 108 | public function sort() |
||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * runs the actual sorting... |
||
| 117 | */ |
||
| 118 | public function dosort($request) |
||
| 133 | |||
| 134 | private static $_children_cache_for_sorting = null; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * runs the actual sorting... |
||
| 138 | * @return Object - return dataobject set of items to be sorted |
||
| 139 | */ |
||
| 140 | public function Children() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * adds functionality for actual sorting... |
||
| 202 | * |
||
| 203 | * @param string $className - name of the class being sorted |
||
| 204 | */ |
||
| 205 | protected function addRequirements($className) |
||
| 215 | } |
||
| 216 |