Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
016d95d3 | 13 | * Class CRM_Grant_BAO_Grant |
6a488035 TO |
14 | */ |
15 | class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant { | |
16 | ||
6a488035 | 17 | /** |
fe482240 | 18 | * Get events Summary. |
6a488035 | 19 | * |
6a488035 | 20 | * |
77b97be7 EM |
21 | * @param bool $admin |
22 | * | |
a6c01b45 CW |
23 | * @return array |
24 | * Array of event summary values | |
6a488035 | 25 | */ |
00be9182 | 26 | public static function getGrantSummary($admin = FALSE) { |
6a488035 | 27 | $query = " |
7d9f60da BS |
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 | "; | |
6a488035 | 35 | |
33621c4f | 36 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 | 37 | |
be2fb01f CW |
38 | $status = []; |
39 | $summary = []; | |
6a488035 | 40 | $summary['total_grants'] = NULL; |
fb1fd730 | 41 | $status = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id'); |
6a488035 TO |
42 | |
43 | foreach ($status as $id => $name) { | |
be2fb01f | 44 | $stats[$id] = [ |
6a488035 TO |
45 | 'label' => $name, |
46 | 'total' => 0, | |
be2fb01f | 47 | ]; |
6a488035 TO |
48 | } |
49 | ||
50 | while ($dao->fetch()) { | |
be2fb01f | 51 | $stats[$dao->status_id] = [ |
6a488035 TO |
52 | 'label' => $status[$dao->status_id], |
53 | 'total' => $dao->status_total, | |
be2fb01f | 54 | ]; |
6a488035 TO |
55 | $summary['total_grants'] += $dao->status_total; |
56 | } | |
57 | ||
58 | $summary['per_status'] = $stats; | |
59 | return $summary; | |
60 | } | |
61 | ||
62 | /** | |
fe482240 | 63 | * Get events Summary. |
6a488035 | 64 | * |
6a488035 | 65 | * |
a6c01b45 CW |
66 | * @return array |
67 | * Array of event summary values | |
ee3db087 SL |
68 | * |
69 | * @throws CRM_Core_Exception | |
6a488035 | 70 | */ |
00be9182 | 71 | public static function getGrantStatusOptGroup() { |
6a488035 | 72 | |
be2fb01f | 73 | $params = []; |
6a488035 TO |
74 | $params['name'] = CRM_Grant_BAO_Grant::$statusGroupName; |
75 | ||
be2fb01f | 76 | $defaults = []; |
6a488035 TO |
77 | |
78 | $og = CRM_Core_BAO_OptionGroup::retrieve($params, $defaults); | |
79 | if (!$og) { | |
ee3db087 | 80 | throw new CRM_Core_Exception('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql'); |
6a488035 TO |
81 | } |
82 | ||
83 | return $og; | |
84 | } | |
85 | ||
6a488035 | 86 | /** |
fe482240 | 87 | * Fetch object based on array of properties. |
6a488035 | 88 | * |
85511635 TO |
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. | |
6a488035 | 93 | * |
73e70470 | 94 | * @return CRM_Grant_DAO_Grant |
6a488035 | 95 | */ |
00be9182 | 96 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
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 | /** | |
fe482240 | 107 | * Add grant. |
6a488035 | 108 | * |
85511635 | 109 | * @param array $params |
85511635 | 110 | * @param array $ids |
6a488035 TO |
111 | * |
112 | * @return object | |
113 | */ | |
1b0cadcf CW |
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); | |
6a488035 | 118 | |
6a488035 | 119 | $grant = new CRM_Grant_DAO_Grant(); |
1b0cadcf | 120 | $grant->id = $id; |
6a488035 TO |
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 | ||
fb1fd730 | 136 | $grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id'); |
a7488080 | 137 | if (empty($params['skipRecentView'])) { |
9b873358 | 138 | if (!isset($grant->contact_id) || !isset($grant->grant_type_id)) { |
6a488035 TO |
139 | $grant->find(TRUE); |
140 | } | |
141 | $title = CRM_Contact_BAO_Contact::displayName($grant->contact_id) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id]; | |
142 | ||
be2fb01f | 143 | $recentOther = []; |
6a488035 TO |
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 | ||
1afc557a | 155 | // add the recently created Grant |
6a488035 TO |
156 | CRM_Utils_Recent::add($title, |
157 | $url, | |
158 | $grant->id, | |
159 | 'Grant', | |
160 | $grant->contact_id, | |
161 | NULL, | |
162 | $recentOther | |
163 | ); | |
164 | } | |
165 | ||
1b0cadcf | 166 | CRM_Utils_Hook::post($hook, 'Grant', $grant->id, $grant); |
6a488035 TO |
167 | |
168 | return $result; | |
169 | } | |
170 | ||
171 | /** | |
1b0cadcf | 172 | * Adds a grant. |
6a488035 | 173 | * |
85511635 | 174 | * @param array $params |
85511635 | 175 | * @param array $ids |
6a488035 | 176 | * |
77b97be7 | 177 | * @return object |
6a488035 | 178 | */ |
1b0cadcf | 179 | public static function create($params, $ids = []) { |
6a488035 TO |
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) { | |
9c1bc317 | 192 | $id = $params['contact_id'] ?? NULL; |
6a488035 | 193 | } |
8fa02267 | 194 | if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) { |
be2fb01f | 195 | $noteParams = [ |
6a488035 TO |
196 | 'entity_table' => 'civicrm_grant', |
197 | 'note' => $params['note'] = $params['note'] ? $params['note'] : "null", | |
198 | 'entity_id' => $grant->id, | |
199 | 'contact_id' => $id, | |
be2fb01f | 200 | ]; |
6a488035 | 201 | |
8fa02267 | 202 | CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids)); |
6a488035 TO |
203 | } |
204 | // Log the information on successful add/edit of Grant | |
be2fb01f | 205 | $logParams = [ |
6a488035 TO |
206 | 'entity_table' => 'civicrm_grant', |
207 | 'entity_id' => $grant->id, | |
208 | 'modified_id' => $id, | |
209 | 'modified_date' => date('Ymd'), | |
be2fb01f | 210 | ]; |
6a488035 TO |
211 | |
212 | CRM_Core_BAO_Log::add($logParams); | |
213 | ||
214 | // add custom field values | |
a7488080 | 215 | if (!empty($params['custom']) && is_array($params['custom'])) { |
6a488035 TO |
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 | /** | |
fe482240 | 231 | * Delete the Contact. |
6a488035 | 232 | * |
85511635 TO |
233 | * @param int $id |
234 | * Contact id. | |
77b97be7 EM |
235 | * |
236 | * @return bool | |
6a488035 | 237 | * |
6a488035 | 238 | */ |
00be9182 | 239 | public static function deleteContact($id) { |
6a488035 TO |
240 | $grant = new CRM_Grant_DAO_Grant(); |
241 | $grant->contact_id = $id; | |
242 | $grant->delete(); | |
243 | return FALSE; | |
244 | } | |
245 | ||
246 | /** | |
fe482240 | 247 | * Delete the grant. |
6a488035 | 248 | * |
85511635 TO |
249 | * @param int $id |
250 | * Grant id. | |
6a488035 | 251 | * |
77b97be7 | 252 | * @return bool|mixed |
6a488035 | 253 | */ |
00be9182 | 254 | public static function del($id) { |
be12df5a | 255 | CRM_Utils_Hook::pre('delete', 'Grant', $id); |
6a488035 TO |
256 | |
257 | $grant = new CRM_Grant_DAO_Grant(); | |
258 | $grant->id = $id; | |
259 | ||
260 | $grant->find(); | |
261 | ||
262 | // delete the recently created Grant | |
be2fb01f | 263 | $grantRecent = [ |
6a488035 TO |
264 | 'id' => $id, |
265 | 'type' => 'Grant', | |
be2fb01f | 266 | ]; |
6a488035 TO |
267 | CRM_Utils_Recent::del($grantRecent); |
268 | ||
269 | if ($grant->fetch()) { | |
270 | $results = $grant->delete(); | |
271 | CRM_Utils_Hook::post('delete', 'Grant', $grant->id, $grant); | |
272 | return $results; | |
273 | } | |
274 | return FALSE; | |
275 | } | |
276 | ||
277 | /** | |
fe482240 | 278 | * Combine all the exportable fields from the lower levels object. |
6a488035 | 279 | * |
a6c01b45 CW |
280 | * @return array |
281 | * array of exportable Fields | |
6a488035 | 282 | */ |
00be9182 | 283 | public static function &exportableFields() { |
ae7f34ec | 284 | $fields = CRM_Grant_DAO_Grant::export(); |
be2fb01f CW |
285 | $grantNote = [ |
286 | 'grant_note' => [ | |
ae7f34ec PN |
287 | 'title' => ts('Grant Note'), |
288 | 'name' => 'grant_note', | |
289 | 'data_type' => CRM_Utils_Type::T_TEXT, | |
be2fb01f CW |
290 | ], |
291 | ]; | |
ae7f34ec PN |
292 | $fields = array_merge($fields, $grantNote, |
293 | CRM_Core_BAO_CustomField::getFieldsForImport('Grant') | |
294 | ); | |
6a488035 | 295 | |
ae7f34ec | 296 | return $fields; |
6a488035 TO |
297 | } |
298 | ||
299 | /** | |
fe482240 | 300 | * Get grant record count for a Contact. |
6a488035 | 301 | * |
c490a46a | 302 | * @param int $contactID |
6a488035 | 303 | * |
a6c01b45 CW |
304 | * @return int |
305 | * count of grant records | |
6a488035 | 306 | */ |
00be9182 | 307 | public static function getContactGrantCount($contactID) { |
6a488035 TO |
308 | $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} "; |
309 | return CRM_Core_DAO::singleValueQuery($query); | |
310 | } | |
96025800 | 311 | |
6a488035 | 312 | } |