Added extension to add address format for country
This commit is contained in:
23
CRM/AddressFormat/APIWrapper.php
Normal file
23
CRM/AddressFormat/APIWrapper.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
class CRM_AddressFormat_APIWrapper implements API_Wrapper {
|
||||
/**
|
||||
* the wrapper contains a method that allows you to alter the parameters of the api request (including the action and the entity)
|
||||
*/
|
||||
public function fromApiInput($apiRequest) {
|
||||
return $apiRequest;
|
||||
}
|
||||
|
||||
/**
|
||||
* alter the result before returning it to the caller.
|
||||
*/
|
||||
public function toApiOutput($apiRequest, $result) {
|
||||
if (isset($result['id'])) {
|
||||
$countryId = CRM_Utils_Array::value('country_id', $apiRequest['params']);
|
||||
if ($countryId) {
|
||||
CRM_AddressFormat_BAO_CountryAddressFormat::updateCountry($countryId, $result['id']);
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
96
CRM/AddressFormat/BAO/CountryAddressFormat.php
Normal file
96
CRM/AddressFormat/BAO/CountryAddressFormat.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
class CRM_AddressFormat_BAO_CountryAddressFormat extends CRM_Core_DAO {
|
||||
/**
|
||||
* Get list of all country having address format as NULL
|
||||
* and also include country for addres format id
|
||||
*
|
||||
* @param int $addressId
|
||||
*/
|
||||
public static function getCountryList($addressId) {
|
||||
$additonalWhereClause = $addressId ? " OR cc.address_format_id = {$addressId}" : '';
|
||||
$query = "SELECT cc.id, cc.name
|
||||
FROM civicrm_country cc
|
||||
WHERE cc.address_format_id IS NULL {$additonalWhereClause}
|
||||
ORDER BY name
|
||||
";
|
||||
$countryList = [];
|
||||
$dao = CRM_Core_DAO::executeQuery($query);
|
||||
while ($dao->fetch()) {
|
||||
$countryList[$dao->id] = $dao->name;
|
||||
}
|
||||
return $countryList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process form submission.
|
||||
*
|
||||
* @param array $submitValues
|
||||
* @param int $addressId
|
||||
*/
|
||||
public static function processForm($submitValues, $addressId) {
|
||||
$addressFormatParams = [
|
||||
'format' => $submitValues['format'],
|
||||
];
|
||||
if ($addressId) {
|
||||
$addressFormatParams['id'] = $addressId;
|
||||
}
|
||||
$addressFormat = civicrm_api3('AddressFormat', 'create', $addressFormatParams);
|
||||
if (empty($addressFormat['is_error'])) {
|
||||
self::updateCountry($submitValues['country_id'], $addressFormat['id']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set address format id for country.
|
||||
*
|
||||
* @param int $countryId
|
||||
* @param int $addressFormatId
|
||||
*/
|
||||
public static function updateCountry($countryId, $addressFormatId) {
|
||||
self::updatePreviousCountry($addressFormatId);
|
||||
$params = [
|
||||
'id' => $countryId,
|
||||
'address_format_id' => $addressFormatId,
|
||||
];
|
||||
civicrm_api3('Country', 'create', $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete address format and update country.
|
||||
*
|
||||
* @param int $addressFormatId
|
||||
*/
|
||||
public static function deleteAddressFormat($addressFormatId) {
|
||||
try {
|
||||
self::updatePreviousCountry($addressFormatId);
|
||||
return civicrm_api3('AddressFormat', 'delete', ['id' => $addressFormatId]);
|
||||
}
|
||||
catch (CiviCRM_API3_Exception $e) {
|
||||
return ['is_error' => TRUE, 'error_message' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set NULL to Address format for Country.
|
||||
*
|
||||
* @param int $addressFormatId
|
||||
*/
|
||||
public static function updatePreviousCountry($addressFormatId) {
|
||||
try {
|
||||
$countryId = civicrm_api3('Country', 'getvalue', [
|
||||
'return' => "id",
|
||||
'address_format_id' => $addressFormatId,
|
||||
]);
|
||||
}
|
||||
catch (CiviCRM_API3_Exception $e) {
|
||||
return NULL;
|
||||
}
|
||||
$params = [
|
||||
'id' => $countryId,
|
||||
'address_format_id' => '',
|
||||
];
|
||||
civicrm_api3('Country', 'create', $params);
|
||||
}
|
||||
|
||||
}
|
127
CRM/AddressFormat/Form/Address.php
Normal file
127
CRM/AddressFormat/Form/Address.php
Normal file
@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This class generates form components for Address Format by Country
|
||||
*/
|
||||
class CRM_AddressFormat_Form_Address extends CRM_Core_Form {
|
||||
|
||||
/**
|
||||
* Set variables up before form is built.
|
||||
*/
|
||||
public function preProcess() {
|
||||
parent::preProcess();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the form object.
|
||||
*/
|
||||
public function buildQuickForm() {
|
||||
parent::buildQuickForm();
|
||||
$this->setPageTitle(ts('Country - Address Format'));
|
||||
|
||||
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
|
||||
if ($this->_action & CRM_Core_Action::DELETE) {
|
||||
$this->addButtons([
|
||||
[
|
||||
'type' => 'next',
|
||||
'name' => ts('Delete'),
|
||||
'isDefault' => TRUE,
|
||||
],
|
||||
[
|
||||
'type' => 'cancel',
|
||||
'name' => ts('Cancel'),
|
||||
],
|
||||
]);
|
||||
return;
|
||||
}
|
||||
|
||||
//get the tokens for Mailing Label field
|
||||
$tokens = CRM_Core_SelectValues::contactTokens();
|
||||
$this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens));
|
||||
$this->applyFilter('__ALL__', 'trim');
|
||||
$countryList = CRM_AddressFormat_BAO_CountryAddressFormat::getCountryList($this->_id);
|
||||
$this->add('select', 'country_id', ts('Country'),
|
||||
['' => '- select -'] + $countryList, TRUE, ['class' => 'crm-select2 huge']
|
||||
);
|
||||
$this->add('textarea',
|
||||
'format',
|
||||
ts('Address Format'),
|
||||
NULL,
|
||||
TRUE
|
||||
);
|
||||
$this->addButtons([
|
||||
[
|
||||
'type' => 'next',
|
||||
'name' => ts('Save'),
|
||||
'isDefault' => TRUE,
|
||||
],
|
||||
[
|
||||
'type' => 'next',
|
||||
'name' => ts('Save and New'),
|
||||
'subName' => 'new',
|
||||
],
|
||||
[
|
||||
'type' => 'cancel',
|
||||
'name' => ts('Cancel'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default values for the form.
|
||||
*/
|
||||
public function setDefaultValues() {
|
||||
$defaults = [];
|
||||
if ($this->_id) {
|
||||
$result = civicrm_api3('Country', 'getsingle', [
|
||||
'return' => ["id", "address_format_id.format"],
|
||||
'address_format_id' => $this->_id,
|
||||
]);
|
||||
$defaults = [
|
||||
'country_id' => $result['id'],
|
||||
'format' => $result['address_format_id.format'],
|
||||
];
|
||||
}
|
||||
return $defaults;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the form submission.
|
||||
*/
|
||||
public function postProcess() {
|
||||
if ($this->_action & CRM_Core_Action::DELETE) {
|
||||
$result = CRM_AddressFormat_BAO_CountryAddressFormat::deleteAddressFormat($this->_id);
|
||||
if (!empty($result['is_error'])) {
|
||||
CRM_Core_Error::statusBounce($result['error_message'], CRM_Utils_System::url('civicrm/country/addressformat', "reset=1&action=browse"), ts('Cannot Delete'));
|
||||
}
|
||||
CRM_Core_Session::setStatus(ts('Selected Address Format by Country has been deleted.'), ts('Record Deleted'), 'success');
|
||||
}
|
||||
else {
|
||||
// store the submitted values in an array
|
||||
$params = $this->exportValues();
|
||||
try {
|
||||
CRM_AddressFormat_BAO_CountryAddressFormat::processForm($params, $this->_id);
|
||||
CRM_Core_Session::setStatus(ts('The Address format for the country has been saved.'), ts('Saved'), 'success');
|
||||
}
|
||||
catch (CRM_Core_Exception $e) {
|
||||
CRM_Core_Error::statusBounce($e->getMessage());
|
||||
}
|
||||
|
||||
$buttonName = $this->controller->getButtonName();
|
||||
$session = CRM_Core_Session::singleton();
|
||||
|
||||
if ($buttonName == $this->getButtonName('next', 'new')) {
|
||||
CRM_Core_Session::setStatus(ts(' You can add another Address format for a country.'));
|
||||
$session->replaceUserContext(CRM_Utils_System::url('civicrm/country/addressformat',
|
||||
"reset=1&action=add")
|
||||
);
|
||||
}
|
||||
else {
|
||||
$session->replaceUserContext(CRM_Utils_System::url('civicrm/country/addressformat',
|
||||
"reset=1&action=browse")
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
112
CRM/AddressFormat/Page/Address.php
Normal file
112
CRM/AddressFormat/Page/Address.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
use CRM_Myextension_ExtensionUtil as E;
|
||||
|
||||
class CRM_AddressFormat_Page_Address extends CRM_Core_Page_Basic {
|
||||
/**
|
||||
* The action links that we need to display for the browse screen.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static $_links = NULL;
|
||||
|
||||
/**
|
||||
* Get BAO Name.
|
||||
*
|
||||
* @return string
|
||||
* Classname of BAO.
|
||||
*/
|
||||
public function getBAOName() {
|
||||
return 'CRM_Core_DAO_AddressFormat';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get action Links.
|
||||
*
|
||||
* @return array
|
||||
* (reference) of action links
|
||||
*/
|
||||
public function &links() {
|
||||
if (!(self::$_links)) {
|
||||
self::$_links = [
|
||||
CRM_Core_Action::UPDATE => [
|
||||
'name' => ts('Edit'),
|
||||
'url' => 'civicrm/country/addressformat',
|
||||
'qs' => 'action=update&id=%%id%%&reset=1',
|
||||
'title' => ts('Edit Address Format'),
|
||||
],
|
||||
CRM_Core_Action::DELETE => [
|
||||
'name' => ts('Delete'),
|
||||
'url' => 'civicrm/country/addressformat',
|
||||
'qs' => 'action=delete&id=%%id%%',
|
||||
'title' => ts('Delete Address Format'),
|
||||
],
|
||||
];
|
||||
}
|
||||
return self::$_links;
|
||||
}
|
||||
|
||||
/**
|
||||
* Browse all address formats for country.
|
||||
*/
|
||||
public function browse() {
|
||||
$addressFormats = [];
|
||||
$sql = 'SELECT caf.*, cc.name
|
||||
FROM civicrm_country cc
|
||||
INNER JOIN civicrm_address_format caf
|
||||
ON caf.id = cc.address_format_id
|
||||
';
|
||||
$dao = CRM_Core_DAO::executeQuery($sql);
|
||||
|
||||
while ($dao->fetch()) {
|
||||
$addressFormats[$dao->id] = [
|
||||
'name' => $dao->name,
|
||||
'format' => $dao->format,
|
||||
];
|
||||
$action = array_sum(array_keys($this->links()));
|
||||
$addressFormats[$dao->id]['action'] = CRM_Core_Action::formLink(
|
||||
self::links(),
|
||||
$action,
|
||||
['id' => $dao->id],
|
||||
ts('more'),
|
||||
FALSE,
|
||||
'addressformat.manage.action',
|
||||
'AddressFormat',
|
||||
$dao->id
|
||||
);
|
||||
}
|
||||
$this->assign('rows', $addressFormats);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get name of edit form.
|
||||
*
|
||||
* @return string
|
||||
* Classname of edit form.
|
||||
*/
|
||||
public function editForm() {
|
||||
return 'CRM_AddressFormat_Form_Address';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get edit form name.
|
||||
*
|
||||
* @return string
|
||||
* name of this page.
|
||||
*/
|
||||
public function editName() {
|
||||
return ts('Country - Address Format');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user context.
|
||||
*
|
||||
* @param null $mode
|
||||
*
|
||||
* @return string
|
||||
* user context.
|
||||
*/
|
||||
public function userContext($mode = NULL) {
|
||||
return 'civicrm/country/addressformat';
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user