From 456360d2a6dfbbe03d36bb4a5ca310b120b2926f Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Thu, 17 Oct 2024 11:21:36 +1300 Subject: [PATCH] API Stop refering to "pages" when dealing with generic records --- code/CMSMenu.php | 2 +- code/CMSProfileController.php | 2 +- code/LeftAndMain.php | 70 +++++++++++++++++-------------- code/LeftAndMain_SearchFilter.php | 16 +++---- code/ModelAdmin.php | 4 +- 5 files changed, 50 insertions(+), 44 deletions(-) diff --git a/code/CMSMenu.php b/code/CMSMenu.php index b1be43881..50a8882f3 100644 --- a/code/CMSMenu.php +++ b/code/CMSMenu.php @@ -264,7 +264,7 @@ public static function get_viewable_menu_items($member = null) $controllerObj = singleton($menuItem->controller); if (Controller::has_curr()) { // Necessary for canView() to have request data available, - // e.g. to check permissions against LeftAndMain->currentPage() + // e.g. to check permissions against LeftAndMain->currentRecord() $controllerObj->setRequest(Controller::curr()->getRequest()); if (!$controllerObj->canView($member)) { continue; diff --git a/code/CMSProfileController.php b/code/CMSProfileController.php index 7bb527e46..6769f12c0 100644 --- a/code/CMSProfileController.php +++ b/code/CMSProfileController.php @@ -24,7 +24,7 @@ class CMSProfileController extends LeftAndMain public function getEditForm($id = null, $fields = null) { - $this->setCurrentPageID(Security::getCurrentUser()->ID); + $this->setCurrentRecordID(Security::getCurrentUser()->ID); $form = parent::getEditForm($id, $fields); diff --git a/code/LeftAndMain.php b/code/LeftAndMain.php index 9eb920f7b..ef79a9f12 100644 --- a/code/LeftAndMain.php +++ b/code/LeftAndMain.php @@ -1043,7 +1043,7 @@ public static function menu_icon_class_for_class($class) public function show(HTTPRequest $request): HTTPResponse { if ($request->param('ID')) { - $this->setCurrentPageID($request->param('ID')); + $this->setCurrentRecordID($request->param('ID')); } return $this->getResponseNegotiator()->respond($request); } @@ -1232,15 +1232,23 @@ public function PreviewPanel() return null; } + /** + * Get the class of the model which is managed by this controller. + * @return class-string + */ + public function getModelClass(): string + { + return static::config()->get('tree_class') ?? ''; + } + /** * Get dataobject from the current ID * * @param int|DataObject $id ID or object - * @return DataObject */ - public function getRecord($id) + public function getRecord($id): ?DataObject { - $className = $this->config()->get('tree_class'); + $className = $this->getModelClass(); if (!$className) { return null; } @@ -1313,7 +1321,7 @@ protected function getSearchFilter() public function save(array $data, Form $form): HTTPResponse { $request = $this->getRequest(); - $className = $this->config()->get('tree_class'); + $className = $this->getModelClass(); // Existing or new record? $id = $data['ID']; @@ -1326,7 +1334,7 @@ public function save(array $data, Form $form): HTTPResponse $this->httpError(404, "Bad record ID #" . (int)$id); } } else { - if (!singleton($this->config()->get('tree_class'))->canCreate()) { + if (!singleton($className)->canCreate()) { return Security::permissionFailure($this); } $record = $this->getNewItem($id, false); @@ -1336,7 +1344,7 @@ public function save(array $data, Form $form): HTTPResponse $form->saveInto($record, true); $record->write(); $this->extend('onAfterSave', $record); - $this->setCurrentPageID($record->ID); + $this->setCurrentRecordID($record->ID); $message = _t(__CLASS__ . '.SAVEDUP', 'Saved.'); if ($this->getSchemaRequested()) { @@ -1362,7 +1370,7 @@ public function save(array $data, Form $form): HTTPResponse */ public function getNewItem($id, $setID = true) { - $class = $this->config()->get('tree_class'); + $class = $this->getModelClass(); $object = Injector::inst()->create($class); if ($setID) { $object->ID = $id; @@ -1372,7 +1380,7 @@ public function getNewItem($id, $setID = true) public function delete(array $data, Form $form): HTTPResponse { - $className = $this->config()->get('tree_class'); + $className = $this->getModelClass(); $id = $data['ID']; $record = DataObject::get_by_id($className, $id); @@ -1402,9 +1410,9 @@ public function delete(array $data, Form $form): HTTPResponse * * This is a "pseudo-abstract" method, usually connected to a {@link getEditForm()} * method in an entwine subclass. This method can accept a record identifier, - * selected either in custom logic, or through {@link currentPageID()}. + * selected either in custom logic, or through {@link currentRecordID()}. * The form usually construct itself from {@link DataObject->getCMSFields()} - * for the specific managed subclass defined in {@link LeftAndMain::$tree_class}. + * for the specific managed subclass defined in {@link LeftAndMain::getModelClass()}. * * @param HTTPRequest $request Passed if executing a HTTPRequest directly on the form. * If empty, this is invoked as $EditForm in the template @@ -1427,7 +1435,7 @@ public function EditForm($request = null) public function getEditForm($id = null, $fields = null) { if (!$id) { - $id = $this->currentPageID(); + $id = $this->currentRecordID(); } // Check record exists @@ -1457,8 +1465,8 @@ public function getEditForm($id = null, $fields = null) $fields->push(new HiddenField('ClassName')); } - $tree_class = $this->config()->get('tree_class'); - if ($tree_class::has_extension(Hierarchy::class) + $modelClass = $this->getModelClass(); + if ($modelClass::has_extension(Hierarchy::class) && !$fields->dataFieldByName('ParentID') ) { $fields->push(new HiddenField('ParentID')); @@ -1636,7 +1644,7 @@ public function EditFormTools() */ public function batchactions() { - return new CMSBatchActionHandler($this, 'batchactions', $this->config()->get('tree_class')); + return new CMSBatchActionHandler($this, 'batchactions', $this->getModelClass()); } /** @@ -1681,7 +1689,7 @@ public function BatchActionsForm() public function printable() { - $form = $this->getEditForm($this->currentPageID()); + $form = $this->getEditForm($this->currentRecordID()); if (!$form) { return false; } @@ -1704,7 +1712,7 @@ public function printable() public function getSilverStripeNavigator(?DataObject $record = null) { if (!$record) { - $record = $this->currentPage(); + $record = $this->currentRecord(); } if ($record && (($record instanceof CMSPreviewable) || $record->has_extension(CMSPreviewable::class))) { $navigator = new SilverStripeNavigator($record); @@ -1719,11 +1727,11 @@ public function getSilverStripeNavigator(?DataObject $record = null) * sources (in this order): * - GET/POST parameter named 'ID' * - URL parameter named 'ID' - * - Session value namespaced by classname, e.g. "CMSMain.currentPage" + * - Session value namespaced by classname, e.g. "CMSMain.currentRecord" * * @return int */ - public function currentPageID() + public function currentRecordID() { if ($this->pageID) { return $this->pageID; @@ -1732,9 +1740,9 @@ public function currentPageID() return $this->getRequest()->requestVar('ID'); } - if ($this->getRequest()->requestVar('CMSMainCurrentPageID') && is_numeric($this->getRequest()->requestVar('CMSMainCurrentPageID'))) { + if ($this->getRequest()->requestVar('CMSMainCurrentRecordID') && is_numeric($this->getRequest()->requestVar('CMSMainCurrentRecordID'))) { // see GridFieldDetailForm::ItemEditForm - return $this->getRequest()->requestVar('CMSMainCurrentPageID'); + return $this->getRequest()->requestVar('CMSMainCurrentRecordID'); } if (isset($this->urlParams['ID']) && is_numeric($this->urlParams['ID'])) { @@ -1747,34 +1755,34 @@ public function currentPageID() /** @deprecated */ $session = $this->getRequest()->getSession(); - return $session->get($this->sessionNamespace() . ".currentPage") ?: null; + return $session->get($this->sessionNamespace() . ".currentRecord") ?: null; } /** * Forces the current page to be set in session, - * which can be retrieved later through {@link currentPageID()}. + * which can be retrieved later through {@link currentRecordID()}. * Keep in mind that setting an ID through GET/POST or * as a URL parameter will overrule this value. * * @param int $id */ - public function setCurrentPageID($id) + public function setCurrentRecordID($id) { $this->pageID = $id; $id = (int)$id; /** @deprecated */ - $this->getRequest()->getSession()->set($this->sessionNamespace() . ".currentPage", $id); + $this->getRequest()->getSession()->set($this->sessionNamespace() . ".currentRecord", $id); } /** - * Uses {@link getRecord()} and {@link currentPageID()} + * Uses {@link getRecord()} and {@link currentRecordID()} * to get the currently selected record. * * @return DataObject */ - public function currentPage() + public function currentRecord() { - return $this->getRecord($this->currentPageID()); + return $this->getRecord($this->currentRecordID()); } /** @@ -1784,9 +1792,9 @@ public function currentPage() * @param DataObject $record * @return bool */ - public function isCurrentPage(DataObject $record) + public function isCurrentRecord(DataObject $record) { - return ($record->ID == $this->currentPageID()); + return ($record->ID == $this->currentRecordID()); } /** @@ -1845,7 +1853,7 @@ public function CMSVersionNumber() */ public function SwitchView() { - $page = $this->currentPage(); + $page = $this->currentRecord(); if (!$page) { return null; } diff --git a/code/LeftAndMain_SearchFilter.php b/code/LeftAndMain_SearchFilter.php index 1e96f54a6..c00a093e9 100644 --- a/code/LeftAndMain_SearchFilter.php +++ b/code/LeftAndMain_SearchFilter.php @@ -19,7 +19,7 @@ interface LeftAndMain_SearchFilter public function getChildrenMethod(); /** - * Method on {@link Hierarchy} objects which is used find the number of children for a parent page + * Method on {@link Hierarchy} objects which is used find the number of children for a parent record * * @return string */ @@ -27,19 +27,19 @@ public function getNumChildrenMethod(); /** - * Returns TRUE if the given page should be included in the tree. - * Caution: Does NOT check view permissions on the page. + * Returns TRUE if the given record should be included in the tree. + * Caution: Does NOT check view permissions on the record. * - * @param DataObject $page + * @param DataObject $record * @return bool */ - public function isPageIncluded($page); + public function isRecordIncluded($page); /** - * Given a page, determine any additional CSS classes to apply to the tree node + * Given a record, determine any additional CSS classes to apply to the tree node * - * @param DataObject $page + * @param DataObject $record * @return array|string */ - public function getPageClasses($page); + public function getRecordClasses($record); } diff --git a/code/ModelAdmin.php b/code/ModelAdmin.php index 14fdb532a..b72d6e6a6 100644 --- a/code/ModelAdmin.php +++ b/code/ModelAdmin.php @@ -410,10 +410,8 @@ public function getList() /** * The model managed by this instance. * See $managed_models for potential values. - * - * @return string */ - public function getModelClass() + public function getModelClass(): string { return $this->modelClass; }