PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Grant/Form/Grant.php

https://github.com/michaelmcandrew/cic
PHP | 332 lines | 195 code | 48 blank | 89 comment | 30 complexity | 65890c04f3fc4111373f932acf5442e1 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 3.1 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2010 |
  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. * @package CRM
  30. * @copyright CiviCRM LLC (c) 2004-2010
  31. * $Id$
  32. *
  33. */
  34. require_once 'CRM/Core/Form.php';
  35. require_once "CRM/Custom/Form/CustomData.php";
  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. * @protected
  47. */
  48. protected $_id;
  49. /**
  50. * the id of the contact associated with this contribution
  51. *
  52. * @var int
  53. * @protected
  54. */
  55. protected $_contactID;
  56. protected $_context;
  57. /**
  58. * Function to set variables up before form is built
  59. *
  60. * @return void
  61. * @access public
  62. */
  63. public function preProcess()
  64. {
  65. $this->_contactID = CRM_Utils_Request::retrieve( 'cid', 'Positive', $this );
  66. $this->_id = CRM_Utils_Request::retrieve( 'id', 'Positive', $this );
  67. $this->_context = CRM_Utils_Request::retrieve( 'context', 'String', $this );
  68. $this->assign( 'action', $this->_action );
  69. $this->assign( 'context', $this->_context );
  70. //check permission for action.
  71. if ( !CRM_Core_Permission::checkActionPermission( 'CiviGrant', $this->_action ) ) {
  72. CRM_Core_Error::fatal( ts( 'You do not have permission to access this page' ) );
  73. }
  74. if ( $this->_action & CRM_Core_Action::DELETE ) {
  75. return;
  76. }
  77. $this->_noteId =null;
  78. if ( $this->_id) {
  79. require_once 'CRM/Core/BAO/Note.php';
  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;
  85. }
  86. }
  87. //build custom data
  88. CRM_Custom_Form_Customdata::preProcess( $this, null, null, 1, 'Grant', $this->_id );
  89. }
  90. function setDefaultValues( )
  91. {
  92. $defaults = array( );
  93. $defaults = parent::setDefaultValues();
  94. if ( $this->_action & CRM_Core_Action::DELETE ) {
  95. return $defaults;
  96. }
  97. $params['id'] = $this->_id;
  98. if ( $this->_noteId ) {
  99. $defaults['note'] = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_Note', $this->_noteId, 'note' );
  100. }
  101. if ( $this->_id){
  102. CRM_Grant_BAO_Grant::retrieve( $params, $defaults);
  103. // fix the display of the monetary value, CRM-4038
  104. require_once 'CRM/Utils/Money.php';
  105. if (isset($defaults['amount_total'])) {
  106. $defaults['amount_total'] = CRM_Utils_Money::format($defaults['amount_total'], null, '%a');
  107. }
  108. if (isset($defaults['amount_requested'])) {
  109. $defaults['amount_requested'] = CRM_Utils_Money::format($defaults['amount_requested'], null, '%a');
  110. }
  111. if (isset($defaults['amount_granted'])) {
  112. $defaults['amount_granted'] = CRM_Utils_Money::format($defaults['amount_granted'], null, '%a');
  113. }
  114. $dates = array( 'application_received_date',
  115. 'decision_date',
  116. 'money_transfer_date',
  117. 'grant_due_date' );
  118. foreach( $dates as $key ) {
  119. if ( CRM_Utils_Array::value( $key, $defaults ) ) {
  120. list( $defaults[$key] ) = CRM_Utils_Date::setDateDefaults( $defaults[$key] );
  121. }
  122. }
  123. } else {
  124. require_once 'CRM/Utils/Date.php';
  125. list( $defaults['application_received_date'] ) = CRM_Utils_Date::setDateDefaults( );
  126. }
  127. // custom data set defaults
  128. $defaults += CRM_Custom_Form_Customdata::setDefaultValues( $this );
  129. return $defaults;
  130. }
  131. /**
  132. * Function to build the form
  133. *
  134. * @return None
  135. * @access public
  136. */
  137. public function buildQuickForm( )
  138. {
  139. if ( $this->_action & CRM_Core_Action::DELETE ) {
  140. $this->addButtons(array(
  141. array ( 'type' => 'next',
  142. 'name' => ts('Delete'),
  143. 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
  144. 'isDefault' => true ),
  145. array ( 'type' => 'cancel',
  146. 'name' => ts('Cancel') ),
  147. )
  148. );
  149. return;
  150. }
  151. require_once 'CRM/Core/OptionGroup.php';
  152. require_once 'CRM/Grant/BAO/Grant.php';
  153. $attributes = CRM_Core_DAO::getAttribute('CRM_Grant_DAO_Grant');
  154. $grantType = CRM_Core_OptionGroup::values( 'grant_type' );
  155. $this->add('select', 'grant_type_id', ts( 'Grant Type' ),
  156. array( '' => ts( '- select -' ) ) + $grantType , true);
  157. $grantStatus = CRM_Core_OptionGroup::values( 'grant_status' );
  158. $this->add('select', 'status_id', ts( 'Grant Status' ),
  159. array( '' => ts( '- select -' ) ) + $grantStatus , true);
  160. $this->addDate( 'application_received_date', ts('Application Received'), false, array( 'formatType' => 'custom') );
  161. $this->addDate( 'decision_date', ts('Grant Decision'), false, array( 'formatType' => 'custom') );
  162. $this->addDate( 'money_transfer_date', ts('Money Transferred'), false, array( 'formatType' => 'custom') );
  163. $this->addDate( 'grant_due_date', ts('Grant Report Due'), false, array( 'formatType' => 'custom') );
  164. $this->addElement('checkbox','grant_report_received', ts('Grant Report Received?'),null );
  165. $this->add('textarea', 'rationale', ts('Rationale'), $attributes['rationale']);
  166. $this->add( 'text', 'amount_total', ts('Amount Requested'), null, true );
  167. $this->addRule('amount_total', ts('Please enter a valid amount.'), 'money');
  168. $this->add( 'text', 'amount_granted', ts('Amount Granted') );
  169. $this->addRule('amount_granted', ts('Please enter a valid amount.'), 'money');
  170. $this->add( 'text', 'amount_requested', ts('Amount Requested<br />(original currency)') );
  171. $this->addRule('amount_requested', ts('Please enter a valid amount.'), 'money');
  172. $noteAttrib = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Note');
  173. $this->add( 'textarea', 'note', ts('Notes'), $noteAttrib['note'] );
  174. //build custom data
  175. CRM_Custom_Form_Customdata::buildQuickForm( $this );
  176. // add attachments part
  177. require_once 'CRM/Core/BAO/File.php';
  178. CRM_Core_BAO_File::buildAttachment( $this,
  179. 'civicrm_grant',
  180. $this->_id );
  181. // make this form an upload since we dont know if the custom data injected dynamically
  182. // is of type file etc $uploadNames = $this->get( 'uploadNames' );
  183. $this->addButtons(array(
  184. array ( 'type' => 'upload',
  185. 'name' => ts('Save'),
  186. 'isDefault' => true ),
  187. array ( 'type' => 'upload',
  188. 'name' => ts('Save and New'),
  189. 'js' => array( 'onclick' => "return verify( );" ),
  190. 'subName' => 'new' ),
  191. array ( 'type' => 'cancel',
  192. 'name' => ts('Cancel') ),
  193. )
  194. );
  195. if ( $this->_context == 'standalone' ) {
  196. require_once 'CRM/Contact/Form/NewContact.php';
  197. CRM_Contact_Form_NewContact::buildQuickForm( $this );
  198. $this->addFormRule( array( 'CRM_Grant_Form_Grant', 'formRule' ), $this );
  199. }
  200. }
  201. /**
  202. * global form rule
  203. *
  204. * @param array $fields the input form values
  205. * @param array $files the uploaded files if any
  206. * @param array $options additional user data
  207. *
  208. * @return true if no errors, else array of errors
  209. * @access public
  210. * @static
  211. */
  212. static function formRule( $fields, $files, $self ) {
  213. $errors = array( );
  214. if ( isset( $fields['contact_select_id'] ) && !$fields['contact_select_id'] ) {
  215. $errors['contact'] = ts('Please select a contact or create new contact');
  216. }
  217. return $errors;
  218. }
  219. /**
  220. * Function to process the form
  221. *
  222. * @access public
  223. * @return None
  224. */
  225. public function postProcess( )
  226. {
  227. if ( $this->_action & CRM_Core_Action::DELETE ) {
  228. require_once 'CRM/Grant/BAO/Grant.php';
  229. CRM_Grant_BAO_Grant::del( $this->_id );
  230. return;
  231. }
  232. if ( $this->_action & CRM_Core_Action::UPDATE ) {
  233. $ids['grant'] = $this->_id ;
  234. }
  235. // get the submitted form values.
  236. $params = $this->controller->exportValues( $this->_name );
  237. if (!$params['grant_report_received']) {
  238. $params['grant_report_received'] = "null";
  239. }
  240. // set the contact, when contact is selected
  241. if ( CRM_Utils_Array::value('contact_select_id', $params ) ) {
  242. $this->_contactID = CRM_Utils_Array::value('contact_select_id', $params);
  243. }
  244. $params['contact_id'] = $this->_contactID;
  245. $dates = array( 'application_received_date',
  246. 'decision_date',
  247. 'money_transfer_date',
  248. 'grant_due_date' );
  249. foreach ( $dates as $d ) {
  250. $params[$d] = CRM_Utils_Date::processDate( $params[$d], null, true );
  251. }
  252. $ids['note'] = array( );
  253. if ( $this->_noteId ) {
  254. $ids['note']['id'] = $this->_noteId;
  255. }
  256. // process custom data
  257. $customFields = CRM_Core_BAO_CustomField::getFields( 'Grant' );
  258. $params['custom'] = CRM_Core_BAO_CustomField::postProcess( $params,
  259. $customFields,
  260. $this->_id,
  261. 'Grant' );
  262. // add attachments as needed
  263. CRM_Core_BAO_File::formatAttachment( $params,
  264. $params,
  265. 'civicrm_grant',
  266. $this->_id );
  267. require_once 'CRM/Grant/BAO/Grant.php';
  268. $grant = CRM_Grant_BAO_Grant::create($params, $ids);
  269. $buttonName = $this->controller->getButtonName( );
  270. $session = CRM_Core_Session::singleton( );
  271. if ( $this->_context == 'standalone' ) {
  272. if ( $buttonName == $this->getButtonName( 'upload', 'new' ) ) {
  273. $session->replaceUserContext(CRM_Utils_System::url('civicrm/grant/add',
  274. 'reset=1&action=add&context=standalone') );
  275. } else {
  276. $session->replaceUserContext(CRM_Utils_System::url( 'civicrm/contact/view',
  277. "reset=1&cid={$this->_contactID}&selectedChild=grant" ) );
  278. }
  279. } else if ( $buttonName == $this->getButtonName( 'upload', 'new' ) ) {
  280. $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/grant',
  281. "reset=1&action=add&context=grant&cid={$this->_contactID}") );
  282. }
  283. }
  284. }