Merge pull request #15843 from totten/master-simplehead
[civicrm-core.git] / CRM / Grant / Form / 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
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 /**
27 * The id of the case that we are proceessing.
28 *
29 * @var int
30 */
31 protected $_id;
32
33 /**
34 * The id of the contact associated with this contribution.
35 *
36 * @var int
37 */
38 protected $_contactID;
39
40 protected $_context;
41
42 /**
43 * Explicitly declare the entity api name.
44 */
45 public function getDefaultEntity() {
46 return 'Grant';
47 }
48
49 /**
50 * Set variables up before form is built.
51 *
52 * @return void
53 */
54 public function preProcess() {
55
56 $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
57 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
58 $this->_grantType = NULL;
59 if ($this->_id) {
60 $this->_grantType = CRM_Core_DAO::getFieldValue('CRM_Grant_DAO_Grant', $this->_id, 'grant_type_id');
61 }
62 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
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)) {
69 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
70 }
71
72 $this->setPageTitle(ts('Grant'));
73
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
89 if (!empty($_POST['hidden_custom'])) {
90 $grantTypeId = empty($_POST['grant_type_id']) ? NULL : $_POST['grant_type_id'];
91 $this->set('type', 'Grant');
92 $this->set('subType', $grantTypeId);
93 $this->set('entityId', $this->_id);
94 CRM_Custom_Form_CustomData::preProcess($this, NULL, $grantTypeId, 1, 'Grant', $this->_id);
95 CRM_Custom_Form_CustomData::buildQuickForm($this);
96 CRM_Custom_Form_CustomData::setDefaultValues($this);
97 }
98 }
99
100 /**
101 * @return array
102 */
103 public function setDefaultValues() {
104
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 }
128 }
129 else {
130 if ($this->_contactID) {
131 $defaults['contact_id'] = $this->_contactID;
132 }
133 }
134
135 return $defaults;
136 }
137
138 /**
139 * Build the form object.
140 *
141 * @return void
142 */
143 public function buildQuickForm() {
144
145 if ($this->_action & CRM_Core_Action::DELETE) {
146 $this->addButtons([
147 [
148 'type' => 'next',
149 'name' => ts('Delete'),
150 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
151 'isDefault' => TRUE,
152 ],
153 [
154 'type' => 'cancel',
155 'name' => ts('Cancel'),
156 ],
157 ]);
158 return;
159 }
160
161 $attributes = CRM_Core_DAO::getAttribute('CRM_Grant_DAO_Grant');
162 $this->addSelect('grant_type_id', ['onChange' => "CRM.buildCustomData( 'Grant', this.value );"], TRUE);
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
169 $this->addSelect('status_id', [], TRUE);
170
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]);
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' );
198 $this->addButtons([
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 ]);
215
216 $contactField = $this->addEntityRef('contact_id', ts('Applicant'), ['create' => TRUE], TRUE);
217 if ($this->_context != 'standalone') {
218 $contactField->freeze();
219 }
220 }
221
222 /**
223 * Process the form submission.
224 *
225 *
226 * @return void
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) {
235 $ids['grant_id'] = $this->_id;
236 }
237
238 // get the submitted form values.
239 $params = $this->controller->exportValues($this->_name);
240
241 if (empty($params['grant_report_received'])) {
242 $params['grant_report_received'] = "null";
243 }
244
245 // set the contact, when contact is selected
246 if ($this->_context == 'standalone') {
247 $this->_contactID = $params['contact_id'];
248 }
249
250 $params['contact_id'] = $this->_contactID;
251 $ids['note'] = [];
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,
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',
282 'reset=1&action=add&context=standalone'
283 ));
284 }
285 else {
286 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
287 "reset=1&cid={$this->_contactID}&selectedChild=grant"
288 ));
289 }
290 }
291 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
292 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/grant',
293 "reset=1&action=add&context=grant&cid={$this->_contactID}"
294 ));
295 }
296 }
297
298 }