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