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