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