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 public static function getGrantStatusOptGroup() {
78 $params['name'] = CRM_Grant_BAO_Grant
::$statusGroupName;
82 $og = CRM_Core_BAO_OptionGroup
::retrieve($params, $defaults);
84 CRM_Core_Error
::fatal('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql');
91 * Fetch object based on array of properties.
93 * @param array $params
94 * (reference ) an assoc array of name/value pairs.
95 * @param array $defaults
96 * (reference ) an assoc array to hold the flattened values.
98 * @return CRM_Grant_BAO_ManageGrant
100 public static function retrieve(&$params, &$defaults) {
101 $grant = new CRM_Grant_DAO_Grant();
102 $grant->copyValues($params);
103 if ($grant->find(TRUE)) {
104 CRM_Core_DAO
::storeValues($grant, $defaults);
113 * @param array $params
114 * Reference array contains the values submitted by the form.
116 * Reference array contains the id.
121 public static function add(&$params, &$ids) {
123 if (!empty($ids['grant_id'])) {
124 CRM_Utils_Hook
::pre('edit', 'Grant', $ids['grant_id'], $params);
127 CRM_Utils_Hook
::pre('create', 'Grant', NULL, $params);
130 // first clean up all the money fields
136 foreach ($moneyFields as $field) {
137 if (isset($params[$field])) {
138 $params[$field] = CRM_Utils_Rule
::cleanMoney($params[$field]);
141 // convert dates to mysql format
143 'application_received_date',
145 'money_transfer_date',
149 foreach ($dates as $d) {
150 if (isset($params[$d])) {
151 $params[$d] = CRM_Utils_Date
::processDate($params[$d], NULL, TRUE);
154 $grant = new CRM_Grant_DAO_Grant();
155 $grant->id
= CRM_Utils_Array
::value('grant_id', $ids);
157 $grant->copyValues($params);
159 // set currency for CRM-1496
160 if (!isset($grant->currency
)) {
161 $config = CRM_Core_Config
::singleton();
162 $grant->currency
= $config->defaultCurrency
;
165 $result = $grant->save();
167 $url = CRM_Utils_System
::url('civicrm/contact/view/grant',
168 "action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
171 $grantTypes = CRM_Core_PseudoConstant
::get('CRM_Grant_DAO_Grant', 'grant_type_id');
172 if (empty($params['skipRecentView'])) {
173 if (!isset($grant->contact_id
) ||
!isset($grant->grant_type_id
)) {
176 $title = CRM_Contact_BAO_Contact
::displayName($grant->contact_id
) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id
];
179 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::UPDATE
)) {
180 $recentOther['editUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
181 "action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
184 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::DELETE
)) {
185 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
186 "action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
190 // add the recently created Grant
191 CRM_Utils_Recent
::add($title,
201 if (!empty($ids['grant'])) {
202 CRM_Utils_Hook
::post('edit', 'Grant', $grant->id
, $grant);
205 CRM_Utils_Hook
::post('create', 'Grant', $grant->id
, $grant);
214 * @param array $params
215 * Reference array contains the values submitted by the form.
217 * Reference array contains the id.
221 public static function create(&$params, &$ids) {
222 $transaction = new CRM_Core_Transaction();
224 $grant = self
::add($params, $ids);
226 if (is_a($grant, 'CRM_Core_Error')) {
227 $transaction->rollback();
231 $session = CRM_Core_Session
::singleton();
232 $id = $session->get('userID');
234 $id = CRM_Utils_Array
::value('contact_id', $params);
236 if (!empty($params['note']) || CRM_Utils_Array
::value('id', CRM_Utils_Array
::value('note', $ids))) {
238 'entity_table' => 'civicrm_grant',
239 'note' => $params['note'] = $params['note'] ?
$params['note'] : "null",
240 'entity_id' => $grant->id
,
242 'modified_date' => date('Ymd'),
245 CRM_Core_BAO_Note
::add($noteParams, (array) CRM_Utils_Array
::value('note', $ids));
247 // Log the information on successful add/edit of Grant
249 'entity_table' => 'civicrm_grant',
250 'entity_id' => $grant->id
,
251 'modified_id' => $id,
252 'modified_date' => date('Ymd'),
255 CRM_Core_BAO_Log
::add($logParams);
257 // add custom field values
258 if (!empty($params['custom']) && is_array($params['custom'])) {
259 CRM_Core_BAO_CustomValueTable
::store($params['custom'], 'civicrm_grant', $grant->id
);
262 // check and attach and files as needed
263 CRM_Core_BAO_File
::processAttachment($params,
268 $transaction->commit();
274 * Delete the Contact.
282 public static function deleteContact($id) {
283 $grant = new CRM_Grant_DAO_Grant();
284 $grant->contact_id
= $id;
297 public static function del($id) {
298 CRM_Utils_Hook
::pre('delete', 'Grant', $id, CRM_Core_DAO
::$_nullArray);
300 $grant = new CRM_Grant_DAO_Grant();
305 // delete the recently created Grant
310 CRM_Utils_Recent
::del($grantRecent);
312 if ($grant->fetch()) {
313 $results = $grant->delete();
314 CRM_Utils_Hook
::post('delete', 'Grant', $grant->id
, $grant);
321 * Combine all the exportable fields from the lower levels object.
324 * array of exportable Fields
326 public static function &exportableFields() {
327 $fields = CRM_Grant_DAO_Grant
::export();
330 'title' => ts('Grant Note'),
331 'name' => 'grant_note',
332 'data_type' => CRM_Utils_Type
::T_TEXT
,
335 $fields = array_merge($fields, $grantNote,
336 CRM_Core_BAO_CustomField
::getFieldsForImport('Grant')
343 * Get grant record count for a Contact.
345 * @param int $contactID
348 * count of grant records
350 public static function getContactGrantCount($contactID) {
351 $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
352 return CRM_Core_DAO
::singleValueQuery($query);