Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
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 | */ | |
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 | * | |
77b97be7 EM |
56 | * @param bool $admin |
57 | * | |
6a488035 TO |
58 | * @return array Array of event summary values |
59 | */ | |
60 | 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; | |
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 | /** | |
92 | * Function to get events Summary | |
93 | * | |
94 | * @static | |
95 | * | |
96 | * @return array Array of event summary values | |
97 | */ | |
98 | 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 | * Function to retrieve statistics for grants. | |
115 | * | |
116 | * @static | |
117 | * | |
fd31fa4c EM |
118 | * @param bool $admin |
119 | * | |
6a488035 TO |
120 | * @return array Array of grant summary statistics |
121 | */ | |
122 | static function getGrantStatistics($admin = FALSE) { | |
123 | $grantStatuses = array(); | |
124 | } | |
125 | ||
126 | /** | |
127 | * Takes a bunch of params that are needed to match certain criteria and | |
128 | * retrieves the relevant objects. Typically the valid params are only | |
129 | * contact_id. We'll tweak this function to be more full featured over a period | |
130 | * of time. This is the inverse function of create. It also stores all the retrieved | |
131 | * values in the default array | |
132 | * | |
133 | * @param array $params (reference ) an assoc array of name/value pairs | |
134 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
135 | * | |
136 | * @return object CRM_Grant_BAO_ManageGrant object | |
137 | * @access public | |
138 | * @static | |
139 | */ | |
140 | static function retrieve(&$params, &$defaults) { | |
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 | /** | |
151 | * function to add grant | |
152 | * | |
153 | * @param array $params reference array contains the values submitted by the form | |
154 | * @param array $ids reference array contains the id | |
155 | * | |
156 | * @access public | |
157 | * @static | |
158 | * | |
159 | * @return object | |
160 | */ | |
161 | static function add(&$params, &$ids) { | |
162 | ||
a7488080 | 163 | if (!empty($ids['grant_id'])) { |
6a488035 TO |
164 | CRM_Utils_Hook::pre('edit', 'Grant', $ids['grant_id'], $params); |
165 | } | |
166 | else { | |
167 | CRM_Utils_Hook::pre('create', 'Grant', NULL, $params); | |
168 | } | |
169 | ||
170 | // first clean up all the money fields | |
171 | $moneyFields = array( | |
172 | 'amount_total', | |
173 | 'amount_granted', | |
174 | 'amount_requested', | |
175 | ); | |
176 | foreach ($moneyFields as $field) { | |
177 | if (isset($params[$field])) { | |
178 | $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]); | |
179 | } | |
180 | } | |
181 | // convert dates to mysql format | |
182 | $dates = array( | |
183 | 'application_received_date', | |
184 | 'decision_date', | |
185 | 'money_transfer_date', | |
186 | 'grant_due_date', | |
187 | ); | |
188 | ||
189 | foreach ($dates as $d) { | |
190 | if (isset($params[$d])) { | |
191 | $params[$d] = CRM_Utils_Date::processDate($params[$d], NULL, TRUE); | |
192 | } | |
193 | } | |
194 | $grant = new CRM_Grant_DAO_Grant(); | |
5bf99dd1 | 195 | $grant->id = CRM_Utils_Array::value('grant_id', $ids); |
6a488035 TO |
196 | |
197 | $grant->copyValues($params); | |
198 | ||
199 | // set currency for CRM-1496 | |
200 | if (!isset($grant->currency)) { | |
201 | $config = CRM_Core_Config::singleton(); | |
202 | $grant->currency = $config->defaultCurrency; | |
203 | } | |
204 | ||
205 | $result = $grant->save(); | |
206 | ||
207 | $url = CRM_Utils_System::url('civicrm/contact/view/grant', | |
208 | "action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home" | |
209 | ); | |
210 | ||
fb1fd730 | 211 | $grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id'); |
a7488080 | 212 | if (empty($params['skipRecentView'])) { |
6a488035 TO |
213 | if(!isset($grant->contact_id) || !isset($grant->grant_type_id)){ |
214 | $grant->find(TRUE); | |
215 | } | |
216 | $title = CRM_Contact_BAO_Contact::displayName($grant->contact_id) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id]; | |
217 | ||
218 | $recentOther = array(); | |
219 | if (CRM_Core_Permission::checkActionPermission('CiviGrant', CRM_Core_Action::UPDATE)) { | |
220 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant', | |
221 | "action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home" | |
222 | ); | |
223 | } | |
224 | if (CRM_Core_Permission::checkActionPermission('CiviGrant', CRM_Core_Action::DELETE)) { | |
225 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant', | |
226 | "action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home" | |
227 | ); | |
228 | } | |
229 | ||
230 | // add the recently created Grant | |
231 | CRM_Utils_Recent::add($title, | |
232 | $url, | |
233 | $grant->id, | |
234 | 'Grant', | |
235 | $grant->contact_id, | |
236 | NULL, | |
237 | $recentOther | |
238 | ); | |
239 | } | |
240 | ||
a7488080 | 241 | if (!empty($ids['grant'])) { |
6a488035 TO |
242 | CRM_Utils_Hook::post('edit', 'Grant', $grant->id, $grant); |
243 | } | |
244 | else { | |
245 | CRM_Utils_Hook::post('create', 'Grant', $grant->id, $grant); | |
246 | } | |
247 | ||
248 | return $result; | |
249 | } | |
250 | ||
251 | /** | |
252 | * function to create the event | |
253 | * | |
254 | * @param array $params reference array contains the values submitted by the form | |
77b97be7 | 255 | * @param array $ids reference array contains the id |
6a488035 | 256 | * |
77b97be7 | 257 | * @return object |
6a488035 TO |
258 | * @access public |
259 | * @static | |
260 | * | |
261 | */ | |
262 | public static function create(&$params, &$ids) { | |
263 | $transaction = new CRM_Core_Transaction(); | |
264 | ||
265 | $grant = self::add($params, $ids); | |
266 | ||
267 | if (is_a($grant, 'CRM_Core_Error')) { | |
268 | $transaction->rollback(); | |
269 | return $grant; | |
270 | } | |
271 | ||
272 | $session = CRM_Core_Session::singleton(); | |
273 | $id = $session->get('userID'); | |
274 | if (!$id) { | |
275 | $id = CRM_Utils_Array::value('contact_id', $params); | |
276 | } | |
8fa02267 | 277 | if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) { |
6a488035 TO |
278 | $noteParams = array( |
279 | 'entity_table' => 'civicrm_grant', | |
280 | 'note' => $params['note'] = $params['note'] ? $params['note'] : "null", | |
281 | 'entity_id' => $grant->id, | |
282 | 'contact_id' => $id, | |
283 | 'modified_date' => date('Ymd'), | |
284 | ); | |
285 | ||
8fa02267 | 286 | CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids)); |
6a488035 TO |
287 | } |
288 | // Log the information on successful add/edit of Grant | |
289 | $logParams = array( | |
290 | 'entity_table' => 'civicrm_grant', | |
291 | 'entity_id' => $grant->id, | |
292 | 'modified_id' => $id, | |
293 | 'modified_date' => date('Ymd'), | |
294 | ); | |
295 | ||
296 | CRM_Core_BAO_Log::add($logParams); | |
297 | ||
298 | // add custom field values | |
a7488080 | 299 | if (!empty($params['custom']) && is_array($params['custom'])) { |
6a488035 TO |
300 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_grant', $grant->id); |
301 | } | |
302 | ||
303 | // check and attach and files as needed | |
304 | CRM_Core_BAO_File::processAttachment($params, | |
305 | 'civicrm_grant', | |
306 | $grant->id | |
307 | ); | |
308 | ||
309 | $transaction->commit(); | |
310 | ||
311 | return $grant; | |
312 | } | |
313 | ||
314 | /** | |
315 | * Function to delete the Contact | |
316 | * | |
77b97be7 EM |
317 | * @param $id |
318 | * | |
319 | * @return bool | |
320 | * @internal param int $cid contact id | |
6a488035 TO |
321 | * |
322 | * @access public | |
323 | * @static | |
6a488035 TO |
324 | */ |
325 | static function deleteContact($id) { | |
326 | $grant = new CRM_Grant_DAO_Grant(); | |
327 | $grant->contact_id = $id; | |
328 | $grant->delete(); | |
329 | return FALSE; | |
330 | } | |
331 | ||
332 | /** | |
333 | * Function to delete the grant | |
334 | * | |
77b97be7 | 335 | * @param int $id grant id |
6a488035 | 336 | * |
77b97be7 | 337 | * @return bool|mixed |
6a488035 TO |
338 | * @access public |
339 | * @static | |
340 | * | |
341 | */ | |
342 | static function del($id) { | |
343 | CRM_Utils_Hook::pre('delete', 'Grant', $id, CRM_Core_DAO::$_nullArray); | |
344 | ||
345 | $grant = new CRM_Grant_DAO_Grant(); | |
346 | $grant->id = $id; | |
347 | ||
348 | $grant->find(); | |
349 | ||
350 | // delete the recently created Grant | |
351 | $grantRecent = array( | |
352 | 'id' => $id, | |
353 | 'type' => 'Grant', | |
354 | ); | |
355 | CRM_Utils_Recent::del($grantRecent); | |
356 | ||
357 | if ($grant->fetch()) { | |
358 | $results = $grant->delete(); | |
359 | CRM_Utils_Hook::post('delete', 'Grant', $grant->id, $grant); | |
360 | return $results; | |
361 | } | |
362 | return FALSE; | |
363 | } | |
364 | ||
365 | /** | |
366 | * combine all the exportable fields from the lower levels object | |
367 | * | |
368 | * @return array array of exportable Fields | |
369 | * @access public | |
370 | * @static | |
371 | */ | |
372 | static function &exportableFields() { | |
373 | if (!self::$_exportableFields) { | |
374 | if (!self::$_exportableFields) { | |
375 | self::$_exportableFields = array(); | |
376 | } | |
377 | ||
378 | $grantFields = array( | |
379 | 'grant_status' => array( | |
380 | 'title' => 'Grant Status', | |
381 | 'name' => 'grant_status', | |
382 | 'data_type' => CRM_Utils_Type::T_STRING, | |
383 | ), | |
384 | 'grant_type' => array( | |
385 | 'title' => 'Grant Type', | |
386 | 'name' => 'grant_type', | |
387 | 'data_type' => CRM_Utils_Type::T_STRING, | |
388 | ), | |
389 | 'grant_money_transfer_date' => array( | |
390 | 'title' => 'Grant Money Transfer Date', | |
391 | 'name' => 'grant_money_transfer_date', | |
392 | 'data_type' => CRM_Utils_Type::T_DATE, | |
393 | ), | |
394 | 'grant_amount_requested' => array( | |
395 | 'title' => 'Grant Amount Requested', | |
396 | 'name' => 'grant_amount_requested', | |
397 | 'data_type' => CRM_Utils_Type::T_FLOAT, | |
398 | ), | |
399 | 'grant_application_received_date' => array( | |
400 | 'title' => 'Grant Application Recieved Date', | |
401 | 'name' => 'grant_application_received_date', | |
402 | 'data_type' => CRM_Utils_Type::T_DATE, | |
403 | ), | |
404 | ); | |
405 | ||
406 | $fields = CRM_Grant_DAO_Grant::export(); | |
407 | $grantNote = array('grant_note' => array('title' => ts('Grant Note'), | |
408 | 'name' => 'grant_note', | |
409 | 'data_type' => CRM_Utils_Type::T_TEXT, | |
410 | )); | |
411 | $fields = array_merge($fields, $grantFields, $grantNote, | |
412 | CRM_Core_BAO_CustomField::getFieldsForImport('Grant') | |
413 | ); | |
414 | self::$_exportableFields = $fields; | |
415 | } | |
416 | ||
417 | return self::$_exportableFields; | |
418 | } | |
419 | ||
420 | /** | |
421 | * Function to get grant record count for a Contact | |
422 | * | |
2a6da8d7 EM |
423 | * @param $contactID |
424 | * | |
425 | * @internal param int $contactId Contact ID | |
6a488035 TO |
426 | * |
427 | * @return int count of grant records | |
428 | * @access public | |
429 | * @static | |
430 | */ | |
431 | static function getContactGrantCount($contactID) { | |
432 | $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} "; | |
433 | return CRM_Core_DAO::singleValueQuery($query); | |
434 | } | |
435 | } | |
436 |