Merge pull request #15850 from civicrm/5.20
[civicrm-core.git] / CRM / Grant / BAO / Grant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 * $Id$
17 *
18 */
19 class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant {
20
21 /**
22 * Class constructor.
23 */
24 public function __construct() {
25 parent::__construct();
26 }
27
28 /**
29 * Get events Summary.
30 *
31 *
32 * @param bool $admin
33 *
34 * @return array
35 * Array of event summary values
36 */
37 public static function getGrantSummary($admin = FALSE) {
38 $query = "
39 SELECT status_id, count(id) as status_total
40 FROM civicrm_grant GROUP BY status_id";
41
42 $dao = CRM_Core_DAO::executeQuery($query);
43
44 $status = [];
45 $summary = [];
46 $summary['total_grants'] = NULL;
47 $status = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id');
48
49 foreach ($status as $id => $name) {
50 $stats[$id] = [
51 'label' => $name,
52 'total' => 0,
53 ];
54 }
55
56 while ($dao->fetch()) {
57 $stats[$dao->status_id] = [
58 'label' => $status[$dao->status_id],
59 'total' => $dao->status_total,
60 ];
61 $summary['total_grants'] += $dao->status_total;
62 }
63
64 $summary['per_status'] = $stats;
65 return $summary;
66 }
67
68 /**
69 * Get events Summary.
70 *
71 *
72 * @return array
73 * Array of event summary values
74 */
75 public static function getGrantStatusOptGroup() {
76
77 $params = [];
78 $params['name'] = CRM_Grant_BAO_Grant::$statusGroupName;
79
80 $defaults = [];
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
90 /**
91 * Fetch object based on array of properties.
92 *
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.
97 *
98 * @return CRM_Grant_BAO_ManageGrant
99 */
100 public static function retrieve(&$params, &$defaults) {
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 /**
111 * Add grant.
112 *
113 * @param array $params
114 * Reference array contains the values submitted by the form.
115 * @param array $ids
116 * Reference array contains the id.
117 *
118 *
119 * @return object
120 */
121 public static function add(&$params, &$ids) {
122
123 if (!empty($ids['grant_id'])) {
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
131 $moneyFields = [
132 'amount_total',
133 'amount_granted',
134 'amount_requested',
135 ];
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
142 $dates = [
143 'application_received_date',
144 'decision_date',
145 'money_transfer_date',
146 'grant_due_date',
147 ];
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();
155 $grant->id = CRM_Utils_Array::value('grant_id', $ids);
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
171 $grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id');
172 if (empty($params['skipRecentView'])) {
173 if (!isset($grant->contact_id) || !isset($grant->grant_type_id)) {
174 $grant->find(TRUE);
175 }
176 $title = CRM_Contact_BAO_Contact::displayName($grant->contact_id) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id];
177
178 $recentOther = [];
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
190 // add the recently created Grant
191 CRM_Utils_Recent::add($title,
192 $url,
193 $grant->id,
194 'Grant',
195 $grant->contact_id,
196 NULL,
197 $recentOther
198 );
199 }
200
201 if (!empty($ids['grant'])) {
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 /**
212 * Create the event.
213 *
214 * @param array $params
215 * Reference array contains the values submitted by the form.
216 * @param array $ids
217 * Reference array contains the id.
218 *
219 * @return object
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 }
236 if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) {
237 $noteParams = [
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'),
243 ];
244
245 CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids));
246 }
247 // Log the information on successful add/edit of Grant
248 $logParams = [
249 'entity_table' => 'civicrm_grant',
250 'entity_id' => $grant->id,
251 'modified_id' => $id,
252 'modified_date' => date('Ymd'),
253 ];
254
255 CRM_Core_BAO_Log::add($logParams);
256
257 // add custom field values
258 if (!empty($params['custom']) && is_array($params['custom'])) {
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 /**
274 * Delete the Contact.
275 *
276 * @param int $id
277 * Contact id.
278 *
279 * @return bool
280 *
281 */
282 public static function deleteContact($id) {
283 $grant = new CRM_Grant_DAO_Grant();
284 $grant->contact_id = $id;
285 $grant->delete();
286 return FALSE;
287 }
288
289 /**
290 * Delete the grant.
291 *
292 * @param int $id
293 * Grant id.
294 *
295 * @return bool|mixed
296 */
297 public static function del($id) {
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
306 $grantRecent = [
307 'id' => $id,
308 'type' => 'Grant',
309 ];
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 /**
321 * Combine all the exportable fields from the lower levels object.
322 *
323 * @return array
324 * array of exportable Fields
325 */
326 public static function &exportableFields() {
327 $fields = CRM_Grant_DAO_Grant::export();
328 $grantNote = [
329 'grant_note' => [
330 'title' => ts('Grant Note'),
331 'name' => 'grant_note',
332 'data_type' => CRM_Utils_Type::T_TEXT,
333 ],
334 ];
335 $fields = array_merge($fields, $grantNote,
336 CRM_Core_BAO_CustomField::getFieldsForImport('Grant')
337 );
338
339 return $fields;
340 }
341
342 /**
343 * Get grant record count for a Contact.
344 *
345 * @param int $contactID
346 *
347 * @return int
348 * count of grant records
349 */
350 public static function getContactGrantCount($contactID) {
351 $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
352 return CRM_Core_DAO::singleValueQuery($query);
353 }
354
355 }