From bf2c97d771e765b70940eb392d514a68d2f0ff6b Mon Sep 17 00:00:00 2001 From: colemanw Date: Wed, 21 Jun 2023 16:53:31 -0700 Subject: [PATCH] CiviGrant - Switch to writeRecord/deleteRecord + BAO hooks --- ext/civigrant/CRM/Grant/BAO/Grant.php | 176 ++++++------------- ext/civigrant/CRM/Grant/Form/Grant.php | 37 ++-- ext/civigrant/CRM/Grant/Form/Task/Update.php | 5 +- 3 files changed, 86 insertions(+), 132 deletions(-) diff --git a/ext/civigrant/CRM/Grant/BAO/Grant.php b/ext/civigrant/CRM/Grant/BAO/Grant.php index 27538b0428..abb0389775 100644 --- a/ext/civigrant/CRM/Grant/BAO/Grant.php +++ b/ext/civigrant/CRM/Grant/BAO/Grant.php @@ -9,10 +9,12 @@ +--------------------------------------------------------------------+ */ +use CRM_Grant_ExtensionUtil as E; + /** * Class CRM_Grant_BAO_Grant */ -class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant { +class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant implements \Civi\Core\HookInterface { /** * Get events Summary. @@ -77,122 +79,75 @@ class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant { } /** - * Add grant. - * - * @param array $params - * @param array $ids - * - * @return object + * @deprecated */ - public static function add($params, $ids = []) { - $id = $ids['grant_id'] ?? $params['id'] ?? NULL; - $hook = $id ? 'edit' : 'create'; - CRM_Utils_Hook::pre($hook, 'Grant', $id, $params); - - $grant = new CRM_Grant_DAO_Grant(); - $grant->id = $id; - - $grant->copyValues($params); - - // set currency for CRM-1496 - if (!isset($grant->currency)) { - $config = CRM_Core_Config::singleton(); - $grant->currency = $config->defaultCurrency; - } + public static function add($params) { + CRM_Core_Error::deprecatedFunctionWarning('writeRecord'); + return self::writeRecord($params); + } - $result = $grant->save(); + /** + * @deprecated + */ + public static function create($params) { + return self::add($params); + } - if (empty($params['skipRecentView'])) { - if (!isset($grant->contact_id) || !isset($grant->grant_type_id)) { - $grant->find(TRUE); + /** + * Callback for hook_civicrm_pre(). + * @param \Civi\Core\Event\PreEvent $event + */ + public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event): void { + if ($event->action === 'create') { + // set currency for CRM-1496 + if (empty($event->params['currency'])) { + $event->params['currency'] = Civi::settings()->get('defaultCurrency'); } - $grantTypes = self::buildOptions('grant_type_id'); - $title = CRM_Contact_BAO_Contact::displayName($grant->contact_id) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id]; - - civicrm_api4('RecentItem', 'create', [ - 'checkPermissions' => FALSE, - 'values' => [ - 'entity_type' => 'Grant', - 'entity_id' => $grant->id, - 'title' => $title, - ], - ]); } - - CRM_Utils_Hook::post($hook, 'Grant', $grant->id, $grant); - - return $result; } /** - * Adds a grant. - * - * @param array $params - * @param array $ids - * - * @return object + * Callback for hook_civicrm_post(). + * @param \Civi\Core\Event\PostEvent $e */ - public static function create($params, $ids = []) { - $transaction = new CRM_Core_Transaction(); - - $grant = self::add($params, $ids); - - if (is_a($grant, 'CRM_Core_Error')) { - $transaction->rollback(); - return $grant; - } - - $session = CRM_Core_Session::singleton(); - $id = $session->get('userID'); - if (!$id) { - $id = $params['contact_id'] ?? NULL; - } - if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) { - $noteParams = [ + public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $e): void { + /** @var CRM_Grant_DAO_Grant $grant */ + $grant = $e->object; + $params = $e->params; + if (in_array($e->action, ['create', 'edit'])) { + $grant->find(TRUE); + $cid = CRM_Core_Session::getLoggedInContactID() ?: $grant->contact_id; + + // Log the information on successful add/edit of Grant + $logParams = [ 'entity_table' => 'civicrm_grant', - 'note' => $params['note'] = $params['note'] ? $params['note'] : "null", 'entity_id' => $grant->id, - 'contact_id' => $id, + 'modified_id' => $cid, + 'modified_date' => date('Ymd'), ]; - - CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids)); - } - // Log the information on successful add/edit of Grant - $logParams = [ - 'entity_table' => 'civicrm_grant', - 'entity_id' => $grant->id, - 'modified_id' => $id, - 'modified_date' => date('Ymd'), - ]; - - CRM_Core_BAO_Log::add($logParams); - - // add custom field values - if (!empty($params['custom']) && is_array($params['custom'])) { - CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_grant', $grant->id); + CRM_Core_BAO_Log::add($logParams); + + // Add to recent items list + if (empty($params['skipRecentView'])) { + $grantTypes = self::buildOptions('grant_type_id'); + $title = CRM_Contact_BAO_Contact::displayName($grant->contact_id) . ' - ' . E::ts('Grant: %1', [1 => $grantTypes[$grant->grant_type_id]]); + civicrm_api4('RecentItem', 'create', [ + 'checkPermissions' => FALSE, + 'values' => [ + 'entity_type' => 'Grant', + 'entity_id' => $grant->id, + 'title' => $title, + ], + ]); + } } - - // check and attach and files as needed - CRM_Core_BAO_File::processAttachment($params, - 'civicrm_grant', - $grant->id - ); - - $transaction->commit(); - - return $grant; } /** - * Delete the Contact. - * - * @param int $id - * Contact id. - * - * @return bool - * + * @deprecated */ public static function deleteContact($id) { + CRM_Core_Error::deprecatedFunctionWarning('deleteRecord'); $grant = new CRM_Grant_DAO_Grant(); $grant->contact_id = $id; $grant->delete(); @@ -200,26 +155,11 @@ class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant { } /** - * Delete the grant. - * - * @param int $id - * Grant id. - * - * @return bool|mixed + * @deprecated */ public static function del($id) { - CRM_Utils_Hook::pre('delete', 'Grant', $id); - - $grant = new CRM_Grant_DAO_Grant(); - $grant->id = $id; - - $grant->find(); - - if ($grant->fetch()) { - $results = $grant->delete(); - CRM_Utils_Hook::post('delete', 'Grant', $grant->id, $grant); - return $results; - } + CRM_Core_Error::deprecatedFunctionWarning('deleteRecord'); + self::deleteRecord(['id' => $id]); return FALSE; } @@ -233,7 +173,7 @@ class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant { $fields = CRM_Grant_DAO_Grant::export(); $grantNote = [ 'grant_note' => [ - 'title' => ts('Grant Note'), + 'title' => E::ts('Grant Note'), 'name' => 'grant_note', 'data_type' => CRM_Utils_Type::T_TEXT, ], diff --git a/ext/civigrant/CRM/Grant/Form/Grant.php b/ext/civigrant/CRM/Grant/Form/Grant.php index 870ddf4326..661607e60c 100644 --- a/ext/civigrant/CRM/Grant/Form/Grant.php +++ b/ext/civigrant/CRM/Grant/Form/Grant.php @@ -16,7 +16,7 @@ class CRM_Grant_Form_Grant extends CRM_Core_Form { /** - * The id of the case that we are proceessing. + * The id of the grant when ACTION is update or delete. * * @var int */ @@ -31,6 +31,8 @@ class CRM_Grant_Form_Grant extends CRM_Core_Form { protected $_context; + public $_noteId; + /** * Explicitly declare the entity api name. */ @@ -66,7 +68,6 @@ class CRM_Grant_Form_Grant extends CRM_Core_Form { return; } - $this->_noteId = NULL; if ($this->_id) { $noteDAO = new CRM_Core_BAO_Note(); $noteDAO->entity_table = 'civicrm_grant'; @@ -195,7 +196,6 @@ class CRM_Grant_Form_Grant extends CRM_Core_Form { [ 'type' => 'upload', 'name' => ts('Save and New'), - 'js' => ['onclick' => "return verify( );"], 'subName' => 'new', ], [ @@ -218,16 +218,13 @@ class CRM_Grant_Form_Grant extends CRM_Core_Form { */ public function postProcess() { if ($this->_action & CRM_Core_Action::DELETE) { - CRM_Grant_BAO_Grant::del($this->_id); + CRM_Grant_BAO_Grant::deleteRecord(['id' => $this->_id]); return; } - if ($this->_action & CRM_Core_Action::UPDATE) { - $ids['grant_id'] = $this->_id; - } - // get the submitted form values. $params = $this->controller->exportValues($this->_name); + $params['id'] = $this->_id; if (empty($params['grant_report_received'])) { $params['grant_report_received'] = "null"; @@ -239,10 +236,6 @@ class CRM_Grant_Form_Grant extends CRM_Core_Form { } $params['contact_id'] = $this->_contactID; - $ids['note'] = []; - if ($this->_noteId) { - $ids['note']['id'] = $this->_noteId; - } // build custom data array $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, @@ -267,7 +260,25 @@ class CRM_Grant_Form_Grant extends CRM_Core_Form { } } - $grant = CRM_Grant_BAO_Grant::create($params, $ids); + $grant = CRM_Grant_BAO_Grant::writeRecord($params); + + if (!empty($params['note']) || $this->_noteId) { + $noteParams = [ + 'id' => $this->_noteId, + 'entity_table' => 'civicrm_grant', + 'note' => $params['note'], + 'entity_id' => $grant->id, + 'contact_id' => $grant->contact_id, + ]; + + CRM_Core_BAO_Note::add($noteParams); + } + + // check and attach and files as needed + CRM_Core_BAO_File::processAttachment($params, + 'civicrm_grant', + $grant->id + ); $buttonName = $this->controller->getButtonName(); $session = CRM_Core_Session::singleton(); diff --git a/ext/civigrant/CRM/Grant/Form/Task/Update.php b/ext/civigrant/CRM/Grant/Form/Task/Update.php index 074ef3fee8..80514bc5b4 100644 --- a/ext/civigrant/CRM/Grant/Form/Task/Update.php +++ b/ext/civigrant/CRM/Grant/Form/Task/Update.php @@ -74,6 +74,9 @@ class CRM_Grant_Form_Task_Update extends CRM_Grant_Form_Task { unset($params[$key]); } } + $values = [ + 'skipRecentView' => TRUE, + ]; if (!empty($params)) { foreach ($params as $key => $value) { @@ -82,7 +85,7 @@ class CRM_Grant_Form_Task_Update extends CRM_Grant_Form_Task { foreach ($this->_grantIds as $grantId) { $values['id'] = $grantId; - CRM_Grant_BAO_Grant::add($values); + CRM_Grant_BAO_Grant::writeRecord($values); $updatedGrants++; } } -- 2.25.1