3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Class CRM_Grant_BAO_Grant
15 class CRM_Grant_BAO_Grant
extends CRM_Grant_DAO_Grant
{
24 * Array of event summary values
26 public static function getGrantSummary($admin = FALSE) {
28 SELECT status_id, count(g.id) as status_total
30 JOIN civicrm_contact c
31 ON g.contact_id = c.id
32 WHERE c.is_deleted = 0
36 $dao = CRM_Core_DAO
::executeQuery($query);
40 $summary['total_grants'] = NULL;
41 $status = CRM_Core_PseudoConstant
::get('CRM_Grant_DAO_Grant', 'status_id');
43 foreach ($status as $id => $name) {
50 while ($dao->fetch()) {
51 $stats[$dao->status_id
] = [
52 'label' => $status[$dao->status_id
],
53 'total' => $dao->status_total
,
55 $summary['total_grants'] +
= $dao->status_total
;
58 $summary['per_status'] = $stats;
67 * Array of event summary values
69 * @throws CRM_Core_Exception
71 public static function getGrantStatusOptGroup() {
74 $params['name'] = CRM_Grant_BAO_Grant
::$statusGroupName;
78 $og = CRM_Core_BAO_OptionGroup
::retrieve($params, $defaults);
80 throw new CRM_Core_Exception('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql');
87 * Fetch object based on array of properties.
89 * @param array $params
90 * (reference ) an assoc array of name/value pairs.
91 * @param array $defaults
92 * (reference ) an assoc array to hold the flattened values.
94 * @return CRM_Grant_DAO_Grant
96 public static function retrieve(&$params, &$defaults) {
97 $grant = new CRM_Grant_DAO_Grant();
98 $grant->copyValues($params);
99 if ($grant->find(TRUE)) {
100 CRM_Core_DAO
::storeValues($grant, $defaults);
109 * @param array $params
114 public static function add($params, $ids = []) {
115 $id = $ids['grant_id'] ??
$params['id'] ??
NULL;
116 $hook = $id ?
'edit' : 'create';
117 CRM_Utils_Hook
::pre($hook, 'Grant', $id, $params);
119 $grant = new CRM_Grant_DAO_Grant();
122 $grant->copyValues($params);
124 // set currency for CRM-1496
125 if (!isset($grant->currency
)) {
126 $config = CRM_Core_Config
::singleton();
127 $grant->currency
= $config->defaultCurrency
;
130 $result = $grant->save();
132 $url = CRM_Utils_System
::url('civicrm/contact/view/grant',
133 "action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
136 $grantTypes = CRM_Core_PseudoConstant
::get('CRM_Grant_DAO_Grant', 'grant_type_id');
137 if (empty($params['skipRecentView'])) {
138 if (!isset($grant->contact_id
) ||
!isset($grant->grant_type_id
)) {
141 $title = CRM_Contact_BAO_Contact
::displayName($grant->contact_id
) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id
];
144 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::UPDATE
)) {
145 $recentOther['editUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
146 "action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
149 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::DELETE
)) {
150 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
151 "action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
155 // add the recently created Grant
156 CRM_Utils_Recent
::add($title,
166 CRM_Utils_Hook
::post($hook, 'Grant', $grant->id
, $grant);
174 * @param array $params
179 public static function create($params, $ids = []) {
180 $transaction = new CRM_Core_Transaction();
182 $grant = self
::add($params, $ids);
184 if (is_a($grant, 'CRM_Core_Error')) {
185 $transaction->rollback();
189 $session = CRM_Core_Session
::singleton();
190 $id = $session->get('userID');
192 $id = $params['contact_id'] ??
NULL;
194 if (!empty($params['note']) || CRM_Utils_Array
::value('id', CRM_Utils_Array
::value('note', $ids))) {
196 'entity_table' => 'civicrm_grant',
197 'note' => $params['note'] = $params['note'] ?
$params['note'] : "null",
198 'entity_id' => $grant->id
,
202 CRM_Core_BAO_Note
::add($noteParams, (array) CRM_Utils_Array
::value('note', $ids));
204 // Log the information on successful add/edit of Grant
206 'entity_table' => 'civicrm_grant',
207 'entity_id' => $grant->id
,
208 'modified_id' => $id,
209 'modified_date' => date('Ymd'),
212 CRM_Core_BAO_Log
::add($logParams);
214 // add custom field values
215 if (!empty($params['custom']) && is_array($params['custom'])) {
216 CRM_Core_BAO_CustomValueTable
::store($params['custom'], 'civicrm_grant', $grant->id
);
219 // check and attach and files as needed
220 CRM_Core_BAO_File
::processAttachment($params,
225 $transaction->commit();
231 * Delete the Contact.
239 public static function deleteContact($id) {
240 $grant = new CRM_Grant_DAO_Grant();
241 $grant->contact_id
= $id;
254 public static function del($id) {
255 CRM_Utils_Hook
::pre('delete', 'Grant', $id);
257 $grant = new CRM_Grant_DAO_Grant();
262 if ($grant->fetch()) {
263 $results = $grant->delete();
264 CRM_Utils_Hook
::post('delete', 'Grant', $grant->id
, $grant);
271 * Combine all the exportable fields from the lower levels object.
274 * array of exportable Fields
276 public static function &exportableFields() {
277 $fields = CRM_Grant_DAO_Grant
::export();
280 'title' => ts('Grant Note'),
281 'name' => 'grant_note',
282 'data_type' => CRM_Utils_Type
::T_TEXT
,
285 $fields = array_merge($fields, $grantNote,
286 CRM_Core_BAO_CustomField
::getFieldsForImport('Grant')
293 * Get grant record count for a Contact.
295 * @param int $contactID
298 * count of grant records
300 public static function getContactGrantCount($contactID) {
301 $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
302 return CRM_Core_DAO
::singleValueQuery($query);