Import from SVN (r45945, r596)
[civicrm-core.git] / CRM / Grant / Form / Grant.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for processing a case
38 *
39 */
40class 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
108 if (CRM_Utils_Array::value('hidden_custom', $_POST)) {
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
118 function setDefaultValues() {
119 if ($this->_cdType) {
120 return CRM_Custom_Form_CustomData::setDefaultValues($this);
121 }
122
123 $defaults = array();
124 $defaults = parent::setDefaultValues();
125
126 if ($this->_action & CRM_Core_Action::DELETE) {
127 return $defaults;
128 }
129
130 $params['id'] = $this->_id;
131 if ($this->_noteId) {
132 $defaults['note'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Note', $this->_noteId, 'note');
133 }
134 if ($this->_id) {
135 CRM_Grant_BAO_Grant::retrieve($params, $defaults);
136
137 // fix the display of the monetary value, CRM-4038
138 if (isset($defaults['amount_total'])) {
139 $defaults['amount_total'] = CRM_Utils_Money::format($defaults['amount_total'], NULL, '%a');
140 }
141 if (isset($defaults['amount_requested'])) {
142 $defaults['amount_requested'] = CRM_Utils_Money::format($defaults['amount_requested'], NULL, '%a');
143 }
144 if (isset($defaults['amount_granted'])) {
145 $defaults['amount_granted'] = CRM_Utils_Money::format($defaults['amount_granted'], NULL, '%a');
146 }
147
148 $dates = array(
149 'application_received_date',
150 'decision_date',
151 'money_transfer_date',
152 'grant_due_date',
153 );
154
155 foreach ($dates as $key) {
156 if (CRM_Utils_Array::value($key, $defaults)) {
157 list($defaults[$key]) = CRM_Utils_Date::setDateDefaults($defaults[$key]);
158 }
159 }
160 }
161 else {
162 list($defaults['application_received_date']) = CRM_Utils_Date::setDateDefaults();
163 }
164
165 return $defaults;
166 }
167
168 /**
169 * Function to build the form
170 *
171 * @return None
172 * @access public
173 */
174 public function buildQuickForm() {
175 if ($this->_cdType) {
176 return CRM_Custom_Form_CustomData::buildQuickForm($this);
177 }
178
179 if ($this->_action & CRM_Core_Action::DELETE) {
180 $this->addButtons(array(
181 array(
182 'type' => 'next',
183 'name' => ts('Delete'),
184 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
185 'isDefault' => TRUE,
186 ),
187 array(
188 'type' => 'cancel',
189 'name' => ts('Cancel'),
190 ),
191 )
192 );
193 return;
194 }
195
196 $attributes = CRM_Core_DAO::getAttribute('CRM_Grant_DAO_Grant');
197 $grantType = CRM_Core_OptionGroup::values('grant_type');
198 $this->add('select', 'grant_type_id', ts('Grant Type'),
199 array(
200 '' => ts('- select -')) + $grantType, TRUE,
201 array('onChange' => "CRM.buildCustomData( 'Grant', this.value );")
202 );
203
204 //need to assign custom data type and subtype to the template
205 $this->assign('customDataType', 'Grant');
206 $this->assign('customDataSubType', $this->_grantType);
207 $this->assign('entityID', $this->_id);
208
209 $grantStatus = CRM_Core_OptionGroup::values('grant_status');
210 $this->add('select', 'status_id', ts('Grant Status'),
211 array(
212 '' => ts('- select -')) + $grantStatus, TRUE
213 );
214
215 $this->addDate('application_received_date', ts('Application Received'), FALSE, array('formatType' => 'custom'));
216 $this->addDate('decision_date', ts('Grant Decision'), FALSE, array('formatType' => 'custom'));
217 $this->addDate('money_transfer_date', ts('Money Transferred'), FALSE, array('formatType' => 'custom'));
218 $this->addDate('grant_due_date', ts('Grant Report Due'), FALSE, array('formatType' => 'custom'));
219
220 $this->addElement('checkbox', 'grant_report_received', ts('Grant Report Received?'), NULL);
221 $this->add('textarea', 'rationale', ts('Rationale'), $attributes['rationale']);
222 $this->add('text', 'amount_total', ts('Amount Requested'), NULL, TRUE);
223 $this->addRule('amount_total', ts('Please enter a valid amount.'), 'money');
224
225 $this->add('text', 'amount_granted', ts('Amount Granted'));
226 $this->addRule('amount_granted', ts('Please enter a valid amount.'), 'money');
227
228 $this->add('text', 'amount_requested', ts('Amount Requested<br />(original currency)'));
229 $this->addRule('amount_requested', ts('Please enter a valid amount.'), 'money');
230
231 $noteAttrib = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note');
232 $this->add('textarea', 'note', ts('Notes'), $noteAttrib['note']);
233
234 // add attachments part
235 CRM_Core_BAO_File::buildAttachment($this,
236 'civicrm_grant',
237 $this->_id
238 );
239
240 // make this form an upload since we dont know if the custom data injected dynamically
241 // is of type file etc $uploadNames = $this->get( 'uploadNames' );
242 $this->addButtons(array(
243 array(
244 'type' => 'upload',
245 'name' => ts('Save'),
246 'isDefault' => TRUE,
247 ),
248 array(
249 'type' => 'upload',
250 'name' => ts('Save and New'),
251 'js' => array('onclick' => "return verify( );"),
252 'subName' => 'new',
253 ),
254 array(
255 'type' => 'cancel',
256 'name' => ts('Cancel'),
257 ),
258 )
259 );
260
261 if ($this->_context == 'standalone') {
262 CRM_Contact_Form_NewContact::buildQuickForm($this);
263 $this->addFormRule(array('CRM_Grant_Form_Grant', 'formRule'), $this);
264 }
265 }
266
267 /**
268 * global form rule
269 *
270 * @param array $fields the input form values
271 * @param array $files the uploaded files if any
272 * @param array $options additional user data
273 *
274 * @return true if no errors, else array of errors
275 * @access public
276 * @static
277 */
278 static function formRule($fields, $files, $self) {
279 $errors = array();
280
281 //check if contact is selected in standalone mode
282 if (isset($fields['contact_select_id'][1]) && !$fields['contact_select_id'][1]) {
283 $errors['contact[1]'] = ts('Please select a contact or create new contact');
284 }
285
286 return $errors;
287 }
288
289 /**
290 * Function to process the form
291 *
292 * @access public
293 *
294 * @return None
295 */
296 public function postProcess() {
297 if ($this->_action & CRM_Core_Action::DELETE) {
298 CRM_Grant_BAO_Grant::del($this->_id);
299 return;
300 }
301
302 if ($this->_action & CRM_Core_Action::UPDATE) {
303 $ids['grant'] = $this->_id;
304 }
305
306 // get the submitted form values.
307 $params = $this->controller->exportValues($this->_name);
308
309 if (!CRM_Utils_Array::value('grant_report_received', $params)) {
310 $params['grant_report_received'] = "null";
311 }
312
313 // set the contact, when contact is selected
314 if (CRM_Utils_Array::value('contact_select_id', $params)) {
315 $this->_contactID = $params['contact_select_id'][1];
316 }
317
318 $params['contact_id'] = $this->_contactID;
319 $ids['note'] = array();
320 if ($this->_noteId) {
321 $ids['note']['id'] = $this->_noteId;
322 }
323
324 // build custom data getFields array
325 $customFieldsGrantType = CRM_Core_BAO_CustomField::getFields('Grant', FALSE, FALSE,
326 CRM_Utils_Array::value('grant_type_id', $params)
327 );
328 $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsGrantType,
329 CRM_Core_BAO_CustomField::getFields('Grant', FALSE, FALSE, NULL, NULL, TRUE)
330 );
331 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
332 $customFields,
333 $this->_id,
334 'Grant'
335 );
336
337 // add attachments as needed
338 CRM_Core_BAO_File::formatAttachment($params,
339 $params,
340 'civicrm_grant',
341 $this->_id
342 );
343
344 $grant = CRM_Grant_BAO_Grant::create($params, $ids);
345
346 $buttonName = $this->controller->getButtonName();
347 $session = CRM_Core_Session::singleton();
348 if ($this->_context == 'standalone') {
349 if ($buttonName == $this->getButtonName('upload', 'new')) {
350 $session->replaceUserContext(CRM_Utils_System::url('civicrm/grant/add',
351 'reset=1&action=add&context=standalone'
352 ));
353 }
354 else {
355 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
356 "reset=1&cid={$this->_contactID}&selectedChild=grant"
357 ));
358 }
359 }
360 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
361 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/grant',
362 "reset=1&action=add&context=grant&cid={$this->_contactID}"
363 ));
364 }
365 }
366}
367