From 2dc414b8cd2318abf942340e9291b97777abbe07 Mon Sep 17 00:00:00 2001 From: Praesidiarius Date: Sat, 8 Feb 2020 00:27:07 +0100 Subject: [PATCH] basic address working --- config/module.config.php | 6 ++ data/install.sql | 40 ++++++++- src/Controller/AddressController.php | 37 ++++++-- src/Model/Address.php | 52 +++++++++++ src/Model/AddressTable.php | 112 ++++++++++++++++++++++++ src/Module.php | 31 ++++++- view/partial/contact_address-add.phtml | 16 +++- view/partial/contact_address-edit.phtml | 33 ++++++- view/partial/contact_address-view.phtml | 30 ++++++- 9 files changed, 345 insertions(+), 12 deletions(-) create mode 100644 src/Model/Address.php create mode 100644 src/Model/AddressTable.php diff --git a/config/module.config.php b/config/module.config.php index 017690c..77aada8 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -20,4 +20,10 @@ use Laminas\ServiceManager\Factory\InvokableFactory; return [ + # View Settings + 'view_manager' => [ + 'template_path_stack' => [ + 'contact-address' => __DIR__ . '/../view', + ], + ], ]; diff --git a/data/install.sql b/data/install.sql index 5c64701..d4056c4 100644 --- a/data/install.sql +++ b/data/install.sql @@ -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', '', '', ''); \ No newline at end of file +(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'); diff --git a/src/Controller/AddressController.php b/src/Controller/AddressController.php index 58688d5..6f64f22 100644 --- a/src/Controller/AddressController.php +++ b/src/Controller/AddressController.php @@ -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; @@ -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) { @@ -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 []; } } diff --git a/src/Model/Address.php b/src/Model/Address.php new file mode 100644 index 0000000..7abbeed --- /dev/null +++ b/src/Model/Address.php @@ -0,0 +1,52 @@ + + * @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; + } +} \ No newline at end of file diff --git a/src/Model/AddressTable.php b/src/Model/AddressTable.php new file mode 100644 index 0000000..f48da9d --- /dev/null +++ b/src/Model/AddressTable.php @@ -0,0 +1,112 @@ + + * @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()); + } +} \ No newline at end of file diff --git a/src/Module.php b/src/Module.php index 6f279a3..3aa2aa4 100644 --- a/src/Module.php +++ b/src/Module.php @@ -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 { @@ -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); + }, + ], + ]; } /** @@ -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 diff --git a/view/partial/contact_address-add.phtml b/view/partial/contact_address-add.phtml index 46fcf04..5ed2ec2 100644 --- a/view/partial/contact_address-add.phtml +++ b/view/partial/contact_address-add.phtml @@ -1,3 +1,17 @@ select(['form_key'=>'contactaddress-single']); +if(count($oForm) > 0) { + $aFields = [ + 'address-base' => CoreController::$aCoreTables['core-form-field']->select(['form'=>'contactaddress-single']), + ]; + ?> + partial('partial/basicformfields', ['sFormName'=>'contactaddress-single','sTab'=>'address-base','aFieldsByTab'=>$aFields]); ?> + -

Add Address

+ diff --git a/view/partial/contact_address-edit.phtml b/view/partial/contact_address-edit.phtml index 65c2f9c..de72a40 100644 --- a/view/partial/contact_address-edit.phtml +++ b/view/partial/contact_address-edit.phtml @@ -1,3 +1,34 @@ 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; + ?> + + + partial('partial/basicformfields', $aPartialData); ?> + -

Edit Address

+ diff --git a/view/partial/contact_address-view.phtml b/view/partial/contact_address-view.phtml index d6cb236..7e217db 100644 --- a/view/partial/contact_address-view.phtml +++ b/view/partial/contact_address-view.phtml @@ -1,3 +1,31 @@ select(['form_key'=>'contactaddress-single']); + +$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; + } +} +if(count($oForm) > 0) { + $aFields = [ + 'address-base' => CoreController::$aCoreTables['core-form-field']->select(['form'=>'contactaddress-single']), + ]; + ?> + + partial('partial/viewformfields', ['sFormName'=>'contactaddress-single','sTab'=>'address-base','oItem'=>$oPrimaryAddress,'aFormFieldsByTab'=>$aFields]); ?> + +
No Address
+ -

View Address

+