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