Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-12-01-14-40-22
[civicrm-core.git] / CRM / Grant / Form / Grant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 * 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 $this->setPageTitle(ts('Grant'));
94
95 if ($this->_action & CRM_Core_Action::DELETE) {
96 return;
97 }
98
99 $this->_noteId = NULL;
100 if ($this->_id) {
101 $noteDAO = new CRM_Core_BAO_Note();
102 $noteDAO->entity_table = 'civicrm_grant';
103 $noteDAO->entity_id = $this->_id;
104 if ($noteDAO->find(TRUE)) {
105 $this->_noteId = $noteDAO->id;
106 }
107 }
108
109 // when custom data is included in this page
110 if (!empty($_POST['hidden_custom'])) {
111 $this->set('type', 'Grant');
112 $this->set('subType', CRM_Utils_Array::value('grant_type_id', $_POST));
113 $this->set('entityId', $this->_id);
114 CRM_Custom_Form_CustomData::preProcess($this);
115 CRM_Custom_Form_CustomData::buildQuickForm($this);
116 CRM_Custom_Form_CustomData::setDefaultValues($this);
117 }
118 }
119
120 /**
121 * @return array
122 */
123 function setDefaultValues() {
124 if ($this->_cdType) {
125 return CRM_Custom_Form_CustomData::setDefaultValues($this);
126 }
127
128 $defaults = parent::setDefaultValues();
129
130 if ($this->_action & CRM_Core_Action::DELETE) {
131 return $defaults;
132 }
133
134 $params['id'] = $this->_id;
135 if ($this->_noteId) {
136 $defaults['note'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Note', $this->_noteId, 'note');
137 }
138 if ($this->_id) {
139 CRM_Grant_BAO_Grant::retrieve($params, $defaults);
140
141 // fix the display of the monetary value, CRM-4038
142 if (isset($defaults['amount_total'])) {
143 $defaults['amount_total'] = CRM_Utils_Money::format($defaults['amount_total'], NULL, '%a');
144 }
145 if (isset($defaults['amount_requested'])) {
146 $defaults['amount_requested'] = CRM_Utils_Money::format($defaults['amount_requested'], NULL, '%a');
147 }
148 if (isset($defaults['amount_granted'])) {
149 $defaults['amount_granted'] = CRM_Utils_Money::format($defaults['amount_granted'], NULL, '%a');
150 }
151
152 $dates = array(
153 'application_received_date',
154 'decision_date',
155 'money_transfer_date',
156 'grant_due_date',
157 );
158
159 foreach ($dates as $key) {
160 if (!empty($defaults[$key])) {
161 list($defaults[$key]) = CRM_Utils_Date::setDateDefaults($defaults[$key]);
162 }
163 }
164 }
165 else {
166 list($defaults['application_received_date']) = CRM_Utils_Date::setDateDefaults();
167 }
168
169 return $defaults;
170 }
171
172 /**
173 * Build the form object
174 *
175 * @return void
176 * @access public
177 */
178 public function buildQuickForm() {
179 if ($this->_cdType) {
180 return CRM_Custom_Form_CustomData::buildQuickForm($this);
181 }
182
183 if ($this->_action & CRM_Core_Action::DELETE) {
184 $this->addButtons(array(
185 array(
186 'type' => 'next',
187 'name' => ts('Delete'),
188 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
189 'isDefault' => TRUE,
190 ),
191 array(
192 'type' => 'cancel',
193 'name' => ts('Cancel'),
194 ),
195 )
196 );
197 return;
198 }
199
200 $attributes = CRM_Core_DAO::getAttribute('CRM_Grant_DAO_Grant');
201 $this->addSelect('grant_type_id', array('onChange' => "CRM.buildCustomData( 'Grant', this.value );"), TRUE);
202
203 //need to assign custom data type and subtype to the template
204 $this->assign('customDataType', 'Grant');
205 $this->assign('customDataSubType', $this->_grantType);
206 $this->assign('entityID', $this->_id);
207
208 $this->addSelect('status_id', array(), TRUE);
209
210 $this->addDate('application_received_date', ts('Application Received'), FALSE, array('formatType' => 'custom'));
211 $this->addDate('decision_date', ts('Grant Decision'), FALSE, array('formatType' => 'custom'));
212 $this->addDate('money_transfer_date', ts('Money Transferred'), FALSE, array('formatType' => 'custom'));
213 $this->addDate('grant_due_date', ts('Grant Report Due'), FALSE, array('formatType' => 'custom'));
214
215 $this->addElement('checkbox', 'grant_report_received', ts('Grant Report Received?'), NULL);
216 $this->add('textarea', 'rationale', ts('Rationale'), $attributes['rationale']);
217 $this->add('text', 'amount_total', ts('Amount Requested'), NULL, TRUE);
218 $this->addRule('amount_total', ts('Please enter a valid amount.'), 'money');
219
220 $this->add('text', 'amount_granted', ts('Amount Granted'));
221 $this->addRule('amount_granted', ts('Please enter a valid amount.'), 'money');
222
223 $this->add('text', 'amount_requested', ts('Amount Requested<br />(original currency)'));
224 $this->addRule('amount_requested', ts('Please enter a valid amount.'), 'money');
225
226 $noteAttrib = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note');
227 $this->add('textarea', 'note', ts('Notes'), $noteAttrib['note']);
228
229 // add attachments part
230 CRM_Core_BAO_File::buildAttachment($this,
231 'civicrm_grant',
232 $this->_id
233 );
234
235 // make this form an upload since we dont know if the custom data injected dynamically
236 // is of type file etc $uploadNames = $this->get( 'uploadNames' );
237 $this->addButtons(array(
238 array(
239 'type' => 'upload',
240 'name' => ts('Save'),
241 'isDefault' => TRUE,
242 ),
243 array(
244 'type' => 'upload',
245 'name' => ts('Save and New'),
246 'js' => array('onclick' => "return verify( );"),
247 'subName' => 'new',
248 ),
249 array(
250 'type' => 'cancel',
251 'name' => ts('Cancel'),
252 ),
253 )
254 );
255
256 if ($this->_context == 'standalone') {
257 $this->addEntityRef('contact_id', ts('Applicant'), array('create' => TRUE), TRUE);
258 }
259 }
260
261 /**
262 * Process the form submission
263 *
264 * @access public
265 *
266 * @return void
267 */
268 public function postProcess() {
269 if ($this->_action & CRM_Core_Action::DELETE) {
270 CRM_Grant_BAO_Grant::del($this->_id);
271 return;
272 }
273
274 if ($this->_action & CRM_Core_Action::UPDATE) {
275 $ids['grant_id'] = $this->_id;
276 }
277
278 // get the submitted form values.
279 $params = $this->controller->exportValues($this->_name);
280
281 if (empty($params['grant_report_received'])) {
282 $params['grant_report_received'] = "null";
283 }
284
285 // set the contact, when contact is selected
286 if ($this->_context == 'standalone') {
287 $this->_contactID = $params['contact_id'];
288 }
289
290 $params['contact_id'] = $this->_contactID;
291 $ids['note'] = array();
292 if ($this->_noteId) {
293 $ids['note']['id'] = $this->_noteId;
294 }
295
296 // build custom data getFields array
297 $customFieldsGrantType = CRM_Core_BAO_CustomField::getFields('Grant', FALSE, FALSE,
298 CRM_Utils_Array::value('grant_type_id', $params)
299 );
300 $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsGrantType,
301 CRM_Core_BAO_CustomField::getFields('Grant', FALSE, FALSE, NULL, NULL, TRUE)
302 );
303 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
304 $customFields,
305 $this->_id,
306 'Grant'
307 );
308
309 // add attachments as needed
310 CRM_Core_BAO_File::formatAttachment($params,
311 $params,
312 'civicrm_grant',
313 $this->_id
314 );
315
316 $grant = CRM_Grant_BAO_Grant::create($params, $ids);
317
318 $buttonName = $this->controller->getButtonName();
319 $session = CRM_Core_Session::singleton();
320 if ($this->_context == 'standalone') {
321 if ($buttonName == $this->getButtonName('upload', 'new')) {
322 $session->replaceUserContext(CRM_Utils_System::url('civicrm/grant/add',
323 'reset=1&action=add&context=standalone'
324 ));
325 }
326 else {
327 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
328 "reset=1&cid={$this->_contactID}&selectedChild=grant"
329 ));
330 }
331 }
332 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
333 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/grant',
334 "reset=1&action=add&context=grant&cid={$this->_contactID}"
335 ));
336 }
337 }
338 }
339