Merge pull request #17330 from colemanw/dao_field
[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 * @throws CRM_Core_Exception
76 */
77 public static function getGrantStatusOptGroup() {
78
79 $params = [];
80 $params['name'] = CRM_Grant_BAO_Grant::$statusGroupName;
81
82 $defaults = [];
83
84 $og = CRM_Core_BAO_OptionGroup::retrieve($params, $defaults);
85 if (!$og) {
86 throw new CRM_Core_Exception('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql');
87 }
88
89 return $og;
90 }
91
92 /**
93 * Fetch object based on array of properties.
94 *
95 * @param array $params
96 * (reference ) an assoc array of name/value pairs.
97 * @param array $defaults
98 * (reference ) an assoc array to hold the flattened values.
99 *
100 * @return CRM_Grant_BAO_ManageGrant
101 */
102 public static function retrieve(&$params, &$defaults) {
103 $grant = new CRM_Grant_DAO_Grant();
104 $grant->copyValues($params);
105 if ($grant->find(TRUE)) {
106 CRM_Core_DAO::storeValues($grant, $defaults);
107 return $grant;
108 }
109 return NULL;
110 }
111
112 /**
113 * Add grant.
114 *
115 * @param array $params
116 * @param array $ids
117 *
118 * @return object
119 */
120 public static function add($params, $ids = []) {
121 $id = $ids['grant_id'] ?? $params['id'] ?? NULL;
122 $hook = $id ? 'edit' : 'create';
123 CRM_Utils_Hook::pre($hook, 'Grant', $id, $params);
124
125 // first clean up all the money fields
126 $moneyFields = [
127 'amount_total',
128 'amount_granted',
129 'amount_requested',
130 ];
131 foreach ($moneyFields as $field) {
132 if (isset($params[$field])) {
133 $params[$field] = CRM_Utils_Rule::cleanMoney($params[$field]);
134 }
135 }
136 // convert dates to mysql format
137 $dates = [
138 'application_received_date',
139 'decision_date',
140 'money_transfer_date',
141 'grant_due_date',
142 ];
143
144 foreach ($dates as $d) {
145 if (isset($params[$d])) {
146 $params[$d] = CRM_Utils_Date::processDate($params[$d], NULL, TRUE);
147 }
148 }
149 $grant = new CRM_Grant_DAO_Grant();
150 $grant->id = $id;
151
152 $grant->copyValues($params);
153
154 // set currency for CRM-1496
155 if (!isset($grant->currency)) {
156 $config = CRM_Core_Config::singleton();
157 $grant->currency = $config->defaultCurrency;
158 }
159
160 $result = $grant->save();
161
162 $url = CRM_Utils_System::url('civicrm/contact/view/grant',
163 "action=view&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
164 );
165
166 $grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id');
167 if (empty($params['skipRecentView'])) {
168 if (!isset($grant->contact_id) || !isset($grant->grant_type_id)) {
169 $grant->find(TRUE);
170 }
171 $title = CRM_Contact_BAO_Contact::displayName($grant->contact_id) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id];
172
173 $recentOther = [];
174 if (CRM_Core_Permission::checkActionPermission('CiviGrant', CRM_Core_Action::UPDATE)) {
175 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant',
176 "action=update&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
177 );
178 }
179 if (CRM_Core_Permission::checkActionPermission('CiviGrant', CRM_Core_Action::DELETE)) {
180 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/grant',
181 "action=delete&reset=1&id={$grant->id}&cid={$grant->contact_id}&context=home"
182 );
183 }
184
185 // add the recently created Grant
186 CRM_Utils_Recent::add($title,
187 $url,
188 $grant->id,
189 'Grant',
190 $grant->contact_id,
191 NULL,
192 $recentOther
193 );
194 }
195
196 CRM_Utils_Hook::post($hook, 'Grant', $grant->id, $grant);
197
198 return $result;
199 }
200
201 /**
202 * Adds a grant.
203 *
204 * @param array $params
205 * @param array $ids
206 *
207 * @return object
208 */
209 public static function create($params, $ids = []) {
210 $transaction = new CRM_Core_Transaction();
211
212 $grant = self::add($params, $ids);
213
214 if (is_a($grant, 'CRM_Core_Error')) {
215 $transaction->rollback();
216 return $grant;
217 }
218
219 $session = CRM_Core_Session::singleton();
220 $id = $session->get('userID');
221 if (!$id) {
222 $id = $params['contact_id'] ?? NULL;
223 }
224 if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) {
225 $noteParams = [
226 'entity_table' => 'civicrm_grant',
227 'note' => $params['note'] = $params['note'] ? $params['note'] : "null",
228 'entity_id' => $grant->id,
229 'contact_id' => $id,
230 ];
231
232 CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids));
233 }
234 // Log the information on successful add/edit of Grant
235 $logParams = [
236 'entity_table' => 'civicrm_grant',
237 'entity_id' => $grant->id,
238 'modified_id' => $id,
239 'modified_date' => date('Ymd'),
240 ];
241
242 CRM_Core_BAO_Log::add($logParams);
243
244 // add custom field values
245 if (!empty($params['custom']) && is_array($params['custom'])) {
246 CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_grant', $grant->id);
247 }
248
249 // check and attach and files as needed
250 CRM_Core_BAO_File::processAttachment($params,
251 'civicrm_grant',
252 $grant->id
253 );
254
255 $transaction->commit();
256
257 return $grant;
258 }
259
260 /**
261 * Delete the Contact.
262 *
263 * @param int $id
264 * Contact id.
265 *
266 * @return bool
267 *
268 */
269 public static function deleteContact($id) {
270 $grant = new CRM_Grant_DAO_Grant();
271 $grant->contact_id = $id;
272 $grant->delete();
273 return FALSE;
274 }
275
276 /**
277 * Delete the grant.
278 *
279 * @param int $id
280 * Grant id.
281 *
282 * @return bool|mixed
283 */
284 public static function del($id) {
285 CRM_Utils_Hook::pre('delete', 'Grant', $id, CRM_Core_DAO::$_nullArray);
286
287 $grant = new CRM_Grant_DAO_Grant();
288 $grant->id = $id;
289
290 $grant->find();
291
292 // delete the recently created Grant
293 $grantRecent = [
294 'id' => $id,
295 'type' => 'Grant',
296 ];
297 CRM_Utils_Recent::del($grantRecent);
298
299 if ($grant->fetch()) {
300 $results = $grant->delete();
301 CRM_Utils_Hook::post('delete', 'Grant', $grant->id, $grant);
302 return $results;
303 }
304 return FALSE;
305 }
306
307 /**
308 * Combine all the exportable fields from the lower levels object.
309 *
310 * @return array
311 * array of exportable Fields
312 */
313 public static function &exportableFields() {
314 $fields = CRM_Grant_DAO_Grant::export();
315 $grantNote = [
316 'grant_note' => [
317 'title' => ts('Grant Note'),
318 'name' => 'grant_note',
319 'data_type' => CRM_Utils_Type::T_TEXT,
320 ],
321 ];
322 $fields = array_merge($fields, $grantNote,
323 CRM_Core_BAO_CustomField::getFieldsForImport('Grant')
324 );
325
326 return $fields;
327 }
328
329 /**
330 * Get grant record count for a Contact.
331 *
332 * @param int $contactID
333 *
334 * @return int
335 * count of grant records
336 */
337 public static function getContactGrantCount($contactID) {
338 $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
339 return CRM_Core_DAO::singleValueQuery($query);
340 }
341
342 }