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