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 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | * $Id$ |
17 | * | |
18 | */ | |
19 | ||
20 | /** | |
21 | * This class generates form components for processing a case | |
22 | * | |
23 | */ | |
24 | class CRM_Grant_Form_Grant extends CRM_Core_Form { | |
25 | ||
26 | /** | |
fe482240 | 27 | * The id of the case that we are proceessing. |
6a488035 TO |
28 | * |
29 | * @var int | |
6a488035 TO |
30 | */ |
31 | protected $_id; | |
32 | ||
33 | /** | |
fe482240 | 34 | * The id of the contact associated with this contribution. |
6a488035 TO |
35 | * |
36 | * @var int | |
6a488035 TO |
37 | */ |
38 | protected $_contactID; | |
39 | ||
40 | protected $_context; | |
d5965a37 | 41 | |
6e62b28c TM |
42 | /** |
43 | * Explicitly declare the entity api name. | |
44 | */ | |
45 | public function getDefaultEntity() { | |
46 | return 'Grant'; | |
47 | } | |
6a488035 TO |
48 | |
49 | /** | |
fe482240 | 50 | * Set variables up before form is built. |
6a488035 TO |
51 | * |
52 | * @return void | |
6a488035 TO |
53 | */ |
54 | public function preProcess() { | |
6a488035 TO |
55 | |
56 | $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
353ffa53 | 57 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); |
6a488035 TO |
58 | $this->_grantType = NULL; |
59 | if ($this->_id) { | |
6c552737 | 60 | $this->_grantType = CRM_Core_DAO::getFieldValue('CRM_Grant_DAO_Grant', $this->_id, 'grant_type_id'); |
6a488035 | 61 | } |
edc80cda | 62 | $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this); |
6a488035 TO |
63 | |
64 | $this->assign('action', $this->_action); | |
65 | $this->assign('context', $this->_context); | |
66 | ||
67 | //check permission for action. | |
68 | if (!CRM_Core_Permission::checkActionPermission('CiviGrant', $this->_action)) { | |
0499b0ad | 69 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
70 | } |
71 | ||
e2046b33 CW |
72 | $this->setPageTitle(ts('Grant')); |
73 | ||
6a488035 TO |
74 | if ($this->_action & CRM_Core_Action::DELETE) { |
75 | return; | |
76 | } | |
77 | ||
78 | $this->_noteId = NULL; | |
79 | if ($this->_id) { | |
80 | $noteDAO = new CRM_Core_BAO_Note(); | |
81 | $noteDAO->entity_table = 'civicrm_grant'; | |
82 | $noteDAO->entity_id = $this->_id; | |
83 | if ($noteDAO->find(TRUE)) { | |
84 | $this->_noteId = $noteDAO->id; | |
85 | } | |
86 | } | |
87 | ||
88 | // when custom data is included in this page | |
a7488080 | 89 | if (!empty($_POST['hidden_custom'])) { |
c5366541 | 90 | $grantTypeId = empty($_POST['grant_type_id']) ? NULL : $_POST['grant_type_id']; |
6a488035 | 91 | $this->set('type', 'Grant'); |
c5366541 | 92 | $this->set('subType', $grantTypeId); |
6a488035 | 93 | $this->set('entityId', $this->_id); |
c5366541 | 94 | CRM_Custom_Form_CustomData::preProcess($this, NULL, $grantTypeId, 1, 'Grant', $this->_id); |
6a488035 TO |
95 | CRM_Custom_Form_CustomData::buildQuickForm($this); |
96 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
97 | } | |
98 | } | |
99 | ||
e0ef6999 EM |
100 | /** |
101 | * @return array | |
102 | */ | |
00be9182 | 103 | public function setDefaultValues() { |
6a488035 | 104 | |
6a488035 TO |
105 | $defaults = parent::setDefaultValues(); |
106 | ||
107 | if ($this->_action & CRM_Core_Action::DELETE) { | |
108 | return $defaults; | |
109 | } | |
110 | ||
111 | $params['id'] = $this->_id; | |
112 | if ($this->_noteId) { | |
113 | $defaults['note'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Note', $this->_noteId, 'note'); | |
114 | } | |
115 | if ($this->_id) { | |
116 | CRM_Grant_BAO_Grant::retrieve($params, $defaults); | |
117 | ||
118 | // fix the display of the monetary value, CRM-4038 | |
119 | if (isset($defaults['amount_total'])) { | |
120 | $defaults['amount_total'] = CRM_Utils_Money::format($defaults['amount_total'], NULL, '%a'); | |
121 | } | |
122 | if (isset($defaults['amount_requested'])) { | |
123 | $defaults['amount_requested'] = CRM_Utils_Money::format($defaults['amount_requested'], NULL, '%a'); | |
124 | } | |
125 | if (isset($defaults['amount_granted'])) { | |
126 | $defaults['amount_granted'] = CRM_Utils_Money::format($defaults['amount_granted'], NULL, '%a'); | |
127 | } | |
6a488035 | 128 | } |
7d1e6295 | 129 | else { |
130 | if ($this->_contactID) { | |
131 | $defaults['contact_id'] = $this->_contactID; | |
132 | } | |
133 | } | |
6a488035 TO |
134 | |
135 | return $defaults; | |
136 | } | |
137 | ||
138 | /** | |
fe482240 | 139 | * Build the form object. |
6a488035 | 140 | * |
355ba699 | 141 | * @return void |
6a488035 TO |
142 | */ |
143 | public function buildQuickForm() { | |
6a488035 TO |
144 | |
145 | if ($this->_action & CRM_Core_Action::DELETE) { | |
be2fb01f | 146 | $this->addButtons([ |
7e8c8317 SL |
147 | [ |
148 | 'type' => 'next', | |
149 | 'name' => ts('Delete'), | |
150 | 'spacing' => ' ', | |
151 | 'isDefault' => TRUE, | |
152 | ], | |
153 | [ | |
154 | 'type' => 'cancel', | |
155 | 'name' => ts('Cancel'), | |
156 | ], | |
157 | ]); | |
6a488035 TO |
158 | return; |
159 | } | |
160 | ||
161 | $attributes = CRM_Core_DAO::getAttribute('CRM_Grant_DAO_Grant'); | |
be2fb01f | 162 | $this->addSelect('grant_type_id', ['onChange' => "CRM.buildCustomData( 'Grant', this.value );"], TRUE); |
6a488035 TO |
163 | |
164 | //need to assign custom data type and subtype to the template | |
165 | $this->assign('customDataType', 'Grant'); | |
166 | $this->assign('customDataSubType', $this->_grantType); | |
167 | $this->assign('entityID', $this->_id); | |
168 | ||
be2fb01f | 169 | $this->addSelect('status_id', [], TRUE); |
6a488035 | 170 | |
56e00d58 CW |
171 | $this->add('datepicker', 'application_received_date', ts('Application Received'), [], FALSE, ['time' => FALSE]); |
172 | $this->add('datepicker', 'decision_date', ts('Grant Decision'), [], FALSE, ['time' => FALSE]); | |
173 | $this->add('datepicker', 'money_transfer_date', ts('Money Transferred'), [], FALSE, ['time' => FALSE]); | |
174 | $this->add('datepicker', 'grant_due_date', ts('Grant Report Due'), [], FALSE, ['time' => FALSE]); | |
6a488035 TO |
175 | |
176 | $this->addElement('checkbox', 'grant_report_received', ts('Grant Report Received?'), NULL); | |
177 | $this->add('textarea', 'rationale', ts('Rationale'), $attributes['rationale']); | |
178 | $this->add('text', 'amount_total', ts('Amount Requested'), NULL, TRUE); | |
179 | $this->addRule('amount_total', ts('Please enter a valid amount.'), 'money'); | |
180 | ||
181 | $this->add('text', 'amount_granted', ts('Amount Granted')); | |
182 | $this->addRule('amount_granted', ts('Please enter a valid amount.'), 'money'); | |
183 | ||
184 | $this->add('text', 'amount_requested', ts('Amount Requested<br />(original currency)')); | |
185 | $this->addRule('amount_requested', ts('Please enter a valid amount.'), 'money'); | |
186 | ||
187 | $noteAttrib = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note'); | |
188 | $this->add('textarea', 'note', ts('Notes'), $noteAttrib['note']); | |
189 | ||
190 | // add attachments part | |
191 | CRM_Core_BAO_File::buildAttachment($this, | |
192 | 'civicrm_grant', | |
193 | $this->_id | |
194 | ); | |
195 | ||
196 | // make this form an upload since we dont know if the custom data injected dynamically | |
197 | // is of type file etc $uploadNames = $this->get( 'uploadNames' ); | |
be2fb01f | 198 | $this->addButtons([ |
7e8c8317 SL |
199 | [ |
200 | 'type' => 'upload', | |
201 | 'name' => ts('Save'), | |
202 | 'isDefault' => TRUE, | |
203 | ], | |
204 | [ | |
205 | 'type' => 'upload', | |
206 | 'name' => ts('Save and New'), | |
207 | 'js' => ['onclick' => "return verify( );"], | |
208 | 'subName' => 'new', | |
209 | ], | |
210 | [ | |
211 | 'type' => 'cancel', | |
212 | 'name' => ts('Cancel'), | |
213 | ], | |
214 | ]); | |
6a488035 | 215 | |
3266bf68 | 216 | $contactField = $this->addEntityRef('contact_id', ts('Applicant'), ['create' => TRUE], TRUE); |
217 | if ($this->_context != 'standalone') { | |
218 | $contactField->freeze(); | |
6a488035 TO |
219 | } |
220 | } | |
221 | ||
6a488035 | 222 | /** |
fe482240 | 223 | * Process the form submission. |
6a488035 | 224 | * |
6a488035 | 225 | * |
355ba699 | 226 | * @return void |
6a488035 TO |
227 | */ |
228 | public function postProcess() { | |
229 | if ($this->_action & CRM_Core_Action::DELETE) { | |
230 | CRM_Grant_BAO_Grant::del($this->_id); | |
231 | return; | |
232 | } | |
233 | ||
234 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
82756a3b | 235 | $ids['grant_id'] = $this->_id; |
6a488035 TO |
236 | } |
237 | ||
238 | // get the submitted form values. | |
239 | $params = $this->controller->exportValues($this->_name); | |
240 | ||
a7488080 | 241 | if (empty($params['grant_report_received'])) { |
6a488035 TO |
242 | $params['grant_report_received'] = "null"; |
243 | } | |
244 | ||
245 | // set the contact, when contact is selected | |
5260bb5c CW |
246 | if ($this->_context == 'standalone') { |
247 | $this->_contactID = $params['contact_id']; | |
6a488035 TO |
248 | } |
249 | ||
250 | $params['contact_id'] = $this->_contactID; | |
be2fb01f | 251 | $ids['note'] = []; |
6a488035 TO |
252 | if ($this->_noteId) { |
253 | $ids['note']['id'] = $this->_noteId; | |
254 | } | |
255 | ||
256 | // build custom data getFields array | |
257 | $customFieldsGrantType = CRM_Core_BAO_CustomField::getFields('Grant', FALSE, FALSE, | |
258 | CRM_Utils_Array::value('grant_type_id', $params) | |
259 | ); | |
260 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsGrantType, | |
261 | CRM_Core_BAO_CustomField::getFields('Grant', FALSE, FALSE, NULL, NULL, TRUE) | |
262 | ); | |
263 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
6a488035 TO |
264 | $this->_id, |
265 | 'Grant' | |
266 | ); | |
267 | ||
268 | // add attachments as needed | |
269 | CRM_Core_BAO_File::formatAttachment($params, | |
270 | $params, | |
271 | 'civicrm_grant', | |
272 | $this->_id | |
273 | ); | |
274 | ||
275 | $grant = CRM_Grant_BAO_Grant::create($params, $ids); | |
276 | ||
277 | $buttonName = $this->controller->getButtonName(); | |
278 | $session = CRM_Core_Session::singleton(); | |
279 | if ($this->_context == 'standalone') { | |
280 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
281 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/grant/add', | |
353ffa53 TO |
282 | 'reset=1&action=add&context=standalone' |
283 | )); | |
6a488035 TO |
284 | } |
285 | else { | |
286 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
353ffa53 TO |
287 | "reset=1&cid={$this->_contactID}&selectedChild=grant" |
288 | )); | |
6a488035 TO |
289 | } |
290 | } | |
291 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
292 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/grant', | |
353ffa53 TO |
293 | "reset=1&action=add&context=grant&cid={$this->_contactID}" |
294 | )); | |
6a488035 TO |
295 | } |
296 | } | |
96025800 | 297 | |
6a488035 | 298 | } |