-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DB migration view and controller
- Loading branch information
1 parent
c9da1b7
commit 1dae73b
Showing
3 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* Controller for database migration. | ||
* This controller will try to delete itself after migration is done. If this does not work it is strongly recommended to manually delete it. | ||
* | ||
* @author Orif (ViDi,AeDa) | ||
* @link https://github.com/OrifInformatique | ||
* @copyright Copyright (c), Orif (https://www.orif.ch) | ||
*/ | ||
namespace Stock\Controllers; | ||
use App\Controllers\BaseController; | ||
use CodeIgniter\HTTP\RequestInterface; | ||
use CodeIgniter\HTTP\ResponseInterface; | ||
use Exception; | ||
use Psr\Log\LoggerInterface; | ||
|
||
|
||
class Migrate extends BaseController | ||
{ | ||
/** | ||
* Constructor | ||
*/ | ||
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) | ||
{ | ||
// Set Access level before calling parent constructor | ||
|
||
parent::initController($request,$response,$logger); | ||
|
||
// Load required services | ||
$this->validation = \Config\Services::validation(); | ||
$this->migrate = \Config\Services::migrations(); | ||
|
||
// Load required helpers | ||
helper('form'); | ||
|
||
//get db instance | ||
$this->db = \CodeIgniter\Database\Config::connect(); | ||
} | ||
|
||
public function index() | ||
{ | ||
// Display migration form | ||
$this->display_view('\Stock\Views\migration\migration_form'); | ||
} | ||
|
||
public function toLatest() | ||
{ | ||
if (!empty($_POST)) | ||
{ | ||
// Check the given password before doing the migration | ||
$validationRules = [ | ||
'password' => 'required|min_length[10]' | ||
]; | ||
|
||
if ($this->validate($validationRules)) | ||
{ | ||
if ($_POST['password'] == 'uzdSb8U8ZUD5h24') | ||
{ | ||
try | ||
{ | ||
// Migrate to latest | ||
$this->migrate->setNamespace('Stock')->latest(); | ||
|
||
// Delete migration files | ||
unlink(ROOTPATH.'orif/stock/Views/migration/migration_form.php'); | ||
rmdir(ROOTPATH.'orif/stock/Views/migration'); | ||
unlink(ROOTPATH.'orif/stock/Controllers/Migrate.php'); | ||
|
||
// Go back to homepage | ||
return redirect()->to(base_url()); | ||
} | ||
catch(Exception $e) | ||
{ | ||
// Display migration form with error message | ||
session()->setFlashdata('migration-error', lang('migrate_lang.err_msg_migration_failed') . $e->getMessage()); | ||
return $this->display_view('\Stock\Views\migration\migration_form'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Display migration form | ||
return $this->display_view('\Stock\Views\migration\migration_form'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
$session = \Config\Services::session(); | ||
$validation = \Config\Services::validation(); | ||
?> | ||
|
||
<div class="container"> | ||
<?php echo form_open('stock/migrate/toLatest', 'password'); ?> | ||
<!-- TITLE --> | ||
<div class="row"> | ||
<div class="col"> | ||
<h1 class="title-section"><?= lang('migrate_lang.title_migration') ?></h1> | ||
</div> | ||
</div> | ||
|
||
<!-- ERROR MESSAGES --> | ||
<?php if ( ! is_null($session->getFlashdata('migration-error'))) : ?> | ||
<div class="alert alert-danger text-justify" role="alert"> | ||
<?= $session->getFlashdata('migration-error'); ?> | ||
</div> | ||
<?php endif ?> | ||
|
||
<!-- INFORMATION MESSAGE --> | ||
<div class="col-12 alert alert-info"> | ||
<?= lang("migrate_lang.warning"); ?> | ||
</div> | ||
|
||
<div class="col form-group pl-0 pr-0"> | ||
<?= form_label(lang('user_lang.field_password'), 'password', ['class' => 'form-label']); ?> | ||
<?= form_password('password', '', [ | ||
'class' => 'form-control', 'id' => 'password' | ||
]); ?> | ||
<span class="text-danger"><?= $validation->showError('password'); ?></span> | ||
</div> | ||
|
||
<!-- FORM BUTTONS --> | ||
<div class="row"> | ||
<div class="col text-right"> | ||
<a class="btn btn-default" href="<?= base_url() ?>"><?= lang('common_lang.btn_cancel'); ?></a> | ||
<?= form_submit('send', lang('migrate_lang.btn_migrate'), ['class' => 'btn btn-danger']); ?> | ||
</div> | ||
</div> | ||
<?php form_close(); ?> | ||
</div> |