|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Heyday\GridFieldBulkDelete; |
| 4 | + |
| 5 | +use SilverStripe\Forms\GridField\GridField_HTMLProvider; |
| 6 | +use SilverStripe\Forms\GridField\GridField_FormAction; |
| 7 | +use SilverStripe\Forms\GridField\GridField_DataManipulator; |
| 8 | +use SilverStripe\Forms\GridField\GridField_ActionProvider; |
| 9 | +use SilverStripe\Forms\GridField\GridFieldPaginator; |
| 10 | +use SilverStripe\Forms\GridField\GridField; |
| 11 | +use SilverStripe\Control\Controller; |
| 12 | +use SilverStripe\ORM\SS_List; |
| 13 | + |
| 14 | +/** |
| 15 | + * Adds a dropdown and a button |
| 16 | + * Allowig users to delete records in the gridfield in bulk |
| 17 | + * with the option to delete only records older than 1,2,3 or 6 months. |
| 18 | + * |
| 19 | + * Also, if available, a queued job will be used if too many record need to be deleted. |
| 20 | + * Both this task and queuedjob loop trhough the record and invoke "delete" |
| 21 | + * so any dependant record may be deleted as well. |
| 22 | + * |
| 23 | + */ |
| 24 | +class GridFieldBulkDeleteForm implements GridField_HTMLProvider, GridField_ActionProvider |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + const STATUS_GOOD = 'good'; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var string |
| 33 | + */ |
| 34 | + protected $targetFragment; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var string |
| 38 | + */ |
| 39 | + protected $message; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var string |
| 43 | + */ |
| 44 | + protected $status = self::STATUS_GOOD; |
| 45 | + |
| 46 | + public function __construct($targetFragment = 'before', $threshold = null) |
| 47 | + { |
| 48 | + $this->targetFragment = $targetFragment; |
| 49 | + |
| 50 | + if ($threshold && $threshold > 0) { |
| 51 | + $this->use_queued_threshold = $threshold; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Return all HTML fragment of delete form |
| 57 | + * |
| 58 | + * @param GridField $gridField |
| 59 | + * @return array |
| 60 | + */ |
| 61 | + public function getHTMLFragments($gridField) |
| 62 | + { |
| 63 | + $form = $gridField->getForm(); |
| 64 | + $records = $this->getFilteredRecordList($gridField); |
| 65 | + if (!$records->exists()) { |
| 66 | + /** |
| 67 | + * If a message exists, but no record |
| 68 | + * it means we have deleted them all |
| 69 | + * so display the message anyway |
| 70 | + */ |
| 71 | + if ($this->message) { |
| 72 | + $fragment = sprintf('<p class="message %s">%s</p>', $this->status, $this->message); |
| 73 | + return [$this->targetFragment => $fragment]; |
| 74 | + } |
| 75 | + // Otherwise hide the form |
| 76 | + return []; |
| 77 | + } |
| 78 | + |
| 79 | + $singleton = singleton($gridField->getModelClass()); |
| 80 | + if (!$singleton->canDelete()) { |
| 81 | + return []; |
| 82 | + } |
| 83 | + |
| 84 | + $buttonTitle = sprintf('Delete %s record(s)', $records->Count()); |
| 85 | + |
| 86 | + $button = new GridField_FormAction( |
| 87 | + $gridField, |
| 88 | + 'bulkdelete', |
| 89 | + $buttonTitle, |
| 90 | + 'bulkdelete', |
| 91 | + null |
| 92 | + ); |
| 93 | + |
| 94 | + $button->setForm($gridField->getForm()); |
| 95 | + $button->addExtraClass('bulkdelete_button'); // For JS purpose |
| 96 | + $button->addExtraClass('font-icon-trash btn btn-outline-danger'); // For styling purpose |
| 97 | + |
| 98 | + // Set message |
| 99 | + if ($this->message) { |
| 100 | + $form->setMessage($this->message, $this->status); |
| 101 | + } |
| 102 | + |
| 103 | + $template = '<div><table><tr><td style="vertical-align:top;"><div style="margin-left: 7px;">%s</div></td></tr></table></div>'; |
| 104 | + |
| 105 | + $fragment = sprintf($template, $button->Field()); |
| 106 | + |
| 107 | + return [ |
| 108 | + $this->targetFragment => $fragment |
| 109 | + ]; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * export is an action button |
| 114 | + * |
| 115 | + * @param GridField |
| 116 | + * @return array |
| 117 | + */ |
| 118 | + public function getActions($gridField) |
| 119 | + { |
| 120 | + return ['bulkdelete']; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * export is an action button |
| 125 | + * |
| 126 | + * @param string $actionName |
| 127 | + * @param mixed $arguments |
| 128 | + * @param mixed $data |
| 129 | + * |
| 130 | + * @return array |
| 131 | + */ |
| 132 | + public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
| 133 | + { |
| 134 | + if ($actionName == 'bulkdelete') { |
| 135 | + return $this->handleBulkDelete($gridField); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Handle the export, for both the action button and the URL |
| 141 | + */ |
| 142 | + public function handleBulkDelete(GridField $gridField) |
| 143 | + { |
| 144 | + $records = $this->getFilteredRecordList($gridField); |
| 145 | + |
| 146 | + $ids = []; |
| 147 | + foreach ($records as $record) { |
| 148 | + $ids[] = $record->ID; |
| 149 | + $record->delete(); |
| 150 | + } |
| 151 | + |
| 152 | + $this->message = sprintf('%s records have been successfully deleted.', count($ids)); |
| 153 | + $this->status = self::STATUS_GOOD; |
| 154 | + |
| 155 | + Controller::curr()->getResponse()->setStatusCode( |
| 156 | + 200, |
| 157 | + $this->message |
| 158 | + ); |
| 159 | + |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * To get the entire list of records with the potential filters |
| 165 | + * we need to remove the pagination but apply all other filters |
| 166 | + */ |
| 167 | + public function getFilteredRecordList(GridField $gridfield): SS_List |
| 168 | + { |
| 169 | + $list = $gridfield->getList(); |
| 170 | + |
| 171 | + foreach ($gridfield->getComponents() as $item) { |
| 172 | + if ($item instanceof GridField_DataManipulator && !$item instanceof GridFieldPaginator) { |
| 173 | + $list = $item->getManipulatedData($gridfield, $list); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return $list; |
| 178 | + } |
| 179 | +} |
0 commit comments