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 7, 2020
1 parent cf8a185 commit 2dc414b
Show file tree
Hide file tree
Showing 9 changed files with 345 additions and 12 deletions.
6 changes: 6 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@
use Laminas\ServiceManager\Factory\InvokableFactory;

return [
# View Settings
'view_manager' => [
'template_path_stack' => [
'contact-address' => __DIR__ . '/../view',
],
],
];
40 changes: 39 additions & 1 deletion data/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,42 @@ INSERT INTO `core_form_tab` (`Tab_ID`, `form`, `title`, `subtitle`, `icon`, `cou
-- Add new partial
--
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, 'partial', 'Address', 'contact_address', 'contact-address', 'contact-single', 'col-md-12', '', '', '0', '1', '0', '', '', '');
(NULL, 'partial', 'Address', 'contact_address', 'contact-address', 'contact-single', 'col-md-12', '', '', '0', '1', '0', '', '', '');

--
-- create address table
--
CREATE TABLE `contact_address` (
`Address_ID` int(11) NOT NULL,
`contact_idfs` int(11) NOT NULL,
`street` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL,
`street_extra` varchar(150) COLLATE utf8mb4_unicode_ci NOT NULL,
`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,
`created_by` int(11) NOT NULL,
`created_date` datetime NOT NULL,
`modified_by` int(11) NOT NULL,
`modified_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE `contact_address`
ADD PRIMARY KEY (`Address_ID`);

ALTER TABLE `contact_address`
MODIFY `Address_ID` int(11) NOT NULL AUTO_INCREMENT;


--
-- add address form
--
INSERT INTO `core_form` (`form_key`, `label`, `entity_class`, `entity_tbl_class`) VALUES
('contactaddress-single', 'Contact Address', 'OnePlace\\Contact\\Address\\Model\\Address', 'OnePlace\\Contact\\Address\\Model\\AddressTable');

--
-- add address fields
--
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');
37 changes: 32 additions & 5 deletions src/Controller/AddressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

use Application\Controller\CoreEntityController;
use Application\Model\CoreEntityModel;
use OnePlace\Contact\Model\Contact;
use OnePlace\Contact\Model\ContactTable;
use OnePlace\Contact\Address\Model\AddressTable;
use Laminas\View\Model\ViewModel;
use Laminas\Db\Adapter\AdapterInterface;

Expand All @@ -39,9 +38,9 @@ class AddressController extends CoreEntityController {
* @param ContactTable $oTableGateway
* @since 1.0.0
*/
public function __construct(AdapterInterface $oDbAdapter,ContactTable $oTableGateway,$oServiceManager) {
public function __construct(AdapterInterface $oDbAdapter,AddressTable $oTableGateway,$oServiceManager) {
$this->oTableGateway = $oTableGateway;
$this->sSingleForm = 'contact-single';
$this->sSingleForm = 'contactaddress-single';
parent::__construct($oDbAdapter,$oTableGateway,$oServiceManager);

if($oTableGateway) {
Expand All @@ -62,6 +61,34 @@ public function __construct(AdapterInterface $oDbAdapter,ContactTable $oTableGat
* @since 1.0.0
*/
public function attachAddressToContact($oContact,$aFormData,$sState) {
return false;
# 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];
}
}

# 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'];
}

# Generate New Address
$oAddress = $this->oTableGateway->generateNew();

# Attach Data
$oAddress->exchangeArray($aAddressData);

# Save to Database
$iAddressID = $this->oTableGateway->saveSingle($oAddress);

return true;
}

public function attachAddressForm() {
return [];
}
}
52 changes: 52 additions & 0 deletions src/Model/Address.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Address.php - Address Entity
*
* Entity Model for Contact Address
*
* @category Model
* @package Contact\Address
* @author Verein onePlace
* @copyright (C) 2020 Verein onePlace <[email protected]>
* @license https://opensource.org/licenses/BSD-3-Clause
* @version 1.0.0
* @since 1.0.0
*/

namespace OnePlace\Contact\Address\Model;

use Application\Model\CoreEntityModel;

class Address extends CoreEntityModel {
/**
* Contact constructor.
*
* @param AdapterInterface $oDbAdapter
* @since 1.0.0
*/
public function __construct($oDbAdapter) {
parent::__construct($oDbAdapter);

# Set Single Form Name
$this->sSingleForm = 'contactaddress-single';

# Attach Dynamic Fields to Entity Model
$this->attachDynamicFields();
}

/**
* Set Entity Data based on Data given
*
* @param array $aData
* @since 1.0.0
*/
public function exchangeArray(array $aData) {
$this->id = !empty($aData['Address_ID']) ? $aData['Address_ID'] : 0;

$this->updateDynamicFields($aData);
}

public function getLabel() {
return $this->street;
}
}
112 changes: 112 additions & 0 deletions src/Model/AddressTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* AddressTable.php - Address Table
*
* Table Model for Contact Address
*
* @category Model
* @package Contact\Address
* @author Verein onePlace
* @copyright (C) 2020 Verein onePlace <[email protected]>
* @license https://opensource.org/licenses/BSD-3-Clause
* @version 1.0.0
* @since 1.0.0
*/

namespace OnePlace\Contact\Address\Model;

use Application\Controller\CoreController;
use Application\Model\CoreEntityTable;
use Laminas\Db\TableGateway\TableGateway;
use Laminas\Db\ResultSet\ResultSet;
use Laminas\Db\Sql\Select;
use Laminas\Db\Sql\Where;
use Laminas\Paginator\Paginator;
use Laminas\Paginator\Adapter\DbSelect;

class AddressTable extends CoreEntityTable {

/**
* AddressTable constructor.
*
* @param TableGateway $tableGateway
* @since 1.0.0
*/
public function __construct(TableGateway $tableGateway) {
parent::__construct($tableGateway);

# Set Single Form Name
$this->sSingleForm = 'contactaddress-single';
}

/**
* Get Contact Entity
*
* @param int $id
* @return mixed
* @since 1.0.0
*/
public function getSingle($id) {
# Use core function
return $this->getSingleEntity($id,'Address_ID');
}

/**
* Save Contact Entity
*
* @param Contact $oContact
* @return int Contact ID
* @since 1.0.0
*/
public function saveSingle(Address $oContact) {
$aData = [];

$aData = $this->attachDynamicFields($aData,$oContact);

$id = (int) $oContact->id;

if ($id === 0) {
# Add Metadata
$aData['created_by'] = CoreController::$oSession->oUser->getID();
$aData['created_date'] = date('Y-m-d H:i:s',time());
$aData['modified_by'] = CoreController::$oSession->oUser->getID();
$aData['modified_date'] = date('Y-m-d H:i:s',time());

echo 'save new address';
# Insert Contact
$this->oTableGateway->insert($aData);

# Return ID
return $this->oTableGateway->lastInsertValue;
}

# Check if Contact Entity already exists
try {
$this->getSingle($id);
} catch (\RuntimeException $e) {
throw new \RuntimeException(sprintf(
'Cannot update Address with identifier %d; does not exist',
$id
));
}

# Update Metadata
$aData['modified_by'] = CoreController::$oSession->oUser->getID();
$aData['modified_date'] = date('Y-m-d H:i:s',time());

# Update Contact
$this->oTableGateway->update($aData, ['Address_ID' => $id]);

return $id;
}

/**
* Generate new single Entity
*
* @return Contact
* @since 1.0.0
*/
public function generateNew() {
return new Address($this->oTableGateway->getAdapter());
}
}
31 changes: 28 additions & 3 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

use Application\Controller\CoreEntityController;
use Laminas\Mvc\MvcEvent;
use Laminas\Db\ResultSet\ResultSet;
use Laminas\Db\TableGateway\TableGateway;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\EventManager\EventInterface as Event;
use Laminas\ModuleManager\ModuleManager;
use Laminas\Db\Adapter\AdapterInterface;
use OnePlace\Contact\Address\Controller\AddressController;
use OnePlace\Contact\Address\Model\AddressTable;
use OnePlace\Contact\Model\ContactTable;

class Module {
Expand All @@ -47,10 +50,32 @@ public function onBootstrap(Event $e)
$application = $e->getApplication();
$container = $application->getServiceManager();
$oDbAdapter = $container->get(AdapterInterface::class);
$tableGateway = $container->get(ContactTable::class);
$tableGateway = $container->get(AddressTable::class);

# Register Filter Plugin Hook
CoreEntityController::addHook('contact-add-after-save',(object)['sFunction'=>'attachAddressToContact','oItem'=>new AddressController($oDbAdapter,$tableGateway,$container)]);
CoreEntityController::addHook('contact-edit-after-save',(object)['sFunction'=>'attachAddressToContact','oItem'=>new AddressController($oDbAdapter,$tableGateway,$container)]);
}

/**
* Load Models
*/
public function getServiceConfig() : array {
return [
'factories' => [
# Address Plugin - Base Model
Model\AddressTable::class => function($container) {
$tableGateway = $container->get(Model\AddressTableGateway::class);
return new Model\AddressTable($tableGateway,$container);
},
Model\AddressTableGateway::class => function ($container) {
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Model\Address($dbAdapter));
return new TableGateway('contact_address', $dbAdapter, null, $resultSetPrototype);
},
],
];
}

/**
Expand All @@ -62,7 +87,7 @@ public function getControllerConfig() : array {
# Plugin Example Controller
Controller\AddressController::class => function($container) {
$oDbAdapter = $container->get(AdapterInterface::class);
$tableGateway = $container->get(ContactTable::class);
$tableGateway = $container->get(AddressTable::class);

# hook start
# hook end
Expand Down
16 changes: 15 additions & 1 deletion view/partial/contact_address-add.phtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
<?php
use Application\Controller\CoreController;
use OnePlace\Contact\Address\Model\AddressTable;

$oForm = CoreController::$aCoreTables['core-form']->select(['form_key'=>'contactaddress-single']);
if(count($oForm) > 0) {
$aFields = [
'address-base' => CoreController::$aCoreTables['core-form-field']->select(['form'=>'contactaddress-single']),
];
?>
<?= $this->partial('partial/basicformfields', ['sFormName'=>'contactaddress-single','sTab'=>'address-base','aFieldsByTab'=>$aFields]); ?>
<?php
} else {
echo 'Could not load address form';
}
?>
<h2>Add Address</h2>

33 changes: 32 additions & 1 deletion view/partial/contact_address-edit.phtml
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
<?php
use Application\Controller\CoreController;
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);

$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']),
];

$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
} else {
echo 'Could not load address form';
}
?>
<h2>Edit Address</h2>

Loading

0 comments on commit 2dc414b

Please sign in to comment.