$value) {
if (array_key_exists( $key, $profiles)) {
unset($profiles[$key]);
}
}
if (empty($profiles)) {
$this->assign('noProfile', TRUE);
}
$this->add('select', 'custom_pre_id', ts('Include Profile') . '
' . ts('(top of page)'), array('' => ts('- select -')) + $profiles);
$this->add('select', 'custom_post_id', ts('Include Profile') . '
' . ts('(bottom of page)'), array('' => ts('- select -')) + $profiles);
$this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Custom', 'formRule'), $this->_id);
parent::buildQuickForm();
}
/**
* This function sets the default values for the form. Note that in edit/view mode
* the default values are retrieved from the database
*
* @access public
*
* @return void
*/
function setDefaultValues() {
$defaults = parent::setDefaultValues();
if ($this->_id) {
$title = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $this->_id, 'title');
CRM_Utils_System::setTitle(ts('Include Profiles (%1)', array(1 => $title)));
}
$ufJoinParams = array(
'module' => 'CiviContribute',
'entity_table' => 'civicrm_contribution_page',
'entity_id' => $this->_id,
);
list($defaults['custom_pre_id'],
$defaults['custom_post_id']
) = CRM_Core_BAO_UFJoin::getUFGroupIds($ufJoinParams);
return $defaults;
}
/**
* Process the form
*
* @return void
* @access public
*/
public function postProcess() {
// get the submitted form values.
$params = $this->controller->exportValues($this->_name);
if ($this->_action & CRM_Core_Action::UPDATE) {
$params['id'] = $this->_id;
}
$transaction = new CRM_Core_Transaction();
// also update uf join table
$ufJoinParams = array(
'is_active' => 1,
'module' => 'CiviContribute',
'entity_table' => 'civicrm_contribution_page',
'entity_id' => $this->_id,
);
// first delete all past entries
CRM_Core_BAO_UFJoin::deleteAll($ufJoinParams);
if (!empty($params['custom_pre_id'])) {
$ufJoinParams['weight'] = 1;
$ufJoinParams['uf_group_id'] = $params['custom_pre_id'];
CRM_Core_BAO_UFJoin::create($ufJoinParams);
}
unset($ufJoinParams['id']);
if (!empty($params['custom_post_id'])) {
$ufJoinParams['weight'] = 2;
$ufJoinParams['uf_group_id'] = $params['custom_post_id'];
CRM_Core_BAO_UFJoin::create($ufJoinParams);
}
$transaction->commit();
parent::endPostProcess();
}
/**
* Return a descriptive name for the page, used in wizard header
*
* @return string
* @access public
*/
public function getTitle() {
return ts('Include Profiles');
}
/**
* global form rule
*
* @param array $fields the input form values
*
* @return true if no errors, else array of errors
* @access public
* @static
*/
static function formRule($fields, $files, $contributionPageId) {
$errors = array();
$preProfileType = $postProfileType = NULL;
// for membership profile make sure Membership section is enabled
// get membership section for this contribution page
$dao = new CRM_Member_DAO_MembershipBlock();
$dao->entity_table = 'civicrm_contribution_page';
$dao->entity_id = $contributionPageId;
$membershipEnable = FALSE;
if ($dao->find(TRUE) && $dao->is_active) {
$membershipEnable = TRUE;
}
if ($fields['custom_pre_id']) {
$preProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_pre_id']);
}
if ($fields['custom_post_id']) {
$postProfileType = CRM_Core_BAO_UFField::getProfileType($fields['custom_post_id']);
}
$errorMsg = ts('You must enable the Membership Block for this Contribution Page if you want to include a Profile with Membership fields.');
if (($preProfileType == 'Membership') && !$membershipEnable) {
$errors['custom_pre_id'] = $errorMsg;
}
if (($postProfileType == 'Membership') && !$membershipEnable) {
$errors['custom_post_id'] = $errorMsg;
}
$behalf = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_ContributionPage', $contributionPageId, 'is_for_organization');
if ($fields['custom_pre_id']) {
$errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
if ($preProfileType == 'Membership' && $behalf) {
$errors['custom_pre_id'] = isset($errors['custom_pre_id']) ? $errors['custom_pre_id'] . $errorMsg : $errorMsg;
}
}
if ($fields['custom_post_id']) {
$errorMsg = ts('You should move the membership related fields in the "On Behalf" profile for this Contribution Page');
if ($postProfileType == 'Membership' && $behalf) {
$errors['custom_post_id'] = isset($errors['custom_post_id']) ? $errors['custom_post_id'] . $errorMsg : $errorMsg;
}
}
return empty($errors) ? TRUE : $errors;
}
}