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