1 | <?php |
||||
2 | |||||
3 | namespace SilverCommerce\ContactAdmin\Admin; |
||||
4 | |||||
5 | use SilverStripe\Forms\TextField; |
||||
6 | use Silverstripe\Admin\ModelAdmin; |
||||
7 | use SilverStripe\Dev\CsvBulkLoader; |
||||
8 | use SilverStripe\TagField\TagField; |
||||
9 | use SilverStripe\Control\Controller; |
||||
10 | use SilverStripe\Core\Config\Config; |
||||
11 | use Colymba\BulkManager\BulkManager; |
||||
12 | use SilverStripe\Forms\CheckboxField; |
||||
13 | use TractorCow\AutoComplete\AutoCompleteField; |
||||
14 | use SilverCommerce\ContactAdmin\Model\Contact; |
||||
15 | use Colymba\BulkManager\BulkAction\UnlinkHandler; |
||||
16 | use SilverCommerce\ContactAdmin\Model\ContactTag; |
||||
17 | use SilverCommerce\ContactAdmin\Model\ContactList; |
||||
18 | use SilverStripe\Forms\GridField\GridFieldPrintButton; |
||||
19 | use SilverStripe\Forms\GridField\GridFieldExportButton; |
||||
20 | use SilverCommerce\ContactAdmin\BulkActions\AddTagsHandler; |
||||
21 | use SilverCommerce\ContactAdmin\BulkActions\AddToListHandler; |
||||
22 | use SilverCommerce\ContactAdmin\Forms\ModelAdminAutoCompleteField; |
||||
23 | |||||
24 | /** |
||||
25 | * Management interface for contacts |
||||
26 | * |
||||
27 | * @author ilateral |
||||
28 | * @package Contacts |
||||
29 | */ |
||||
30 | class ContactAdmin extends ModelAdmin |
||||
31 | { |
||||
32 | |||||
33 | private static $menu_priority = 0; |
||||
34 | |||||
35 | private static $managed_models = [ |
||||
36 | Contact::class, |
||||
37 | ContactTag::class, |
||||
38 | ContactList::class |
||||
39 | ]; |
||||
40 | |||||
41 | private static $url_segment = 'contacts'; |
||||
42 | |||||
43 | private static $menu_title = 'Contacts'; |
||||
44 | |||||
45 | private static $model_importers = [ |
||||
46 | Contact::class => CSVBulkLoader::class, |
||||
47 | ContactTag::class => CSVBulkLoader::class, |
||||
48 | ContactList::class => CSVBulkLoader::class |
||||
49 | ]; |
||||
50 | |||||
51 | private static $allowed_actions = [ |
||||
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'; |
||||
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 = new BulkManager(); |
||||
105 | $manager->removeBulkAction(UnlinkHandler::class); |
||||
106 | |||||
107 | if ($this->modelClass == Contact::class) { |
||||
108 | $manager->addBulkAction(AddTagsHandler::class); |
||||
109 | $manager->addBulkAction(AddToListHandler::class); |
||||
110 | } else { |
||||
111 | $config |
||||
112 | ->removeComponentsByType(GridFieldExportButton::class) |
||||
113 | ->removeComponentsByType(GridFieldPrintButton::class); |
||||
114 | } |
||||
115 | |||||
116 | $config->addComponents($manager); |
||||
117 | |||||
118 | $this->extend("updateEditForm", $form); |
||||
119 | |||||
120 | return $form; |
||||
121 | } |
||||
122 | |||||
123 | public function SearchForm() |
||||
124 | { |
||||
125 | $form = parent::SearchForm(); |
||||
126 | $fields = $form->Fields(); |
||||
127 | |||||
128 | if ($this->modelClass == Contact::class) { |
||||
129 | $contacts = Contact::get(); |
||||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||||
130 | $config = Contact::config(); |
||||
131 | $has_one = $config->has_one; |
||||
132 | $has_many = $config->has_many; |
||||
133 | $many_many = $config->many_many; |
||||
134 | $belongs_many_many = $config->belongs_many_many; |
||||
135 | $associations = array_merge( |
||||
136 | $has_one, |
||||
137 | $has_many, |
||||
138 | $many_many, |
||||
139 | $belongs_many_many |
||||
140 | ); |
||||
141 | |||||
142 | foreach ($fields as $field) { |
||||
143 | if ($field instanceof TextField) { |
||||
144 | $name = $field->getName(); |
||||
145 | $title = $field->Title(); |
||||
146 | $db_field = str_replace(["q[", "]"], "", $name); |
||||
147 | $class = $this->modelClass; |
||||
148 | |||||
149 | // If this is a relation, switch class name |
||||
150 | if (strpos($name, "__")) { |
||||
151 | $parts = explode("__", $db_field); |
||||
152 | $class = $associations[$parts[0]]; |
||||
153 | $db_field = $parts[1]; |
||||
154 | } |
||||
155 | |||||
156 | $fields->replaceField( |
||||
157 | $name, |
||||
158 | ModelAdminAutoCompleteField::create( |
||||
159 | $name, |
||||
0 ignored issues
–
show
$name of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
160 | $title, |
||||
161 | $field->Value(), |
||||
162 | $class, |
||||
163 | $db_field |
||||
164 | )->setForm($form) |
||||
165 | ->setDisplayField($db_field) |
||||
166 | ->setLabelField($db_field) |
||||
167 | ->setStoredField($db_field) |
||||
168 | ); |
||||
169 | } |
||||
170 | } |
||||
171 | |||||
172 | } |
||||
173 | |||||
174 | return $form; |
||||
175 | } |
||||
176 | } |