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 | /** | |
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 TO |
46 | */ |
47 | function __construct() { | |
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 | */ | |
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 | /** | |
100fef9d | 92 | * Get events Summary |
6a488035 TO |
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 | /** | |
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 | */ | |
122 | static function getGrantStatistics($admin = FALSE) { | |
123 | $grantStatuses = array(); | |
124 | } | |
125 | ||
126 | /** | |
c490a46a | 127 | * Fetch object based on array of properties |
6a488035 TO |
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 | * | |
c490a46a | 132 | * @return CRM_Grant_BAO_ManageGrant object |
6a488035 TO |
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 | /** | |
100fef9d | 147 | * Add grant |
6a488035 TO |
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 | ||
a7488080 | 159 | if (!empty($ids['grant_id'])) { |
6a488035 TO |
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(); | |
5bf99dd1 | 191 | $grant->id = CRM_Utils_Array::value('grant_id', $ids); |
6a488035 TO |
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 | ||
fb1fd730 | 207 | $grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id'); |
a7488080 | 208 | if (empty($params['skipRecentView'])) { |
6a488035 TO |
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 | ||
a7488080 | 237 | if (!empty($ids['grant'])) { |
6a488035 TO |
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 | /** | |
100fef9d | 248 | * Create the event |
6a488035 TO |
249 | * |
250 | * @param array $params reference array contains the values submitted by the form | |
77b97be7 | 251 | * @param array $ids reference array contains the id |
6a488035 | 252 | * |
77b97be7 | 253 | * @return object |
6a488035 TO |
254 | * @access public |
255 | * @static | |
256 | * | |
257 | */ | |
258 | public static function create(&$params, &$ids) { | |
259 | $transaction = new CRM_Core_Transaction(); | |
260 | ||
261 | $grant = self::add($params, $ids); | |
262 | ||
263 | if (is_a($grant, 'CRM_Core_Error')) { | |
264 | $transaction->rollback(); | |
265 | return $grant; | |
266 | } | |
267 | ||
268 | $session = CRM_Core_Session::singleton(); | |
269 | $id = $session->get('userID'); | |
270 | if (!$id) { | |
271 | $id = CRM_Utils_Array::value('contact_id', $params); | |
272 | } | |
8fa02267 | 273 | if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) { |
6a488035 TO |
274 | $noteParams = array( |
275 | 'entity_table' => 'civicrm_grant', | |
276 | 'note' => $params['note'] = $params['note'] ? $params['note'] : "null", | |
277 | 'entity_id' => $grant->id, | |
278 | 'contact_id' => $id, | |
279 | 'modified_date' => date('Ymd'), | |
280 | ); | |
281 | ||
8fa02267 | 282 | CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids)); |
6a488035 TO |
283 | } |
284 | // Log the information on successful add/edit of Grant | |
285 | $logParams = array( | |
286 | 'entity_table' => 'civicrm_grant', | |
287 | 'entity_id' => $grant->id, | |
288 | 'modified_id' => $id, | |
289 | 'modified_date' => date('Ymd'), | |
290 | ); | |
291 | ||
292 | CRM_Core_BAO_Log::add($logParams); | |
293 | ||
294 | // add custom field values | |
a7488080 | 295 | if (!empty($params['custom']) && is_array($params['custom'])) { |
6a488035 TO |
296 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_grant', $grant->id); |
297 | } | |
298 | ||
299 | // check and attach and files as needed | |
300 | CRM_Core_BAO_File::processAttachment($params, | |
301 | 'civicrm_grant', | |
302 | $grant->id | |
303 | ); | |
304 | ||
305 | $transaction->commit(); | |
306 | ||
307 | return $grant; | |
308 | } | |
309 | ||
310 | /** | |
100fef9d | 311 | * Delete the Contact |
6a488035 | 312 | * |
c490a46a | 313 | * @param int $id contact id |
77b97be7 EM |
314 | * |
315 | * @return bool | |
6a488035 TO |
316 | * |
317 | * @access public | |
318 | * @static | |
6a488035 TO |
319 | */ |
320 | static function deleteContact($id) { | |
321 | $grant = new CRM_Grant_DAO_Grant(); | |
322 | $grant->contact_id = $id; | |
323 | $grant->delete(); | |
324 | return FALSE; | |
325 | } | |
326 | ||
327 | /** | |
100fef9d | 328 | * Delete the grant |
6a488035 | 329 | * |
77b97be7 | 330 | * @param int $id grant id |
6a488035 | 331 | * |
77b97be7 | 332 | * @return bool|mixed |
6a488035 TO |
333 | * @access public |
334 | * @static | |
335 | * | |
336 | */ | |
337 | static function del($id) { | |
338 | CRM_Utils_Hook::pre('delete', 'Grant', $id, CRM_Core_DAO::$_nullArray); | |
339 | ||
340 | $grant = new CRM_Grant_DAO_Grant(); | |
341 | $grant->id = $id; | |
342 | ||
343 | $grant->find(); | |
344 | ||
345 | // delete the recently created Grant | |
346 | $grantRecent = array( | |
347 | 'id' => $id, | |
348 | 'type' => 'Grant', | |
349 | ); | |
350 | CRM_Utils_Recent::del($grantRecent); | |
351 | ||
352 | if ($grant->fetch()) { | |
353 | $results = $grant->delete(); | |
354 | CRM_Utils_Hook::post('delete', 'Grant', $grant->id, $grant); | |
355 | return $results; | |
356 | } | |
357 | return FALSE; | |
358 | } | |
359 | ||
360 | /** | |
100fef9d | 361 | * Combine all the exportable fields from the lower levels object |
6a488035 TO |
362 | * |
363 | * @return array array of exportable Fields | |
364 | * @access public | |
365 | * @static | |
366 | */ | |
367 | static function &exportableFields() { | |
368 | if (!self::$_exportableFields) { | |
369 | if (!self::$_exportableFields) { | |
370 | self::$_exportableFields = array(); | |
371 | } | |
372 | ||
373 | $grantFields = array( | |
374 | 'grant_status' => array( | |
375 | 'title' => 'Grant Status', | |
376 | 'name' => 'grant_status', | |
377 | 'data_type' => CRM_Utils_Type::T_STRING, | |
378 | ), | |
379 | 'grant_type' => array( | |
380 | 'title' => 'Grant Type', | |
381 | 'name' => 'grant_type', | |
382 | 'data_type' => CRM_Utils_Type::T_STRING, | |
383 | ), | |
384 | 'grant_money_transfer_date' => array( | |
385 | 'title' => 'Grant Money Transfer Date', | |
386 | 'name' => 'grant_money_transfer_date', | |
387 | 'data_type' => CRM_Utils_Type::T_DATE, | |
388 | ), | |
389 | 'grant_amount_requested' => array( | |
390 | 'title' => 'Grant Amount Requested', | |
391 | 'name' => 'grant_amount_requested', | |
392 | 'data_type' => CRM_Utils_Type::T_FLOAT, | |
393 | ), | |
394 | 'grant_application_received_date' => array( | |
395 | 'title' => 'Grant Application Recieved Date', | |
396 | 'name' => 'grant_application_received_date', | |
397 | 'data_type' => CRM_Utils_Type::T_DATE, | |
398 | ), | |
399 | ); | |
400 | ||
401 | $fields = CRM_Grant_DAO_Grant::export(); | |
402 | $grantNote = array('grant_note' => array('title' => ts('Grant Note'), | |
403 | 'name' => 'grant_note', | |
404 | 'data_type' => CRM_Utils_Type::T_TEXT, | |
405 | )); | |
406 | $fields = array_merge($fields, $grantFields, $grantNote, | |
407 | CRM_Core_BAO_CustomField::getFieldsForImport('Grant') | |
408 | ); | |
409 | self::$_exportableFields = $fields; | |
410 | } | |
411 | ||
412 | return self::$_exportableFields; | |
413 | } | |
414 | ||
415 | /** | |
100fef9d | 416 | * Get grant record count for a Contact |
6a488035 | 417 | * |
c490a46a | 418 | * @param int $contactID |
6a488035 TO |
419 | * |
420 | * @return int count of grant records | |
421 | * @access public | |
422 | * @static | |
423 | */ | |
424 | static function getContactGrantCount($contactID) { | |
425 | $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} "; | |
426 | return CRM_Core_DAO::singleValueQuery($query); | |
427 | } | |
428 | } | |
429 |