3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * This class generates form components for processing a case
24 class CRM_Grant_Form_Grant
extends CRM_Core_Form
{
27 * The id of the case that we are proceessing.
34 * The id of the contact associated with this contribution.
38 protected $_contactID;
43 * Explicitly declare the entity api name.
45 public function getDefaultEntity() {
50 * Set variables up before form is built.
54 public function preProcess() {
56 $this->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
57 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
58 $this->_grantType
= NULL;
60 $this->_grantType
= CRM_Core_DAO
::getFieldValue('CRM_Grant_DAO_Grant', $this->_id
, 'grant_type_id');
62 $this->_context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
64 $this->assign('action', $this->_action
);
65 $this->assign('context', $this->_context
);
67 //check permission for action.
68 if (!CRM_Core_Permission
::checkActionPermission('CiviGrant', $this->_action
)) {
69 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
72 $this->setPageTitle(ts('Grant'));
74 if ($this->_action
& CRM_Core_Action
::DELETE
) {
78 $this->_noteId
= NULL;
80 $noteDAO = new CRM_Core_BAO_Note();
81 $noteDAO->entity_table
= 'civicrm_grant';
82 $noteDAO->entity_id
= $this->_id
;
83 if ($noteDAO->find(TRUE)) {
84 $this->_noteId
= $noteDAO->id
;
88 // when custom data is included in this page
89 if (!empty($_POST['hidden_custom'])) {
90 $grantTypeId = empty($_POST['grant_type_id']) ?
NULL : $_POST['grant_type_id'];
91 $this->set('type', 'Grant');
92 $this->set('subType', $grantTypeId);
93 $this->set('entityId', $this->_id
);
94 CRM_Custom_Form_CustomData
::preProcess($this, NULL, $grantTypeId, 1, 'Grant', $this->_id
);
95 CRM_Custom_Form_CustomData
::buildQuickForm($this);
96 CRM_Custom_Form_CustomData
::setDefaultValues($this);
103 public function setDefaultValues() {
105 $defaults = parent
::setDefaultValues();
107 if ($this->_action
& CRM_Core_Action
::DELETE
) {
111 $params['id'] = $this->_id
;
112 if ($this->_noteId
) {
113 $defaults['note'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Note', $this->_noteId
, 'note');
116 CRM_Grant_BAO_Grant
::retrieve($params, $defaults);
118 // fix the display of the monetary value, CRM-4038
119 if (isset($defaults['amount_total'])) {
120 $defaults['amount_total'] = CRM_Utils_Money
::format($defaults['amount_total'], NULL, '%a');
122 if (isset($defaults['amount_requested'])) {
123 $defaults['amount_requested'] = CRM_Utils_Money
::format($defaults['amount_requested'], NULL, '%a');
125 if (isset($defaults['amount_granted'])) {
126 $defaults['amount_granted'] = CRM_Utils_Money
::format($defaults['amount_granted'], NULL, '%a');
130 if ($this->_contactID
) {
131 $defaults['contact_id'] = $this->_contactID
;
139 * Build the form object.
143 public function buildQuickForm() {
145 if ($this->_action
& CRM_Core_Action
::DELETE
) {
149 'name' => ts('Delete'),
150 'spacing' => ' ',
155 'name' => ts('Cancel'),
161 $attributes = CRM_Core_DAO
::getAttribute('CRM_Grant_DAO_Grant');
162 $this->addSelect('grant_type_id', ['onChange' => "CRM.buildCustomData( 'Grant', this.value );"], TRUE);
164 //need to assign custom data type and subtype to the template
165 $this->assign('customDataType', 'Grant');
166 $this->assign('customDataSubType', $this->_grantType
);
167 $this->assign('entityID', $this->_id
);
169 $this->addSelect('status_id', [], TRUE);
171 $this->add('datepicker', 'application_received_date', ts('Application Received'), [], FALSE, ['time' => FALSE]);
172 $this->add('datepicker', 'decision_date', ts('Grant Decision'), [], FALSE, ['time' => FALSE]);
173 $this->add('datepicker', 'money_transfer_date', ts('Money Transferred'), [], FALSE, ['time' => FALSE]);
174 $this->add('datepicker', 'grant_due_date', ts('Grant Report Due'), [], FALSE, ['time' => FALSE]);
176 $this->addElement('checkbox', 'grant_report_received', ts('Grant Report Received?'), NULL);
177 $this->add('textarea', 'rationale', ts('Rationale'), $attributes['rationale']);
178 $this->add('text', 'amount_total', ts('Amount Requested'), NULL, TRUE);
179 $this->addRule('amount_total', ts('Please enter a valid amount.'), 'money');
181 $this->add('text', 'amount_granted', ts('Amount Granted'));
182 $this->addRule('amount_granted', ts('Please enter a valid amount.'), 'money');
184 $this->add('text', 'amount_requested', ts('Amount Requested<br />(original currency)'));
185 $this->addRule('amount_requested', ts('Please enter a valid amount.'), 'money');
187 $noteAttrib = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Note');
188 $this->add('textarea', 'note', ts('Notes'), $noteAttrib['note']);
190 // add attachments part
191 CRM_Core_BAO_File
::buildAttachment($this,
196 // make this form an upload since we dont know if the custom data injected dynamically
197 // is of type file etc $uploadNames = $this->get( 'uploadNames' );
201 'name' => ts('Save'),
206 'name' => ts('Save and New'),
207 'js' => ['onclick' => "return verify( );"],
212 'name' => ts('Cancel'),
216 $contactField = $this->addEntityRef('contact_id', ts('Applicant'), ['create' => TRUE], TRUE);
217 if ($this->_context
!= 'standalone') {
218 $contactField->freeze();
223 * Process the form submission.
228 public function postProcess() {
229 if ($this->_action
& CRM_Core_Action
::DELETE
) {
230 CRM_Grant_BAO_Grant
::del($this->_id
);
234 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
235 $ids['grant_id'] = $this->_id
;
238 // get the submitted form values.
239 $params = $this->controller
->exportValues($this->_name
);
241 if (empty($params['grant_report_received'])) {
242 $params['grant_report_received'] = "null";
245 // set the contact, when contact is selected
246 if ($this->_context
== 'standalone') {
247 $this->_contactID
= $params['contact_id'];
250 $params['contact_id'] = $this->_contactID
;
252 if ($this->_noteId
) {
253 $ids['note']['id'] = $this->_noteId
;
256 // build custom data getFields array
257 $customFieldsGrantType = CRM_Core_BAO_CustomField
::getFields('Grant', FALSE, FALSE,
258 CRM_Utils_Array
::value('grant_type_id', $params)
260 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsGrantType,
261 CRM_Core_BAO_CustomField
::getFields('Grant', FALSE, FALSE, NULL, NULL, TRUE)
263 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
268 // add attachments as needed
269 CRM_Core_BAO_File
::formatAttachment($params,
275 $grant = CRM_Grant_BAO_Grant
::create($params, $ids);
277 $buttonName = $this->controller
->getButtonName();
278 $session = CRM_Core_Session
::singleton();
279 if ($this->_context
== 'standalone') {
280 if ($buttonName == $this->getButtonName('upload', 'new')) {
281 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/grant/add',
282 'reset=1&action=add&context=standalone'
286 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
287 "reset=1&cid={$this->_contactID}&selectedChild=grant"
291 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
292 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/grant',
293 "reset=1&action=add&context=grant&cid={$this->_contactID}"