3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 class CRM_Grant_BAO_Grant
extends CRM_Grant_DAO_Grant
{
38 * Static field for all the grant information that we can potentially export.
41 static $_exportableFields = NULL;
46 public function __construct() {
47 parent
::__construct();
57 * Array of event summary values
59 public static function getGrantSummary($admin = FALSE) {
61 SELECT status_id, count(id) as status_total
62 FROM civicrm_grant GROUP BY status_id";
64 $dao = CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray);
68 $summary['total_grants'] = NULL;
69 $status = CRM_Core_PseudoConstant
::get('CRM_Grant_DAO_Grant', 'status_id');
71 foreach ($status as $id => $name) {
78 while ($dao->fetch()) {
79 $stats[$dao->status_id
] = array(
80 'label' => $status[$dao->status_id
],
81 'total' => $dao->status_total
,
83 $summary['total_grants'] +
= $dao->status_total
;
86 $summary['per_status'] = $stats;
95 * Array of event summary values
97 public static function getGrantStatusOptGroup() {
100 $params['name'] = CRM_Grant_BAO_Grant
::$statusGroupName;
104 $og = CRM_Core_BAO_OptionGroup
::retrieve($params, $defaults);
106 CRM_Core_Error
::fatal('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql');
113 * Fetch object based on array of properties.
115 * @param array $params
116 * (reference ) an assoc array of name/value pairs.
117 * @param array $defaults
118 * (reference ) an assoc array to hold the flattened values.
120 * @return CRM_Grant_BAO_ManageGrant
122 public static function retrieve(&$params, &$defaults) {
123 $grant = new CRM_Grant_DAO_Grant();
124 $grant->copyValues($params);
125 if ($grant->find(TRUE)) {
126 CRM_Core_DAO
::storeValues($grant, $defaults);
135 * @param array $params
136 * Reference array contains the values submitted by the form.
138 * Reference array contains the id.
143 public static function add(&$params, &$ids) {
145 if (!empty($ids['grant_id'])) {
146 CRM_Utils_Hook
::pre('edit', 'Grant', $ids['grant_id'], $params);
149 CRM_Utils_Hook
::pre('create', 'Grant', NULL, $params);
152 // first clean up all the money fields
153 $moneyFields = array(
158 foreach ($moneyFields as $field) {
159 if (isset($params[$field])) {
160 $params[$field] = CRM_Utils_Rule
::cleanMoney($params[$field]);
163 // convert dates to mysql format
165 'application_received_date',
167 'money_transfer_date',
171 foreach ($dates as $d) {
172 if (isset($params[$d])) {
173 $params[$d] = CRM_Utils_Date
::processDate($params[$d], NULL, TRUE);
176 $grant = new CRM_Grant_DAO_Grant();
177 $grant->id
= CRM_Utils_Array
::value('grant_id', $ids);
179 $grant->copyValues($params);
181 // set currency for CRM-1496
182 if (!isset($grant->currency
)) {
183 $config = CRM_Core_Config
::singleton();
184 $grant->currency
= $config->defaultCurrency
;
187 $result = $grant->save();
189 $url = CRM_Utils_System
::url('civicrm/contact/view/grant',
190 "action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
193 $grantTypes = CRM_Core_PseudoConstant
::get('CRM_Grant_DAO_Grant', 'grant_type_id');
194 if (empty($params['skipRecentView'])) {
195 if (!isset($grant->contact_id
) ||
!isset($grant->grant_type_id
)) {
198 $title = CRM_Contact_BAO_Contact
::displayName($grant->contact_id
) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id
];
200 $recentOther = array();
201 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::UPDATE
)) {
202 $recentOther['editUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
203 "action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
206 if (CRM_Core_Permission
::checkActionPermission('CiviGrant', CRM_Core_Action
::DELETE
)) {
207 $recentOther['deleteUrl'] = CRM_Utils_System
::url('civicrm/contact/view/grant',
208 "action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
212 // add the recently created Grant
213 CRM_Utils_Recent
::add($title,
223 if (!empty($ids['grant'])) {
224 CRM_Utils_Hook
::post('edit', 'Grant', $grant->id
, $grant);
227 CRM_Utils_Hook
::post('create', 'Grant', $grant->id
, $grant);
236 * @param array $params
237 * Reference array contains the values submitted by the form.
239 * Reference array contains the id.
243 public static function create(&$params, &$ids) {
244 $transaction = new CRM_Core_Transaction();
246 $grant = self
::add($params, $ids);
248 if (is_a($grant, 'CRM_Core_Error')) {
249 $transaction->rollback();
253 $session = CRM_Core_Session
::singleton();
254 $id = $session->get('userID');
256 $id = CRM_Utils_Array
::value('contact_id', $params);
258 if (!empty($params['note']) || CRM_Utils_Array
::value('id', CRM_Utils_Array
::value('note', $ids))) {
260 'entity_table' => 'civicrm_grant',
261 'note' => $params['note'] = $params['note'] ?
$params['note'] : "null",
262 'entity_id' => $grant->id
,
264 'modified_date' => date('Ymd'),
267 CRM_Core_BAO_Note
::add($noteParams, (array) CRM_Utils_Array
::value('note', $ids));
269 // Log the information on successful add/edit of Grant
271 'entity_table' => 'civicrm_grant',
272 'entity_id' => $grant->id
,
273 'modified_id' => $id,
274 'modified_date' => date('Ymd'),
277 CRM_Core_BAO_Log
::add($logParams);
279 // add custom field values
280 if (!empty($params['custom']) && is_array($params['custom'])) {
281 CRM_Core_BAO_CustomValueTable
::store($params['custom'], 'civicrm_grant', $grant->id
);
284 // check and attach and files as needed
285 CRM_Core_BAO_File
::processAttachment($params,
290 $transaction->commit();
296 * Delete the Contact.
304 public static function deleteContact($id) {
305 $grant = new CRM_Grant_DAO_Grant();
306 $grant->contact_id
= $id;
319 public static function del($id) {
320 CRM_Utils_Hook
::pre('delete', 'Grant', $id, CRM_Core_DAO
::$_nullArray);
322 $grant = new CRM_Grant_DAO_Grant();
327 // delete the recently created Grant
328 $grantRecent = array(
332 CRM_Utils_Recent
::del($grantRecent);
334 if ($grant->fetch()) {
335 $results = $grant->delete();
336 CRM_Utils_Hook
::post('delete', 'Grant', $grant->id
, $grant);
343 * Combine all the exportable fields from the lower levels object.
346 * array of exportable Fields
348 public static function &exportableFields() {
349 if (!self
::$_exportableFields) {
350 if (!self
::$_exportableFields) {
351 self
::$_exportableFields = array();
354 $grantFields = array(
355 'grant_status' => array(
356 'title' => 'Grant Status',
357 'name' => 'grant_status',
358 'data_type' => CRM_Utils_Type
::T_STRING
,
360 'grant_type' => array(
361 'title' => 'Grant Type',
362 'name' => 'grant_type',
363 'data_type' => CRM_Utils_Type
::T_STRING
,
365 'grant_money_transfer_date' => array(
366 'title' => 'Grant Money Transfer Date',
367 'name' => 'grant_money_transfer_date',
368 'data_type' => CRM_Utils_Type
::T_DATE
,
370 'grant_amount_requested' => array(
371 'title' => 'Grant Amount Requested',
372 'name' => 'grant_amount_requested',
373 'data_type' => CRM_Utils_Type
::T_FLOAT
,
375 'grant_application_received_date' => array(
376 'title' => 'Grant Application Recieved Date',
377 'name' => 'grant_application_received_date',
378 'data_type' => CRM_Utils_Type
::T_DATE
,
382 $fields = CRM_Grant_DAO_Grant
::export();
384 'grant_note' => array(
385 'title' => ts('Grant Note'),
386 'name' => 'grant_note',
387 'data_type' => CRM_Utils_Type
::T_TEXT
,
390 $fields = array_merge($fields, $grantFields, $grantNote,
391 CRM_Core_BAO_CustomField
::getFieldsForImport('Grant')
393 self
::$_exportableFields = $fields;
396 return self
::$_exportableFields;
400 * Get grant record count for a Contact.
402 * @param int $contactID
405 * count of grant records
407 public static function getContactGrantCount($contactID) {
408 $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
409 return CRM_Core_DAO
::singleValueQuery($query);