Newer
Older
ErikHommel
committed
<?php
use CRM_Streetrecruitmentimport_ExtensionUtil as E;
/**
* Class for Street Recruitment Field Mapping Form
ErikHommel
committed
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @date 31 Jan 2023
* @license AGPL-3.0
ErikHommel
committed
*/
class CRM_Streetrecruitmentimport_Form_SrMapping extends CRM_Core_Form {
private $_fpInputName = NULL;
private $_recruitingOrganizationId = NULL;
private $_fieldMappingName = NULL;
ErikHommel
committed
private $_fieldMappingDateFormat = NULL;
private $_mandateOrganizationValue = NULL;
ErikHommel
committed
private $_fpType = NULL;
/**
* Overridden method to build form
*
* @return void
* @throws CRM_Core_Exception
*/
ErikHommel
committed
public function buildQuickForm() {
$this->add('hidden', 'fp_input_name');
$this->add('hidden', 'recruiting_organization_id');
ErikHommel
committed
$this->add('hidden', 'fp_type');
$this->add('text', 'sr_fp_input_name', E::ts("Form Processor Input Field"), ['class' => 'huge', 'readonly' => 'readonly']);
$this->add('text', 'sr_mapped_field_name', E::ts('Mapped to Field'));
ErikHommel
committed
$helpTxt = E::ts("You see the name of the input in the Form Processor and you can enter the field name as it is used by the recruiting organization.");
if ($this->_fpType == 'date') {
$helpTxt .= E::ts('<br /><br />You have to enter the date format the recruiting organization uses for this field. The date mapping should always contain the characters dd, mm and yyyy for day, month and year respectively. <br />
So for example if the birth date field contains 23-11-1974 you will add dd-mm-yyyy, if it contains 1974/11/23T00:00:00 you enter yyyy/mm/dd<br /><br />
The mapping function will check the date format pattern and pick up the digits in the positions of the date format, insert "-" in between and ignore the rest. <br />
In the first example it will see the 1st and 2nd digit as day, the 4th and 5th digit as month and the 7th, 8th, 9th and 10th digit as year.<br />
In the second example it will see the 1st, 2nd, 3rd and 4th digit as year, the 6th and 7th digit as month and the 9th and 10th digit as day.');
$this->add('text', 'sr_mapped_field_date_format', E::ts("Date format for field"));
}
if ($this->_fpInputName == "mandate_organization") {
$helpTxt .= E::ts('<br /><br />You have to specify the value that means that the mandate is on the organization if the value is NOT one of: Y, y, Yes, yes, J, j , Ja, ja, 1.');
$this->add('text', 'sr_mandate_organization_value', E::ts("Value When Mandate is on Organization"));
}
ErikHommel
committed
$this->assign('help_txt', $helpTxt);
['type' => 'next', 'name' => E::ts('Save'), 'isDefault' => TRUE],
['type' => 'cancel', 'name' => E::ts('Cancel')],
]);
ErikHommel
committed
// export form elements
$this->assign('elementNames', $this->getRenderableElementNames());
parent::buildQuickForm();
}
/**
* Overridden parent method to set defaults
*
* @return array
*/
public function setDefaultValues(): array {
$defaults['fp_input_name'] = $this->_fpInputName;
ErikHommel
committed
$defaults['fp_type'] = $this->_fpType;
$defaults['sr_fp_input_name'] = $this->_fpInputName;
$defaults['recruiting_organization_id'] = $this->_recruitingOrganizationId;
if ($this->_action == CRM_Core_Action::UPDATE && !empty($this->_fieldMappingName)) {
$defaults['sr_mapped_field_name'] = $this->_fieldMappingName;
ErikHommel
committed
$defaults['sr_mapped_field_date_format'] = $this->_fieldMappingDateFormat;
$defaults['sr_mandate_organization_value'] = $this->_mandateOrganizationValue;
}
return $defaults;
}
/**
* Overridden method to prepare form
*
* @return void
* @throws CRM_Core_Exception
*/
public function preProcess() {
ErikHommel
committed
$this->_recruitingOrganizationId = CRM_Streetrecruitmentimport_BAO_SrRecruitingOrg::recruitingOrganizationPreProcess($this->_submitValues);
ErikHommel
committed
$this->_fpType = CRM_Utils_Request::retrieveValue('type', 'String');
if (!$this->_fpType && $this->_submitValues['fp_type']) {
$this->_fpType = $this->_submitValues['fp_type'];
}
if ($this->_recruitingOrganizationId) {
CRM_Utils_System::setTitle("Edit Field Mapping for " . Civi::service('aivlgeneric')->getContactDisplayName($this->_recruitingOrganizationId));
if ($this->_action == CRM_Core_Action::UPDATE) {
$fpInputName = CRM_Utils_Request::retrieveValue('id', 'String');
if (!$fpInputName && isset($this->_submitValues['fp_input_name'])) {
$fpInputName = $this->_submitValues['fp_input_name'];
}
if ($fpInputName) {
$this->_fpInputName = $fpInputName;
ErikHommel
committed
$fieldMapping = CRM_Streetrecruitmentimport_BAO_SrMapping::getFieldMapping($this->_recruitingOrganizationId, $this->_fpInputName);
if (isset($fieldMapping['sr_mapped_field_name'])) {
$this->_fieldMappingName = $fieldMapping['sr_mapped_field_name'];
}
if (isset($fieldMapping['sr_mapped_field_date_format'])) {
$this->_fieldMappingDateFormat = $fieldMapping['sr_mapped_field_date_format'];
}
if (isset($fieldMapping['sr_mandate_organization_value'])) {
$this->_mandateOrganizationValue = $fieldMapping['sr_mandate_organization_value'];
}
ErikHommel
committed
}
/**
* Overridden parent method to process form submission
*
* @return void
*/
public function postProcess() {
if ($this->_submitValues['fp_input_name']) {
$this->_fpInputName = $this->_submitValues['fp_input_name'];
}
ErikHommel
committed
if ($this->_submitValues['fp_type']) {
$this->_fpType = $this->_submitValues['fp_type'];
}
if ($this->_submitValues['recruiting_organization_id']) {
$this->_recruitingOrganizationId = $this->_submitValues['recruiting_organization_id'];
ErikHommel
committed
}
ErikHommel
committed
$fieldMappingName = $this->_submitValues['sr_mapped_field_name'];
ErikHommel
committed
if (isset($this->_submitValues['sr_mapped_field_date_format'])) {
$dateFormat = CRM_Streetrecruitmentimport_BAO_SrMapping::sanitizeDateFormatSeparator($this->_submitValues['sr_mapped_field_date_format']);
$fieldDateFormat = strtolower($dateFormat);
}
else {
$fieldDateFormat = NULL;
}
if (isset($this->_submitValues['sr_mandate_organization_value'])) {
$mandateOrganizationValue = $this->_submitValues['sr_mandate_organization_value'];
}
else {
ErikHommel
committed
$mandateOrganizationValue = NULL;
ErikHommel
committed
if (empty($fieldMappingName)) {
CRM_Streetrecruitmentimport_BAO_SrMapping::deleteFieldMapping((int) $this->_recruitingOrganizationId, $this->_fpInputName);
CRM_Streetrecruitmentimport_BAO_SrMapping::saveFieldMapping((int) $this->_recruitingOrganizationId, $this->_fpInputName, $fieldMappingName, $fieldDateFormat, $mandateOrganizationValue);
ErikHommel
committed
}
/**
* Get the fields/elements defined in this form.
*
* @return array (string)
*/
public function getRenderableElementNames() {
// The _elements list includes some items which should not be
// auto-rendered in the loop -- such as "qfKey" and "buttons". These
// items don't have labels. We'll identify renderable by filtering on
// the 'label'.
$elementNames = array();
foreach ($this->_elements as $element) {
/** @var HTML_QuickForm_Element $element */
$label = $element->getLabel();
if (!empty($label)) {
$elementNames[] = $element->getName();
}
}
return $elementNames;
}
ErikHommel
committed
/**
* Overridden parent method to add validation rules if required
*
*/
public function addRules() {
ErikHommel
committed
if (method_exists("CRM_Streetrecruitmentimport_BAO_SrMapping", "validateDateField")) {
$this->addFormRule(['CRM_Streetrecruitmentimport_BAO_SrMapping', "validateDateField"]);
ErikHommel
committed
}
}