Convert civicrm_note.modified_date to timestamp
[civicrm-core.git] / CRM / Grant / BAO / Grant.php
CommitLineData
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/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 * $Id$
17 *
18 */
19class CRM_Grant_BAO_Grant extends CRM_Grant_DAO_Grant {
20
6a488035 21 /**
fe482240 22 * Class constructor.
6a488035 23 */
00be9182 24 public function __construct() {
6a488035
TO
25 parent::__construct();
26 }
27
28 /**
fe482240 29 * Get events Summary.
6a488035 30 *
6a488035 31 *
77b97be7
EM
32 * @param bool $admin
33 *
a6c01b45
CW
34 * @return array
35 * Array of event summary values
6a488035 36 */
00be9182 37 public static function getGrantSummary($admin = FALSE) {
6a488035
TO
38 $query = "
39 SELECT status_id, count(id) as status_total
40 FROM civicrm_grant GROUP BY status_id";
41
33621c4f 42 $dao = CRM_Core_DAO::executeQuery($query);
6a488035 43
be2fb01f
CW
44 $status = [];
45 $summary = [];
6a488035 46 $summary['total_grants'] = NULL;
fb1fd730 47 $status = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'status_id');
6a488035
TO
48
49 foreach ($status as $id => $name) {
be2fb01f 50 $stats[$id] = [
6a488035
TO
51 'label' => $name,
52 'total' => 0,
be2fb01f 53 ];
6a488035
TO
54 }
55
56 while ($dao->fetch()) {
be2fb01f 57 $stats[$dao->status_id] = [
6a488035
TO
58 'label' => $status[$dao->status_id],
59 'total' => $dao->status_total,
be2fb01f 60 ];
6a488035
TO
61 $summary['total_grants'] += $dao->status_total;
62 }
63
64 $summary['per_status'] = $stats;
65 return $summary;
66 }
67
68 /**
fe482240 69 * Get events Summary.
6a488035 70 *
6a488035 71 *
a6c01b45
CW
72 * @return array
73 * Array of event summary values
ee3db087
SL
74 *
75 * @throws CRM_Core_Exception
6a488035 76 */
00be9182 77 public static function getGrantStatusOptGroup() {
6a488035 78
be2fb01f 79 $params = [];
6a488035
TO
80 $params['name'] = CRM_Grant_BAO_Grant::$statusGroupName;
81
be2fb01f 82 $defaults = [];
6a488035
TO
83
84 $og = CRM_Core_BAO_OptionGroup::retrieve($params, $defaults);
85 if (!$og) {
ee3db087 86 throw new CRM_Core_Exception('No option group for grant statuses - database discrepancy! Make sure you loaded civicrm_data.mysql');
6a488035
TO
87 }
88
89 return $og;
90 }
91
6a488035 92 /**
fe482240 93 * Fetch object based on array of properties.
6a488035 94 *
85511635
TO
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.
6a488035 99 *
16b10e64 100 * @return CRM_Grant_BAO_ManageGrant
6a488035 101 */
00be9182 102 public static function retrieve(&$params, &$defaults) {
6a488035
TO
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 /**
fe482240 113 * Add grant.
6a488035 114 *
85511635
TO
115 * @param array $params
116 * Reference array contains the values submitted by the form.
117 * @param array $ids
118 * Reference array contains the id.
6a488035 119 *
6a488035
TO
120 *
121 * @return object
122 */
00be9182 123 public static function add(&$params, &$ids) {
6a488035 124
a7488080 125 if (!empty($ids['grant_id'])) {
6a488035
TO
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
be2fb01f 133 $moneyFields = [
6a488035
TO
134 'amount_total',
135 'amount_granted',
136 'amount_requested',
be2fb01f 137 ];
6a488035
TO
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
be2fb01f 144 $dates = [
6a488035
TO
145 'application_received_date',
146 'decision_date',
147 'money_transfer_date',
148 'grant_due_date',
be2fb01f 149 ];
6a488035
TO
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();
5bf99dd1 157 $grant->id = CRM_Utils_Array::value('grant_id', $ids);
6a488035
TO
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
fb1fd730 173 $grantTypes = CRM_Core_PseudoConstant::get('CRM_Grant_DAO_Grant', 'grant_type_id');
a7488080 174 if (empty($params['skipRecentView'])) {
9b873358 175 if (!isset($grant->contact_id) || !isset($grant->grant_type_id)) {
6a488035
TO
176 $grant->find(TRUE);
177 }
178 $title = CRM_Contact_BAO_Contact::displayName($grant->contact_id) . ' - ' . ts('Grant') . ': ' . $grantTypes[$grant->grant_type_id];
179
be2fb01f 180 $recentOther = [];
6a488035
TO
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
1afc557a 192 // add the recently created Grant
6a488035
TO
193 CRM_Utils_Recent::add($title,
194 $url,
195 $grant->id,
196 'Grant',
197 $grant->contact_id,
198 NULL,
199 $recentOther
200 );
201 }
202
a7488080 203 if (!empty($ids['grant'])) {
6a488035
TO
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 /**
fe482240 214 * Create the event.
6a488035 215 *
85511635
TO
216 * @param array $params
217 * Reference array contains the values submitted by the form.
218 * @param array $ids
219 * Reference array contains the id.
6a488035 220 *
77b97be7 221 * @return object
6a488035
TO
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 }
8fa02267 238 if (!empty($params['note']) || CRM_Utils_Array::value('id', CRM_Utils_Array::value('note', $ids))) {
be2fb01f 239 $noteParams = [
6a488035
TO
240 'entity_table' => 'civicrm_grant',
241 'note' => $params['note'] = $params['note'] ? $params['note'] : "null",
242 'entity_id' => $grant->id,
243 'contact_id' => $id,
be2fb01f 244 ];
6a488035 245
8fa02267 246 CRM_Core_BAO_Note::add($noteParams, (array) CRM_Utils_Array::value('note', $ids));
6a488035
TO
247 }
248 // Log the information on successful add/edit of Grant
be2fb01f 249 $logParams = [
6a488035
TO
250 'entity_table' => 'civicrm_grant',
251 'entity_id' => $grant->id,
252 'modified_id' => $id,
253 'modified_date' => date('Ymd'),
be2fb01f 254 ];
6a488035
TO
255
256 CRM_Core_BAO_Log::add($logParams);
257
258 // add custom field values
a7488080 259 if (!empty($params['custom']) && is_array($params['custom'])) {
6a488035
TO
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 /**
fe482240 275 * Delete the Contact.
6a488035 276 *
85511635
TO
277 * @param int $id
278 * Contact id.
77b97be7
EM
279 *
280 * @return bool
6a488035 281 *
6a488035 282 */
00be9182 283 public static function deleteContact($id) {
6a488035
TO
284 $grant = new CRM_Grant_DAO_Grant();
285 $grant->contact_id = $id;
286 $grant->delete();
287 return FALSE;
288 }
289
290 /**
fe482240 291 * Delete the grant.
6a488035 292 *
85511635
TO
293 * @param int $id
294 * Grant id.
6a488035 295 *
77b97be7 296 * @return bool|mixed
6a488035 297 */
00be9182 298 public static function del($id) {
6a488035
TO
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
be2fb01f 307 $grantRecent = [
6a488035
TO
308 'id' => $id,
309 'type' => 'Grant',
be2fb01f 310 ];
6a488035
TO
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 /**
fe482240 322 * Combine all the exportable fields from the lower levels object.
6a488035 323 *
a6c01b45
CW
324 * @return array
325 * array of exportable Fields
6a488035 326 */
00be9182 327 public static function &exportableFields() {
ae7f34ec 328 $fields = CRM_Grant_DAO_Grant::export();
be2fb01f
CW
329 $grantNote = [
330 'grant_note' => [
ae7f34ec
PN
331 'title' => ts('Grant Note'),
332 'name' => 'grant_note',
333 'data_type' => CRM_Utils_Type::T_TEXT,
be2fb01f
CW
334 ],
335 ];
ae7f34ec
PN
336 $fields = array_merge($fields, $grantNote,
337 CRM_Core_BAO_CustomField::getFieldsForImport('Grant')
338 );
6a488035 339
ae7f34ec 340 return $fields;
6a488035
TO
341 }
342
343 /**
fe482240 344 * Get grant record count for a Contact.
6a488035 345 *
c490a46a 346 * @param int $contactID
6a488035 347 *
a6c01b45
CW
348 * @return int
349 * count of grant records
6a488035 350 */
00be9182 351 public static function getContactGrantCount($contactID) {
6a488035
TO
352 $query = "SELECT count(*) FROM civicrm_grant WHERE civicrm_grant.contact_id = {$contactID} ";
353 return CRM_Core_DAO::singleValueQuery($query);
354 }
96025800 355
6a488035 356}