-
-
Notifications
You must be signed in to change notification settings - Fork 827
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce use of undeclared properties, use trait to track entities
A lot of the undeclared properties on this form & similar are just tracking values of various relevant entities. This adds some functionality for that tracking onto a trait & uses that. This reduces the need to pass around values whose only importance is that they have been loaded from the database & would require another db call to access them from elsewhere. Note I have swapped out the interaction with _contributorEmail & _contributorDisplayName in getStatusMessage and send email receipts - these are the last 2 functions called in the submit routine & the only reason the receipt function needs to set it is to pass them to getStatusMessage. There are a couple of earlier interactions which are odd so I ignored them for now. The loop for sending out receipts is pretty messed up - so the first step is getting the inputs & outputs for that loop clarified - but this property is bang in the middle
- Loading branch information
1 parent
0aa8f20
commit 89887f6
Showing
2 changed files
with
129 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
/* | ||
+--------------------------------------------------------------------+ | ||
| Copyright CiviCRM LLC. All rights reserved. | | ||
| | | ||
| This work is published under the GNU AGPLv3 license with some | | ||
| permitted exceptions and without any warranty. For full license | | ||
| and copyright information, see https://civicrm.org/licensing | | ||
+--------------------------------------------------------------------+ | ||
*/ | ||
|
||
/** | ||
* This trait provides a form of lazy loading for forms. | ||
* | ||
* It is intended to reduce the need for forms to pass values around while avoiding | ||
* constantly reloading them from the database. | ||
* | ||
* @package CRM | ||
* @copyright CiviCRM LLC https://civicrm.org/licensing | ||
*/ | ||
trait CRM_Core_Form_EntityTrackingTrait { | ||
|
||
/** | ||
* Array of loaded entities. | ||
* | ||
* These are stored with keys that are consistent with apiv4 style parameters. | ||
* | ||
* @var array | ||
*/ | ||
private $entities = []; | ||
|
||
public function setEntity($entity, $identifier, $values): void { | ||
$this->entities[$entity][$identifier] = $values; | ||
} | ||
|
||
/** | ||
* Get the value for a property of an entity, loading from the database if needed. | ||
* | ||
* Permissions are not applied to the api call. | ||
* | ||
* @param string $entity | ||
* @param int $id | ||
* @param string $key | ||
* | ||
* @api supported for use outside of core. Will not change in a point release. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getEntityValue(string $entity, int $id, string $key) { | ||
if (!isset($this->entities[$entity][$id]) || !array_key_exists($key, $this->entities[$entity][$id])) { | ||
$this->loadEntity($entity, $id); | ||
} | ||
return $this->entities[$entity][$id][$key]; | ||
} | ||
|
||
/** | ||
* Get a value from a participant record. | ||
* | ||
* This function requires that the form implements `getParticipantID()`. | ||
* | ||
* @param string $fieldName | ||
* @param int|null $id If not provided getParticipantID() is called. | ||
* | ||
* @api supported for use outside of core. Will not change in a point release. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getParticipantValue(string $fieldName, ?int $id = NULL) { | ||
$id = $id ?? $this->getParticipantID(); | ||
return $this->getEntityValue('Participant', $id, $fieldName); | ||
} | ||
|
||
/** | ||
* Get a value from a contact record. | ||
* | ||
* This function requires that the form implements `getContactID()`. | ||
* | ||
* @param string $fieldName | ||
* @param int|null $id If not provided getContactID() is called. | ||
* | ||
* @api supported for use outside of core. Will not change in a point release. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getContactValue(string $fieldName, ?int $id = NULL) { | ||
$id = $id ?? $this->getContactID(); | ||
return $this->getEntityValue('Contact', $id, $fieldName); | ||
} | ||
|
||
/** | ||
* Load the requested entity. | ||
* | ||
* If we are going to load an entity we generally load all the values for it. | ||
* | ||
* @param string $entity | ||
* @param int $id | ||
* | ||
* @noinspection PhpUnhandledExceptionInspection | ||
* @noinspection PhpDocMissingThrowsInspection | ||
*/ | ||
protected function loadEntity(string $entity, int $id): void { | ||
if ($entity === 'Contact') { | ||
// If we are loading a contact we generally also want their email. | ||
$select = ['email_primary.email', 'email_primary.on_hold', '*', 'custom.*']; | ||
} | ||
else { | ||
$select = ['*', 'custom.*']; | ||
} | ||
$this->entities[$entity][$id] = civicrm_api4($entity, 'get', [ | ||
'where' => [['id', '=', $id]], | ||
'checkPermissions' => FALSE, | ||
// @todo - load pseudoconstants too... | ||
'select' => $select, | ||
])->first(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters