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
{
20 public function __construct() {
21 parent
::__construct();
31 * Array of event summary values
33 public static function getGrantSummary($admin = FALSE) {
35 SELECT status_id, count(id) as status_total
36 FROM civicrm_grant GROUP BY status_id";
38 $dao = CRM_Core_DAO
::executeQuery($query);
42 $summary['total_grants'] = NULL;
43 $status = CRM_Core_PseudoConstant
::get('CRM_Grant_DAO_Grant', 'status_id');
45 foreach ($status as $id => $name) {
52 while ($dao->fetch()) {
53 $stats[$dao->status_id
] = [
54 'label' => $status[$dao->status_id
],
55 'total' => $dao->status_total
,
57 $summary['total_grants'] +
= $dao->status_total
;
60 $summary['per_status'] = $stats;
69 * Array of event summary values
71 * @throws CRM_Core_Exception
73 public static function getGrantStatusOptGroup() {
76 $params['name'] = CRM_Grant_BAO_Grant
::$statusGroupName;
80 $og = CRM_Core_BAO_OptionGroup
::retrieve($params, $defaults);
82 throw new CRM_Core_Exception('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql');
89 * Fetch object based on array of properties.
91 * @param array $params
92 * (reference ) an assoc array of name/value pairs.
93 * @param array $defaults
94 * (reference ) an assoc array to hold the flattened values.
96 * @return CRM_Grant_BAO_ManageGrant
98 public static function retrieve(&$params, &$defaults) {
99 $grant = new CRM_Grant_DAO_Grant();
100 $grant->copyValues($params);
101 if ($grant->find(TRUE)) {
102 CRM_Core_DAO
::storeValues($grant, $defaults);
111 * @param array $params
116 public static function add($params, $ids = []) {
117 $id = $ids['grant_id'] ??
$params['id'] ??
NULL;
118 $hook = $id ?
'edit' : 'create';
119 CRM_Utils_Hook
::pre($hook, 'Grant', $id, $params);
121 // first clean up all the money fields
127 foreach ($moneyFields as $field) {
128 if (isset($params[$field])) {
129 $params[$field] = CRM_Utils_Rule
::cleanMoney($params[$field]);
132 // convert dates to mysql format
134 'application_received_date',
136 'money_transfer_date',
140 foreach ($dates as $d) {
141 if (isset($params[$d])) {
142 $params[$d] = CRM_Utils_Date
::processDate($params[$d], NULL, TRUE);
145 $grant = new CRM_Grant_DAO_Grant();
148 $grant->copyValues($params);
150 // set currency for CRM-1496
151 if (!isset($grant->currency
)) {
152 $config = CRM_Core_Config
::singleton();
153 $grant->currency
= $config->defaultCurrency
;
156 $result = $grant->save();
158 $url = CRM_Utils_System
::url('civicrm/contact/view/grant',
159 "action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
162 $grantTypes = CRM_Core_PseudoConstant
::get('CRM_Grant_DAO_Grant', 'grant_type_id');
163 if (empty($params['skipRecentView'])) {
164 if (!isset($grant->contact_id
) ||
!isset($grant->grant_type_id
)) {
167 $title = CRM_Contact_BAO_Contact
::displayName($grant->contact_id
) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id
];
170 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::UPDATE
)) {
171 $recentOther['editUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
172 "action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
175 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::DELETE
)) {
176 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
177 "action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
181 // add the recently created Grant
182 CRM_Utils_Recent
::add($title,
192 CRM_Utils_Hook
::post($hook, 'Grant', $grant->id
, $grant);
200 * @param array $params
205 public static function create($params, $ids = []) {
206 $transaction = new CRM_Core_Transaction();
208 $grant = self
::add($params, $ids);
210 if (is_a($grant, 'CRM_Core_Error')) {
211 $transaction->rollback();
215 $session = CRM_Core_Session
::singleton();
216 $id = $session->get('userID');
218 $id = $params['contact_id'] ??
NULL;
220 if (!empty($params['note']) || CRM_Utils_Array
::value('id', CRM_Utils_Array
::value('note', $ids))) {
222 'entity_table' => 'civicrm_grant',
223 'note' => $params['note'] = $params['note'] ?
$params['note'] : "null",
224 'entity_id' => $grant->id
,
228 CRM_Core_BAO_Note
::add($noteParams, (array) CRM_Utils_Array
::value('note', $ids));
230 // Log the information on successful add/edit of Grant
232 'entity_table' => 'civicrm_grant',
233 'entity_id' => $grant->id
,
234 'modified_id' => $id,
235 'modified_date' => date('Ymd'),
238 CRM_Core_BAO_Log
::add($logParams);
240 // add custom field values
241 if (!empty($params['custom']) && is_array($params['custom'])) {
242 CRM_Core_BAO_CustomValueTable
::store($params['custom'], 'civicrm_grant', $grant->id
);
245 // check and attach and files as needed
246 CRM_Core_BAO_File
::processAttachment($params,
251 $transaction->commit();
257 * Delete the Contact.
265 public static function deleteContact($id) {
266 $grant = new CRM_Grant_DAO_Grant();
267 $grant->contact_id
= $id;
280 public static function del($id) {
281 CRM_Utils_Hook
::pre('delete', 'Grant', $id, CRM_Core_DAO
::$_nullArray);
283 $grant = new CRM_Grant_DAO_Grant();
288 // delete the recently created Grant
293 CRM_Utils_Recent
::del($grantRecent);
295 if ($grant->fetch()) {
296 $results = $grant->delete();
297 CRM_Utils_Hook
::post('delete', 'Grant', $grant->id
, $grant);
304 * Combine all the exportable fields from the lower levels object.
307 * array of exportable Fields
309 public static function &exportableFields() {
310 $fields = CRM_Grant_DAO_Grant
::export();
313 'title' => ts('Grant Note'),
314 'name' => 'grant_note',
315 'data_type' => CRM_Utils_Type
::T_TEXT
,
318 $fields = array_merge($fields, $grantNote,
319 CRM_Core_BAO_CustomField
::getFieldsForImport('Grant')
326 * Get grant record count for a Contact.
328 * @param int $contactID
331 * count of grant records
333 public static function getContactGrantCount($contactID) {
334 $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
335 return CRM_Core_DAO
::singleValueQuery($query);