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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 class CRM_Grant_BAO_Grant
extends CRM_Grant_DAO_Grant
{
24 public function __construct() {
25 parent
::__construct();
35 * Array of event summary values
37 public static function getGrantSummary($admin = FALSE) {
39 SELECT status_id, count(id) as status_total
40 FROM civicrm_grant GROUP BY status_id";
42 $dao = CRM_Core_DAO
::executeQuery($query);
46 $summary['total_grants'] = NULL;
47 $status = CRM_Core_PseudoConstant
::get('CRM_Grant_DAO_Grant', 'status_id');
49 foreach ($status as $id => $name) {
56 while ($dao->fetch()) {
57 $stats[$dao->status_id
] = [
58 'label' => $status[$dao->status_id
],
59 'total' => $dao->status_total
,
61 $summary['total_grants'] +
= $dao->status_total
;
64 $summary['per_status'] = $stats;
73 * Array of event summary values
75 * @throws CRM_Core_Exception
77 public static function getGrantStatusOptGroup() {
80 $params['name'] = CRM_Grant_BAO_Grant
::$statusGroupName;
84 $og = CRM_Core_BAO_OptionGroup
::retrieve($params, $defaults);
86 throw new CRM_Core_Exception('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql');
93 * Fetch object based on array of properties.
95 * @param array $params
96 * (reference ) an assoc array of name/value pairs.
97 * @param array $defaults
98 * (reference ) an assoc array to hold the flattened values.
100 * @return CRM_Grant_BAO_ManageGrant
102 public static function retrieve(&$params, &$defaults) {
103 $grant = new CRM_Grant_DAO_Grant();
104 $grant->copyValues($params);
105 if ($grant->find(TRUE)) {
106 CRM_Core_DAO
::storeValues($grant, $defaults);
115 * @param array $params
120 public static function add($params, $ids = []) {
121 $id = $ids['grant_id'] ??
$params['id'] ??
NULL;
122 $hook = $id ?
'edit' : 'create';
123 CRM_Utils_Hook
::pre($hook, 'Grant', $id, $params);
125 // first clean up all the money fields
131 foreach ($moneyFields as $field) {
132 if (isset($params[$field])) {
133 $params[$field] = CRM_Utils_Rule
::cleanMoney($params[$field]);
136 // convert dates to mysql format
138 'application_received_date',
140 'money_transfer_date',
144 foreach ($dates as $d) {
145 if (isset($params[$d])) {
146 $params[$d] = CRM_Utils_Date
::processDate($params[$d], NULL, TRUE);
149 $grant = new CRM_Grant_DAO_Grant();
152 $grant->copyValues($params);
154 // set currency for CRM-1496
155 if (!isset($grant->currency
)) {
156 $config = CRM_Core_Config
::singleton();
157 $grant->currency
= $config->defaultCurrency
;
160 $result = $grant->save();
162 $url = CRM_Utils_System
::url('civicrm/contact/view/grant',
163 "action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
166 $grantTypes = CRM_Core_PseudoConstant
::get('CRM_Grant_DAO_Grant', 'grant_type_id');
167 if (empty($params['skipRecentView'])) {
168 if (!isset($grant->contact_id
) ||
!isset($grant->grant_type_id
)) {
171 $title = CRM_Contact_BAO_Contact
::displayName($grant->contact_id
) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id
];
174 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::UPDATE
)) {
175 $recentOther['editUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
176 "action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
179 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::DELETE
)) {
180 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
181 "action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
185 // add the recently created Grant
186 CRM_Utils_Recent
::add($title,
196 CRM_Utils_Hook
::post($hook, 'Grant', $grant->id
, $grant);
204 * @param array $params
209 public static function create($params, $ids = []) {
210 $transaction = new CRM_Core_Transaction();
212 $grant = self
::add($params, $ids);
214 if (is_a($grant, 'CRM_Core_Error')) {
215 $transaction->rollback();
219 $session = CRM_Core_Session
::singleton();
220 $id = $session->get('userID');
222 $id = $params['contact_id'] ??
NULL;
224 if (!empty($params['note']) || CRM_Utils_Array
::value('id', CRM_Utils_Array
::value('note', $ids))) {
226 'entity_table' => 'civicrm_grant',
227 'note' => $params['note'] = $params['note'] ?
$params['note'] : "null",
228 'entity_id' => $grant->id
,
232 CRM_Core_BAO_Note
::add($noteParams, (array) CRM_Utils_Array
::value('note', $ids));
234 // Log the information on successful add/edit of Grant
236 'entity_table' => 'civicrm_grant',
237 'entity_id' => $grant->id
,
238 'modified_id' => $id,
239 'modified_date' => date('Ymd'),
242 CRM_Core_BAO_Log
::add($logParams);
244 // add custom field values
245 if (!empty($params['custom']) && is_array($params['custom'])) {
246 CRM_Core_BAO_CustomValueTable
::store($params['custom'], 'civicrm_grant', $grant->id
);
249 // check and attach and files as needed
250 CRM_Core_BAO_File
::processAttachment($params,
255 $transaction->commit();
261 * Delete the Contact.
269 public static function deleteContact($id) {
270 $grant = new CRM_Grant_DAO_Grant();
271 $grant->contact_id
= $id;
284 public static function del($id) {
285 CRM_Utils_Hook
::pre('delete', 'Grant', $id, CRM_Core_DAO
::$_nullArray);
287 $grant = new CRM_Grant_DAO_Grant();
292 // delete the recently created Grant
297 CRM_Utils_Recent
::del($grantRecent);
299 if ($grant->fetch()) {
300 $results = $grant->delete();
301 CRM_Utils_Hook
::post('delete', 'Grant', $grant->id
, $grant);
308 * Combine all the exportable fields from the lower levels object.
311 * array of exportable Fields
313 public static function &exportableFields() {
314 $fields = CRM_Grant_DAO_Grant
::export();
317 'title' => ts('Grant Note'),
318 'name' => 'grant_note',
319 'data_type' => CRM_Utils_Type
::T_TEXT
,
322 $fields = array_merge($fields, $grantNote,
323 CRM_Core_BAO_CustomField
::getFieldsForImport('Grant')
330 * Get grant record count for a Contact.
332 * @param int $contactID
335 * count of grant records
337 public static function getContactGrantCount($contactID) {
338 $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
339 return CRM_Core_DAO
::singleValueQuery($query);