3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
37 * This class generates form components for processing a case
40 class CRM_Grant_Form_Grant
extends CRM_Core_Form
{
43 * The id of the case that we are proceessing.
50 * The id of the contact associated with this contribution.
54 protected $_contactID;
59 * Explicitly declare the entity api name.
61 public function getDefaultEntity() {
66 * Set variables up before form is built.
70 public function preProcess() {
72 $this->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
73 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
74 $this->_grantType
= NULL;
76 $this->_grantType
= CRM_Core_DAO
::getFieldValue('CRM_Grant_DAO_Grant', $this->_id
, 'grant_type_id');
78 $this->_context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
80 $this->assign('action', $this->_action
);
81 $this->assign('context', $this->_context
);
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.'));
88 $this->setPageTitle(ts('Grant'));
90 if ($this->_action
& CRM_Core_Action
::DELETE
) {
94 $this->_noteId
= NULL;
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
;
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);
119 public function setDefaultValues() {
121 $defaults = parent
::setDefaultValues();
123 if ($this->_action
& CRM_Core_Action
::DELETE
) {
127 $params['id'] = $this->_id
;
128 if ($this->_noteId
) {
129 $defaults['note'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Note', $this->_noteId
, 'note');
132 CRM_Grant_BAO_Grant
::retrieve($params, $defaults);
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');
138 if (isset($defaults['amount_requested'])) {
139 $defaults['amount_requested'] = CRM_Utils_Money
::format($defaults['amount_requested'], NULL, '%a');
141 if (isset($defaults['amount_granted'])) {
142 $defaults['amount_granted'] = CRM_Utils_Money
::format($defaults['amount_granted'], NULL, '%a');
146 if ($this->_contactID
) {
147 $defaults['contact_id'] = $this->_contactID
;
155 * Build the form object.
159 public function buildQuickForm() {
161 if ($this->_action
& CRM_Core_Action
::DELETE
) {
165 'name' => ts('Delete'),
166 'spacing' => ' ',
171 'name' => ts('Cancel'),
177 $attributes = CRM_Core_DAO
::getAttribute('CRM_Grant_DAO_Grant');
178 $this->addSelect('grant_type_id', ['onChange' => "CRM.buildCustomData( 'Grant', this.value );"], TRUE);
180 //need to assign custom data type and subtype to the template
181 $this->assign('customDataType', 'Grant');
182 $this->assign('customDataSubType', $this->_grantType
);
183 $this->assign('entityID', $this->_id
);
185 $this->addSelect('status_id', [], TRUE);
187 $this->add('datepicker', 'application_received_date', ts('Application Received'), [], FALSE, ['time' => FALSE]);
188 $this->add('datepicker', 'decision_date', ts('Grant Decision'), [], FALSE, ['time' => FALSE]);
189 $this->add('datepicker', 'money_transfer_date', ts('Money Transferred'), [], FALSE, ['time' => FALSE]);
190 $this->add('datepicker', 'grant_due_date', ts('Grant Report Due'), [], FALSE, ['time' => FALSE]);
192 $this->addElement('checkbox', 'grant_report_received', ts('Grant Report Received?'), NULL);
193 $this->add('textarea', 'rationale', ts('Rationale'), $attributes['rationale']);
194 $this->add('text', 'amount_total', ts('Amount Requested'), NULL, TRUE);
195 $this->addRule('amount_total', ts('Please enter a valid amount.'), 'money');
197 $this->add('text', 'amount_granted', ts('Amount Granted'));
198 $this->addRule('amount_granted', ts('Please enter a valid amount.'), 'money');
200 $this->add('text', 'amount_requested', ts('Amount Requested<br />(original currency)'));
201 $this->addRule('amount_requested', ts('Please enter a valid amount.'), 'money');
203 $noteAttrib = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Note');
204 $this->add('textarea', 'note', ts('Notes'), $noteAttrib['note']);
206 // add attachments part
207 CRM_Core_BAO_File
::buildAttachment($this,
212 // make this form an upload since we dont know if the custom data injected dynamically
213 // is of type file etc $uploadNames = $this->get( 'uploadNames' );
217 'name' => ts('Save'),
222 'name' => ts('Save and New'),
223 'js' => ['onclick' => "return verify( );"],
228 'name' => ts('Cancel'),
232 $contactField = $this->addEntityRef('contact_id', ts('Applicant'), ['create' => TRUE], TRUE);
233 if ($this->_context
!= 'standalone') {
234 $contactField->freeze();
239 * Process the form submission.
244 public function postProcess() {
245 if ($this->_action
& CRM_Core_Action
::DELETE
) {
246 CRM_Grant_BAO_Grant
::del($this->_id
);
250 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
251 $ids['grant_id'] = $this->_id
;
254 // get the submitted form values.
255 $params = $this->controller
->exportValues($this->_name
);
257 if (empty($params['grant_report_received'])) {
258 $params['grant_report_received'] = "null";
261 // set the contact, when contact is selected
262 if ($this->_context
== 'standalone') {
263 $this->_contactID
= $params['contact_id'];
266 $params['contact_id'] = $this->_contactID
;
268 if ($this->_noteId
) {
269 $ids['note']['id'] = $this->_noteId
;
272 // build custom data getFields array
273 $customFieldsGrantType = CRM_Core_BAO_CustomField
::getFields('Grant', FALSE, FALSE,
274 CRM_Utils_Array
::value('grant_type_id', $params)
276 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsGrantType,
277 CRM_Core_BAO_CustomField
::getFields('Grant', FALSE, FALSE, NULL, NULL, TRUE)
279 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
284 // add attachments as needed
285 CRM_Core_BAO_File
::formatAttachment($params,
291 $grant = CRM_Grant_BAO_Grant
::create($params, $ids);
293 $buttonName = $this->controller
->getButtonName();
294 $session = CRM_Core_Session
::singleton();
295 if ($this->_context
== 'standalone') {
296 if ($buttonName == $this->getButtonName('upload', 'new')) {
297 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/grant/add',
298 'reset=1&action=add&context=standalone'
302 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
303 "reset=1&cid={$this->_contactID}&selectedChild=grant"
307 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
308 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/grant',
309 "reset=1&action=add&context=grant&cid={$this->_contactID}"