Skip to content

Commit

Permalink
Merge pull request #17516 from eileenmcnaughton/pf_field
Browse files Browse the repository at this point in the history
Add EntityFormTrait to pricefieldForm - stdised getEntityId()
  • Loading branch information
mlutfy authored Jun 16, 2020
2 parents 02bf398 + 44374cb commit 9130c7c
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions CRM/Price/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,31 @@
*/
class CRM_Price_Form_Field extends CRM_Core_Form {

use CRM_Core_Form_EntityFormTrait;

/**
* Explicitly declare the entity api name.
*/
public function getDefaultEntity() {
return 'PriceField';
}

/**
* Explicitly declare the form context.
*/
public function getDefaultContext() {
return 'create';
}

/**
* Get the entity id being edited.
*
* @return int|null
*/
public function getEntityId() {
return $this->_fid;
}

/**
* Constants for number of options for data types of multiple option.
*/
Expand Down Expand Up @@ -57,8 +82,8 @@ class CRM_Price_Form_Field extends CRM_Core_Form {
*/
public function preProcess() {

$this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this, FALSE, NULL, 'REQUEST');
$this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', $this, FALSE, NULL, 'REQUEST');
$this->_sid = CRM_Utils_Request::retrieve('sid', 'Positive', $this);
$this->_fid = CRM_Utils_Request::retrieve('fid', 'Positive', $this);
$url = CRM_Utils_System::url('civicrm/admin/price/field', "reset=1&action=browse&sid={$this->_sid}");
$breadCrumb = [['title' => ts('Price Set Fields'), 'url' => $url]];

Expand All @@ -78,20 +103,21 @@ public function preProcess() {
*
* @return array
* array of default values
* @throws \CRM_Core_Exception
*/
public function setDefaultValues() {
$defaults = [];
// is it an edit operation ?
if (isset($this->_fid)) {
$params = ['id' => $this->_fid];
$this->assign('fid', $this->_fid);
if ($this->getEntityId()) {
$params = ['id' => $this->getEntityId()];
$this->assign('fid', $this->getEntityId());
CRM_Price_BAO_PriceField::retrieve($params, $defaults);
$this->_sid = $defaults['price_set_id'];

// if text, retrieve price
if ($defaults['html_type'] == 'Text') {
$isActive = $defaults['is_active'];
$valueParams = ['price_field_id' => $this->_fid];
$valueParams = ['price_field_id' => $this->getEntityId()];

CRM_Price_BAO_PriceFieldValue::retrieve($valueParams, $defaults);

Expand Down Expand Up @@ -142,7 +168,7 @@ public function buildQuickForm() {
// add a hidden field to remember the price set id
// this get around the browser tab issue
$this->add('hidden', 'sid', $this->_sid);
$this->add('hidden', 'fid', $this->_fid);
$this->add('hidden', 'fid', $this->getEntityId());

// label
$this->add('text', 'label', ts('Field Label'), CRM_Core_DAO::getAttribute('CRM_Price_DAO_PriceField', 'label'), TRUE);
Expand Down Expand Up @@ -364,8 +390,8 @@ public function buildQuickForm() {
* @param array $fields
* Posted values of the form.
*
* @param $files
* @param CRM_Core_Form $form
* @param array $files
* @param self $form
*
* @return array
* if errors then list of errors to be posted back to the form,
Expand All @@ -381,7 +407,7 @@ public static function formRule($fields, $files, $form) {
* Incomplete row checking is also required.
*/
if (($form->_action & CRM_Core_Action::ADD || $form->_action & CRM_Core_Action::UPDATE) &&
$fields['html_type'] == 'Text'
$fields['html_type'] === 'Text'
) {
if ($fields['price'] == NULL) {
$errors['price'] = ts('Price is a required field');
Expand All @@ -397,7 +423,7 @@ public static function formRule($fields, $files, $form) {
$priceFieldLabel->price_set_id = $form->_sid;

$dupeLabel = FALSE;
if ($priceFieldLabel->find(TRUE) && $form->_fid != $priceFieldLabel->id) {
if ($priceFieldLabel->find(TRUE) && $form->getEntityId() != $priceFieldLabel->id) {
$dupeLabel = TRUE;
}

Expand Down Expand Up @@ -630,8 +656,8 @@ public function postProcess() {
if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::ADD)) {
$fieldValues = ['price_set_id' => $this->_sid];
$oldWeight = NULL;
if ($this->_fid) {
$oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->_fid, 'weight', 'id');
if ($this->getEntityId()) {
$oldWeight = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceField', $this->getEntityId(), 'weight', 'id');
}
$params['weight'] = CRM_Utils_Weight::updateOtherWeights('CRM_Price_DAO_PriceField', $oldWeight, $params['weight'], $fieldValues);
}
Expand All @@ -657,9 +683,7 @@ public function postProcess() {
$params['option_visibility_id'] = [1 => CRM_Utils_Array::value('visibility_id', $params)];
}

if ($this->_fid) {
$params['id'] = $this->_fid;
}
$params['id'] = $this->getEntityId();

$params['membership_num_terms'] = (!empty($params['membership_type_id'])) ? CRM_Utils_Array::value('membership_num_terms', $params, 1) : NULL;

Expand Down

0 comments on commit 9130c7c

Please sign in to comment.