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
116 * Reference array contains the values submitted by the form.
118 * Reference array contains the id.
123 public static function add(&$params, &$ids) {
125 if (!empty($ids['grant_id'])) {
126 CRM_Utils_Hook
::pre('edit', 'Grant', $ids['grant_id'], $params);
129 CRM_Utils_Hook
::pre('create', 'Grant', NULL, $params);
132 // first clean up all the money fields
138 foreach ($moneyFields as $field) {
139 if (isset($params[$field])) {
140 $params[$field] = CRM_Utils_Rule
::cleanMoney($params[$field]);
143 // convert dates to mysql format
145 'application_received_date',
147 'money_transfer_date',
151 foreach ($dates as $d) {
152 if (isset($params[$d])) {
153 $params[$d] = CRM_Utils_Date
::processDate($params[$d], NULL, TRUE);
156 $grant = new CRM_Grant_DAO_Grant();
157 $grant->id
= CRM_Utils_Array
::value('grant_id', $ids);
159 $grant->copyValues($params);
161 // set currency for CRM-1496
162 if (!isset($grant->currency
)) {
163 $config = CRM_Core_Config
::singleton();
164 $grant->currency
= $config->defaultCurrency
;
167 $result = $grant->save();
169 $url = CRM_Utils_System
::url('civicrm/contact/view/grant',
170 "action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
173 $grantTypes = CRM_Core_PseudoConstant
::get('CRM_Grant_DAO_Grant', 'grant_type_id');
174 if (empty($params['skipRecentView'])) {
175 if (!isset($grant->contact_id
) ||
!isset($grant->grant_type_id
)) {
178 $title = CRM_Contact_BAO_Contact
::displayName($grant->contact_id
) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id
];
181 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::UPDATE
)) {
182 $recentOther['editUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
183 "action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
186 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::DELETE
)) {
187 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
188 "action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
192 // add the recently created Grant
193 CRM_Utils_Recent
::add($title,
203 if (!empty($ids['grant'])) {
204 CRM_Utils_Hook
::post('edit', 'Grant', $grant->id
, $grant);
207 CRM_Utils_Hook
::post('create', 'Grant', $grant->id
, $grant);
216 * @param array $params
217 * Reference array contains the values submitted by the form.
219 * Reference array contains the id.
223 public static function create(&$params, &$ids) {
224 $transaction = new CRM_Core_Transaction();
226 $grant = self
::add($params, $ids);
228 if (is_a($grant, 'CRM_Core_Error')) {
229 $transaction->rollback();
233 $session = CRM_Core_Session
::singleton();
234 $id = $session->get('userID');
236 $id = CRM_Utils_Array
::value('contact_id', $params);
238 if (!empty($params['note']) || CRM_Utils_Array
::value('id', CRM_Utils_Array
::value('note', $ids))) {
240 'entity_table' => 'civicrm_grant',
241 'note' => $params['note'] = $params['note'] ?
$params['note'] : "null",
242 'entity_id' => $grant->id
,
244 'modified_date' => date('Ymd'),
247 CRM_Core_BAO_Note
::add($noteParams, (array) CRM_Utils_Array
::value('note', $ids));
249 // Log the information on successful add/edit of Grant
251 'entity_table' => 'civicrm_grant',
252 'entity_id' => $grant->id
,
253 'modified_id' => $id,
254 'modified_date' => date('Ymd'),
257 CRM_Core_BAO_Log
::add($logParams);
259 // add custom field values
260 if (!empty($params['custom']) && is_array($params['custom'])) {
261 CRM_Core_BAO_CustomValueTable
::store($params['custom'], 'civicrm_grant', $grant->id
);
264 // check and attach and files as needed
265 CRM_Core_BAO_File
::processAttachment($params,
270 $transaction->commit();
276 * Delete the Contact.
284 public static function deleteContact($id) {
285 $grant = new CRM_Grant_DAO_Grant();
286 $grant->contact_id
= $id;
299 public static function del($id) {
300 CRM_Utils_Hook
::pre('delete', 'Grant', $id, CRM_Core_DAO
::$_nullArray);
302 $grant = new CRM_Grant_DAO_Grant();
307 // delete the recently created Grant
312 CRM_Utils_Recent
::del($grantRecent);
314 if ($grant->fetch()) {
315 $results = $grant->delete();
316 CRM_Utils_Hook
::post('delete', 'Grant', $grant->id
, $grant);
323 * Combine all the exportable fields from the lower levels object.
326 * array of exportable Fields
328 public static function &exportableFields() {
329 $fields = CRM_Grant_DAO_Grant
::export();
332 'title' => ts('Grant Note'),
333 'name' => 'grant_note',
334 'data_type' => CRM_Utils_Type
::T_TEXT
,
337 $fields = array_merge($fields, $grantNote,
338 CRM_Core_BAO_CustomField
::getFieldsForImport('Grant')
345 * Get grant record count for a Contact.
347 * @param int $contactID
350 * count of grant records
352 public static function getContactGrantCount($contactID) {
353 $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
354 return CRM_Core_DAO
::singleValueQuery($query);