Skip to content

Commit

Permalink
After init changes
Browse files Browse the repository at this point in the history
 DB deep coding
  • Loading branch information
SilverFire committed Apr 22, 2015
1 parent da4b87f commit bbcab8d
Show file tree
Hide file tree
Showing 11 changed files with 475 additions and 57 deletions.
60 changes: 60 additions & 0 deletions controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,65 @@

class AccountController extends \hipanel\base\CrudController
{
public function actionCreateFtp () {
return $this->actionCreate('ftponly');
}

public function actionCreate ($type = 'user') {
if (!in_array($type, ['user', 'ftponly'])) {
throw new NotFoundHttpException('Account type is unknown');
}
$model = new Account(['scenario' => 'insert-' . $type]);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}

return $this->render('create', [
'model' => $model,
'type' => $type,
]);
}

public function actionSetPassword ($id) {
$model = $this->findModel($id);
$model->scenario = 'set-password';
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()) {
Yii::$app->getSession()->addFlash('success', [
'title' => $model->login,
'text' => \Yii::t('app', 'Password changing task has been successfully added to queue'),
]);

return $this->redirect(['view', 'id' => $model->id]);
} else {
/// TODO: do
return $this->render('view', ['model' => $model]);
}
}

/**
* @param $id
* @return string|yii\web\Response
* @throws NotFoundHttpException
*/
public function actionSetAllowedIps ($id) {
$model = $this->findModel($id);
$model->scenario = 'set-allowed-ips';
if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()) {
$flash = [
'type' => 'success',
'text' => Yii::t('app', 'Allowed IPs changing task has been successfully added to queue')
];
} else {
$flash['type'] = 'error';
if ($model->hasErrors()) {
$flash['text'] = $model->getFirstError('sshftp_ips');
} else {
$flash['text'] = Yii::t('app', 'An error occurred when trying to change allowed IPs');
}
}

Yii::$app->getSession()->addFlash($flash['type'], $flash['text']);

return $this->redirect(['view', 'id' => $model->id]);
}
}
75 changes: 74 additions & 1 deletion controllers/DBController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,80 @@

namespace hipanel\modules\hosting\controllers;

class DBController extends \hipanel\base\CrudController
use hipanel\base\CrudController;
use hipanel\modules\hosting\models\DB;
use Yii;
use yii\filters\VerbFilter;

class DBController extends CrudController
{
public function behaviors () {
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'create' => ['get', 'post']
]
]
];
}

public function actionCreate () {
$model = new Db(['scenario' => 'create']);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}

return $this->render('create', ['model' => $model]);
}

public function actionDelete () {
return $this->perform([
'success' => [
'message' => Yii::t('app', 'DB deleting task has been created successfully'),
'result' => [
'POST pjax' => [
'action',
['index'],
'addFlash' => true,
'changeUrl' => function ($model) {
return ['index'];
}
]
],
],
'error' => [
'message' => Yii::t('app', 'Error while deleting DB'),
'result' => [
'POST pjax' => ['action', ['view', function ($model) { return ['id' => $model->id]; }], 'addFlash' => true]
],
],
]);
}

public function actionSetPassword () {
return $this->perform([
'result' => [ ///
'POST pjax' => ['action', ['view', function ($model) { return ['id' => $model->id]; }], 'addFlash' => true]
],
]);
}

public function actionTruncate () {
return $this->perform([
'success' => [
'message' => Yii::t('app', 'DB truncate task has been created successfully'),
],
'error' => [
'message' => Yii::t('app', 'Error while truncating DB'),
],
'result' => [
'pjax' => [
'action',
['view', function ($model) { return ['id' => $model->id]; }],
]
],
'addFlash' => true
]);
}
}
52 changes: 46 additions & 6 deletions grid/DBGridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,57 @@

namespace hipanel\modules\hosting\grid;

use frontend\modules\server\grid\ServerColumn;
use hipanel\grid\ActionColumn;
use hipanel\grid\BoxedGridView;
use hipanel\grid\MainColumn;
use hipanel\grid\EditableColumn;
use hipanel\grid\RefColumn;
use hipanel\modules\hosting\widgets\db\State;
use Yii;

class DBGridView extends \hipanel\grid\BoxedGridView
class DbGridView extends BoxedGridView
{
static public function defaultColumns()
{
static public function defaultColumns () {
return [
'db' => [
'class' => MainColumn::className(),
'filterAttribute' => 'db_like',
'db' => [
'class' => MainColumn::className(),
'attribute' => 'name',
'filterAttribute' => 'name_like'
],
'state' => [
'class' => RefColumn::className(),
'format' => 'raw',
'value' => function ($model) {
return State::widget(compact('model'));
},
'gtype' => 'state,db',
],
'server' => [
'class' => ServerColumn::className()
],
'service_ip' => [
'filter' => false
],
'description' => [
'class' => EditableColumn::className(),
'filter' => true,
'popover' => Yii::t('app', 'Make any notes for your convenience'),
'action' => ['set-description'],
],
'password' => [
'class' => EditableColumn::className(),
'filter' => true,
'popover' => Yii::t('app', 'Change the DB password'),
'action' => ['set-password'],
'value' => function () {
return Yii::t('app', 'Change password');
}
],
'actions' => [
'class' => ActionColumn::className(),
'template' => '{view} {update} {truncate} {delete}'
]
];
}
}
51 changes: 38 additions & 13 deletions models/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,54 @@

namespace hipanel\modules\hosting\models;

use hipanel\base\Model;
use hipanel\base\ModelTrait;
use hipanel\modules\hosting\validators\DbNameValidator;
use Yii;

class DB extends \hipanel\base\Model
class Db extends Model
{
use ModelTrait;

use \hipanel\base\ModelTrait;
/** @inheritdoc */
public function rules () {
return [
[['id', 'account_id', 'client_id', 'seller_id', 'service_id', 'device_id', 'server_id'], 'integer'],
[['name', 'account', 'client', 'seller', 'service', 'device', 'server'], 'safe'],
[['service_ip', 'description'], 'safe'],
[['type', 'state', 'backuping_type', 'type_label', 'state_label', 'backuping_type_label'], 'safe'],
/// Create
[['server', 'account', 'service_id', 'name', 'password'], 'required', 'on' => 'create'],
[['name'], DbNameValidator::className(), 'on' => 'create'],
[
['password'],
'match',
'pattern' => '/^[\x20-\x7f]+$/',
'message' => \Yii::t('app', '{attribute} should not contain non-latin characters'),
'on' => ['create', 'set-password']
],
[['password'], 'required', 'on' => ['set-password']]
];
}

/**
* @inheritdoc
*/
public function rules ()
{
public function scenarios () {
return [
'create' => ['server', 'account', 'service_id', 'name', 'password', 'description'],
'truncate' => ['id'],
'set-description' => ['id', 'description'],
'set-password' => ['id', 'password'],
'delete' => ['id'],
];
}

/**
* @inheritdoc
*/
public function attributeLabels ()
{
/** @inheritdoc */
public function attributeLabels () {
return $this->mergeAttributeLabels([
'remoteid' => Yii::t('app', 'Remote ID'),
'name' => Yii::t('app', 'DB name'),
'service_ip' => Yii::t('app', 'Service IP'),
'service_id' => Yii::t('app', 'Service'),
'backuping_type' => Yii::t('app', 'Type of backuping'),
'backuping_type_label' => Yii::t('app', 'Backuping type label'),
]);
}
}
6 changes: 4 additions & 2 deletions models/DBSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace hipanel\modules\hosting\models;

class DBSearch extends DB{
use \hipanel\base\SearchModelTrait;
use hipanel\base\SearchModelTrait;

class DbSearch extends Db {
use SearchModelTrait;
}
22 changes: 22 additions & 0 deletions validators/DbNameValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace hipanel\modules\hosting\validators;
use yii\validators\RegularExpressionValidator;

/**
* Class LoginValidator is used to validate logins of clients
*/
class DbNameValidator extends RegularExpressionValidator
{
/**
* @inheritdoc
*/
public $pattern = '/^[a-z0-9_-]{2,31}$/';

/**
* @inheritdoc
*/
public function init () {
$this->message = \Yii::t('app', '{attribute} should contain only letters, digits, underscores or hyphens and be at least 2 symbols length');
}
}
58 changes: 58 additions & 0 deletions views/db/_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/* @var $this View */
/* @var $model frontend\modules\hosting\models\Db */
/* @var $type string */

use hipanel\base\View;
use hipanel\widgets\Combo2;
use hipanel\widgets\PasswordInput;
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\helpers\Url;

?>
<div class="row">
<div class="col-md-4">
<div class="box box-danger">
<div class="box-body">
<div class="ticket-form" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
<?php $form = ActiveForm::begin([
'action' => $model->isNewRecord ? Url::to('create') : Url::toRoute([
'update',
'id' => $model->id
]),
]);
?>
<!-- Properties -->

<?php
print $form->field($model, 'client')->widget(Combo2::className(), ['type' => 'client']);
print $form->field($model, 'server')->widget(Combo2::className(), ['type' => 'server']);
print $form->field($model, 'account')->widget(Combo2::className(), ['type' => 'account']);
print $form->field($model, 'service_id')->widget(Combo2::className(), ['type' => 'dbService', ]);

print $form->field($model, 'name');
print $form->field($model, 'password')->widget(PasswordInput::className());

print $form->field($model, 'description');
?>

<div class="form-group">
<?= Html::submitButton(Yii::t('app', 'Create'), ['class' => 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>
</div>
</div>
</div>

<!-- ticket-_form -->
</div>
</div>

<?php

$this->registerJs("
$('#account-sshftp_ips').popover({placement: 'top', trigger: 'focus'});
");
13 changes: 13 additions & 0 deletions views/db/create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/* @var $this yii\web\View */
/* @var $model frontend\modules\ticket\models\Thread */
/* @var $type string */

$this->title = $title[$type];
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Databases'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="account-create">
<?= $this->render('_form', compact('model')) ?>
</div>
Loading

0 comments on commit bbcab8d

Please sign in to comment.