Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Consolidate code to add custom fields to form #11910

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions CRM/Contribute/Form/AbstractEditPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*
*/
class CRM_Contribute_Form_AbstractEditPayment extends CRM_Contact_Form_Task {
use CRM_Custom_Form_CustomDataTrait;
public $_mode;

public $_action;
Expand Down Expand Up @@ -302,22 +303,6 @@ public function buildValuesAndAssignOnline_Note_Type($id, &$values) {
$this->_contributionType = $values['financial_type_id'];
}

/**
* @param string $type
* Eg 'Contribution'.
* @param string $subType
* @param int $entityId
*/
public function applyCustomData($type, $subType, $entityId) {
$this->set('type', $type);
$this->set('subType', $subType);
$this->set('entityId', $entityId);

CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, 1, $type, $entityId);
CRM_Custom_Form_CustomData::buildQuickForm($this);
CRM_Custom_Form_CustomData::setDefaultValues($this);
}

/**
* @param int $id
* @todo - this function is a long way, non standard of saying $dao = new CRM_Contribute_DAO_ContributionProduct(); $dao->id = $id; $dao->find();
Expand Down
10 changes: 1 addition & 9 deletions CRM/Contribute/Form/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,7 @@ public function preProcess() {
$this->buildValuesAndAssignOnline_Note_Type($this->_id, $this->_values);
}

// when custom data is included in this page
if (!empty($_POST['hidden_custom'])) {
$this->applyCustomData('Contribution', CRM_Utils_Array::value('financial_type_id', $_POST), $this->_id);
}
$this->addCustomDataToFormIfSubmitted('Contribution', CRM_Utils_Array::value('financial_type_id', $_POST), $this->_id);

$this->_lineItems = array();
if ($this->_id) {
Expand Down Expand Up @@ -601,11 +598,6 @@ public function buildQuickForm() {

$this->applyFilter('__ALL__', 'trim');

//need to assign custom data type and subtype to the template
$this->assign('customDataType', 'Contribution');
$this->assign('customDataSubType', $this->_contributionType);
$this->assign('entityID', $this->_id);

if ($this->_context == 'standalone') {
$this->addEntityRef('contact_id', ts('Contact'), array(
'create' => TRUE,
Expand Down
77 changes: 77 additions & 0 deletions CRM/Custom/Form/CustomDataTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2018 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2018
*/

trait CRM_Custom_Form_CustomDataTrait {

/**
* Apply custom data to the form.
*
* @param string $type Eg 'Contact'.
* @param string $subType Eg 'Individual'
* @param int $entityId
*
* @throws \CRM_Core_Exception
*/
public function applyCustomData($type, $subType, $entityId) {
$this->set('type', $type);
$this->set('subType', $subType);
$this->set('entityId', $entityId);

$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
CRM_Custom_Form_CustomData::preProcess($this, NULL, $subType, 1, $type, $entityId);
CRM_Custom_Form_CustomData::buildQuickForm($this);
CRM_Custom_Form_CustomData::setDefaultValues($this);

// Assign custom data type and subtype to the template so that custom data edit elements can be built via javascript/ajax.
$this->assign('customDataType', $type);
$this->assign('customDataSubType', $subType);
$this->assign('entityID', $entityId);
}

/**
* If the custom data is in the submitted data (eg. added via ajax loaded form) add to form.
*
* @param string $type Eg 'Contact'.
* @param string $subType Eg 'Individual'
* @param int $entityID
*
* @throws \CRM_Core_Exception
*/
public function addCustomDataToFormIfSubmitted($entity, $subType, $entityID) {
// when custom data is included in this page
if (!empty($_POST['hidden_custom'])) {
$this->applyCustomData($entity, $subType, $entityID);
}
}

}
8 changes: 2 additions & 6 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,8 @@ public function preProcess() {
$this->_memType = $params['membership_type_id'][1];
}
}
// when custom data is included in this page
if (!empty($_POST['hidden_custom'])) {
CRM_Custom_Form_CustomData::preProcess($this, NULL, $this->_memType, 1, 'Membership', $this->_id);
CRM_Custom_Form_CustomData::buildQuickForm($this);
CRM_Custom_Form_CustomData::setDefaultValues($this);
}

$this->addCustomDataToFormIfSubmitted('Membership', $this->_memType, $this->_id);

// CRM-4395, get the online pending contribution id.
$this->_onlinePendingContributionId = NULL;
Expand Down