Passed
Push — 1.0 ( 3dec11...cb0871 )
by Morven
02:11
created

ContactAdmin::SearchForm()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 51
rs 9.0328
c 0
b 0
f 0
cc 5
nc 2
nop 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace SilverCommerce\ContactAdmin\Admin;
4
5
use SilverStripe\Forms\TextField;
6
use SilverStripe\Dev\CsvBulkLoader;
7
use SilverStripe\TagField\TagField;
8
use SilverStripe\Control\Controller;
9
use SilverStripe\Core\Config\Config;
10
use Colymba\BulkManager\BulkManager;
11
use SilverStripe\Forms\CheckboxField;
12
use TractorCow\AutoComplete\AutoCompleteField;
13
use SilverCommerce\ContactAdmin\Model\Contact;
14
use Colymba\BulkManager\BulkAction\UnlinkHandler;
15
use SilverCommerce\ContactAdmin\Model\ContactTag;
16
use SilverCommerce\ContactAdmin\Model\ContactList;
17
use SilverStripe\Forms\GridField\GridFieldPrintButton;
18
use SilverStripe\Forms\GridField\GridFieldExportButton;
19
use SilverCommerce\ContactAdmin\BulkActions\AddTagsHandler;
20
use SilverCommerce\ContactAdmin\BulkActions\AddToListHandler;
21
use SilverCommerce\ContactAdmin\Forms\ModelAdminAutoCompleteField;
0 ignored issues
show
Bug introduced by
The type SilverCommerce\ContactAd...lAdminAutoCompleteField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use ilateral\SilverStripe\ModelAdminPlus\ModelAdminPlus;
23
24
/**
25
 * Management interface for contacts
26
 * 
27
 * @author ilateral
28
 * @package Contacts
29
 */
30
class ContactAdmin extends ModelAdminPlus
31
{
32
    
33
    private static $menu_priority = 0;
0 ignored issues
show
introduced by
The private property $menu_priority is not used, and could be removed.
Loading history...
34
35
    private static $managed_models = [
0 ignored issues
show
introduced by
The private property $managed_models is not used, and could be removed.
Loading history...
36
        Contact::class,
37
        ContactTag::class,
38
        ContactList::class
39
    ];
40
41
    private static $url_segment = 'contacts';
0 ignored issues
show
introduced by
The private property $url_segment is not used, and could be removed.
Loading history...
42
43
    private static $menu_title = 'Contacts';
0 ignored issues
show
introduced by
The private property $menu_title is not used, and could be removed.
Loading history...
44
45
    private static $model_importers = [
0 ignored issues
show
introduced by
The private property $model_importers is not used, and could be removed.
Loading history...
46
        Contact::class => CSVBulkLoader::class,
47
        ContactTag::class => CSVBulkLoader::class,
48
        ContactList::class => CSVBulkLoader::class
49
    ];
50
51
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
52
        "SearchForm"
53
    ];
54
55
    public $showImportForm = [
56
        Contact::class,
57
        ContactTag::class,
58
        ContactList::class
59
    ];
60
61
    /**
62
     * @var string
63
     */
64
    private static $menu_icon_class = 'font-icon-torso';
0 ignored issues
show
introduced by
The private property $menu_icon_class is not used, and could be removed.
Loading history...
65
66
    public function getSearchContext()
67
    {
68
        $context = parent::getSearchContext();
69
70
        if ($this->modelClass == Contact::class) {
71
            $context
72
                ->getFields()
73
                ->push(new CheckboxField('q[Flagged]', _t("Contacts.ShowFlaggedOnly", 'Show flagged only')));
74
        }
75
76
        return $context;
77
    }
78
79
    public function getList()
80
    {
81
        $list = parent::getList();
82
83
        // use this to access search parameters
84
        $params = $this->request->requestVar('q');
85
86
        if ($this->modelClass == Contact::class && isset($params['Flagged']) && $params['Flagged']) {
87
            $list = $list->filter(
88
                "Notes.Flag",
89
                true
90
            );
91
        }
92
93
        return $list;
94
    }
95
96
    public function getEditForm($id = null, $fields = null)
97
    {
98
        $form = parent::getEditForm($id, $fields);
99
        $class = $this->sanitiseClassName($this->modelClass);
100
        $gridField = $form->Fields()->fieldByName($class);
101
        $config = $gridField->getConfig();
102
103
        // Add bulk editing to gridfield
104
        $manager = $config->getComponentByType(BulkManager::class);
105
106
        if ($this->modelClass == Contact::class) {
107
            $manager->addBulkAction(AddTagsHandler::class);
108
            $manager->addBulkAction(AddToListHandler::class);
109
        }
110
111
        $this->extend("updateEditForm", $form);
112
113
        return $form;
114
    }
115
}