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 |
||
| 29 | class SearchForm extends Form |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * How many results are shown per page. |
||
| 33 | * Relies on pagination being implemented in the search results template. |
||
| 34 | * |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | protected $pageLength = 10; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Classes to search |
||
| 41 | * |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $classesToSearch = array( |
||
| 45 | SiteTree::class, |
||
| 46 | File::class |
||
| 47 | ); |
||
| 48 | |||
| 49 | private static $casting = array( |
||
|
|
|||
| 50 | 'SearchQuery' => 'Text' |
||
| 51 | ); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @skipUpgrade |
||
| 55 | * @param RequestHandler $controller |
||
| 56 | * @param string $name The name of the form (used in URL addressing) |
||
| 57 | * @param FieldList $fields Optional, defaults to a single field named "Search". Search logic needs to be customized |
||
| 58 | * if fields are added to the form. |
||
| 59 | * @param FieldList $actions Optional, defaults to a single field named "Go". |
||
| 60 | */ |
||
| 61 | public function __construct( |
||
| 62 | RequestHandler $controller = null, |
||
| 63 | $name = 'SearchForm', |
||
| 64 | FieldList $fields = null, |
||
| 65 | FieldList $actions = null |
||
| 66 | ) { |
||
| 67 | if (!$fields) { |
||
| 68 | $fields = new FieldList( |
||
| 69 | new TextField('Search', _t('SearchForm.SEARCH', 'Search')) |
||
| 70 | ); |
||
| 71 | } |
||
| 72 | |||
| 73 | if (class_exists('Translatable') |
||
| 74 | && SiteTree::singleton()->hasExtension('Translatable') |
||
| 75 | ) { |
||
| 76 | $fields->push(new HiddenField('searchlocale', 'searchlocale', Translatable::get_current_locale())); |
||
| 77 | } |
||
| 78 | |||
| 79 | if (!$actions) { |
||
| 80 | $actions = new FieldList( |
||
| 81 | new FormAction("results", _t('SearchForm.GO', 'Go')) |
||
| 82 | ); |
||
| 83 | } |
||
| 84 | |||
| 85 | parent::__construct($controller, $name, $fields, $actions); |
||
| 86 | |||
| 87 | $this->setFormMethod('get'); |
||
| 88 | |||
| 89 | $this->disableSecurityToken(); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Set the classes to search. |
||
| 94 | * Currently you can only choose from "SiteTree" and "File", but a future version might improve this. |
||
| 95 | * |
||
| 96 | * @param array $classes |
||
| 97 | */ |
||
| 98 | public function classesToSearch($classes) |
||
| 99 | { |
||
| 100 | $supportedClasses = array(SiteTree::class, File::class); |
||
| 101 | $illegalClasses = array_diff($classes, $supportedClasses); |
||
| 102 | if ($illegalClasses) { |
||
| 103 | throw new BadMethodCallException( |
||
| 104 | "SearchForm::classesToSearch() passed illegal classes '" . implode("', '", $illegalClasses) |
||
| 105 | . "'. At this stage, only File and SiteTree are allowed" |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | $legalClasses = array_intersect($classes, $supportedClasses); |
||
| 109 | $this->classesToSearch = $legalClasses; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Get the classes to search |
||
| 114 | * |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public function getClassesToSearch() |
||
| 118 | { |
||
| 119 | return $this->classesToSearch; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Return dataObjectSet of the results using current request to get info from form. |
||
| 124 | * Wraps around {@link searchEngine()}. |
||
| 125 | * |
||
| 126 | * @return SS_List |
||
| 127 | */ |
||
| 128 | public function getResults() |
||
| 129 | { |
||
| 130 | // Get request data from request handler |
||
| 131 | $request = $this->getRequestHandler()->getRequest(); |
||
| 132 | |||
| 133 | // set language (if present) |
||
| 134 | $locale = null; |
||
| 135 | $origLocale = null; |
||
| 136 | if (class_exists('Translatable')) { |
||
| 137 | $locale = $request->requestVar('searchlocale'); |
||
| 138 | View Code Duplication | if (SiteTree::singleton()->hasExtension('Translatable') && $locale) { |
|
| 139 | if ($locale === "ALL") { |
||
| 140 | Translatable::disable_locale_filter(); |
||
| 141 | } else { |
||
| 142 | $origLocale = Translatable::get_current_locale(); |
||
| 143 | |||
| 144 | Translatable::set_current_locale($locale); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | $keywords = $request->requestVar('Search'); |
||
| 150 | |||
| 151 | $andProcessor = create_function('$matches', ' |
||
| 152 | return " +" . $matches[2] . " +" . $matches[4] . " "; |
||
| 153 | '); |
||
| 154 | $notProcessor = create_function('$matches', ' |
||
| 155 | return " -" . $matches[3]; |
||
| 156 | '); |
||
| 157 | |||
| 158 | $keywords = preg_replace_callback('/()("[^()"]+")( and )("[^"()]+")()/i', $andProcessor, $keywords); |
||
| 159 | $keywords = preg_replace_callback('/(^| )([^() ]+)( and )([^ ()]+)( |$)/i', $andProcessor, $keywords); |
||
| 160 | $keywords = preg_replace_callback('/(^| )(not )("[^"()]+")/i', $notProcessor, $keywords); |
||
| 161 | $keywords = preg_replace_callback('/(^| )(not )([^() ]+)( |$)/i', $notProcessor, $keywords); |
||
| 162 | |||
| 163 | $keywords = $this->addStarsToKeywords($keywords); |
||
| 164 | |||
| 165 | $pageLength = $this->getPageLength(); |
||
| 166 | $start = $request->requestVar('start') ?: 0; |
||
| 167 | |||
| 168 | $booleanSearch = |
||
| 169 | strpos($keywords, '"') !== false || |
||
| 170 | strpos($keywords, '+') !== false || |
||
| 171 | strpos($keywords, '-') !== false || |
||
| 172 | strpos($keywords, '*') !== false; |
||
| 173 | $results = DB::get_conn()->searchEngine($this->classesToSearch, $keywords, $start, $pageLength, "\"Relevance\" DESC", "", $booleanSearch); |
||
| 174 | |||
| 175 | // filter by permission |
||
| 176 | if ($results) { |
||
| 177 | foreach ($results as $result) { |
||
| 178 | if (!$result->canView()) { |
||
| 179 | $results->remove($result); |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | // reset locale |
||
| 185 | View Code Duplication | if (class_exists('Translatable')) { |
|
| 186 | if (SiteTree::singleton()->hasExtension('Translatable') && $locale) { |
||
| 187 | if ($locale == "ALL") { |
||
| 188 | Translatable::enable_locale_filter(); |
||
| 189 | } else { |
||
| 190 | Translatable::set_current_locale($origLocale); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | return $results; |
||
| 196 | } |
||
| 197 | |||
| 198 | protected function addStarsToKeywords($keywords) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get the search query for display in a "You searched for ..." sentence. |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function getSearchQuery() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Set the maximum number of records shown on each page. |
||
| 234 | * |
||
| 235 | * @param int $length |
||
| 236 | */ |
||
| 237 | public function setPageLength($length) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @return int |
||
| 244 | */ |
||
| 245 | public function getPageLength() |
||
| 249 | } |
||
| 250 |