PageRenderTime 36ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/sites/all/modules/civicrm/CRM/Member/Form/Task/Batch.php

https://github.com/timstephenson/NatureBridge
PHP | 271 lines | 144 code | 40 blank | 87 comment | 20 complexity | 06fd86c5a691b41ccddd45dd4024fae2 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 4.0 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2011 |
  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-2011
  31. * $Id$
  32. *
  33. */
  34. require_once 'CRM/Profile/Form.php';
  35. require_once 'CRM/Member/Form/Task.php';
  36. /**
  37. * This class provides the functionality for batch profile update for members
  38. */
  39. class CRM_Member_Form_Task_Batch extends CRM_Member_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->_memberIds,
  77. 'CiviMember', $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 Members') . ' - ' . 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 Members(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->_memberIds );
  126. $fileFieldExists = false;
  127. //load all campaigns.
  128. if ( array_key_exists( 'member_campaign_id', $this->_fields ) ) {
  129. $this->_componentCampaigns = array( );
  130. CRM_Core_PseudoConstant::populate( $this->_componentCampaigns,
  131. 'CRM_Member_DAO_Membership',
  132. true, 'campaign_id', 'id',
  133. ' id IN ('. implode(' , ',array_values( $this->_memberIds ) ) .' ) ');
  134. }
  135. require_once "CRM/Core/BAO/CustomField.php";
  136. $customFields = CRM_Core_BAO_CustomField::getFields( 'Membership' );
  137. foreach ( $this->_memberIds as $memberId ) {
  138. $typeId = CRM_Core_DAO::getFieldValue( "CRM_Member_DAO_Membership", $memberId, 'membership_type_id' );
  139. foreach ( $this->_fields as $name => $field ) {
  140. if ( $customFieldID = CRM_Core_BAO_CustomField::getKeyID( $name ) ) {
  141. $customValue = CRM_Utils_Array::value( $customFieldID, $customFields );
  142. if ( CRM_Utils_Array::value( 'extends_entity_column_value', $customValue ) ) {
  143. $entityColumnValue = explode( CRM_Core_DAO::VALUE_SEPARATOR,
  144. $customValue['extends_entity_column_value'] );
  145. }
  146. if ( ( CRM_Utils_Array::value( $typeId, $entityColumnValue ) ) ||
  147. CRM_Utils_System::isNull( $entityColumnValue[$typeId] ) ) {
  148. CRM_Core_BAO_UFGroup::buildProfile( $this, $field, null, $memberId );
  149. }
  150. } else {
  151. // handle non custom fields
  152. CRM_Core_BAO_UFGroup::buildProfile( $this, $field, null, $memberId );
  153. }
  154. }
  155. }
  156. $this->assign( 'fields', $this->_fields );
  157. // don't set the status message when form is submitted.
  158. $buttonName = $this->controller->getButtonName('submit');
  159. if ( $suppressFields && $buttonName != '_qf_Batch_next' ) {
  160. 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." );
  161. }
  162. $this->addDefaultButtons( ts( 'Update Memberships' ) );
  163. }
  164. /**
  165. * This function sets the default values for the form.
  166. *
  167. * @access public
  168. * @return None
  169. */
  170. function setDefaultValues( )
  171. {
  172. if (empty($this->_fields)) {
  173. return;
  174. }
  175. $defaults = array( );
  176. foreach ($this->_memberIds as $memberId) {
  177. $details[$memberId] = array( );
  178. CRM_Core_BAO_UFGroup::setProfileDefaults( null, $this->_fields, $defaults, false, $memberId, 'Membership' );
  179. }
  180. return $defaults;
  181. }
  182. /**
  183. * process the form after the input has been submitted and validated
  184. *
  185. * @access public
  186. * @return None
  187. */
  188. public function postProcess()
  189. {
  190. $params = $this->exportValues( );
  191. if ( isset( $params['field'] ) ) {
  192. $customFields = array( );
  193. foreach ( $params['field'] as $key => $value ) {
  194. $ids['membership'] = $key;
  195. if ($value['membership_source']) {
  196. $value['source'] = $value['membership_source'];
  197. }
  198. unset($value['membership_source']);
  199. //Get the membership status
  200. $membership = new CRM_Member_BAO_Membership();
  201. $membership->id = CRM_Utils_Array::value( 'membership', $ids );
  202. $membership->find(true);
  203. $membership->free();
  204. $value['status_id'] = $membership->status_id;
  205. if ( empty( $customFields ) ) {
  206. // membership type custom data
  207. $customFields = CRM_Core_BAO_CustomField::getFields( 'Membership', false, false, $membership->membership_type_id );
  208. $customFields = CRM_Utils_Array::crmArrayMerge( $customFields,
  209. CRM_Core_BAO_CustomField::getFields( 'Membership',
  210. false, false, null, null, true ) );
  211. }
  212. //check for custom data
  213. $value['custom'] = CRM_Core_BAO_CustomField::postProcess( $params['field'][$key],
  214. $customFields,
  215. $key,
  216. 'Membership',
  217. $membership->membership_type_id);
  218. $membership = CRM_Member_BAO_Membership::add( $value ,$ids );
  219. // add custom field values
  220. if ( CRM_Utils_Array::value( 'custom', $value ) &&
  221. is_array( $value['custom'] ) ) {
  222. require_once 'CRM/Core/BAO/CustomValueTable.php';
  223. CRM_Core_BAO_CustomValueTable::store( $value['custom'], 'civicrm_membership', $membership->id );
  224. }
  225. }
  226. CRM_Core_Session::setStatus("Your updates have been saved.");
  227. } else {
  228. CRM_Core_Session::setStatus("No updates have been saved.");
  229. }
  230. }//end of function
  231. }