3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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
51 * the id of the contact associated with this contribution
56 protected $_contactID;
61 * Function to set variables up before form is built
66 public function preProcess() {
67 //custom data related code
68 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
69 $this->assign('cdType', FALSE);
71 $this->assign('cdType', TRUE);
72 CRM_Custom_Form_CustomData
::preProcess($this);
76 $this->_contactID
= CRM_Utils_Request
::retrieve('cid', 'Positive', $this);
77 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
78 $this->_grantType
= NULL;
81 CRM_Core_DAO
::getFieldValue('CRM_Grant_DAO_Grant', $this->_id
, 'grant_type_id');
83 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
85 $this->assign('action', $this->_action
);
86 $this->assign('context', $this->_context
);
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'));
93 if ($this->_action
& CRM_Core_Action
::DELETE
) {
97 $this->_noteId
= NULL;
99 $noteDAO = new CRM_Core_BAO_Note();
100 $noteDAO->entity_table
= 'civicrm_grant';
101 $noteDAO->entity_id
= $this->_id
;
102 if ($noteDAO->find(TRUE)) {
103 $this->_noteId
= $noteDAO->id
;
107 // when custom data is included in this page
108 if (!empty($_POST['hidden_custom'])) {
109 $this->set('type', 'Grant');
110 $this->set('subType', CRM_Utils_Array
::value('grant_type_id', $_POST));
111 $this->set('entityId', $this->_id
);
112 CRM_Custom_Form_CustomData
::preProcess($this);
113 CRM_Custom_Form_CustomData
::buildQuickForm($this);
114 CRM_Custom_Form_CustomData
::setDefaultValues($this);
118 function setDefaultValues() {
119 if ($this->_cdType
) {
120 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
124 $defaults = parent
::setDefaultValues();
126 if ($this->_action
& CRM_Core_Action
::DELETE
) {
130 $params['id'] = $this->_id
;
131 if ($this->_noteId
) {
132 $defaults['note'] = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Note', $this->_noteId
, 'note');
135 CRM_Grant_BAO_Grant
::retrieve($params, $defaults);
137 // fix the display of the monetary value, CRM-4038
138 if (isset($defaults['amount_total'])) {
139 $defaults['amount_total'] = CRM_Utils_Money
::format($defaults['amount_total'], NULL, '%a');
141 if (isset($defaults['amount_requested'])) {
142 $defaults['amount_requested'] = CRM_Utils_Money
::format($defaults['amount_requested'], NULL, '%a');
144 if (isset($defaults['amount_granted'])) {
145 $defaults['amount_granted'] = CRM_Utils_Money
::format($defaults['amount_granted'], NULL, '%a');
149 'application_received_date',
151 'money_transfer_date',
155 foreach ($dates as $key) {
156 if (!empty($defaults[$key])) {
157 list($defaults[$key]) = CRM_Utils_Date
::setDateDefaults($defaults[$key]);
162 list($defaults['application_received_date']) = CRM_Utils_Date
::setDateDefaults();
169 * Function to build the form
174 public function buildQuickForm() {
175 if ($this->_cdType
) {
176 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
179 if ($this->_action
& CRM_Core_Action
::DELETE
) {
180 $this->addButtons(array(
183 'name' => ts('Delete'),
184 'spacing' => ' ',
189 'name' => ts('Cancel'),
196 $attributes = CRM_Core_DAO
::getAttribute('CRM_Grant_DAO_Grant');
197 $this->addSelect('grant_type_id', array('onChange' => "CRM.buildCustomData( 'Grant', this.value );"), TRUE);
199 //need to assign custom data type and subtype to the template
200 $this->assign('customDataType', 'Grant');
201 $this->assign('customDataSubType', $this->_grantType
);
202 $this->assign('entityID', $this->_id
);
204 $this->addSelect('status_id', array(), TRUE);
206 $this->addDate('application_received_date', ts('Application Received'), FALSE, array('formatType' => 'custom'));
207 $this->addDate('decision_date', ts('Grant Decision'), FALSE, array('formatType' => 'custom'));
208 $this->addDate('money_transfer_date', ts('Money Transferred'), FALSE, array('formatType' => 'custom'));
209 $this->addDate('grant_due_date', ts('Grant Report Due'), FALSE, array('formatType' => 'custom'));
211 $this->addElement('checkbox', 'grant_report_received', ts('Grant Report Received?'), NULL);
212 $this->add('textarea', 'rationale', ts('Rationale'), $attributes['rationale']);
213 $this->add('text', 'amount_total', ts('Amount Requested'), NULL, TRUE);
214 $this->addRule('amount_total', ts('Please enter a valid amount.'), 'money');
216 $this->add('text', 'amount_granted', ts('Amount Granted'));
217 $this->addRule('amount_granted', ts('Please enter a valid amount.'), 'money');
219 $this->add('text', 'amount_requested', ts('Amount Requested<br />(original currency)'));
220 $this->addRule('amount_requested', ts('Please enter a valid amount.'), 'money');
222 $noteAttrib = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_Note');
223 $this->add('textarea', 'note', ts('Notes'), $noteAttrib['note']);
225 // add attachments part
226 CRM_Core_BAO_File
::buildAttachment($this,
231 // make this form an upload since we dont know if the custom data injected dynamically
232 // is of type file etc $uploadNames = $this->get( 'uploadNames' );
233 $this->addButtons(array(
236 'name' => ts('Save'),
241 'name' => ts('Save and New'),
242 'js' => array('onclick' => "return verify( );"),
247 'name' => ts('Cancel'),
252 if ($this->_context
== 'standalone') {
253 $this->addEntityRef('contact_id', ts('Applicant'), array('create' => TRUE), TRUE);
258 * Function to process the form
264 public function postProcess() {
265 if ($this->_action
& CRM_Core_Action
::DELETE
) {
266 CRM_Grant_BAO_Grant
::del($this->_id
);
270 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
271 $ids['grant_id'] = $this->_id
;
274 // get the submitted form values.
275 $params = $this->controller
->exportValues($this->_name
);
277 if (empty($params['grant_report_received'])) {
278 $params['grant_report_received'] = "null";
281 // set the contact, when contact is selected
282 if ($this->_context
== 'standalone') {
283 $this->_contactID
= $params['contact_id'];
286 $params['contact_id'] = $this->_contactID
;
287 $ids['note'] = array();
288 if ($this->_noteId
) {
289 $ids['note']['id'] = $this->_noteId
;
292 // build custom data getFields array
293 $customFieldsGrantType = CRM_Core_BAO_CustomField
::getFields('Grant', FALSE, FALSE,
294 CRM_Utils_Array
::value('grant_type_id', $params)
296 $customFields = CRM_Utils_Array
::crmArrayMerge($customFieldsGrantType,
297 CRM_Core_BAO_CustomField
::getFields('Grant', FALSE, FALSE, NULL, NULL, TRUE)
299 $params['custom'] = CRM_Core_BAO_CustomField
::postProcess($params,
305 // add attachments as needed
306 CRM_Core_BAO_File
::formatAttachment($params,
312 $grant = CRM_Grant_BAO_Grant
::create($params, $ids);
314 $buttonName = $this->controller
->getButtonName();
315 $session = CRM_Core_Session
::singleton();
316 if ($this->_context
== 'standalone') {
317 if ($buttonName == $this->getButtonName('upload', 'new')) {
318 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/grant/add',
319 'reset=1&action=add&context=standalone'
323 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view',
324 "reset=1&cid={$this->_contactID}&selectedChild=grant"
328 elseif ($buttonName == $this->getButtonName('upload', 'new')) {
329 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/contact/view/grant',
330 "reset=1&action=add&context=grant&cid={$this->_contactID}"