|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * InstallController.php - Main Controller |
| 4 | + * |
| 5 | + * Installer for Plugin |
| 6 | + * |
| 7 | + * @category Controller |
| 8 | + * @package Contact\Address |
| 9 | + * @author Verein onePlace |
| 10 | + * @copyright (C) 2020 Verein onePlace <[email protected]> |
| 11 | + * @license https://opensource.org/licenses/BSD-3-Clause |
| 12 | + * @version 1.0.0 |
| 13 | + * @since 1.0.0 |
| 14 | + */ |
| 15 | + |
| 16 | +declare(strict_types=1); |
| 17 | + |
| 18 | +namespace OnePlace\Contact\Address\Controller; |
| 19 | + |
| 20 | +use Application\Controller\CoreUpdateController; |
| 21 | +use Application\Model\CoreEntityModel; |
| 22 | +use OnePlace\Contact\Address\Model\AddressTable; |
| 23 | +use Laminas\View\Model\ViewModel; |
| 24 | +use Laminas\Db\Adapter\AdapterInterface; |
| 25 | +use Laminas\Db\TableGateway\TableGateway; |
| 26 | +use Laminas\Db\ResultSet\ResultSet; |
| 27 | + |
| 28 | +class InstallController extends CoreUpdateController { |
| 29 | + /** |
| 30 | + * SkeletonController constructor. |
| 31 | + * |
| 32 | + * @param AdapterInterface $oDbAdapter |
| 33 | + * @param SkeletonTable $oTableGateway |
| 34 | + * @since 1.0.0 |
| 35 | + */ |
| 36 | + public function __construct(AdapterInterface $oDbAdapter, AddressTable $oTableGateway, $oServiceManager) |
| 37 | + { |
| 38 | + $this->oTableGateway = $oTableGateway; |
| 39 | + $this->sSingleForm = 'contactaddress-single'; |
| 40 | + parent::__construct($oDbAdapter, $oTableGateway, $oServiceManager); |
| 41 | + |
| 42 | + if ($oTableGateway) { |
| 43 | + # Attach TableGateway to Entity Models |
| 44 | + if (! isset(CoreEntityModel::$aEntityTables[$this->sSingleForm])) { |
| 45 | + CoreEntityModel::$aEntityTables[$this->sSingleForm] = $oTableGateway; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public function checkdbAction() |
| 51 | + { |
| 52 | + # Set Layout based on users theme |
| 53 | + $this->setThemeBasedLayout('contact'); |
| 54 | + |
| 55 | + $oRequest = $this->getRequest(); |
| 56 | + |
| 57 | + if(! $oRequest->isPost()) { |
| 58 | + |
| 59 | + $bTableExists = false; |
| 60 | + |
| 61 | + try { |
| 62 | + $this->oTableGateway->fetchAll(false); |
| 63 | + $bTableExists = true; |
| 64 | + } catch (\RuntimeException $e) { |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + return new ViewModel([ |
| 69 | + 'bTableExists' => $bTableExists, |
| 70 | + 'sVendor' => 'oneplace', |
| 71 | + 'sModule' => 'oneplace-contact-address', |
| 72 | + ]); |
| 73 | + } else { |
| 74 | + $sSetupConfig = $oRequest->getPost('plc_module_setup_config'); |
| 75 | + |
| 76 | + $sSetupFile = 'vendor/oneplace/oneplace-contact-address/data/install.sql'; |
| 77 | + if(file_exists($sSetupFile)) { |
| 78 | + echo 'got install file..'; |
| 79 | + $this->parseSQLInstallFile($sSetupFile,CoreUpdateController::$oDbAdapter); |
| 80 | + } |
| 81 | + |
| 82 | + if($sSetupConfig != '') { |
| 83 | + $sConfigStruct = 'vendor/oneplace/oneplace-contact-address/data/structure_'.$sSetupConfig.'.sql'; |
| 84 | + if(file_exists($sConfigStruct)) { |
| 85 | + echo 'got struct file for config '.$sSetupConfig; |
| 86 | + $this->parseSQLInstallFile($sConfigStruct,CoreUpdateController::$oDbAdapter); |
| 87 | + } |
| 88 | + $sConfigData = 'vendor/oneplace/oneplace-contact-address/data/data_'.$sSetupConfig.'.sql'; |
| 89 | + if(file_exists($sConfigData)) { |
| 90 | + echo 'got data file for config '.$sSetupConfig; |
| 91 | + $this->parseSQLInstallFile($sConfigData,CoreUpdateController::$oDbAdapter); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + $oModTbl = new TableGateway('core_module', CoreUpdateController::$oDbAdapter); |
| 96 | + $oModTbl->insert([ |
| 97 | + 'module_key' => 'oneplace-contact-address', |
| 98 | + 'type' => 'plugin', |
| 99 | + 'version' => \OnePlace\Contact\Address\Module::VERSION, |
| 100 | + 'label' => 'onePlace Contact Address', |
| 101 | + 'vendor' => 'oneplace', |
| 102 | + ]); |
| 103 | + |
| 104 | + try { |
| 105 | + $this->oTableGateway->fetchAll(false); |
| 106 | + $bTableExists = true; |
| 107 | + } catch (\RuntimeException $e) { |
| 108 | + |
| 109 | + } |
| 110 | + $bTableExists = false; |
| 111 | + |
| 112 | + $this->flashMessenger()->addSuccessMessage('Contact Address DB Update successful'); |
| 113 | + $this->redirect()->toRoute('application', ['action' => 'checkforupdates']); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments