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