3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 class CRM_Grant_BAO_Grant
extends CRM_Grant_DAO_Grant
{
40 public function __construct() {
41 parent
::__construct();
51 * Array of event summary values
53 public static function getGrantSummary($admin = FALSE) {
55 SELECT status_id, count(id) as status_total
56 FROM civicrm_grant GROUP BY status_id";
58 $dao = CRM_Core_DAO
::executeQuery($query);
62 $summary['total_grants'] = NULL;
63 $status = CRM_Core_PseudoConstant
::get('CRM_Grant_DAO_Grant', 'status_id');
65 foreach ($status as $id => $name) {
72 while ($dao->fetch()) {
73 $stats[$dao->status_id
] = [
74 'label' => $status[$dao->status_id
],
75 'total' => $dao->status_total
,
77 $summary['total_grants'] +
= $dao->status_total
;
80 $summary['per_status'] = $stats;
89 * Array of event summary values
91 public static function getGrantStatusOptGroup() {
94 $params['name'] = CRM_Grant_BAO_Grant
::$statusGroupName;
98 $og = CRM_Core_BAO_OptionGroup
::retrieve($params, $defaults);
100 CRM_Core_Error
::fatal('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql');
107 * Fetch object based on array of properties.
109 * @param array $params
110 * (reference ) an assoc array of name/value pairs.
111 * @param array $defaults
112 * (reference ) an assoc array to hold the flattened values.
114 * @return CRM_Grant_BAO_ManageGrant
116 public static function retrieve(&$params, &$defaults) {
117 $grant = new CRM_Grant_DAO_Grant();
118 $grant->copyValues($params);
119 if ($grant->find(TRUE)) {
120 CRM_Core_DAO
::storeValues($grant, $defaults);
129 * @param array $params
130 * Reference array contains the values submitted by the form.
132 * Reference array contains the id.
137 public static function add(&$params, &$ids) {
139 if (!empty($ids['grant_id'])) {
140 CRM_Utils_Hook
::pre('edit', 'Grant', $ids['grant_id'], $params);
143 CRM_Utils_Hook
::pre('create', 'Grant', NULL, $params);
146 // first clean up all the money fields
152 foreach ($moneyFields as $field) {
153 if (isset($params[$field])) {
154 $params[$field] = CRM_Utils_Rule
::cleanMoney($params[$field]);
157 // convert dates to mysql format
159 'application_received_date',
161 'money_transfer_date',
165 foreach ($dates as $d) {
166 if (isset($params[$d])) {
167 $params[$d] = CRM_Utils_Date
::processDate($params[$d], NULL, TRUE);
170 $grant = new CRM_Grant_DAO_Grant();
171 $grant->id
= CRM_Utils_Array
::value('grant_id', $ids);
173 $grant->copyValues($params);
175 // set currency for CRM-1496
176 if (!isset($grant->currency
)) {
177 $config = CRM_Core_Config
::singleton();
178 $grant->currency
= $config->defaultCurrency
;
181 $result = $grant->save();
183 $url = CRM_Utils_System
::url('civicrm/contact/view/grant',
184 "action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
187 $grantTypes = CRM_Core_PseudoConstant
::get('CRM_Grant_DAO_Grant', 'grant_type_id');
188 if (empty($params['skipRecentView'])) {
189 if (!isset($grant->contact_id
) ||
!isset($grant->grant_type_id
)) {
192 $title = CRM_Contact_BAO_Contact
::displayName($grant->contact_id
) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id
];
195 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::UPDATE
)) {
196 $recentOther['editUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
197 "action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
200 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::DELETE
)) {
201 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
202 "action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
206 // add the recently created Grant
207 CRM_Utils_Recent
::add($title,
217 if (!empty($ids['grant'])) {
218 CRM_Utils_Hook
::post('edit', 'Grant', $grant->id
, $grant);
221 CRM_Utils_Hook
::post('create', 'Grant', $grant->id
, $grant);
230 * @param array $params
231 * Reference array contains the values submitted by the form.
233 * Reference array contains the id.
237 public static function create(&$params, &$ids) {
238 $transaction = new CRM_Core_Transaction();
240 $grant = self
::add($params, $ids);
242 if (is_a($grant, 'CRM_Core_Error')) {
243 $transaction->rollback();
247 $session = CRM_Core_Session
::singleton();
248 $id = $session->get('userID');
250 $id = CRM_Utils_Array
::value('contact_id', $params);
252 if (!empty($params['note']) || CRM_Utils_Array
::value('id', CRM_Utils_Array
::value('note', $ids))) {
254 'entity_table' => 'civicrm_grant',
255 'note' => $params['note'] = $params['note'] ?
$params['note'] : "null",
256 'entity_id' => $grant->id
,
258 'modified_date' => date('Ymd'),
261 CRM_Core_BAO_Note
::add($noteParams, (array) CRM_Utils_Array
::value('note', $ids));
263 // Log the information on successful add/edit of Grant
265 'entity_table' => 'civicrm_grant',
266 'entity_id' => $grant->id
,
267 'modified_id' => $id,
268 'modified_date' => date('Ymd'),
271 CRM_Core_BAO_Log
::add($logParams);
273 // add custom field values
274 if (!empty($params['custom']) && is_array($params['custom'])) {
275 CRM_Core_BAO_CustomValueTable
::store($params['custom'], 'civicrm_grant', $grant->id
);
278 // check and attach and files as needed
279 CRM_Core_BAO_File
::processAttachment($params,
284 $transaction->commit();
290 * Delete the Contact.
298 public static function deleteContact($id) {
299 $grant = new CRM_Grant_DAO_Grant();
300 $grant->contact_id
= $id;
313 public static function del($id) {
314 CRM_Utils_Hook
::pre('delete', 'Grant', $id, CRM_Core_DAO
::$_nullArray);
316 $grant = new CRM_Grant_DAO_Grant();
321 // delete the recently created Grant
326 CRM_Utils_Recent
::del($grantRecent);
328 if ($grant->fetch()) {
329 $results = $grant->delete();
330 CRM_Utils_Hook
::post('delete', 'Grant', $grant->id
, $grant);
337 * Combine all the exportable fields from the lower levels object.
340 * array of exportable Fields
342 public static function &exportableFields() {
343 $fields = CRM_Grant_DAO_Grant
::export();
346 'title' => ts('Grant Note'),
347 'name' => 'grant_note',
348 'data_type' => CRM_Utils_Type
::T_TEXT
,
351 $fields = array_merge($fields, $grantNote,
352 CRM_Core_BAO_CustomField
::getFieldsForImport('Grant')
359 * Get grant record count for a Contact.
361 * @param int $contactID
364 * count of grant records
366 public static function getContactGrantCount($contactID) {
367 $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
368 return CRM_Core_DAO
::singleValueQuery($query);