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