commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Grant / BAO / Grant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 */
41 static $_exportableFields = NULL;
42
43 /**
44 * Class constructor.
45 */
46 public function __construct() {
47 parent::__construct();
48 }
49
50 /**
51 * Get events Summary.
52 *
53 *
54 * @param bool $admin
55 *
56 * @return array
57 * Array of event summary values
58 */
59 public static function getGrantSummary($admin = FALSE) {
60 $query = "
61 SELECT status_id, count(id) as status_total
62 FROM civicrm_grant GROUP BY status_id";
63
64 $dao = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
65
66 $status = array();
67 $summary = array();
68 $summary['total_grants'] = NULL;
69 $status = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id');
70
71 foreach ($status as $id => $name) {
72 $stats[$id] = array(
73 'label' => $name,
74 'total' => 0,
75 );
76 }
77
78 while ($dao->fetch()) {
79 $stats[$dao->status_id] = array(
80 'label' => $status[$dao->status_id],
81 'total' => $dao->status_total,
82 );
83 $summary['total_grants'] += $dao->status_total;
84 }
85
86 $summary['per_status'] = $stats;
87 return $summary;
88 }
89
90 /**
91 * Get events Summary.
92 *
93 *
94 * @return array
95 * Array of event summary values
96 */
97 public static function getGrantStatusOptGroup() {
98
99 $params = array();
100 $params['name'] = CRM_Grant_BAO_Grant::$statusGroupName;
101
102 $defaults = array();
103
104 $og = CRM_Core_BAO_OptionGroup::retrieve($params, $defaults);
105 if (!$og) {
106 CRM_Core_Error::fatal('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql');
107 }
108
109 return $og;
110 }
111
112 /**
113 * Fetch object based on array of properties.
114 *
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.
119 *
120 * @return CRM_Grant_BAO_ManageGrant
121 */
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);
127 return $grant;
128 }
129 return NULL;
130 }
131
132 /**
133 * Add grant.
134 *
135 * @param array $params
136 * Reference array contains the values submitted by the form.
137 * @param array $ids
138 * Reference array contains the id.
139 *
140 *
141 * @return object
142 */
143 public static function add(&$params, &$ids) {
144
145 if (!empty($ids['grant_id'])) {
146 CRM_Utils_Hook::pre('edit', 'Grant', $ids['grant_id'], $params);
147 }
148 else {
149 CRM_Utils_Hook::pre('create', 'Grant', NULL, $params);
150 }
151
152 // first clean up all the money fields
153 $moneyFields = array(
154 'amount_total',
155 'amount_granted',
156 'amount_requested',
157 );
158 foreach ($moneyFields as $field) {
159 if (isset($params[$field])) {
160 $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
161 }
162 }
163 // convert dates to mysql format
164 $dates = array(
165 'application_received_date',
166 'decision_date',
167 'money_transfer_date',
168 'grant_due_date',
169 );
170
171 foreach ($dates as $d) {
172 if (isset($params[$d])) {
173 $params[$d] = CRM_Utils_Date::processDate($params[$d], NULL, TRUE);
174 }
175 }
176 $grant = new CRM_Grant_DAO_Grant();
177 $grant->id = CRM_Utils_Array::value('grant_id', $ids);
178
179 $grant->copyValues($params);
180
181 // set currency for CRM-1496
182 if (!isset($grant->currency)) {
183 $config = CRM_Core_Config::singleton();
184 $grant->currency = $config->defaultCurrency;
185 }
186
187 $result = $grant->save();
188
189 $url = CRM_Utils_System::url('civicrm/contact/view/grant',
190 "action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
191 );
192
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)) {
196 $grant->find(TRUE);
197 }
198 $title = CRM_Contact_BAO_Contact::displayName($grant->contact_id) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id];
199
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"
204 );
205 }
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"
209 );
210 }
211
212 // add the recently created Grant
213 CRM_Utils_Recent::add($title,
214 $url,
215 $grant->id,
216 'Grant',
217 $grant->contact_id,
218 NULL,
219 $recentOther
220 );
221 }
222
223 if (!empty($ids['grant'])) {
224 CRM_Utils_Hook::post('edit', 'Grant', $grant->id, $grant);
225 }
226 else {
227 CRM_Utils_Hook::post('create', 'Grant', $grant->id, $grant);
228 }
229
230 return $result;
231 }
232
233 /**
234 * Create the event.
235 *
236 * @param array $params
237 * Reference array contains the values submitted by the form.
238 * @param array $ids
239 * Reference array contains the id.
240 *
241 * @return object
242 */
243 public static function create(&$params, &$ids) {
244 $transaction = new CRM_Core_Transaction();
245
246 $grant = self::add($params, $ids);
247
248 if (is_a($grant, 'CRM_Core_Error')) {
249 $transaction->rollback();
250 return $grant;
251 }
252
253 $session = CRM_Core_Session::singleton();
254 $id = $session->get('userID');
255 if (!$id) {
256 $id = CRM_Utils_Array::value('contact_id', $params);
257 }
258 if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) {
259 $noteParams = array(
260 'entity_table' => 'civicrm_grant',
261 'note' => $params['note'] = $params['note'] ? $params['note'] : "null",
262 'entity_id' => $grant->id,
263 'contact_id' => $id,
264 'modified_date' => date('Ymd'),
265 );
266
267 CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids));
268 }
269 // Log the information on successful add/edit of Grant
270 $logParams = array(
271 'entity_table' => 'civicrm_grant',
272 'entity_id' => $grant->id,
273 'modified_id' => $id,
274 'modified_date' => date('Ymd'),
275 );
276
277 CRM_Core_BAO_Log::add($logParams);
278
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);
282 }
283
284 // check and attach and files as needed
285 CRM_Core_BAO_File::processAttachment($params,
286 'civicrm_grant',
287 $grant->id
288 );
289
290 $transaction->commit();
291
292 return $grant;
293 }
294
295 /**
296 * Delete the Contact.
297 *
298 * @param int $id
299 * Contact id.
300 *
301 * @return bool
302 *
303 */
304 public static function deleteContact($id) {
305 $grant = new CRM_Grant_DAO_Grant();
306 $grant->contact_id = $id;
307 $grant->delete();
308 return FALSE;
309 }
310
311 /**
312 * Delete the grant.
313 *
314 * @param int $id
315 * Grant id.
316 *
317 * @return bool|mixed
318 */
319 public static function del($id) {
320 CRM_Utils_Hook::pre('delete', 'Grant', $id, CRM_Core_DAO::$_nullArray);
321
322 $grant = new CRM_Grant_DAO_Grant();
323 $grant->id = $id;
324
325 $grant->find();
326
327 // delete the recently created Grant
328 $grantRecent = array(
329 'id' => $id,
330 'type' => 'Grant',
331 );
332 CRM_Utils_Recent::del($grantRecent);
333
334 if ($grant->fetch()) {
335 $results = $grant->delete();
336 CRM_Utils_Hook::post('delete', 'Grant', $grant->id, $grant);
337 return $results;
338 }
339 return FALSE;
340 }
341
342 /**
343 * Combine all the exportable fields from the lower levels object.
344 *
345 * @return array
346 * array of exportable Fields
347 */
348 public static function &exportableFields() {
349 if (!self::$_exportableFields) {
350 if (!self::$_exportableFields) {
351 self::$_exportableFields = array();
352 }
353
354 $grantFields = array(
355 'grant_status' => array(
356 'title' => 'Grant Status',
357 'name' => 'grant_status',
358 'data_type' => CRM_Utils_Type::T_STRING,
359 ),
360 'grant_type' => array(
361 'title' => 'Grant Type',
362 'name' => 'grant_type',
363 'data_type' => CRM_Utils_Type::T_STRING,
364 ),
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,
369 ),
370 'grant_amount_requested' => array(
371 'title' => 'Grant Amount Requested',
372 'name' => 'grant_amount_requested',
373 'data_type' => CRM_Utils_Type::T_FLOAT,
374 ),
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,
379 ),
380 );
381
382 $fields = CRM_Grant_DAO_Grant::export();
383 $grantNote = array(
384 'grant_note' => array(
385 'title' => ts('Grant Note'),
386 'name' => 'grant_note',
387 'data_type' => CRM_Utils_Type::T_TEXT,
388 ),
389 );
390 $fields = array_merge($fields, $grantFields, $grantNote,
391 CRM_Core_BAO_CustomField::getFieldsForImport('Grant')
392 );
393 self::$_exportableFields = $fields;
394 }
395
396 return self::$_exportableFields;
397 }
398
399 /**
400 * Get grant record count for a Contact.
401 *
402 * @param int $contactID
403 *
404 * @return int
405 * count of grant records
406 */
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);
410 }
411
412 }