From 681497b958835b772b8b28738104ebb6531adc12 Mon Sep 17 00:00:00 2001
From: colemanw
Date: Tue, 12 Sep 2023 21:24:32 -0400
Subject: [PATCH] CiviGrant - Cleanup and simplify grant form, remove unused
tab code
Deletes unused code for the contact summary tab (now handled by SearchKit).
Simplifies overcomplicated form context and detangles form embedded in page.
---
CRM/Core/Form.php | 14 +-
ext/civigrant/CRM/Grant/DAO/Grant.php | 10 +-
ext/civigrant/CRM/Grant/Form/Grant.php | 51 ++---
ext/civigrant/CRM/Grant/Form/GrantView.php | 12 +-
ext/civigrant/CRM/Grant/Page/Tab.php | 189 ------------------
ext/civigrant/CRM/Grant/Selector/Search.php | 12 +-
ext/civigrant/civigrant.php | 6 +-
.../managed/Navigation_Grants.mgd.php | 2 +-
.../templates/CRM/Grant/Form/Grant.tpl | 2 +
.../templates/CRM/Grant/Form/GrantView.tpl | 32 +--
.../templates/CRM/Grant/Page/Tab.tpl | 43 ----
ext/civigrant/xml/Menu/Grant.xml | 8 +-
ext/civigrant/xml/schema/CRM/Grant/Grant.xml | 8 +-
13 files changed, 72 insertions(+), 317 deletions(-)
delete mode 100644 ext/civigrant/CRM/Grant/Page/Tab.php
delete mode 100644 ext/civigrant/templates/CRM/Grant/Page/Tab.tpl
diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php
index 88968b816f..f26d489f54 100644
--- a/CRM/Core/Form.php
+++ b/CRM/Core/Form.php
@@ -1607,7 +1607,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
return 'delete';
}
// If you get this exception try adding more cases above.
- throw new Exception("Cannot determine api action for " . get_class($this) . '.' . 'CRM_Core_Action "' . CRM_Core_Action::description($action) . '" not recognized.');
+ throw new CRM_Core_Exception("Cannot determine api action for " . get_class($this) . '.' . 'CRM_Core_Action "' . CRM_Core_Action::description($action) . '" not recognized.');
}
/**
@@ -1615,7 +1615,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
* @throws Exception
*/
public function getDefaultEntity() {
- throw new Exception("Cannot determine default entity. " . get_class($this) . " should implement getDefaultEntity().");
+ throw new CRM_Core_Exception("Cannot determine default entity. " . get_class($this) . " should implement getDefaultEntity().");
}
/**
@@ -1625,7 +1625,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
* @throws Exception
*/
public function getDefaultContext() {
- throw new Exception("Cannot determine default context. " . get_class($this) . " should implement getDefaultContext().");
+ throw new CRM_Core_Exception("Cannot determine default context. " . get_class($this) . " should implement getDefaultContext().");
}
/**
@@ -2643,9 +2643,13 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
/**
* Sets page title based on entity and action.
- * @param string $entityLabel
+ * @param string|null $entityLabel
+ * @throws CRM_Core_Exception
*/
- public function setPageTitle($entityLabel) {
+ public function setPageTitle(?string $entityLabel = NULL): void {
+ if (!isset($entityLabel)) {
+ $entityLabel = \Civi\Api4\Utils\CoreUtil::getInfoItem($this->getDefaultEntity(), 'title');
+ }
switch ($this->_action) {
case CRM_Core_Action::ADD:
$this->setTitle(ts('New %1', [1 => $entityLabel]));
diff --git a/ext/civigrant/CRM/Grant/DAO/Grant.php b/ext/civigrant/CRM/Grant/DAO/Grant.php
index f25269dc93..fda65e11e6 100644
--- a/ext/civigrant/CRM/Grant/DAO/Grant.php
+++ b/ext/civigrant/CRM/Grant/DAO/Grant.php
@@ -6,7 +6,7 @@
*
* Generated from civigrant/xml/schema/CRM/Grant/Grant.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
- * (GenCodeChecksum:c3ae6f29f7956693f952034cb68e75a0)
+ * (GenCodeChecksum:35a957c6b295a38c1fc0881e57ab1ec4)
*/
use CRM_Grant_ExtensionUtil as E;
@@ -44,10 +44,10 @@ class CRM_Grant_DAO_Grant extends CRM_Core_DAO {
* @var string[]
*/
protected static $_paths = [
- 'add' => 'civicrm/grant/add?reset=1&action=add&context=standalone',
- 'view' => 'civicrm/contact/view/grant?reset=1&action=view&id=[id]&cid=[contact_id]',
- 'update' => 'civicrm/contact/view/grant?reset=1&action=update&id=[id]&cid=[contact_id]',
- 'delete' => 'civicrm/contact/view/grant?reset=1&action=delete&id=[id]&cid=[contact_id]',
+ 'add' => 'civicrm/grant/add?reset=1&action=add&cid=[contact_id]',
+ 'view' => 'civicrm/grant/add?reset=1&action=view&id=[id]',
+ 'update' => 'civicrm/grant/add?reset=1&action=update&id=[id]',
+ 'delete' => 'civicrm/grant/add?reset=1&action=delete&id=[id]',
];
/**
diff --git a/ext/civigrant/CRM/Grant/Form/Grant.php b/ext/civigrant/CRM/Grant/Form/Grant.php
index 661607e60c..0342d63723 100644
--- a/ext/civigrant/CRM/Grant/Form/Grant.php
+++ b/ext/civigrant/CRM/Grant/Form/Grant.php
@@ -45,16 +45,19 @@ class CRM_Grant_Form_Grant extends CRM_Core_Form {
*/
public function preProcess() {
- $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
$this->_grantType = NULL;
if ($this->_id) {
$this->_grantType = CRM_Core_DAO::getFieldValue('CRM_Grant_DAO_Grant', $this->_id, 'grant_type_id');
+ $this->_contactID = CRM_Core_DAO::getFieldValue('CRM_Grant_DAO_Grant', $this->_id, 'contact_id');
}
- $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
+ else {
+ $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
+ }
+ // Almost-useless _context variable just tells if we have a contact id
+ $this->_context = $this->_contactID ? 'contact' : 'standalone';
$this->assign('action', $this->_action);
- $this->assign('context', $this->_context);
// check permission for action.
$perm = $this->_action & CRM_Core_Action::DELETE ? 'delete in CiviGrant' : 'edit grants';
@@ -62,7 +65,7 @@ class CRM_Grant_Form_Grant extends CRM_Core_Form {
CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
}
- $this->setPageTitle(ts('Grant'));
+ $this->setPageTitle();
if ($this->_action & CRM_Core_Action::DELETE) {
return;
@@ -187,22 +190,24 @@ class CRM_Grant_Form_Grant extends CRM_Core_Form {
// make this form an upload since we dont know if the custom data injected dynamically
// is of type file etc $uploadNames = $this->get( 'uploadNames' );
- $this->addButtons([
- [
- 'type' => 'upload',
- 'name' => ts('Save'),
- 'isDefault' => TRUE,
- ],
- [
- 'type' => 'upload',
- 'name' => ts('Save and New'),
- 'subName' => 'new',
- ],
- [
- 'type' => 'cancel',
- 'name' => ts('Cancel'),
- ],
- ]);
+ if ($this->_action !== CRM_Core_Action::VIEW) {
+ $this->addButtons([
+ [
+ 'type' => 'upload',
+ 'name' => ts('Save'),
+ 'isDefault' => TRUE,
+ ],
+ [
+ 'type' => 'upload',
+ 'name' => ts('Save and New'),
+ 'subName' => 'new',
+ ],
+ [
+ 'type' => 'cancel',
+ 'name' => ts('Cancel'),
+ ],
+ ]);
+ }
$contactField = $this->addEntityRef('contact_id', ts('Applicant'), ['create' => TRUE], TRUE);
if ($this->_context != 'standalone') {
@@ -285,7 +290,7 @@ class CRM_Grant_Form_Grant extends CRM_Core_Form {
if ($this->_context == 'standalone') {
if ($buttonName == $this->getButtonName('upload', 'new')) {
$session->replaceUserContext(CRM_Utils_System::url('civicrm/grant/add',
- 'reset=1&action=add&context=standalone'
+ 'reset=1&action=add'
));
}
else {
@@ -295,8 +300,8 @@ class CRM_Grant_Form_Grant extends CRM_Core_Form {
}
}
elseif ($buttonName == $this->getButtonName('upload', 'new')) {
- $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/grant',
- "reset=1&action=add&context=grant&cid={$this->_contactID}"
+ $session->replaceUserContext(CRM_Utils_System::url('civicrm/grant/add',
+ "reset=1&action=add&cid={$this->_contactID}"
));
}
}
diff --git a/ext/civigrant/CRM/Grant/Form/GrantView.php b/ext/civigrant/CRM/Grant/Form/GrantView.php
index 261060bfeb..0b640ad2d0 100644
--- a/ext/civigrant/CRM/Grant/Form/GrantView.php
+++ b/ext/civigrant/CRM/Grant/Form/GrantView.php
@@ -69,21 +69,21 @@ class CRM_Grant_Form_GrantView extends CRM_Core_Form {
}
// add Grant to Recent Items
- $url = CRM_Utils_System::url('civicrm/contact/view/grant',
- "action=view&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
+ $url = CRM_Utils_System::url('civicrm/grant/add',
+ "action=view&reset=1&id={$values['id']}&cid={$values['contact_id']}"
);
$title = CRM_Contact_BAO_Contact::displayName($values['contact_id']) . ' - ' . ts('Grant') . ': ' . CRM_Utils_Money::format($values['amount_total']) . ' (' . $grantType[$values['grant_type_id']] . ')';
$recentOther = [];
if (CRM_Core_Permission::check('edit grants')) {
- $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant',
- "action=update&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
+ $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/grant/add',
+ "action=update&reset=1&id={$values['id']}&cid={$values['contact_id']}"
);
}
if (CRM_Core_Permission::check('delete in CiviGrant')) {
- $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant',
- "action=delete&reset=1&id={$values['id']}&cid={$values['contact_id']}&context=home"
+ $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/grant/add',
+ "action=delete&reset=1&id={$values['id']}&cid={$values['contact_id']}"
);
}
CRM_Utils_Recent::add($title,
diff --git a/ext/civigrant/CRM/Grant/Page/Tab.php b/ext/civigrant/CRM/Grant/Page/Tab.php
deleted file mode 100644
index 40fe12bcd6..0000000000
--- a/ext/civigrant/CRM/Grant/Page/Tab.php
+++ /dev/null
@@ -1,189 +0,0 @@
-_action);
- $controller->setEmbedded(TRUE);
- $controller->reset();
- $controller->set('cid', $this->_contactId);
- $controller->set('context', 'grant');
- $controller->process();
- $controller->run();
-
- if ($this->_contactId) {
- $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
- $this->assign('displayName', $displayName);
- $this->ajaxResponse['tabCount'] = CRM_Grant_BAO_Grant::getContactGrantCount($this->_contactId);
- }
- }
-
- /**
- * called when action is view.
- *
- * @return null
- */
- public function view() {
- $controller = new CRM_Core_Controller_Simple('CRM_Grant_Form_GrantView', 'View Grant', $this->_action);
- $controller->setEmbedded(TRUE);
- $controller->set('id', $this->_id);
- $controller->set('cid', $this->_contactId);
-
- return $controller->run();
- }
-
- /**
- * called when action is update or new.
- *
- * @return null
- */
- public function edit() {
- $controller = new CRM_Core_Controller_Simple('CRM_Grant_Form_Grant', 'Create grant', $this->_action);
- $controller->setEmbedded(TRUE);
- $controller->set('id', $this->_id);
- $controller->set('cid', $this->_contactId);
-
- return $controller->run();
- }
-
- /**
- * Build all the data structures needed to build the form.
- *
- * @return void
- */
- public function preProcess() {
- $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
- $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'browse');
- $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
-
- if ($context == 'standalone') {
- $this->_action = CRM_Core_Action::ADD;
- }
- else {
- $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
- $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
- $this->assign('contactId', $this->_contactId);
- $this->assign('displayName', $displayName);
-
- // check logged in url permission
- CRM_Contact_Page_View::checkUserPermission($this);
- }
- $this->assign('action', $this->_action);
-
- if ($this->_permission == CRM_Core_Permission::EDIT && !CRM_Core_Permission::check('edit grants')) {
- // demote to view since user does not have edit grant rights
- $this->_permission = CRM_Core_Permission::VIEW;
- $this->assign('permission', 'view');
- }
- }
-
- /**
- * the main function that is called when the page loads,
- * it decides the which action has to be taken for the page.
- *
- * @return null
- */
- public function run() {
- $this->preProcess();
-
- $this->setContext();
-
- if ($this->_action & CRM_Core_Action::VIEW) {
- $this->view();
- }
- elseif ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
- $this->edit();
- }
- else {
- $this->browse();
- }
- return parent::run();
- }
-
- public function setContext() {
- $context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
- $this->_id = CRM_Utils_Request::retrieve('id', 'Integer', $this);
- $session = CRM_Core_Session::singleton();
-
- $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
- //validate the qfKey
- if (!CRM_Utils_Rule::qfKey($qfKey)) {
- $qfKey = NULL;
- }
-
- switch ($context) {
- case 'search':
- $urlParams = 'force=1';
- if ($qfKey) {
- $urlParams .= "&qfKey=$qfKey";
- }
- $this->assign('searchKey', $qfKey);
-
- $url = CRM_Utils_System::url('civicrm/grant/search', $urlParams);
- break;
-
- case 'dashboard':
- $url = CRM_Utils_System::url('civicrm/grant', 'reset=1');
- break;
-
- case 'edit':
- $url = CRM_Utils_System::url('civicrm/contact/view/grant', 'reset=1&id=' . $this->_id . '&cid=' . $this->_contactId . '&action=view&context=grant&selectedChild=grant');
- break;
-
- case 'grant':
- $url = CRM_Utils_System::url('civicrm/contact/view', 'action=browse&selectedChild=grant&cid=' . $this->_contactId);
- break;
-
- case 'standalone':
- $url = CRM_Utils_System::url('civicrm/dashboard', 'reset=1');
- break;
-
- default:
- $cid = NULL;
- if ($this->_contactId) {
- $cid = '&cid=' . $this->_contactId;
- }
- $url = CRM_Utils_System::url('civicrm/grant/search', 'reset=1&force=1' . $cid);
- break;
- }
- $session->pushUserContext($url);
-
- if (CRM_Utils_Request::retrieve('confirmed', 'Boolean')) {
- CRM_Grant_BAO_Grant::del($this->_id);
- CRM_Utils_System::redirect($url);
- }
- }
-
-}
diff --git a/ext/civigrant/CRM/Grant/Selector/Search.php b/ext/civigrant/CRM/Grant/Selector/Search.php
index b3187e1096..c8252ff8e4 100644
--- a/ext/civigrant/CRM/Grant/Selector/Search.php
+++ b/ext/civigrant/CRM/Grant/Selector/Search.php
@@ -171,15 +171,15 @@ class CRM_Grant_Selector_Search extends CRM_Core_Selector_Base implements CRM_Co
self::$_links = [
CRM_Core_Action::VIEW => [
'name' => ts('View'),
- 'url' => 'civicrm/contact/view/grant',
- 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&context=%%cxt%%&selectedChild=grant' . $extraParams,
+ 'url' => 'civicrm/grant/add',
+ 'qs' => 'reset=1&id=%%id%%&cid=%%cid%%&action=view&' . $extraParams,
'title' => ts('View Grant'),
'weight' => -20,
],
CRM_Core_Action::UPDATE => [
'name' => ts('Edit'),
- 'url' => 'civicrm/contact/view/grant',
- 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%&context=%%cxt%%' . $extraParams,
+ 'url' => 'civicrm/grant/add',
+ 'qs' => 'reset=1&action=update&id=%%id%%&cid=%%cid%%' . $extraParams,
'title' => ts('Edit Grant'),
'weight' => -10,
],
@@ -189,8 +189,8 @@ class CRM_Grant_Selector_Search extends CRM_Core_Selector_Base implements CRM_Co
$delLink = [
CRM_Core_Action::DELETE => [
'name' => ts('Delete'),
- 'url' => 'civicrm/contact/view/grant',
- 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%&selectedChild=grant' . $extraParams,
+ 'url' => 'civicrm/grant/add',
+ 'qs' => 'action=delete&reset=1&cid=%%cid%%&id=%%id%%' . $extraParams,
'title' => ts('Delete Grant'),
'weight' => 100,
],
diff --git a/ext/civigrant/civigrant.php b/ext/civigrant/civigrant.php
index d0e24ed5ac..1f491b57d9 100644
--- a/ext/civigrant/civigrant.php
+++ b/ext/civigrant/civigrant.php
@@ -23,7 +23,7 @@ function civigrant_civicrm_links($context, $name, $id, &$links) {
'ref' => 'new-grant',
'name' => 'Grant',
'title' => ts('Grant'),
- 'url' => CRM_Utils_System::url('civicrm/grant/add', 'reset=1&action=add&context=standalone'),
+ 'url' => CRM_Utils_System::url('civicrm/grant/add', 'reset=1&action=add'),
];
}
}
@@ -40,8 +40,8 @@ function civigrant_civicrm_summaryActions(&$menu, $cid) {
'ref' => 'new-grant',
'key' => 'grant',
'tab' => 'grant',
- 'href' => CRM_Utils_System::url('civicrm/contact/view/grant',
- 'reset=1&action=add&context=grant'
+ 'href' => CRM_Utils_System::url('civicrm/grant/add',
+ 'reset=1&action=add'
),
'permissions' => ['edit grants'],
];
diff --git a/ext/civigrant/managed/Navigation_Grants.mgd.php b/ext/civigrant/managed/Navigation_Grants.mgd.php
index d69029d4e5..0356e3c395 100644
--- a/ext/civigrant/managed/Navigation_Grants.mgd.php
+++ b/ext/civigrant/managed/Navigation_Grants.mgd.php
@@ -55,7 +55,7 @@ return [
'values' => [
'label' => E::ts('New Grant'),
'name' => 'New Grant',
- 'url' => 'civicrm/grant/add?reset=1&action=add&context=standalone',
+ 'url' => 'civicrm/grant/add?reset=1&action=add',
'icon' => NULL,
'permission' => 'access CiviGrant,edit grants',
'permission_operator' => 'AND',
diff --git a/ext/civigrant/templates/CRM/Grant/Form/Grant.tpl b/ext/civigrant/templates/CRM/Grant/Form/Grant.tpl
index 22d7865e5c..b717a66c1f 100644
--- a/ext/civigrant/templates/CRM/Grant/Form/Grant.tpl
+++ b/ext/civigrant/templates/CRM/Grant/Form/Grant.tpl
@@ -16,6 +16,8 @@
{ts}Are you sure you want to delete this Grant?{/ts} {ts}This action cannot be undone.{/ts}
{include file="CRM/Grant/Form/Task.tpl"}
+ {elseif $action eq 4 }
+ {include file="CRM/Grant/Form/GrantView.tpl"}
{else}
{include file="CRM/common/formButtons.tpl" location="top"}