Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
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 | /** | |
43 | * the id of the case that we are proceessing | |
44 | * | |
45 | * @var int | |
46 | * @protected | |
47 | */ | |
48 | protected $_id; | |
49 | ||
50 | /** | |
51 | * the id of the contact associated with this contribution | |
52 | * | |
53 | * @var int | |
54 | * @protected | |
55 | */ | |
56 | protected $_contactID; | |
57 | ||
58 | protected $_context; | |
59 | ||
60 | /** | |
61 | * Function to set variables up before form is built | |
62 | * | |
63 | * @return void | |
64 | * @access public | |
65 | */ | |
66 | public function preProcess() { | |
67 | //custom data related code | |
68 | $this->_cdType = CRM_Utils_Array::value('type', $_GET); | |
69 | $this->assign('cdType', FALSE); | |
70 | if ($this->_cdType) { | |
71 | $this->assign('cdType', TRUE); | |
72 | CRM_Custom_Form_CustomData::preProcess($this); | |
73 | return; | |
74 | } | |
75 | ||
76 | $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this); | |
77 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
78 | $this->_grantType = NULL; | |
79 | if ($this->_id) { | |
80 | $this->_grantType = | |
81 | CRM_Core_DAO::getFieldValue('CRM_Grant_DAO_Grant', $this->_id, 'grant_type_id'); | |
82 | } | |
83 | $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); | |
84 | ||
85 | $this->assign('action', $this->_action); | |
86 | $this->assign('context', $this->_context); | |
87 | ||
88 | //check permission for action. | |
89 | if (!CRM_Core_Permission::checkActionPermission('CiviGrant', $this->_action)) { | |
90 | CRM_Core_Error::fatal(ts('You do not have permission to access this page')); | |
91 | } | |
92 | ||
93 | if ($this->_action & CRM_Core_Action::DELETE) { | |
94 | return; | |
95 | } | |
96 | ||
97 | $this->_noteId = NULL; | |
98 | if ($this->_id) { | |
99 | $noteDAO = new CRM_Core_BAO_Note(); | |
100 | $noteDAO->entity_table = 'civicrm_grant'; | |
101 | $noteDAO->entity_id = $this->_id; | |
102 | if ($noteDAO->find(TRUE)) { | |
103 | $this->_noteId = $noteDAO->id; | |
104 | } | |
105 | } | |
106 | ||
107 | // when custom data is included in this page | |
a7488080 | 108 | if (!empty($_POST['hidden_custom'])) { |
6a488035 TO |
109 | $this->set('type', 'Grant'); |
110 | $this->set('subType', CRM_Utils_Array::value('grant_type_id', $_POST)); | |
111 | $this->set('entityId', $this->_id); | |
112 | CRM_Custom_Form_CustomData::preProcess($this); | |
113 | CRM_Custom_Form_CustomData::buildQuickForm($this); | |
114 | CRM_Custom_Form_CustomData::setDefaultValues($this); | |
115 | } | |
116 | } | |
117 | ||
e0ef6999 EM |
118 | /** |
119 | * @return array | |
120 | */ | |
6a488035 TO |
121 | function setDefaultValues() { |
122 | if ($this->_cdType) { | |
123 | return CRM_Custom_Form_CustomData::setDefaultValues($this); | |
124 | } | |
125 | ||
126 | $defaults = array(); | |
127 | $defaults = parent::setDefaultValues(); | |
128 | ||
129 | if ($this->_action & CRM_Core_Action::DELETE) { | |
130 | return $defaults; | |
131 | } | |
132 | ||
133 | $params['id'] = $this->_id; | |
134 | if ($this->_noteId) { | |
135 | $defaults['note'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Note', $this->_noteId, 'note'); | |
136 | } | |
137 | if ($this->_id) { | |
138 | CRM_Grant_BAO_Grant::retrieve($params, $defaults); | |
139 | ||
140 | // fix the display of the monetary value, CRM-4038 | |
141 | if (isset($defaults['amount_total'])) { | |
142 | $defaults['amount_total'] = CRM_Utils_Money::format($defaults['amount_total'], NULL, '%a'); | |
143 | } | |
144 | if (isset($defaults['amount_requested'])) { | |
145 | $defaults['amount_requested'] = CRM_Utils_Money::format($defaults['amount_requested'], NULL, '%a'); | |
146 | } | |
147 | if (isset($defaults['amount_granted'])) { | |
148 | $defaults['amount_granted'] = CRM_Utils_Money::format($defaults['amount_granted'], NULL, '%a'); | |
149 | } | |
150 | ||
151 | $dates = array( | |
152 | 'application_received_date', | |
153 | 'decision_date', | |
154 | 'money_transfer_date', | |
155 | 'grant_due_date', | |
156 | ); | |
157 | ||
158 | foreach ($dates as $key) { | |
a7488080 | 159 | if (!empty($defaults[$key])) { |
6a488035 TO |
160 | list($defaults[$key]) = CRM_Utils_Date::setDateDefaults($defaults[$key]); |
161 | } | |
162 | } | |
163 | } | |
164 | else { | |
165 | list($defaults['application_received_date']) = CRM_Utils_Date::setDateDefaults(); | |
166 | } | |
167 | ||
168 | return $defaults; | |
169 | } | |
170 | ||
171 | /** | |
172 | * Function to build the form | |
173 | * | |
355ba699 | 174 | * @return void |
6a488035 TO |
175 | * @access public |
176 | */ | |
177 | public function buildQuickForm() { | |
178 | if ($this->_cdType) { | |
179 | return CRM_Custom_Form_CustomData::buildQuickForm($this); | |
180 | } | |
181 | ||
182 | if ($this->_action & CRM_Core_Action::DELETE) { | |
183 | $this->addButtons(array( | |
184 | array( | |
185 | 'type' => 'next', | |
186 | 'name' => ts('Delete'), | |
187 | 'spacing' => ' ', | |
188 | 'isDefault' => TRUE, | |
189 | ), | |
190 | array( | |
191 | 'type' => 'cancel', | |
192 | 'name' => ts('Cancel'), | |
193 | ), | |
194 | ) | |
195 | ); | |
196 | return; | |
197 | } | |
198 | ||
199 | $attributes = CRM_Core_DAO::getAttribute('CRM_Grant_DAO_Grant'); | |
0f352c3a | 200 | $this->addSelect('grant_type_id', array('onChange' => "CRM.buildCustomData( 'Grant', this.value );"), TRUE); |
6a488035 TO |
201 | |
202 | //need to assign custom data type and subtype to the template | |
203 | $this->assign('customDataType', 'Grant'); | |
204 | $this->assign('customDataSubType', $this->_grantType); | |
205 | $this->assign('entityID', $this->_id); | |
206 | ||
0f352c3a | 207 | $this->addSelect('status_id', array(), TRUE); |
6a488035 TO |
208 | |
209 | $this->addDate('application_received_date', ts('Application Received'), FALSE, array('formatType' => 'custom')); | |
210 | $this->addDate('decision_date', ts('Grant Decision'), FALSE, array('formatType' => 'custom')); | |
211 | $this->addDate('money_transfer_date', ts('Money Transferred'), FALSE, array('formatType' => 'custom')); | |
212 | $this->addDate('grant_due_date', ts('Grant Report Due'), FALSE, array('formatType' => 'custom')); | |
213 | ||
214 | $this->addElement('checkbox', 'grant_report_received', ts('Grant Report Received?'), NULL); | |
215 | $this->add('textarea', 'rationale', ts('Rationale'), $attributes['rationale']); | |
216 | $this->add('text', 'amount_total', ts('Amount Requested'), NULL, TRUE); | |
217 | $this->addRule('amount_total', ts('Please enter a valid amount.'), 'money'); | |
218 | ||
219 | $this->add('text', 'amount_granted', ts('Amount Granted')); | |
220 | $this->addRule('amount_granted', ts('Please enter a valid amount.'), 'money'); | |
221 | ||
222 | $this->add('text', 'amount_requested', ts('Amount Requested<br />(original currency)')); | |
223 | $this->addRule('amount_requested', ts('Please enter a valid amount.'), 'money'); | |
224 | ||
225 | $noteAttrib = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note'); | |
226 | $this->add('textarea', 'note', ts('Notes'), $noteAttrib['note']); | |
227 | ||
228 | // add attachments part | |
229 | CRM_Core_BAO_File::buildAttachment($this, | |
230 | 'civicrm_grant', | |
231 | $this->_id | |
232 | ); | |
233 | ||
234 | // make this form an upload since we dont know if the custom data injected dynamically | |
235 | // is of type file etc $uploadNames = $this->get( 'uploadNames' ); | |
236 | $this->addButtons(array( | |
237 | array( | |
238 | 'type' => 'upload', | |
239 | 'name' => ts('Save'), | |
240 | 'isDefault' => TRUE, | |
241 | ), | |
242 | array( | |
243 | 'type' => 'upload', | |
244 | 'name' => ts('Save and New'), | |
245 | 'js' => array('onclick' => "return verify( );"), | |
246 | 'subName' => 'new', | |
247 | ), | |
248 | array( | |
249 | 'type' => 'cancel', | |
250 | 'name' => ts('Cancel'), | |
251 | ), | |
252 | ) | |
253 | ); | |
254 | ||
255 | if ($this->_context == 'standalone') { | |
5260bb5c | 256 | $this->addEntityRef('contact_id', ts('Applicant'), array('create' => TRUE), TRUE); |
6a488035 TO |
257 | } |
258 | } | |
259 | ||
6a488035 TO |
260 | /** |
261 | * Function to process the form | |
262 | * | |
263 | * @access public | |
264 | * | |
355ba699 | 265 | * @return void |
6a488035 TO |
266 | */ |
267 | public function postProcess() { | |
268 | if ($this->_action & CRM_Core_Action::DELETE) { | |
269 | CRM_Grant_BAO_Grant::del($this->_id); | |
270 | return; | |
271 | } | |
272 | ||
273 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
82756a3b | 274 | $ids['grant_id'] = $this->_id; |
6a488035 TO |
275 | } |
276 | ||
277 | // get the submitted form values. | |
278 | $params = $this->controller->exportValues($this->_name); | |
279 | ||
a7488080 | 280 | if (empty($params['grant_report_received'])) { |
6a488035 TO |
281 | $params['grant_report_received'] = "null"; |
282 | } | |
283 | ||
284 | // set the contact, when contact is selected | |
5260bb5c CW |
285 | if ($this->_context == 'standalone') { |
286 | $this->_contactID = $params['contact_id']; | |
6a488035 TO |
287 | } |
288 | ||
289 | $params['contact_id'] = $this->_contactID; | |
290 | $ids['note'] = array(); | |
291 | if ($this->_noteId) { | |
292 | $ids['note']['id'] = $this->_noteId; | |
293 | } | |
294 | ||
295 | // build custom data getFields array | |
296 | $customFieldsGrantType = CRM_Core_BAO_CustomField::getFields('Grant', FALSE, FALSE, | |
297 | CRM_Utils_Array::value('grant_type_id', $params) | |
298 | ); | |
299 | $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsGrantType, | |
300 | CRM_Core_BAO_CustomField::getFields('Grant', FALSE, FALSE, NULL, NULL, TRUE) | |
301 | ); | |
302 | $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, | |
303 | $customFields, | |
304 | $this->_id, | |
305 | 'Grant' | |
306 | ); | |
307 | ||
308 | // add attachments as needed | |
309 | CRM_Core_BAO_File::formatAttachment($params, | |
310 | $params, | |
311 | 'civicrm_grant', | |
312 | $this->_id | |
313 | ); | |
314 | ||
315 | $grant = CRM_Grant_BAO_Grant::create($params, $ids); | |
316 | ||
317 | $buttonName = $this->controller->getButtonName(); | |
318 | $session = CRM_Core_Session::singleton(); | |
319 | if ($this->_context == 'standalone') { | |
320 | if ($buttonName == $this->getButtonName('upload', 'new')) { | |
321 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/grant/add', | |
322 | 'reset=1&action=add&context=standalone' | |
323 | )); | |
324 | } | |
325 | else { | |
326 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', | |
327 | "reset=1&cid={$this->_contactID}&selectedChild=grant" | |
328 | )); | |
329 | } | |
330 | } | |
331 | elseif ($buttonName == $this->getButtonName('upload', 'new')) { | |
332 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/grant', | |
333 | "reset=1&action=add&context=grant&cid={$this->_contactID}" | |
334 | )); | |
335 | } | |
336 | } | |
337 | } | |
338 |