Skip to content

Commit

Permalink
rebase to latest skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
JBinggi committed Feb 13, 2020
1 parent e2d9900 commit e25da46
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 70 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "oneplace/oneplace-article-history",
"description": "onePlace Article History Plugin. Manage your Article's history",
"type": "oneplace-plugin",
"version": "1.0.1",
"version": "1.0.2",
"license": "BSD-3-Clause",
"keywords": [
"laminas",
Expand Down
14 changes: 12 additions & 2 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Laminas\ServiceManager\Factory\InvokableFactory;

return [
# Article Module - Routes
# History Module - Routes
'router' => [
'routes' => [
'article-history' => [
Expand All @@ -37,8 +37,18 @@
],
],
],
'article-history-setup' => [
'type' => Literal::class,
'options' => [
'route' => '/article/history/setup',
'defaults' => [
'controller' => Controller\InstallController::class,
'action' => 'checkdb',
],
],
],
],
],
], # Routes

# View Settings
'view_manager' => [
Expand Down
15 changes: 0 additions & 15 deletions mkdocs.yml

This file was deleted.

116 changes: 116 additions & 0 deletions src/Controller/InstallController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* InstallController.php - Main Controller
*
* Installer for Plugin
*
* @category Controller
* @package Article\History
* @author Verein onePlace
* @copyright (C) 2020 Verein onePlace <admin@1plc.ch>
* @license https://opensource.org/licenses/BSD-3-Clause
* @version 1.0.0
* @since 1.0.1
*/

declare(strict_types=1);

namespace OnePlace\Article\History\Controller;

use Application\Controller\CoreUpdateController;
use Application\Model\CoreEntityModel;
use OnePlace\Article\History\Model\HistoryTable;
use Laminas\View\Model\ViewModel;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Db\TableGateway\TableGateway;
use Laminas\Db\ResultSet\ResultSet;

class InstallController extends CoreUpdateController {
/**
* InstallController constructor.
*
* @param AdapterInterface $oDbAdapter
* @param HistoryTable $oTableGateway
* @since 1.0.0
*/
public function __construct(AdapterInterface $oDbAdapter, HistoryTable $oTableGateway, $oServiceManager)
{
$this->oTableGateway = $oTableGateway;
$this->sSingleForm = 'articlehistory-single';
parent::__construct($oDbAdapter, $oTableGateway, $oServiceManager);

if ($oTableGateway) {
# Attach TableGateway to Entity Models
if (! isset(CoreEntityModel::$aEntityTables[$this->sSingleForm])) {
CoreEntityModel::$aEntityTables[$this->sSingleForm] = $oTableGateway;
}
}
}

public function checkdbAction()
{
# Set Layout based on users theme
$this->setThemeBasedLayout('article');

$oRequest = $this->getRequest();

if(! $oRequest->isPost()) {

$bTableExists = false;

try {
$this->oTableGateway->fetchAll(false);
$bTableExists = true;
} catch (\RuntimeException $e) {

}

return new ViewModel([
'bTableExists' => $bTableExists,
'sVendor' => 'oneplace',
'sModule' => 'oneplace-article-history',
]);
} else {
$sSetupConfig = $oRequest->getPost('plc_module_setup_config');

$sSetupFile = 'vendor/oneplace/oneplace-article-history/data/install.sql';
if(file_exists($sSetupFile)) {
echo 'got install file..';
$this->parseSQLInstallFile($sSetupFile,CoreUpdateController::$oDbAdapter);
}

if($sSetupConfig != '') {
$sConfigStruct = 'vendor/oneplace/oneplace-article-history/data/structure_'.$sSetupConfig.'.sql';
if(file_exists($sConfigStruct)) {
echo 'got struct file for config '.$sSetupConfig;
$this->parseSQLInstallFile($sConfigStruct,CoreUpdateController::$oDbAdapter);
}
$sConfigData = 'vendor/oneplace/oneplace-article-history/data/data_'.$sSetupConfig.'.sql';
if(file_exists($sConfigData)) {
echo 'got data file for config '.$sSetupConfig;
$this->parseSQLInstallFile($sConfigData,CoreUpdateController::$oDbAdapter);
}
}

$oModTbl = new TableGateway('core_module', CoreUpdateController::$oDbAdapter);
$oModTbl->insert([
'module_key' => 'oneplace-article-history',
'type' => 'plugin',
'version' => \OnePlace\Article\History\Module::VERSION,
'label' => 'onePlace Article History',
'vendor' => 'oneplace',
]);

try {
$this->oTableGateway->fetchAll(false);
$bTableExists = true;
} catch (\RuntimeException $e) {

}
$bTableExists = false;

$this->flashMessenger()->addSuccessMessage('Article History DB Update successful');
$this->redirect()->toRoute('application', ['action' => 'checkforupdates']);
}
}
}
5 changes: 3 additions & 2 deletions src/Model/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* History.php - History Entity
*
* Entity Model for Article History
* Entity Model for History History
*
* @category Model
* @package Article\History
Expand All @@ -19,7 +19,7 @@

class History extends CoreEntityModel {
/**
* Article constructor.
* History constructor.
*
* @param AdapterInterface $oDbAdapter
* @since 1.0.0
Expand All @@ -42,6 +42,7 @@ public function __construct($oDbAdapter) {
*/
public function exchangeArray(array $aData) {
$this->id = !empty($aData['History_ID']) ? $aData['History_ID'] : 0;
$this->label = !empty($aData['label']) ? $aData['label'] : '';

$this->updateDynamicFields($aData);
}
Expand Down
61 changes: 15 additions & 46 deletions src/Model/HistoryTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* HistoryTable.php - History Table
*
* Table Model for Article History
* Table Model for History History
*
* @category Model
* @package Article\History
Expand All @@ -23,6 +23,7 @@
use Laminas\Db\Sql\Where;
use Laminas\Paginator\Paginator;
use Laminas\Paginator\Adapter\DbSelect;
use OnePlace\History\Model\History;

class HistoryTable extends CoreEntityTable {

Expand All @@ -40,69 +41,37 @@ public function __construct(TableGateway $tableGateway) {
}

/**
* Get Article Entity
* Get History Entity
*
* @param int $id
* @param string $sKey
* @return mixed
* @since 1.0.0
*/
public function getSingle($id) {
public function getSingle($id,$sKey = 'History_ID') {
# Use core function
return $this->getSingleEntity($id,'History_ID');
return $this->getSingleEntity($id,$sKey);
}

/**
* Save Article Entity
* Save History Entity
*
* @param Article $oArticle
* @return int Article ID
* @param History $oHistory
* @return int History ID
* @since 1.0.0
*/
public function saveSingle(History $oArticle) {
$aData = [];
public function saveSingle(History $oHistory) {
$aDefaultData = [
'label' => $oHistory->label,
];

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

$id = (int) $oArticle->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());

# Insert Article
$this->oTableGateway->insert($aData);

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

# Check if Article Entity already exists
try {
$this->getSingle($id);
} catch (\RuntimeException $e) {
throw new \RuntimeException(sprintf(
'Cannot update History 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 Article
$this->oTableGateway->update($aData, ['History_ID' => $id]);

return $id;
return $this->saveSingleEntity($oHistory,'History_ID',$aDefaultData);
}

/**
* Generate new single Entity
*
* @return Article
* @return History
* @since 1.0.0
*/
public function generateNew() {
Expand Down
16 changes: 12 additions & 4 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Module {
*
* @since 1.0.0
*/
const VERSION = '1.0.1';
const VERSION = '1.0.2';

/**
* Load module config file
Expand Down Expand Up @@ -76,15 +76,14 @@ public function getServiceConfig() : array {
},
],
];
}
} # getServiceConfig()

/**
* Load Controllers
*/
public function getControllerConfig() : array {
return [
'factories' => [
# Plugin Example Controller
Controller\HistoryController::class => function($container) {
$oDbAdapter = $container->get(AdapterInterface::class);
$tableGateway = $container->get(HistoryTable::class);
Expand All @@ -97,7 +96,16 @@ public function getControllerConfig() : array {
$container
);
},
# Installer
Controller\InstallController::class => function($container) {
$oDbAdapter = $container->get(AdapterInterface::class);
return new Controller\InstallController(
$oDbAdapter,
$container->get(Model\HistoryTable::class),
$container
);
},
],
];
}
} # getControllerConfig()
}
46 changes: 46 additions & 0 deletions view/one-place/article/history/install/checkdb.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h2>Check for installation</h2>
</div>
<div class="card-body">
<form action="" method="POST">
<div class="form-group">
<?php
?>
<?php
if($bTableExists) { ?>
<div class="alert alert-success py-2 px-2">
<?=$this->translate('Database is up-to-date')?>
</div>
<?php
} else { ?>
<div class="alert alert-warning py-2 px-2">
<?=$this->translate('Database needs update. Please choose config')?>
</div>
<?php
if(file_exists('vendor/'.$sVendor.'/'.$sModule.'/data/structure_simple.sql')) {
?> <br/><input type="radio" name="plc_module_setup_config" value="simple" checked /> Simple
<?php
}
if(file_exists('vendor/'.$sVendor.'/'.$sModule.'/data/structure_extended.sql')) {
?> <br/><input type="radio" name="plc_module_setup_config" value="extended" /> Simple
<?php
}
}
?>
</div>

<?php if (! $bTableExists) { ?>
<div class="form-group">
<button type="submit" class="btn btn-primary">
<?=$this->translate('Update Database')?>
</button>
</div>
<?php } ?>
</form>
</div>
</div>
</div>
</div>

0 comments on commit e25da46

Please sign in to comment.