Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant { | |
36 | ||
6a488035 | 37 | /** |
fe482240 | 38 | * Class constructor. |
6a488035 | 39 | */ |
00be9182 | 40 | public function __construct() { |
6a488035 TO |
41 | parent::__construct(); |
42 | } | |
43 | ||
44 | /** | |
fe482240 | 45 | * Get events Summary. |
6a488035 | 46 | * |
6a488035 | 47 | * |
77b97be7 EM |
48 | * @param bool $admin |
49 | * | |
a6c01b45 CW |
50 | * @return array |
51 | * Array of event summary values | |
6a488035 | 52 | */ |
00be9182 | 53 | public static function getGrantSummary($admin = FALSE) { |
6a488035 TO |
54 | $query = " |
55 | SELECT status_id, count(id) as status_total | |
56 | FROM civicrm_grant GROUP BY status_id"; | |
57 | ||
33621c4f | 58 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
59 | |
60 | $status = array(); | |
61 | $summary = array(); | |
62 | $summary['total_grants'] = NULL; | |
fb1fd730 | 63 | $status = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id'); |
6a488035 TO |
64 | |
65 | foreach ($status as $id => $name) { | |
66 | $stats[$id] = array( | |
67 | 'label' => $name, | |
68 | 'total' => 0, | |
69 | ); | |
70 | } | |
71 | ||
72 | while ($dao->fetch()) { | |
73 | $stats[$dao->status_id] = array( | |
74 | 'label' => $status[$dao->status_id], | |
75 | 'total' => $dao->status_total, | |
76 | ); | |
77 | $summary['total_grants'] += $dao->status_total; | |
78 | } | |
79 | ||
80 | $summary['per_status'] = $stats; | |
81 | return $summary; | |
82 | } | |
83 | ||
84 | /** | |
fe482240 | 85 | * Get events Summary. |
6a488035 | 86 | * |
6a488035 | 87 | * |
a6c01b45 CW |
88 | * @return array |
89 | * Array of event summary values | |
6a488035 | 90 | */ |
00be9182 | 91 | public static function getGrantStatusOptGroup() { |
6a488035 TO |
92 | |
93 | $params = array(); | |
94 | $params['name'] = CRM_Grant_BAO_Grant::$statusGroupName; | |
95 | ||
96 | $defaults = array(); | |
97 | ||
98 | $og = CRM_Core_BAO_OptionGroup::retrieve($params, $defaults); | |
99 | if (!$og) { | |
100 | CRM_Core_Error::fatal('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql'); | |
101 | } | |
102 | ||
103 | return $og; | |
104 | } | |
105 | ||
6a488035 | 106 | /** |
fe482240 | 107 | * Fetch object based on array of properties. |
6a488035 | 108 | * |
85511635 TO |
109 | * @param array $params |
110 | * (reference ) an assoc array of name/value pairs. | |
111 | * @param array $defaults | |
112 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 113 | * |
16b10e64 | 114 | * @return CRM_Grant_BAO_ManageGrant |
6a488035 | 115 | */ |
00be9182 | 116 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
117 | $grant = new CRM_Grant_DAO_Grant(); |
118 | $grant->copyValues($params); | |
119 | if ($grant->find(TRUE)) { | |
120 | CRM_Core_DAO::storeValues($grant, $defaults); | |
121 | return $grant; | |
122 | } | |
123 | return NULL; | |
124 | } | |
125 | ||
126 | /** | |
fe482240 | 127 | * Add grant. |
6a488035 | 128 | * |
85511635 TO |
129 | * @param array $params |
130 | * Reference array contains the values submitted by the form. | |
131 | * @param array $ids | |
132 | * Reference array contains the id. | |
6a488035 | 133 | * |
6a488035 TO |
134 | * |
135 | * @return object | |
136 | */ | |
00be9182 | 137 | public static function add(&$params, &$ids) { |
6a488035 | 138 | |
a7488080 | 139 | if (!empty($ids['grant_id'])) { |
6a488035 TO |
140 | CRM_Utils_Hook::pre('edit', 'Grant', $ids['grant_id'], $params); |
141 | } | |
142 | else { | |
143 | CRM_Utils_Hook::pre('create', 'Grant', NULL, $params); | |
144 | } | |
145 | ||
146 | // first clean up all the money fields | |
147 | $moneyFields = array( | |
148 | 'amount_total', | |
149 | 'amount_granted', | |
150 | 'amount_requested', | |
151 | ); | |
152 | foreach ($moneyFields as $field) { | |
153 | if (isset($params[$field])) { | |
154 | $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]); | |
155 | } | |
156 | } | |
157 | // convert dates to mysql format | |
158 | $dates = array( | |
159 | 'application_received_date', | |
160 | 'decision_date', | |
161 | 'money_transfer_date', | |
162 | 'grant_due_date', | |
163 | ); | |
164 | ||
165 | foreach ($dates as $d) { | |
166 | if (isset($params[$d])) { | |
167 | $params[$d] = CRM_Utils_Date::processDate($params[$d], NULL, TRUE); | |
168 | } | |
169 | } | |
170 | $grant = new CRM_Grant_DAO_Grant(); | |
5bf99dd1 | 171 | $grant->id = CRM_Utils_Array::value('grant_id', $ids); |
6a488035 TO |
172 | |
173 | $grant->copyValues($params); | |
174 | ||
175 | // set currency for CRM-1496 | |
176 | if (!isset($grant->currency)) { | |
177 | $config = CRM_Core_Config::singleton(); | |
178 | $grant->currency = $config->defaultCurrency; | |
179 | } | |
180 | ||
181 | $result = $grant->save(); | |
182 | ||
183 | $url = CRM_Utils_System::url('civicrm/contact/view/grant', | |
184 | "action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home" | |
185 | ); | |
186 | ||
fb1fd730 | 187 | $grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id'); |
a7488080 | 188 | if (empty($params['skipRecentView'])) { |
9b873358 | 189 | if (!isset($grant->contact_id) || !isset($grant->grant_type_id)) { |
6a488035 TO |
190 | $grant->find(TRUE); |
191 | } | |
192 | $title = CRM_Contact_BAO_Contact::displayName($grant->contact_id) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id]; | |
193 | ||
194 | $recentOther = array(); | |
195 | if (CRM_Core_Permission::checkActionPermission('CiviGrant', CRM_Core_Action::UPDATE)) { | |
196 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant', | |
197 | "action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home" | |
198 | ); | |
199 | } | |
200 | if (CRM_Core_Permission::checkActionPermission('CiviGrant', CRM_Core_Action::DELETE)) { | |
201 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant', | |
202 | "action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home" | |
203 | ); | |
204 | } | |
205 | ||
1afc557a | 206 | // add the recently created Grant |
6a488035 TO |
207 | CRM_Utils_Recent::add($title, |
208 | $url, | |
209 | $grant->id, | |
210 | 'Grant', | |
211 | $grant->contact_id, | |
212 | NULL, | |
213 | $recentOther | |
214 | ); | |
215 | } | |
216 | ||
a7488080 | 217 | if (!empty($ids['grant'])) { |
6a488035 TO |
218 | CRM_Utils_Hook::post('edit', 'Grant', $grant->id, $grant); |
219 | } | |
220 | else { | |
221 | CRM_Utils_Hook::post('create', 'Grant', $grant->id, $grant); | |
222 | } | |
223 | ||
224 | return $result; | |
225 | } | |
226 | ||
227 | /** | |
fe482240 | 228 | * Create the event. |
6a488035 | 229 | * |
85511635 TO |
230 | * @param array $params |
231 | * Reference array contains the values submitted by the form. | |
232 | * @param array $ids | |
233 | * Reference array contains the id. | |
6a488035 | 234 | * |
77b97be7 | 235 | * @return object |
6a488035 TO |
236 | */ |
237 | public static function create(&$params, &$ids) { | |
238 | $transaction = new CRM_Core_Transaction(); | |
239 | ||
240 | $grant = self::add($params, $ids); | |
241 | ||
242 | if (is_a($grant, 'CRM_Core_Error')) { | |
243 | $transaction->rollback(); | |
244 | return $grant; | |
245 | } | |
246 | ||
247 | $session = CRM_Core_Session::singleton(); | |
248 | $id = $session->get('userID'); | |
249 | if (!$id) { | |
250 | $id = CRM_Utils_Array::value('contact_id', $params); | |
251 | } | |
8fa02267 | 252 | if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) { |
6a488035 TO |
253 | $noteParams = array( |
254 | 'entity_table' => 'civicrm_grant', | |
255 | 'note' => $params['note'] = $params['note'] ? $params['note'] : "null", | |
256 | 'entity_id' => $grant->id, | |
257 | 'contact_id' => $id, | |
258 | 'modified_date' => date('Ymd'), | |
259 | ); | |
260 | ||
8fa02267 | 261 | CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids)); |
6a488035 TO |
262 | } |
263 | // Log the information on successful add/edit of Grant | |
264 | $logParams = array( | |
265 | 'entity_table' => 'civicrm_grant', | |
266 | 'entity_id' => $grant->id, | |
267 | 'modified_id' => $id, | |
268 | 'modified_date' => date('Ymd'), | |
269 | ); | |
270 | ||
271 | CRM_Core_BAO_Log::add($logParams); | |
272 | ||
273 | // add custom field values | |
a7488080 | 274 | if (!empty($params['custom']) && is_array($params['custom'])) { |
6a488035 TO |
275 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_grant', $grant->id); |
276 | } | |
277 | ||
278 | // check and attach and files as needed | |
279 | CRM_Core_BAO_File::processAttachment($params, | |
280 | 'civicrm_grant', | |
281 | $grant->id | |
282 | ); | |
283 | ||
284 | $transaction->commit(); | |
285 | ||
286 | return $grant; | |
287 | } | |
288 | ||
289 | /** | |
fe482240 | 290 | * Delete the Contact. |
6a488035 | 291 | * |
85511635 TO |
292 | * @param int $id |
293 | * Contact id. | |
77b97be7 EM |
294 | * |
295 | * @return bool | |
6a488035 | 296 | * |
6a488035 | 297 | */ |
00be9182 | 298 | public static function deleteContact($id) { |
6a488035 TO |
299 | $grant = new CRM_Grant_DAO_Grant(); |
300 | $grant->contact_id = $id; | |
301 | $grant->delete(); | |
302 | return FALSE; | |
303 | } | |
304 | ||
305 | /** | |
fe482240 | 306 | * Delete the grant. |
6a488035 | 307 | * |
85511635 TO |
308 | * @param int $id |
309 | * Grant id. | |
6a488035 | 310 | * |
77b97be7 | 311 | * @return bool|mixed |
6a488035 | 312 | */ |
00be9182 | 313 | public static function del($id) { |
6a488035 TO |
314 | CRM_Utils_Hook::pre('delete', 'Grant', $id, CRM_Core_DAO::$_nullArray); |
315 | ||
316 | $grant = new CRM_Grant_DAO_Grant(); | |
317 | $grant->id = $id; | |
318 | ||
319 | $grant->find(); | |
320 | ||
321 | // delete the recently created Grant | |
322 | $grantRecent = array( | |
323 | 'id' => $id, | |
324 | 'type' => 'Grant', | |
325 | ); | |
326 | CRM_Utils_Recent::del($grantRecent); | |
327 | ||
328 | if ($grant->fetch()) { | |
329 | $results = $grant->delete(); | |
330 | CRM_Utils_Hook::post('delete', 'Grant', $grant->id, $grant); | |
331 | return $results; | |
332 | } | |
333 | return FALSE; | |
334 | } | |
335 | ||
336 | /** | |
fe482240 | 337 | * Combine all the exportable fields from the lower levels object. |
6a488035 | 338 | * |
a6c01b45 CW |
339 | * @return array |
340 | * array of exportable Fields | |
6a488035 | 341 | */ |
00be9182 | 342 | public static function &exportableFields() { |
ae7f34ec PN |
343 | $fields = CRM_Grant_DAO_Grant::export(); |
344 | $grantNote = array( | |
345 | 'grant_note' => array( | |
346 | 'title' => ts('Grant Note'), | |
347 | 'name' => 'grant_note', | |
348 | 'data_type' => CRM_Utils_Type::T_TEXT, | |
349 | ), | |
350 | ); | |
351 | $fields = array_merge($fields, $grantNote, | |
352 | CRM_Core_BAO_CustomField::getFieldsForImport('Grant') | |
353 | ); | |
6a488035 | 354 | |
ae7f34ec | 355 | return $fields; |
6a488035 TO |
356 | } |
357 | ||
358 | /** | |
fe482240 | 359 | * Get grant record count for a Contact. |
6a488035 | 360 | * |
c490a46a | 361 | * @param int $contactID |
6a488035 | 362 | * |
a6c01b45 CW |
363 | * @return int |
364 | * count of grant records | |
6a488035 | 365 | */ |
00be9182 | 366 | public static function getContactGrantCount($contactID) { |
6a488035 TO |
367 | $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} "; |
368 | return CRM_Core_DAO::singleValueQuery($query); | |
369 | } | |
96025800 | 370 | |
6a488035 | 371 | } |