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