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