Merge pull request #13289 from mfb/pear-mail
[civicrm-core.git] / CRM / Grant / Form / Grant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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-2019
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 */
47 protected $_id;
48
49 /**
50 * The id of the contact associated with this contribution.
51 *
52 * @var int
53 */
54 protected $_contactID;
55
56 protected $_context;
57
58 /**
59 * Explicitly declare the entity api name.
60 */
61 public function getDefaultEntity() {
62 return 'Grant';
63 }
64
65 /**
66 * Set variables up before form is built.
67 *
68 * @return void
69 */
70 public function preProcess() {
71
72 $this->_contactID = CRM_Utils_Request::retrieve('cid', 'Positive', $this);
73 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
74 $this->_grantType = NULL;
75 if ($this->_id) {
76 $this->_grantType = CRM_Core_DAO::getFieldValue('CRM_Grant_DAO_Grant', $this->_id, 'grant_type_id');
77 }
78 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $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)) {
85 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
86 }
87
88 $this->setPageTitle(ts('Grant'));
89
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
105 if (!empty($_POST['hidden_custom'])) {
106 $grantTypeId = empty($_POST['grant_type_id']) ? NULL : $_POST['grant_type_id'];
107 $this->set('type', 'Grant');
108 $this->set('subType', $grantTypeId);
109 $this->set('entityId', $this->_id);
110 CRM_Custom_Form_CustomData::preProcess($this, NULL, $grantTypeId, 1, 'Grant', $this->_id);
111 CRM_Custom_Form_CustomData::buildQuickForm($this);
112 CRM_Custom_Form_CustomData::setDefaultValues($this);
113 }
114 }
115
116 /**
117 * @return array
118 */
119 public function setDefaultValues() {
120
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
146 return $defaults;
147 }
148
149 /**
150 * Build the form object.
151 *
152 * @return void
153 */
154 public function buildQuickForm() {
155
156 if ($this->_action & CRM_Core_Action::DELETE) {
157 $this->addButtons(array(
158 array(
159 'type' => 'next',
160 'name' => ts('Delete'),
161 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
162 'isDefault' => TRUE,
163 ),
164 array(
165 'type' => 'cancel',
166 'name' => ts('Cancel'),
167 ),
168 )
169 );
170 return;
171 }
172
173 $attributes = CRM_Core_DAO::getAttribute('CRM_Grant_DAO_Grant');
174 $this->addSelect('grant_type_id', array('onChange' => "CRM.buildCustomData( 'Grant', this.value );"), TRUE);
175
176 //need to assign custom data type and subtype to the template
177 $this->assign('customDataType', 'Grant');
178 $this->assign('customDataSubType', $this->_grantType);
179 $this->assign('entityID', $this->_id);
180
181 $this->addSelect('status_id', array(), TRUE);
182
183 $this->add('datepicker', 'application_received_date', ts('Application Received'), [], FALSE, ['time' => FALSE]);
184 $this->add('datepicker', 'decision_date', ts('Grant Decision'), [], FALSE, ['time' => FALSE]);
185 $this->add('datepicker', 'money_transfer_date', ts('Money Transferred'), [], FALSE, ['time' => FALSE]);
186 $this->add('datepicker', 'grant_due_date', ts('Grant Report Due'), [], FALSE, ['time' => FALSE]);
187
188 $this->addElement('checkbox', 'grant_report_received', ts('Grant Report Received?'), NULL);
189 $this->add('textarea', 'rationale', ts('Rationale'), $attributes['rationale']);
190 $this->add('text', 'amount_total', ts('Amount Requested'), NULL, TRUE);
191 $this->addRule('amount_total', ts('Please enter a valid amount.'), 'money');
192
193 $this->add('text', 'amount_granted', ts('Amount Granted'));
194 $this->addRule('amount_granted', ts('Please enter a valid amount.'), 'money');
195
196 $this->add('text', 'amount_requested', ts('Amount Requested<br />(original currency)'));
197 $this->addRule('amount_requested', ts('Please enter a valid amount.'), 'money');
198
199 $noteAttrib = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note');
200 $this->add('textarea', 'note', ts('Notes'), $noteAttrib['note']);
201
202 // add attachments part
203 CRM_Core_BAO_File::buildAttachment($this,
204 'civicrm_grant',
205 $this->_id
206 );
207
208 // make this form an upload since we dont know if the custom data injected dynamically
209 // is of type file etc $uploadNames = $this->get( 'uploadNames' );
210 $this->addButtons(array(
211 array(
212 'type' => 'upload',
213 'name' => ts('Save'),
214 'isDefault' => TRUE,
215 ),
216 array(
217 'type' => 'upload',
218 'name' => ts('Save and New'),
219 'js' => array('onclick' => "return verify( );"),
220 'subName' => 'new',
221 ),
222 array(
223 'type' => 'cancel',
224 'name' => ts('Cancel'),
225 ),
226 )
227 );
228
229 if ($this->_context == 'standalone') {
230 $this->addEntityRef('contact_id', ts('Applicant'), array('create' => TRUE), TRUE);
231 }
232 }
233
234 /**
235 * Process the form submission.
236 *
237 *
238 * @return void
239 */
240 public function postProcess() {
241 if ($this->_action & CRM_Core_Action::DELETE) {
242 CRM_Grant_BAO_Grant::del($this->_id);
243 return;
244 }
245
246 if ($this->_action & CRM_Core_Action::UPDATE) {
247 $ids['grant_id'] = $this->_id;
248 }
249
250 // get the submitted form values.
251 $params = $this->controller->exportValues($this->_name);
252
253 if (empty($params['grant_report_received'])) {
254 $params['grant_report_received'] = "null";
255 }
256
257 // set the contact, when contact is selected
258 if ($this->_context == 'standalone') {
259 $this->_contactID = $params['contact_id'];
260 }
261
262 $params['contact_id'] = $this->_contactID;
263 $ids['note'] = array();
264 if ($this->_noteId) {
265 $ids['note']['id'] = $this->_noteId;
266 }
267
268 // build custom data getFields array
269 $customFieldsGrantType = CRM_Core_BAO_CustomField::getFields('Grant', FALSE, FALSE,
270 CRM_Utils_Array::value('grant_type_id', $params)
271 );
272 $customFields = CRM_Utils_Array::crmArrayMerge($customFieldsGrantType,
273 CRM_Core_BAO_CustomField::getFields('Grant', FALSE, FALSE, NULL, NULL, TRUE)
274 );
275 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
276 $this->_id,
277 'Grant'
278 );
279
280 // add attachments as needed
281 CRM_Core_BAO_File::formatAttachment($params,
282 $params,
283 'civicrm_grant',
284 $this->_id
285 );
286
287 $grant = CRM_Grant_BAO_Grant::create($params, $ids);
288
289 $buttonName = $this->controller->getButtonName();
290 $session = CRM_Core_Session::singleton();
291 if ($this->_context == 'standalone') {
292 if ($buttonName == $this->getButtonName('upload', 'new')) {
293 $session->replaceUserContext(CRM_Utils_System::url('civicrm/grant/add',
294 'reset=1&action=add&context=standalone'
295 ));
296 }
297 else {
298 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
299 "reset=1&cid={$this->_contactID}&selectedChild=grant"
300 ));
301 }
302 }
303 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
304 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/grant',
305 "reset=1&action=add&context=grant&cid={$this->_contactID}"
306 ));
307 }
308 }
309
310 }