| Total Complexity | 53 |
| Total Lines | 464 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Contact often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Contact, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Contact extends DataObject implements PermissionProvider |
||
| 26 | { |
||
| 27 | private static $table_name = 'Contact'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * String used to seperate tags, lists, etc |
||
| 31 | * when rendering a summary. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private static $list_seperator = ", "; |
||
| 36 | |||
| 37 | private static $db = [ |
||
| 38 | "FirstName" => "Varchar(255)", |
||
| 39 | "Surname" => "Varchar(255)", |
||
| 40 | "Company" => "Varchar(255)", |
||
| 41 | "Phone" => "Varchar(15)", |
||
| 42 | "Mobile" => "Varchar(15)", |
||
| 43 | "Email" => "Varchar(255)", |
||
| 44 | "Source" => "Text" |
||
| 45 | ]; |
||
| 46 | |||
| 47 | private static $has_one = [ |
||
| 48 | "Member" => Member::class |
||
| 49 | ]; |
||
| 50 | |||
| 51 | private static $has_many = [ |
||
| 52 | "Locations" => ContactLocation::class, |
||
| 53 | "Notes" => ContactNote::class |
||
| 54 | ]; |
||
| 55 | |||
| 56 | private static $many_many = [ |
||
| 57 | 'Tags' => ContactTag::class |
||
| 58 | ]; |
||
| 59 | |||
| 60 | private static $belongs_many_many = [ |
||
| 61 | 'Lists' => ContactList::class |
||
| 62 | ]; |
||
| 63 | |||
| 64 | private static $casting = [ |
||
| 65 | 'TagsList' => 'Varchar', |
||
| 66 | 'ListsList' => 'Varchar', |
||
| 67 | 'FlaggedNice' => 'Boolean', |
||
| 68 | 'FullName' => 'Varchar', |
||
| 69 | 'Name' => 'Varchar', |
||
| 70 | "DefaultAddress" => "Text" |
||
| 71 | ]; |
||
| 72 | |||
| 73 | private static $summary_fields = [ |
||
| 74 | "FlaggedNice" =>"Flagged", |
||
| 75 | "FirstName" => "FirstName", |
||
| 76 | "Surname" => "Surname", |
||
| 77 | "Email" => "Email", |
||
| 78 | "DefaultAddress" => "Default Address", |
||
| 79 | "TagsList" => "Tags", |
||
| 80 | "ListsList" => "Lists" |
||
| 81 | ]; |
||
| 82 | |||
| 83 | private static $default_sort = [ |
||
| 84 | "FirstName" => "ASC", |
||
| 85 | "Surname" => "ASC" |
||
| 86 | ]; |
||
| 87 | |||
| 88 | private static $searchable_fields = [ |
||
| 89 | "FirstName", |
||
| 90 | "Surname", |
||
| 91 | "Email", |
||
| 92 | "Locations.Address1", |
||
| 93 | "Locations.Address2", |
||
| 94 | "Locations.City", |
||
| 95 | "Locations.Country", |
||
| 96 | "Locations.PostCode", |
||
| 97 | "Tags.Title", |
||
| 98 | "Lists.Title" |
||
| 99 | ]; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Fields that can be synced with associated member |
||
| 103 | * |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private static $sync_fields = [ |
||
| 107 | "FirstName", |
||
| 108 | "Surname", |
||
| 109 | "Company", |
||
| 110 | "Phone", |
||
| 111 | "Mobile", |
||
| 112 | "Email" |
||
| 113 | ]; |
||
| 114 | |||
| 115 | public function getTitle() |
||
| 116 | { |
||
| 117 | $parts = []; |
||
| 118 | |||
| 119 | if (!empty($this->FirstName)) { |
||
|
|
|||
| 120 | $parts[] = $this->FirstName; |
||
| 121 | } |
||
| 122 | |||
| 123 | if (!empty($this->Surname)) { |
||
| 124 | $parts[] = $this->Surname; |
||
| 125 | } |
||
| 126 | |||
| 127 | if (!empty($this->Email)) { |
||
| 128 | $parts[] = "($this->Email)"; |
||
| 129 | } |
||
| 130 | |||
| 131 | $title = implode(" ", $parts); |
||
| 132 | |||
| 133 | $this->extend("updateTitle", $title); |
||
| 134 | |||
| 135 | return $title; |
||
| 136 | } |
||
| 137 | |||
| 138 | public function getFullName() |
||
| 155 | } |
||
| 156 | |||
| 157 | public function getFlaggedNice() |
||
| 158 | { |
||
| 159 | $obj = HTMLText::create(); |
||
| 160 | $obj->setValue(($this->Flagged)? '<span class="red">✱</span>' : ''); |
||
| 161 | |||
| 162 | $this->extend("updateFlaggedNice", $obj); |
||
| 163 | |||
| 164 | return $obj; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Find from our locations one marked as default (of if not the |
||
| 169 | * first in the list). |
||
| 170 | * |
||
| 171 | * @return ContactLocation |
||
| 172 | */ |
||
| 173 | public function DefaultLocation() |
||
| 174 | { |
||
| 175 | $location = $this |
||
| 176 | ->Locations() |
||
| 177 | ->sort("Default", "DESC") |
||
| 178 | ->first(); |
||
| 179 | |||
| 180 | $this->extend("updateDefaultLocation", $location); |
||
| 181 | |||
| 182 | return $location; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Find from our locations one marked as default (of if not the |
||
| 187 | * first in the list). |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function getDefaultAddress() |
||
| 192 | { |
||
| 193 | $location = $this->DefaultLocation(); |
||
| 194 | |||
| 195 | if ($location) { |
||
| 196 | return $location->Address; |
||
| 197 | } else { |
||
| 198 | return ""; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the complete name of the member |
||
| 204 | * |
||
| 205 | * @return string Returns the first- and surname of the member. |
||
| 206 | */ |
||
| 207 | public function getName() |
||
| 208 | { |
||
| 209 | $name = ($this->Surname) ? trim($this->FirstName . ' ' . $this->Surname) : $this->FirstName; |
||
| 210 | |||
| 211 | $this->extend("updateName", $name); |
||
| 212 | |||
| 213 | return $name; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Generate as string of tag titles seperated by a comma |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getTagsList() |
||
| 222 | { |
||
| 223 | $return = ""; |
||
| 224 | $tags = $this->Tags()->column("Title"); |
||
| 225 | |||
| 226 | $this->extend("updateTagsList", $tags); |
||
| 227 | |||
| 228 | return implode( |
||
| 229 | $this->config()->list_seperator, |
||
| 230 | $tags |
||
| 231 | ); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Generate as string of list titles seperated by a comma |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function getListsList() |
||
| 240 | { |
||
| 241 | $return = ""; |
||
| 242 | $list = $this->Lists()->column("Title"); |
||
| 243 | |||
| 244 | $this->extend("updateListsList", $tags); |
||
| 245 | |||
| 246 | return implode( |
||
| 247 | $this->config()->list_seperator, |
||
| 248 | $list |
||
| 249 | ); |
||
| 250 | } |
||
| 251 | |||
| 252 | public function getFlagged() |
||
| 253 | { |
||
| 254 | $flagged = false; |
||
| 255 | |||
| 256 | foreach ($this->Notes() as $note) { |
||
| 257 | if ($note->Flag) { |
||
| 258 | $flagged = true; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | $this->extend("updateFlagged", $flagged); |
||
| 263 | |||
| 264 | return $flagged; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Update an associated member with the data from this contact |
||
| 269 | * |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | public function syncToMember() |
||
| 273 | { |
||
| 274 | $member = $this->Member(); |
||
| 275 | $sync = $this->config()->sync_fields; |
||
| 276 | $write = false; |
||
| 277 | |||
| 278 | if (!$member->exists()) { |
||
| 279 | return; |
||
| 280 | } |
||
| 281 | |||
| 282 | foreach ($this->getChangedFields() as $field => $change) { |
||
| 283 | // If this field is a field to sync, and it is different |
||
| 284 | // then update member |
||
| 285 | if (in_array($field, $sync) && $member->$field != $this->$field) { |
||
| 286 | $member->$field = $this->$field; |
||
| 287 | $write = true; |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | if ($write) { |
||
| 292 | $member->write(); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | public function getCMSFields() |
||
| 297 | { |
||
| 298 | $fields = parent::getCMSFields(); |
||
| 299 | |||
| 300 | $fields->removeByName("Tags"); |
||
| 301 | $fields->removeByName("Notes"); |
||
| 302 | |||
| 303 | $tag_field = TagField::create( |
||
| 304 | 'Tags', |
||
| 305 | null, |
||
| 306 | ContactTag::get(), |
||
| 307 | $this->Tags() |
||
| 308 | )->setRightTitle(_t( |
||
| 309 | "Contacts.TagDescription", |
||
| 310 | "List of tags related to this contact, seperated by a comma." |
||
| 311 | ))->setShouldLazyLoad(true); |
||
| 312 | |||
| 313 | if ($this->ID) { |
||
| 314 | $gridField = GridField::create( |
||
| 315 | 'Notes', |
||
| 316 | 'Notes', |
||
| 317 | $this->Notes() |
||
| 318 | ); |
||
| 319 | |||
| 320 | $config = GridFieldConfig_RelationEditor::create(); |
||
| 321 | |||
| 322 | $gridField->setConfig($config); |
||
| 323 | |||
| 324 | $fields->addFieldToTab( |
||
| 325 | "Root.Notes", |
||
| 326 | $gridField |
||
| 327 | ); |
||
| 328 | } |
||
| 329 | |||
| 330 | $fields->addFieldsToTab( |
||
| 331 | "Root.Main", |
||
| 332 | [ |
||
| 333 | $member_field = HasOneAutocompleteField::create( |
||
| 334 | 'MemberID', |
||
| 335 | _t( |
||
| 336 | 'SilverCommerce\ContactAdmin.LinkContactToAccount', |
||
| 337 | 'Link this contact to a user account?' |
||
| 338 | ), |
||
| 339 | Member::class, |
||
| 340 | 'Title' |
||
| 341 | ), |
||
| 342 | $tag_field |
||
| 343 | ] |
||
| 344 | ); |
||
| 345 | |||
| 346 | return $fields; |
||
| 347 | } |
||
| 348 | |||
| 349 | public function getCMSValidator() |
||
| 350 | { |
||
| 351 | return new RequiredFields(array( |
||
| 352 | "FirstName", |
||
| 353 | "Surname" |
||
| 354 | )); |
||
| 355 | } |
||
| 356 | |||
| 357 | public function providePermissions() |
||
| 358 | { |
||
| 359 | return array( |
||
| 360 | "CONTACTS_MANAGE" => array( |
||
| 361 | 'name' => _t( |
||
| 362 | 'Contacts.PERMISSION_MANAGE_CONTACTS_DESCRIPTION', |
||
| 363 | 'Manage contacts' |
||
| 364 | ), |
||
| 365 | 'help' => _t( |
||
| 366 | 'Contacts.PERMISSION_MANAGE_CONTACTS_HELP', |
||
| 367 | 'Allow creation and editing of contacts' |
||
| 368 | ), |
||
| 369 | 'category' => _t('Contacts.Contacts', 'Contacts') |
||
| 370 | ), |
||
| 371 | "CONTACTS_DELETE" => array( |
||
| 372 | 'name' => _t( |
||
| 373 | 'Contacts.PERMISSION_DELETE_CONTACTS_DESCRIPTION', |
||
| 374 | 'Delete contacts' |
||
| 375 | ), |
||
| 376 | 'help' => _t( |
||
| 377 | 'Contacts.PERMISSION_DELETE_CONTACTS_HELP', |
||
| 378 | 'Allow deleting of contacts' |
||
| 379 | ), |
||
| 380 | 'category' => _t('Contacts.Contacts', 'Contacts') |
||
| 381 | ) |
||
| 382 | ); |
||
| 383 | } |
||
| 384 | |||
| 385 | public function canView($member = null) |
||
| 386 | { |
||
| 387 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 388 | |||
| 389 | if ($extended !== null) { |
||
| 390 | return $extended; |
||
| 391 | } |
||
| 392 | |||
| 393 | if (!$member) { |
||
| 394 | $member = Member::currentUser(); |
||
| 395 | } |
||
| 396 | |||
| 397 | if ($member && Permission::checkMember($member->ID, "CONTACTS_MANAGE")) { |
||
| 398 | return true; |
||
| 399 | } |
||
| 400 | |||
| 401 | return false; |
||
| 402 | } |
||
| 403 | |||
| 404 | public function canCreate($member = null, $context = []) |
||
| 405 | { |
||
| 406 | $extended = $this->extendedCan(__FUNCTION__, $member, $context); |
||
| 407 | |||
| 408 | if ($extended !== null) { |
||
| 409 | return $extended; |
||
| 410 | } |
||
| 411 | |||
| 412 | if (!$member) { |
||
| 413 | $member = Security::getCurrentUser(); |
||
| 414 | } |
||
| 415 | |||
| 416 | if ($member && Permission::checkMember($member->ID, "CONTACTS_MANAGE")) { |
||
| 417 | return true; |
||
| 418 | } |
||
| 419 | |||
| 420 | return false; |
||
| 421 | } |
||
| 422 | |||
| 423 | public function canEdit($member = null) |
||
| 424 | { |
||
| 425 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 426 | |||
| 427 | if ($extended !== null) { |
||
| 428 | return $extended; |
||
| 429 | } |
||
| 430 | |||
| 431 | if (!$member) { |
||
| 432 | $member = Security::getCurrentUser(); |
||
| 433 | } |
||
| 434 | |||
| 435 | if ($member && Permission::checkMember($member->ID, "CONTACTS_MANAGE")) { |
||
| 436 | return true; |
||
| 437 | } |
||
| 438 | |||
| 439 | return false; |
||
| 440 | } |
||
| 441 | |||
| 442 | public function canDelete($member = null, $context = []) |
||
| 443 | { |
||
| 444 | $extended = $this->extendedCan(__FUNCTION__, $member); |
||
| 445 | |||
| 446 | if ($extended !== null) { |
||
| 447 | return $extended; |
||
| 448 | } |
||
| 449 | |||
| 450 | if (!$member) { |
||
| 451 | $member = Security::getCurrentUser(); |
||
| 452 | } |
||
| 453 | |||
| 454 | if ($member && Permission::checkMember($member->ID, "CONTACTS_DELETE")) { |
||
| 455 | return true; |
||
| 456 | } |
||
| 457 | |||
| 458 | return false; |
||
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Sync to associated member (if needed) |
||
| 463 | * |
||
| 464 | * @return void |
||
| 465 | */ |
||
| 466 | public function onAfterWrite() |
||
| 467 | { |
||
| 468 | parent::onAfterWrite(); |
||
| 469 | |||
| 470 | $this->syncToMember(); |
||
| 471 | } |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Cleanup DB on removal |
||
| 475 | * |
||
| 476 | */ |
||
| 477 | public function onBeforeDelete() |
||
| 489 | } |
||
| 490 | } |
||
| 491 | } |
||
| 492 |