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