INFRA-132 - Fix spacing of @return tag in comments
[civicrm-core.git] / CRM / Grant / BAO / Grant.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant {
36
37 /**
100fef9d 38 * Static field for all the grant information that we can potentially export
6a488035
TO
39 * @var array
40 * @static
41 */
42 static $_exportableFields = NULL;
43
44 /**
100fef9d 45 * Class constructor
6a488035 46 */
00be9182 47 public function __construct() {
6a488035
TO
48 parent::__construct();
49 }
50
51 /**
100fef9d 52 * Get events Summary
6a488035
TO
53 *
54 * @static
55 *
77b97be7
EM
56 * @param bool $admin
57 *
a6c01b45
CW
58 * @return array
59 * Array of event summary values
6a488035 60 */
00be9182 61 public static function getGrantSummary($admin = FALSE) {
6a488035
TO
62 $query = "
63 SELECT status_id, count(id) as status_total
64 FROM civicrm_grant GROUP BY status_id";
65
66 $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
67
68 $status = array();
69 $summary = array();
70 $summary['total_grants'] = NULL;
fb1fd730 71 $status = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id');
6a488035
TO
72
73 foreach ($status as $id => $name) {
74 $stats[$id] = array(
75 'label' => $name,
76 'total' => 0,
77 );
78 }
79
80 while ($dao->fetch()) {
81 $stats[$dao->status_id] = array(
82 'label' => $status[$dao->status_id],
83 'total' => $dao->status_total,
84 );
85 $summary['total_grants'] += $dao->status_total;
86 }
87
88 $summary['per_status'] = $stats;
89 return $summary;
90 }
91
92 /**
100fef9d 93 * Get events Summary
6a488035
TO
94 *
95 * @static
96 *
a6c01b45
CW
97 * @return array
98 * Array of event summary values
6a488035 99 */
00be9182 100 public static function getGrantStatusOptGroup() {
6a488035
TO
101
102 $params = array();
103 $params['name'] = CRM_Grant_BAO_Grant::$statusGroupName;
104
105 $defaults = array();
106
107 $og = CRM_Core_BAO_OptionGroup::retrieve($params, $defaults);
108 if (!$og) {
109 CRM_Core_Error::fatal('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql');
110 }
111
112 return $og;
113 }
114
115 /**
100fef9d 116 * Retrieve statistics for grants.
6a488035
TO
117 *
118 * @static
119 *
fd31fa4c
EM
120 * @param bool $admin
121 *
a6c01b45
CW
122 * @return array
123 * Array of grant summary statistics
6a488035 124 */
00be9182 125 public static function getGrantStatistics($admin = FALSE) {
6a488035
TO
126 $grantStatuses = array();
127 }
128
129 /**
c490a46a 130 * Fetch object based on array of properties
6a488035 131 *
85511635
TO
132 * @param array $params
133 * (reference ) an assoc array of name/value pairs.
134 * @param array $defaults
135 * (reference ) an assoc array to hold the flattened values.
6a488035 136 *
c490a46a 137 * @return CRM_Grant_BAO_ManageGrant object
6a488035
TO
138 * @static
139 */
00be9182 140 public static function retrieve(&$params, &$defaults) {
6a488035
TO
141 $grant = new CRM_Grant_DAO_Grant();
142 $grant->copyValues($params);
143 if ($grant->find(TRUE)) {
144 CRM_Core_DAO::storeValues($grant, $defaults);
145 return $grant;
146 }
147 return NULL;
148 }
149
150 /**
100fef9d 151 * Add grant
6a488035 152 *
85511635
TO
153 * @param array $params
154 * Reference array contains the values submitted by the form.
155 * @param array $ids
156 * Reference array contains the id.
6a488035 157 *
6a488035
TO
158 * @static
159 *
160 * @return object
161 */
00be9182 162 public static function add(&$params, &$ids) {
6a488035 163
a7488080 164 if (!empty($ids['grant_id'])) {
6a488035
TO
165 CRM_Utils_Hook::pre('edit', 'Grant', $ids['grant_id'], $params);
166 }
167 else {
168 CRM_Utils_Hook::pre('create', 'Grant', NULL, $params);
169 }
170
171 // first clean up all the money fields
172 $moneyFields = array(
173 'amount_total',
174 'amount_granted',
175 'amount_requested',
176 );
177 foreach ($moneyFields as $field) {
178 if (isset($params[$field])) {
179 $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
180 }
181 }
182 // convert dates to mysql format
183 $dates = array(
184 'application_received_date',
185 'decision_date',
186 'money_transfer_date',
187 'grant_due_date',
188 );
189
190 foreach ($dates as $d) {
191 if (isset($params[$d])) {
192 $params[$d] = CRM_Utils_Date::processDate($params[$d], NULL, TRUE);
193 }
194 }
195 $grant = new CRM_Grant_DAO_Grant();
5bf99dd1 196 $grant->id = CRM_Utils_Array::value('grant_id', $ids);
6a488035
TO
197
198 $grant->copyValues($params);
199
200 // set currency for CRM-1496
201 if (!isset($grant->currency)) {
202 $config = CRM_Core_Config::singleton();
203 $grant->currency = $config->defaultCurrency;
204 }
205
206 $result = $grant->save();
207
208 $url = CRM_Utils_System::url('civicrm/contact/view/grant',
209 "action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
210 );
211
fb1fd730 212 $grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id');
a7488080 213 if (empty($params['skipRecentView'])) {
9b873358 214 if (!isset($grant->contact_id) || !isset($grant->grant_type_id)) {
6a488035
TO
215 $grant->find(TRUE);
216 }
217 $title = CRM_Contact_BAO_Contact::displayName($grant->contact_id) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id];
218
219 $recentOther = array();
220 if (CRM_Core_Permission::checkActionPermission('CiviGrant', CRM_Core_Action::UPDATE)) {
221 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant',
222 "action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
223 );
224 }
225 if (CRM_Core_Permission::checkActionPermission('CiviGrant', CRM_Core_Action::DELETE)) {
226 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant',
227 "action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
228 );
229 }
230
1afc557a 231 // add the recently created Grant
6a488035
TO
232 CRM_Utils_Recent::add($title,
233 $url,
234 $grant->id,
235 'Grant',
236 $grant->contact_id,
237 NULL,
238 $recentOther
239 );
240 }
241
a7488080 242 if (!empty($ids['grant'])) {
6a488035
TO
243 CRM_Utils_Hook::post('edit', 'Grant', $grant->id, $grant);
244 }
245 else {
246 CRM_Utils_Hook::post('create', 'Grant', $grant->id, $grant);
247 }
248
249 return $result;
250 }
251
252 /**
100fef9d 253 * Create the event
6a488035 254 *
85511635
TO
255 * @param array $params
256 * Reference array contains the values submitted by the form.
257 * @param array $ids
258 * Reference array contains the id.
6a488035 259 *
77b97be7 260 * @return object
6a488035
TO
261 * @static
262 *
263 */
264 public static function create(&$params, &$ids) {
265 $transaction = new CRM_Core_Transaction();
266
267 $grant = self::add($params, $ids);
268
269 if (is_a($grant, 'CRM_Core_Error')) {
270 $transaction->rollback();
271 return $grant;
272 }
273
274 $session = CRM_Core_Session::singleton();
275 $id = $session->get('userID');
276 if (!$id) {
277 $id = CRM_Utils_Array::value('contact_id', $params);
278 }
8fa02267 279 if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) {
6a488035
TO
280 $noteParams = array(
281 'entity_table' => 'civicrm_grant',
282 'note' => $params['note'] = $params['note'] ? $params['note'] : "null",
283 'entity_id' => $grant->id,
284 'contact_id' => $id,
285 'modified_date' => date('Ymd'),
286 );
287
8fa02267 288 CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids));
6a488035
TO
289 }
290 // Log the information on successful add/edit of Grant
291 $logParams = array(
292 'entity_table' => 'civicrm_grant',
293 'entity_id' => $grant->id,
294 'modified_id' => $id,
295 'modified_date' => date('Ymd'),
296 );
297
298 CRM_Core_BAO_Log::add($logParams);
299
300 // add custom field values
a7488080 301 if (!empty($params['custom']) && is_array($params['custom'])) {
6a488035
TO
302 CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_grant', $grant->id);
303 }
304
305 // check and attach and files as needed
306 CRM_Core_BAO_File::processAttachment($params,
307 'civicrm_grant',
308 $grant->id
309 );
310
311 $transaction->commit();
312
313 return $grant;
314 }
315
316 /**
100fef9d 317 * Delete the Contact
6a488035 318 *
85511635
TO
319 * @param int $id
320 * Contact id.
77b97be7
EM
321 *
322 * @return bool
6a488035 323 *
6a488035 324 * @static
6a488035 325 */
00be9182 326 public static function deleteContact($id) {
6a488035
TO
327 $grant = new CRM_Grant_DAO_Grant();
328 $grant->contact_id = $id;
329 $grant->delete();
330 return FALSE;
331 }
332
333 /**
100fef9d 334 * Delete the grant
6a488035 335 *
85511635
TO
336 * @param int $id
337 * Grant id.
6a488035 338 *
77b97be7 339 * @return bool|mixed
6a488035
TO
340 * @static
341 *
342 */
00be9182 343 public static function del($id) {
6a488035
TO
344 CRM_Utils_Hook::pre('delete', 'Grant', $id, CRM_Core_DAO::$_nullArray);
345
346 $grant = new CRM_Grant_DAO_Grant();
347 $grant->id = $id;
348
349 $grant->find();
350
351 // delete the recently created Grant
352 $grantRecent = array(
353 'id' => $id,
354 'type' => 'Grant',
355 );
356 CRM_Utils_Recent::del($grantRecent);
357
358 if ($grant->fetch()) {
359 $results = $grant->delete();
360 CRM_Utils_Hook::post('delete', 'Grant', $grant->id, $grant);
361 return $results;
362 }
363 return FALSE;
364 }
365
366 /**
100fef9d 367 * Combine all the exportable fields from the lower levels object
6a488035 368 *
a6c01b45
CW
369 * @return array
370 * array of exportable Fields
6a488035
TO
371 * @static
372 */
00be9182 373 public static function &exportableFields() {
6a488035
TO
374 if (!self::$_exportableFields) {
375 if (!self::$_exportableFields) {
376 self::$_exportableFields = array();
377 }
378
379 $grantFields = array(
380 'grant_status' => array(
381 'title' => 'Grant Status',
382 'name' => 'grant_status',
383 'data_type' => CRM_Utils_Type::T_STRING,
384 ),
385 'grant_type' => array(
386 'title' => 'Grant Type',
387 'name' => 'grant_type',
388 'data_type' => CRM_Utils_Type::T_STRING,
389 ),
390 'grant_money_transfer_date' => array(
391 'title' => 'Grant Money Transfer Date',
392 'name' => 'grant_money_transfer_date',
393 'data_type' => CRM_Utils_Type::T_DATE,
394 ),
395 'grant_amount_requested' => array(
396 'title' => 'Grant Amount Requested',
397 'name' => 'grant_amount_requested',
398 'data_type' => CRM_Utils_Type::T_FLOAT,
399 ),
400 'grant_application_received_date' => array(
401 'title' => 'Grant Application Recieved Date',
402 'name' => 'grant_application_received_date',
403 'data_type' => CRM_Utils_Type::T_DATE,
404 ),
405 );
406
407 $fields = CRM_Grant_DAO_Grant::export();
1afc557a
TO
408 $grantNote = array(
409 'grant_note' => array(
410 'title' => ts('Grant Note'),
6a488035
TO
411 'name' => 'grant_note',
412 'data_type' => CRM_Utils_Type::T_TEXT,
413 ));
414 $fields = array_merge($fields, $grantFields, $grantNote,
415 CRM_Core_BAO_CustomField::getFieldsForImport('Grant')
416 );
417 self::$_exportableFields = $fields;
418 }
419
420 return self::$_exportableFields;
421 }
422
423 /**
100fef9d 424 * Get grant record count for a Contact
6a488035 425 *
c490a46a 426 * @param int $contactID
6a488035 427 *
a6c01b45
CW
428 * @return int
429 * count of grant records
6a488035
TO
430 * @static
431 */
00be9182 432 public static function getContactGrantCount($contactID) {
6a488035
TO
433 $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
434 return CRM_Core_DAO::singleValueQuery($query);
435 }
436}