Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant { | |
36 | ||
37 | /** | |
fe482240 | 38 | * Static field for all the grant information that we can potentially export. |
6a488035 | 39 | * @var array |
6a488035 TO |
40 | */ |
41 | static $_exportableFields = NULL; | |
42 | ||
43 | /** | |
fe482240 | 44 | * Class constructor. |
6a488035 | 45 | */ |
00be9182 | 46 | public function __construct() { |
6a488035 TO |
47 | parent::__construct(); |
48 | } | |
49 | ||
50 | /** | |
fe482240 | 51 | * Get events Summary. |
6a488035 | 52 | * |
6a488035 | 53 | * |
77b97be7 EM |
54 | * @param bool $admin |
55 | * | |
a6c01b45 CW |
56 | * @return array |
57 | * Array of event summary values | |
6a488035 | 58 | */ |
00be9182 | 59 | public static function getGrantSummary($admin = FALSE) { |
6a488035 TO |
60 | $query = " |
61 | SELECT status_id, count(id) as status_total | |
62 | FROM civicrm_grant GROUP BY status_id"; | |
63 | ||
33621c4f | 64 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
65 | |
66 | $status = array(); | |
67 | $summary = array(); | |
68 | $summary['total_grants'] = NULL; | |
fb1fd730 | 69 | $status = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id'); |
6a488035 TO |
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 | /** | |
fe482240 | 91 | * Get events Summary. |
6a488035 | 92 | * |
6a488035 | 93 | * |
a6c01b45 CW |
94 | * @return array |
95 | * Array of event summary values | |
6a488035 | 96 | */ |
00be9182 | 97 | public static function getGrantStatusOptGroup() { |
6a488035 TO |
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 | ||
6a488035 | 112 | /** |
fe482240 | 113 | * Fetch object based on array of properties. |
6a488035 | 114 | * |
85511635 TO |
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. | |
6a488035 | 119 | * |
16b10e64 | 120 | * @return CRM_Grant_BAO_ManageGrant |
6a488035 | 121 | */ |
00be9182 | 122 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
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 | /** | |
fe482240 | 133 | * Add grant. |
6a488035 | 134 | * |
85511635 TO |
135 | * @param array $params |
136 | * Reference array contains the values submitted by the form. | |
137 | * @param array $ids | |
138 | * Reference array contains the id. | |
6a488035 | 139 | * |
6a488035 TO |
140 | * |
141 | * @return object | |
142 | */ | |
00be9182 | 143 | public static function add(&$params, &$ids) { |
6a488035 | 144 | |
a7488080 | 145 | if (!empty($ids['grant_id'])) { |
6a488035 TO |
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(); | |
5bf99dd1 | 177 | $grant->id = CRM_Utils_Array::value('grant_id', $ids); |
6a488035 TO |
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 | ||
fb1fd730 | 193 | $grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id'); |
a7488080 | 194 | if (empty($params['skipRecentView'])) { |
9b873358 | 195 | if (!isset($grant->contact_id) || !isset($grant->grant_type_id)) { |
6a488035 TO |
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 | ||
1afc557a | 212 | // add the recently created Grant |
6a488035 TO |
213 | CRM_Utils_Recent::add($title, |
214 | $url, | |
215 | $grant->id, | |
216 | 'Grant', | |
217 | $grant->contact_id, | |
218 | NULL, | |
219 | $recentOther | |
220 | ); | |
221 | } | |
222 | ||
a7488080 | 223 | if (!empty($ids['grant'])) { |
6a488035 TO |
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 | /** | |
fe482240 | 234 | * Create the event. |
6a488035 | 235 | * |
85511635 TO |
236 | * @param array $params |
237 | * Reference array contains the values submitted by the form. | |
238 | * @param array $ids | |
239 | * Reference array contains the id. | |
6a488035 | 240 | * |
77b97be7 | 241 | * @return object |
6a488035 TO |
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 | } | |
8fa02267 | 258 | if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) { |
6a488035 TO |
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 | ||
8fa02267 | 267 | CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids)); |
6a488035 TO |
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 | |
a7488080 | 280 | if (!empty($params['custom']) && is_array($params['custom'])) { |
6a488035 TO |
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 | /** | |
fe482240 | 296 | * Delete the Contact. |
6a488035 | 297 | * |
85511635 TO |
298 | * @param int $id |
299 | * Contact id. | |
77b97be7 EM |
300 | * |
301 | * @return bool | |
6a488035 | 302 | * |
6a488035 | 303 | */ |
00be9182 | 304 | public static function deleteContact($id) { |
6a488035 TO |
305 | $grant = new CRM_Grant_DAO_Grant(); |
306 | $grant->contact_id = $id; | |
307 | $grant->delete(); | |
308 | return FALSE; | |
309 | } | |
310 | ||
311 | /** | |
fe482240 | 312 | * Delete the grant. |
6a488035 | 313 | * |
85511635 TO |
314 | * @param int $id |
315 | * Grant id. | |
6a488035 | 316 | * |
77b97be7 | 317 | * @return bool|mixed |
6a488035 | 318 | */ |
00be9182 | 319 | public static function del($id) { |
6a488035 TO |
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 | /** | |
fe482240 | 343 | * Combine all the exportable fields from the lower levels object. |
6a488035 | 344 | * |
a6c01b45 CW |
345 | * @return array |
346 | * array of exportable Fields | |
6a488035 | 347 | */ |
00be9182 | 348 | public static function &exportableFields() { |
6a488035 TO |
349 | if (!self::$_exportableFields) { |
350 | if (!self::$_exportableFields) { | |
351 | self::$_exportableFields = array(); | |
352 | } | |
353 | ||
354 | $grantFields = array( | |
355 | 'grant_status' => array( | |
e300cf31 | 356 | 'title' => ts('Grant Status'), |
6a488035 TO |
357 | 'name' => 'grant_status', |
358 | 'data_type' => CRM_Utils_Type::T_STRING, | |
359 | ), | |
360 | 'grant_type' => array( | |
e300cf31 | 361 | 'title' => ts('Grant Type'), |
6a488035 TO |
362 | 'name' => 'grant_type', |
363 | 'data_type' => CRM_Utils_Type::T_STRING, | |
364 | ), | |
365 | 'grant_money_transfer_date' => array( | |
e300cf31 | 366 | 'title' => ts('Grant Money Transfer Date'), |
6a488035 TO |
367 | 'name' => 'grant_money_transfer_date', |
368 | 'data_type' => CRM_Utils_Type::T_DATE, | |
369 | ), | |
370 | 'grant_amount_requested' => array( | |
e300cf31 | 371 | 'title' => ts('Grant Amount Requested'), |
6a488035 TO |
372 | 'name' => 'grant_amount_requested', |
373 | 'data_type' => CRM_Utils_Type::T_FLOAT, | |
374 | ), | |
375 | 'grant_application_received_date' => array( | |
e300cf31 | 376 | 'title' => ts('Grant Application Received Date'), |
6a488035 TO |
377 | 'name' => 'grant_application_received_date', |
378 | 'data_type' => CRM_Utils_Type::T_DATE, | |
379 | ), | |
380 | ); | |
381 | ||
382 | $fields = CRM_Grant_DAO_Grant::export(); | |
1afc557a | 383 | $grantNote = array( |
353ffa53 TO |
384 | 'grant_note' => array( |
385 | 'title' => ts('Grant Note'), | |
6a488035 TO |
386 | 'name' => 'grant_note', |
387 | 'data_type' => CRM_Utils_Type::T_TEXT, | |
e7483cbe | 388 | ), |
353ffa53 | 389 | ); |
6a488035 TO |
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 | /** | |
fe482240 | 400 | * Get grant record count for a Contact. |
6a488035 | 401 | * |
c490a46a | 402 | * @param int $contactID |
6a488035 | 403 | * |
a6c01b45 CW |
404 | * @return int |
405 | * count of grant records | |
6a488035 | 406 | */ |
00be9182 | 407 | public static function getContactGrantCount($contactID) { |
6a488035 TO |
408 | $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} "; |
409 | return CRM_Core_DAO::singleValueQuery($query); | |
410 | } | |
96025800 | 411 | |
6a488035 | 412 | } |