PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/drupal/sites/all/modules/civicrm/CRM/Contribute/Form/Task/Batch.php

https://github.com/michaelmcandrew/cic
PHP | 256 lines | 135 code | 37 blank | 84 comment | 20 complexity | 47fcead28db53fb537624510973f1a69 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/Profile/Form.php';
  35. require_once 'CRM/Contribute/Form/Task.php';
  36. /**
  37. * This class provides the functionality for batch profile update for contributions
  38. */
  39. class CRM_Contribute_Form_Task_Batch extends CRM_Contribute_Form_Task {
  40. /**
  41. * the title of the group
  42. *
  43. * @var string
  44. */
  45. protected $_title;
  46. /**
  47. * maximum profile fields that will be displayed
  48. *
  49. */
  50. protected $_maxFields = 9;
  51. /**
  52. * variable to store redirect path
  53. *
  54. */
  55. protected $_userContext;
  56. /**
  57. * build all the data structures needed to build the form
  58. *
  59. * @return void
  60. * @access public
  61. */
  62. function preProcess( )
  63. {
  64. /*
  65. * initialize the task and row fields
  66. */
  67. parent::preProcess( );
  68. //get the contact read only fields to display.
  69. require_once 'CRM/Core/BAO/Preferences.php';
  70. $readOnlyFields = array_merge( array( 'sort_name' => ts( 'Name' ) ),
  71. CRM_Core_BAO_Preferences::valueOptions( 'contact_autocomplete_options',
  72. true, null, false, 'name', true ) );
  73. //get the read only field data.
  74. $returnProperties = array_fill_keys( array_keys( $readOnlyFields ), 1 );
  75. require_once 'CRM/Contact/BAO/Contact/Utils.php';
  76. $contactDetails = CRM_Contact_BAO_Contact_Utils::contactDetails( $this->_contributionIds,
  77. 'CiviContribute', $returnProperties );
  78. $this->assign( 'contactDetails', $contactDetails );
  79. $this->assign( 'readOnlyFields', $readOnlyFields );
  80. }
  81. /**
  82. * Build the form
  83. *
  84. * @access public
  85. * @return void
  86. */
  87. function buildQuickForm( )
  88. {
  89. $ufGroupId = $this->get('ufGroupId');
  90. if ( ! $ufGroupId ) {
  91. CRM_Core_Error::fatal( 'ufGroupId is missing' );
  92. }
  93. require_once "CRM/Core/BAO/UFGroup.php";
  94. require_once "CRM/Core/BAO/CustomGroup.php";
  95. $this->_title = ts('Batch Update for Contributions') . ' - ' . CRM_Core_BAO_UFGroup::getTitle ( $ufGroupId );
  96. CRM_Utils_System::setTitle( $this->_title );
  97. $this->addDefaultButtons( ts('Save') );
  98. $this->_fields = array( );
  99. $this->_fields = CRM_Core_BAO_UFGroup::getFields( $ufGroupId, false, CRM_Core_Action::VIEW );
  100. // remove file type field and then limit fields
  101. $suppressFields = false;
  102. $removehtmlTypes = array( 'File', 'Autocomplete-Select' );
  103. foreach ($this->_fields as $name => $field ) {
  104. if ( $cfID = CRM_Core_BAO_CustomField::getKeyID($name) &&
  105. in_array( $this->_fields[$name]['html_type'], $removehtmlTypes ) ) {
  106. $suppressFields = true;
  107. unset($this->_fields[$name]);
  108. }
  109. //fix to reduce size as we are using this field in grid
  110. if ( is_array( $field['attributes'] ) && $this->_fields[$name]['attributes']['size'] > 19 ) {
  111. //shrink class to "form-text-medium"
  112. $this->_fields[$name]['attributes']['size'] = 19;
  113. }
  114. }
  115. $this->_fields = array_slice($this->_fields, 0, $this->_maxFields);
  116. $this->addButtons( array(
  117. array ( 'type' => 'submit',
  118. 'name' => ts('Update Contribution(s)'),
  119. 'isDefault' => true ),
  120. array ( 'type' => 'cancel',
  121. 'name' => ts('Cancel') ),
  122. )
  123. );
  124. $this->assign( 'profileTitle', $this->_title );
  125. $this->assign( 'componentIds', $this->_contributionIds );
  126. $fileFieldExists = false;
  127. //fix for CRM-2752
  128. require_once "CRM/Core/BAO/CustomField.php";
  129. $customFields = CRM_Core_BAO_CustomField::getFields( 'Contribution' );
  130. foreach ( $this->_contributionIds as $contributionId ) {
  131. $typeId = CRM_Core_DAO::getFieldValue( "CRM_Contribute_DAO_Contribution", $contributionId, 'contribution_type_id' );
  132. foreach ( $this->_fields as $name => $field ) {
  133. if ( $customFieldID = CRM_Core_BAO_CustomField::getKeyID( $name ) ) {
  134. $customValue = CRM_Utils_Array::value( $customFieldID, $customFields );
  135. if ( ( $typeId == $customValue['extends_entity_column_value'] ) ||
  136. CRM_Utils_System::isNull( $customValue['extends_entity_column_value'] ) ) {
  137. CRM_Core_BAO_UFGroup::buildProfile( $this, $field, null, $contributionId );
  138. }
  139. } else {
  140. // handle non custom fields
  141. CRM_Core_BAO_UFGroup::buildProfile( $this, $field, null, $contributionId );
  142. }
  143. }
  144. }
  145. $this->assign( 'fields', $this->_fields );
  146. // don't set the status message when form is submitted.
  147. $buttonName = $this->controller->getButtonName('submit');
  148. if ( $suppressFields && $buttonName != '_qf_Batch_next' ) {
  149. CRM_Core_Session::setStatus( "FILE or Autocomplete Select type field(s) in the selected profile are not supported for Batch Update and have been excluded." );
  150. }
  151. $this->addDefaultButtons( ts( 'Update Contributions' ) );
  152. }
  153. /**
  154. * This function sets the default values for the form.
  155. *
  156. * @access public
  157. * @return None
  158. */
  159. function setDefaultValues( )
  160. {
  161. if (empty($this->_fields)) {
  162. return;
  163. }
  164. $defaults = array( );
  165. foreach ($this->_contributionIds as $contributionId) {
  166. $details[$contributionId] = array( );
  167. CRM_Core_BAO_UFGroup::setProfileDefaults( null, $this->_fields, $defaults, false, $contributionId, 'Contribute' );
  168. }
  169. return $defaults;
  170. }
  171. /**
  172. * process the form after the input has been submitted and validated
  173. *
  174. * @access public
  175. * @return None
  176. */
  177. public function postProcess()
  178. {
  179. $params = $this->exportValues( );
  180. $dates = array( 'receive_date',
  181. 'receipt_date',
  182. 'thankyou_date',
  183. 'cancel_date'
  184. );
  185. if ( isset( $params['field'] ) ) {
  186. foreach ( $params['field'] as $key => $value ) {
  187. $value['custom'] = CRM_Core_BAO_CustomField::postProcess( $value,
  188. CRM_Core_DAO::$_nullObject,
  189. $key,
  190. 'Contribution' );
  191. $ids['contribution'] = $key;
  192. foreach ( $dates as $val ) {
  193. $value[$val] = CRM_Utils_Date::processDate( $value[$val] );
  194. }
  195. if ($value['contribution_type']) {
  196. $value['contribution_type_id'] = $value['contribution_type'];
  197. }
  198. if ($value['payment_instrument']) {
  199. $value['payment_instrument_id'] = $value['payment_instrument'];
  200. }
  201. if ($value['contribution_source']) {
  202. $value['source'] = $value['contribution_source'];
  203. }
  204. unset($value['contribution_type']);
  205. unset($value['contribution_source']);
  206. $contribution = CRM_Contribute_BAO_Contribution::add( $value ,$ids );
  207. // add custom field values
  208. if ( CRM_Utils_Array::value( 'custom', $value ) &&
  209. is_array( $value['custom'] ) ) {
  210. require_once 'CRM/Core/BAO/CustomValueTable.php';
  211. CRM_Core_BAO_CustomValueTable::store( $value['custom'], 'civicrm_contribution', $contribution->id );
  212. }
  213. }
  214. CRM_Core_Session::setStatus("Your updates have been saved.");
  215. } else {
  216. CRM_Core_Session::setStatus("No updates have been saved.");
  217. }
  218. }//end of function
  219. }