Skip to content

Commit

Permalink
basic address working
Browse files Browse the repository at this point in the history
  • Loading branch information
Praesidiarius committed Feb 8, 2020
1 parent 2dc414b commit e7d1acd
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 49 deletions.
19 changes: 17 additions & 2 deletions data/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CREATE TABLE `contact_address` (
`appartment` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`zip` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL,
`city` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`country` varchar(3) COLLATE utf8mb4_unicode_ci NOT NULL,
`country_idfs` int(11) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_by` int(11) NOT NULL,
`created_date` datetime NOT NULL,
`modified_by` int(11) NOT NULL,
Expand All @@ -46,4 +46,19 @@ INSERT INTO `core_form` (`form_key`, `label`, `entity_class`, `entity_tbl_class`
--
INSERT INTO `core_form_field` (`Field_ID`, `type`, `label`, `fieldkey`, `tab`, `form`, `class`, `url_view`, `url_list`, `show_widget_left`, `allow_clear`, `readonly`, `tbl_cached_name`, `tbl_class`, `tbl_permission`) VALUES
(NULL, 'text', 'Street', 'street', 'address-base', 'contactaddress-single', 'col-md-6', '', '', '0', '1', '0', '', '', ''),
(NULL, 'select', 'Contact', 'contact_idfs', 'address-base', 'contactaddress-single', 'col-md-3', '', '/contact/api/list/0', '0', '1', '0', 'contact-single', 'OnePlace\\Contact\\Model\\ContactTable', 'add-OnePlace\\Contact\\Controller\\ContactController');
(NULL, 'text', 'Zip', 'zip', 'address-base', 'contactaddress-single', 'col-md-1', '', '', '0', '1', '0', '', '', ''),
(NULL, 'text', 'City', 'city', 'address-base', 'contactaddress-single', 'col-md-4', '', '', '0', '1', '0', '', '', ''),
(NULL, 'hidden', 'Contact', 'contact_idfs', 'address-base', 'contactaddress-single', 'col-md-3', '', '/', '0', '1', '0', '', '', ''),
(NULL, 'select', 'Country', 'country_idfs', 'address-base', 'contactaddress-single', 'col-md-3', '', '/tag/api/list/address-single/country', 0, 1, 0, 'entitytag-single', 'OnePlace\\Tag\\Model\\EntityTagTable', 'add-OnePlace\\Tag\\Controller\\CountryController');

--
-- add new tag country
--
INSERT INTO `core_tag` (`Tag_ID`, `tag_key`, `tag_label`, `created_by`, `created_date`, `modified_by`, `modified_date`) VALUES
(NULL, 'country', 'Country', '1', '0000-00-00 00:00:00', '1', '0000-00-00 00:00:00');

--
-- permission add country
--
INSERT INTO `permission` (`permission_key`, `module`, `label`, `nav_label`, `nav_href`, `show_in_menu`) VALUES
('add', 'OnePlace\\Tag\\Controller\\CountryController', 'Add', '', '', '0');
12 changes: 7 additions & 5 deletions src/Controller/AddressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,22 @@ public function __construct(AdapterInterface $oDbAdapter,AddressTable $oTableGat
* @return bool true or false
* @since 1.0.0
*/
public function attachAddressToContact($oContact,$aFormData,$sState) {
public function attachAddressToContact($oContact,$aRawFormData,$sState) {
$aFormData = $this->parseFormData($aRawFormData);

# Parse Raw Form Data for Address Fields
$aAddressFields = $this->getFormFields($this->sSingleForm);
$aAddressData = [];
foreach($aAddressFields as $oField) {
if(array_key_exists($this->sSingleForm.'_'.$oField->fieldkey,$aFormData)) {
$aAddressData[$oField->fieldkey] = $aFormData[$this->sSingleForm.'_'.$oField->fieldkey];
if(array_key_exists($oField->fieldkey,$aFormData)) {
$aAddressData[$oField->fieldkey] = $aFormData[$oField->fieldkey];
}
}

# Link Contact to ADdress
$aAddressData['contact_idfs'] = $oContact->getID();
if(isset($aFormData[$this->sSingleForm.'_address_primary_id'])) {
$aAddressData['Address_ID'] = $aFormData[$this->sSingleForm.'_address_primary_id'];
if(isset($aRawFormData[$this->sSingleForm.'_address_primary_id'])) {
$aAddressData['Address_ID'] = $aRawFormData[$this->sSingleForm.'_address_primary_id'];
}

# Generate New Address
Expand Down
51 changes: 31 additions & 20 deletions view/partial/contact_address-edit.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,42 @@ use OnePlace\Contact\Address\Model\AddressTable;

$oForm = CoreController::$aCoreTables['core-form']->select(['form_key'=>'contactaddress-single']);
if(count($oForm) > 0) {
$oAddressTbl = CoreController::$oServiceManager->get(AddressTable::class);
try {
$oAddressTbl = CoreController::$oServiceManager->get(AddressTable::class);
} catch(\RuntimeException $e) {
echo '<div class="alert alert-danger"><b>Error:</b> Could not load address table</div>';
}

$oAddresses = $oAddressTbl->fetchAll(false,['contact_idfs'=>$oItem->getID()]);
$oPrimaryAddress = false;
if(count($oAddresses) > 0) {
foreach($oAddresses as $oAddr) {
$oPrimaryAddress = $oAddr;
break;
if(isset($oAddressTbl)) {
# load contact addresses
$oAddresses = $oAddressTbl->fetchAll(false, ['contact_idfs' => $oItem->getID()]);
$oPrimaryAddress = false;
if (count($oAddresses) > 0) {
foreach ($oAddresses as $oAddr) {
$oPrimaryAddress = $oAddr;
break;
}
}
}

$aFields = [
'address-base' => CoreController::$aCoreTables['core-form-field']->select(['form'=>'contactaddress-single']),
];
# add address form fields
$aFields = [
'address-base' => CoreController::$aCoreTables['core-form-field']->select(['form' => 'contactaddress-single']),
];

$aPartialData = ['sFormName'=>'contactaddress-single','sTab'=>'address-base','aFieldsByTab'=>$aFields];
if($oPrimaryAddress) {
$aPartialData['oItem'] = $oPrimaryAddress;
?>
<input type="hidden" name="contactaddress-single_address_primary_id" value="<?=$oPrimaryAddress->getID()?>" />
<?php } ?>
<?= $this->partial('partial/basicformfields', $aPartialData); ?>
<?php
$aPartialData = ['sFormName' => 'contactaddress-single', 'sTab' => 'address-base', 'aFieldsByTab' => $aFields];
if ($oPrimaryAddress) {
$aPartialData['oItem'] = $oPrimaryAddress;
?>
<input type="hidden" name="contactaddress-single_address_primary_id"
value="<?= $oPrimaryAddress->getID() ?>"/>
<?php } ?>
<div class="row">
<?= $this->partial('partial/basicformfields', $aPartialData); ?>
</div>
<?php
}
} else {
echo 'Could not load address form';
echo '<div class="alert alert-danger"><b>Error:</b>Could not load address form</div>';
}
?>

55 changes: 33 additions & 22 deletions view/partial/contact_address-view.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,41 @@
use Application\Controller\CoreController;
use OnePlace\Contact\Address\Model\AddressTable;

# Get Form
$oForm = CoreController::$aCoreTables['core-form']->select(['form_key'=>'contactaddress-single']);

$oAddressTbl = CoreController::$oServiceManager->get(AddressTable::class);
# Try to get adress table
try {
$oAddressTbl = CoreController::$oServiceManager->get(AddressTable::class);
} catch(\RuntimeException $e) {
echo '<div class="alert alert-danger"><b>Error:</b> Could not load address table</div>';
}

$oAddresses = $oAddressTbl->fetchAll(false,['contact_idfs'=>$oItem->getID()]);
$oPrimaryAddress = false;
if(count($oAddresses) > 0) {
foreach($oAddresses as $oAddr) {
$oPrimaryAddress = $oAddr;
break;
if(isset($oAddressTbl)) {
# load contact addresses
$oAddresses = $oAddressTbl->fetchAll(false, ['contact_idfs' => $oItem->getID()]);
# get primary address
$oPrimaryAddress = false;
if (count($oAddresses) > 0) {
foreach ($oAddresses as $oAddr) {
$oPrimaryAddress = $oAddr;
break;
}
}
if (count($oForm) > 0) {
# add address form fields
$aFields = [
'address-base' => CoreController::$aCoreTables['core-form-field']->select(['form' => 'contactaddress-single']),
];
?>
<?php if ($oPrimaryAddress) { ?>
<div class="row">
<?= $this->partial('partial/viewformfields', ['sFormName' => 'contactaddress-single', 'sTab' => 'address-base', 'oItem' => $oPrimaryAddress, 'aFormFieldsByTab' => $aFields]); ?>
</div>
<?php } else { ?>
<div class="alert alert-info">No Address</div>
<?php }
} else {
echo '<div class="alert alert-danger"><b>Error:</b> Could not load address form</div>';
}
}
if(count($oForm) > 0) {
$aFields = [
'address-base' => CoreController::$aCoreTables['core-form-field']->select(['form'=>'contactaddress-single']),
];
?>
<?php if($oPrimaryAddress) { ?>
<?= $this->partial('partial/viewformfields', ['sFormName'=>'contactaddress-single','sTab'=>'address-base','oItem'=>$oPrimaryAddress,'aFormFieldsByTab'=>$aFields]); ?>
<?php } else { ?>
<div>No Address</div>
<?php }
} else {
echo 'Could not load address form';
}
?>

0 comments on commit e7d1acd

Please sign in to comment.