lekoala /
silverstripe-admini
| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\Admini; |
||
| 4 | |||
| 5 | use SilverStripe\Forms\Form; |
||
| 6 | use LeKoala\Admini\LeftAndMain; |
||
| 7 | use LeKoala\Admini\MaterialIcons; |
||
| 8 | use SilverStripe\ORM\ArrayList; |
||
| 9 | use SilverStripe\ORM\DataObject; |
||
| 10 | use SilverStripe\View\ArrayData; |
||
| 11 | use SilverStripe\Control\Director; |
||
| 12 | use SilverStripe\Forms\FormAction; |
||
| 13 | use SilverStripe\ORM\ValidationResult; |
||
| 14 | use SilverStripe\SiteConfig\SiteConfig; |
||
| 15 | use SilverStripe\Versioned\RecursivePublishable; |
||
| 16 | |||
| 17 | class SiteConfigLeftAndMain extends LeftAndMain |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private static $url_segment = 'settings'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private static $url_rule = '/$Action/$ID/$OtherID'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | private static $menu_priority = -1; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private static $menu_title = 'Settings'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private static $menu_icon = MaterialIcons::SETTINGS; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private static $tree_class = SiteConfig::class; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private static $required_permission_codes = array('EDIT_SITECONFIG'); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Initialises the {@link SiteConfig} controller. |
||
| 56 | */ |
||
| 57 | public function init() |
||
| 58 | { |
||
| 59 | parent::init(); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param null $id Not used. |
||
| 64 | * @param null $fields Not used. |
||
| 65 | * |
||
| 66 | * @return Form |
||
| 67 | */ |
||
| 68 | public function getEditForm($id = null, $fields = null) |
||
| 69 | { |
||
| 70 | $siteConfig = SiteConfig::current_site_config(); |
||
| 71 | $fields = $siteConfig->getCMSFields(); |
||
| 72 | |||
| 73 | // Retrieve validator, if one has been setup (e.g. via data extensions). |
||
| 74 | if ($siteConfig->hasMethod("getCMSValidator")) { |
||
| 75 | $validator = $siteConfig->getCMSValidator(); |
||
| 76 | } else { |
||
| 77 | $validator = null; |
||
| 78 | } |
||
| 79 | |||
| 80 | $actions = $siteConfig->getCMSActions(); |
||
| 81 | $saveAction = $actions->fieldByName("action_save_siteconfig"); |
||
| 82 | if ($saveAction) { |
||
| 83 | $saveAction->removeExtraClass("btn-primary"); |
||
| 84 | $saveAction->addExtraClass("btn-outline-success"); |
||
| 85 | $saveAction->setIcon(MaterialIcons::DONE); |
||
| 86 | } |
||
| 87 | // $negotiator = $this->getResponseNegotiator(); |
||
| 88 | /** @var Form $form */ |
||
| 89 | $form = Form::create( |
||
| 90 | $this, |
||
| 91 | 'EditForm', |
||
| 92 | $fields, |
||
| 93 | $actions, |
||
| 94 | $validator |
||
| 95 | )->setHTMLID('Form_EditForm'); |
||
| 96 | |||
| 97 | /* |
||
| 98 | $form->setValidationResponseCallback(function (ValidationResult $errors) use ($negotiator, $form) { |
||
| 99 | $request = $this->getRequest(); |
||
| 100 | if ($request->isAjax() && $negotiator) { |
||
| 101 | $result = $form->forTemplate(); |
||
| 102 | return $negotiator->respond($request, array( |
||
| 103 | 'CurrentForm' => function () use ($result) { |
||
| 104 | return $result; |
||
| 105 | } |
||
| 106 | )); |
||
| 107 | } |
||
| 108 | });*/ |
||
| 109 | |||
| 110 | $this->setCMSTabset($form); |
||
| 111 | |||
| 112 | $form->setHTMLID('Form_EditForm'); |
||
| 113 | $form->loadDataFrom($siteConfig); |
||
| 114 | $form->setTemplate($this->getTemplatesWithSuffix('_EditForm')); |
||
| 115 | |||
| 116 | $actions = $actions->dataFields(); |
||
| 117 | if ($actions) { |
||
|
0 ignored issues
–
show
|
|||
| 118 | /** @var FormAction $action */ |
||
| 119 | foreach ($actions as $action) { |
||
| 120 | $action->setUseButtonTag(true); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | |||
| 124 | $this->extend('updateEditForm', $form); |
||
| 125 | |||
| 126 | return $form; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Save the current sites {@link SiteConfig} into the database. |
||
| 131 | * The method is defined in SiteConfig::getCMSActions |
||
| 132 | * |
||
| 133 | * @param array $data |
||
| 134 | * @param Form $form |
||
| 135 | * @return String |
||
| 136 | */ |
||
| 137 | public function save_siteconfig($data, $form) |
||
| 138 | { |
||
| 139 | $data = $form->getData(); |
||
| 140 | $siteConfig = DataObject::get_by_id(SiteConfig::class, $data['ID']); |
||
| 141 | $form->saveInto($siteConfig); |
||
| 142 | $siteConfig->write(); |
||
| 143 | |||
| 144 | if ($siteConfig->hasExtension(RecursivePublishable::class)) { |
||
| 145 | $siteConfig->publishRecursive(); |
||
| 146 | } |
||
| 147 | |||
| 148 | $message = _t('SilverStripe\\Admin\\LeftAndMain.SAVEDUP', 'Saved.'); |
||
| 149 | |||
| 150 | // TODO: implement our own logic |
||
| 151 | // if (Director::is_ajax()) { |
||
| 152 | // $this->response->addHeader( |
||
| 153 | // 'X-Status', |
||
| 154 | // rawurlencode($message) |
||
| 155 | // ); |
||
| 156 | // return $form->forTemplate(); |
||
| 157 | // } |
||
| 158 | $this->successMessage($message); |
||
| 159 | return $this->redirect($this->LinkHash()); |
||
| 160 | } |
||
| 161 | |||
| 162 | public function Breadcrumbs($unlinked = false) |
||
| 163 | { |
||
| 164 | return new ArrayList(array( |
||
| 165 | new ArrayData(array( |
||
| 166 | 'Title' => static::menu_title(), |
||
| 167 | 'Link' => $this->Link() |
||
| 168 | )) |
||
| 169 | )); |
||
| 170 | } |
||
| 171 | } |
||
| 172 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.