Merge pull request #15764 from civicrm/5.20
[civicrm-core.git] / CRM / Grant / BAO / Grant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2020 |
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
31 * @copyright CiviCRM LLC (c) 2004-2020
32 * $Id$
33 *
34 */
35 class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant {
36
37 /**
38 * Class constructor.
39 */
40 public function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Get events Summary.
46 *
47 *
48 * @param bool $admin
49 *
50 * @return array
51 * Array of event summary values
52 */
53 public static function getGrantSummary($admin = FALSE) {
54 $query = "
55 SELECT status_id, count(id) as status_total
56 FROM civicrm_grant GROUP BY status_id";
57
58 $dao = CRM_Core_DAO::executeQuery($query);
59
60 $status = [];
61 $summary = [];
62 $summary['total_grants'] = NULL;
63 $status = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id');
64
65 foreach ($status as $id => $name) {
66 $stats[$id] = [
67 'label' => $name,
68 'total' => 0,
69 ];
70 }
71
72 while ($dao->fetch()) {
73 $stats[$dao->status_id] = [
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 /**
85 * Get events Summary.
86 *
87 *
88 * @return array
89 * Array of event summary values
90 */
91 public static function getGrantStatusOptGroup() {
92
93 $params = [];
94 $params['name'] = CRM_Grant_BAO_Grant::$statusGroupName;
95
96 $defaults = [];
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
106 /**
107 * Fetch object based on array of properties.
108 *
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.
113 *
114 * @return CRM_Grant_BAO_ManageGrant
115 */
116 public static function retrieve(&$params, &$defaults) {
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 /**
127 * Add grant.
128 *
129 * @param array $params
130 * Reference array contains the values submitted by the form.
131 * @param array $ids
132 * Reference array contains the id.
133 *
134 *
135 * @return object
136 */
137 public static function add(&$params, &$ids) {
138
139 if (!empty($ids['grant_id'])) {
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 = [
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 = [
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();
171 $grant->id = CRM_Utils_Array::value('grant_id', $ids);
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
187 $grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id');
188 if (empty($params['skipRecentView'])) {
189 if (!isset($grant->contact_id) || !isset($grant->grant_type_id)) {
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 = [];
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
206 // add the recently created Grant
207 CRM_Utils_Recent::add($title,
208 $url,
209 $grant->id,
210 'Grant',
211 $grant->contact_id,
212 NULL,
213 $recentOther
214 );
215 }
216
217 if (!empty($ids['grant'])) {
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 /**
228 * Create the event.
229 *
230 * @param array $params
231 * Reference array contains the values submitted by the form.
232 * @param array $ids
233 * Reference array contains the id.
234 *
235 * @return object
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 }
252 if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) {
253 $noteParams = [
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
261 CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids));
262 }
263 // Log the information on successful add/edit of Grant
264 $logParams = [
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
274 if (!empty($params['custom']) && is_array($params['custom'])) {
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 /**
290 * Delete the Contact.
291 *
292 * @param int $id
293 * Contact id.
294 *
295 * @return bool
296 *
297 */
298 public static function deleteContact($id) {
299 $grant = new CRM_Grant_DAO_Grant();
300 $grant->contact_id = $id;
301 $grant->delete();
302 return FALSE;
303 }
304
305 /**
306 * Delete the grant.
307 *
308 * @param int $id
309 * Grant id.
310 *
311 * @return bool|mixed
312 */
313 public static function del($id) {
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 = [
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 /**
337 * Combine all the exportable fields from the lower levels object.
338 *
339 * @return array
340 * array of exportable Fields
341 */
342 public static function &exportableFields() {
343 $fields = CRM_Grant_DAO_Grant::export();
344 $grantNote = [
345 'grant_note' => [
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 );
354
355 return $fields;
356 }
357
358 /**
359 * Get grant record count for a Contact.
360 *
361 * @param int $contactID
362 *
363 * @return int
364 * count of grant records
365 */
366 public static function getContactGrantCount($contactID) {
367 $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
368 return CRM_Core_DAO::singleValueQuery($query);
369 }
370
371 }