diff --git a/models/Account.php b/models/Account.php index 60844c4f..9787d4ff 100644 --- a/models/Account.php +++ b/models/Account.php @@ -19,7 +19,7 @@ class Account extends \hipanel\base\Model public function init() { - $this->on(static::EVENT_BEFORE_INSERT, [$this, 'onBeforeInsert']); + $this->on(static::EVENT_BEFORE_VALIDATE, [$this, 'onBeforeValidate']); } public function rules() @@ -35,7 +35,7 @@ public function rules() [ [ 'login', - 'server_id', + 'server', 'password', 'sshftp_ips', 'type', @@ -46,7 +46,7 @@ public function rules() [ [ 'login', - 'server_id', + 'server', 'password', 'sshftp_ips', 'type', @@ -147,7 +147,7 @@ public function scenarioCommands() ]; } - public function onBeforeInsert() + public function onBeforeValidate() { if ($this->scenario == 'create-user') { $this->type = 'user'; diff --git a/models/Hdomain.php b/models/Hdomain.php index 9410fe73..2d837ad9 100644 --- a/models/Hdomain.php +++ b/models/Hdomain.php @@ -17,10 +17,32 @@ class Hdomain extends \hipanel\base\Model public function rules () { return [ [ - ['id', 'server_id', 'client_id', 'seller_id', 'account_id', 'hdomain_id', 'state_id', 'type_id', 'vhost_id', 'device_id'], + [ + 'id', 'server_id', 'client_id', 'seller_id', + 'account_id', 'hdomain_id', 'state_id', 'type_id', + 'vhost_id', 'device_id' + ], 'integer' ], - [['server', 'vhost', 'device', 'client', 'seller', 'account', 'domain', 'state', 'type', 'ip', 'alias'], 'safe'], + [ + [ + 'server', 'vhost', 'device', 'client', 'seller', + 'account', 'domain', 'state', 'type', 'ip', 'alias', + 'backend_ip', 'backuping_type' + ], + 'safe' + ], + [ + [ + 'server', 'account', 'domain', + 'with_www', 'path', 'ip', + ], + 'required', 'on' => 'create' + ], + [ + ['proxy_enable'], 'safe', 'on' => 'create' + ] + ]; } @@ -32,6 +54,7 @@ function getIsProxied() { public function attributeLabels () { return $this->mergeAttributeLabels([ 'domain' => Yii::t('app', 'Domain Name'), + 'backend_ip' => Yii::t('app', 'Backend IP'), ]); } } diff --git a/models/IpSearch.php b/models/IpSearch.php index 9c5b5959..401ca940 100644 --- a/models/IpSearch.php +++ b/models/IpSearch.php @@ -7,6 +7,27 @@ namespace hipanel\modules\hosting\models; +use hipanel\base\SearchModelTrait; +use yii\helpers\ArrayHelper; + class IpSearch extends Ip{ - use \hipanel\base\SearchModelTrait; + use SearchModelTrait { + searchAttributes as defaultSearchAttributes; + } + + /** + * @inheritdoc + */ + public function searchAttributes() + { + return ArrayHelper::merge($this->defaultSearchAttributes(), [ + 'with_expanded_ips', + 'show_only_device_link', + 'with_links', + 'not_tags', + 'soft_type', + 'server', + 'device', + ]); + } } diff --git a/views/hdomain/_form.php b/views/hdomain/_form.php index e20b8abe..90f3d98a 100644 --- a/views/hdomain/_form.php +++ b/views/hdomain/_form.php @@ -5,15 +5,97 @@ /* @var $type string */ use hipanel\base\View; +use hipanel\models\Ref; use hipanel\modules\client\widgets\combo\ClientCombo; use hipanel\modules\hosting\widgets\combo\AccountCombo; use hipanel\modules\hosting\widgets\combo\DbServiceCombo; +use hipanel\modules\hosting\widgets\ip\BackIpCombo; +use hipanel\modules\hosting\widgets\ip\FrontIpCombo; +use hipanel\modules\hosting\widgets\ip\HdomainIpCombo; use hipanel\modules\server\widgets\combo\ServerCombo; use hipanel\widgets\PasswordInput; +use hiqdev\combo\StaticCombo; use yii\helpers\Html; use yii\bootstrap\ActiveForm; use yii\helpers\Url; +$this->registerJs(<<<'JS' + $('.hdomain-create').on('change', '#hdomain-proxy_enable', function () { + var $checkbox = $(this); + var $scope = $(this).closest('form'); + var $proxied = $scope.find('.field-hdomain-frontend_ip, .field-hdomain-backend_ip'); + var $not_proxied = $scope.find('.field-hdomain-not-proxied_ip'); + + if ($checkbox.prop('checked')) { + $proxied.removeClass('hidden'); + + if ($scope.find('#hdomain-account').select2('data')) { + $proxied.find('input').select2('enable', true); + } else { + $proxied.find('input').select2('enable', false); + } + + $not_proxied.addClass('hidden').find('input').prop({'required': false}).select2('enable', false); + } else { + $proxied.addClass('hidden').find('input').select2('enable', false); + $not_proxied.removeClass('hidden'); + + if ($scope.find('#hdomain-account').select2('data')) { + $not_proxied.find('input').select2('enable', true); + } else { + $not_proxied.find('input').select2('enable', false); + } + } + }); +JS +); + +$this->registerJs(<<<'JS' +/* +TODO: after ABAC - for admin + $('.hdomain-create').find('.field-hdomain-path').on('updatePath', function (e, update) { + var $path = $(this); + var s_path = $path.val().split('/'); + if (s_path.length < 2 || s_path[1] != 'home') { + s_path = ['', 'home', '']; + } + + // 0 - / + // 1 - home + // 2 - user + // 3 - domain + + if (update.account) { + if (s_path.length > 1) { + s_path[2] = update.account; + if (!s_path[3]) s_path[3] = ''; + } + } else if (update.domain) { + s_path[s_path.length - 1] = update.domain.replace(/\\//g, ''); /// удаляем слеши, чтобы не собрать паровоз из пути + } else if (update.clear) { + s_path = []; + } + + $path.val(s_path.join('/')); + }); +*/ + + $('.hdomain-create').find('.field-hdomain-path').on('updatePath', function (e, update) { + var $group = $(this); + var $input = $group.find('input'); + var $span = $group.find('span'); + + if (update.account) $span.text('/home/' + update.account + '/'); + if (update.domain !== undefined) $input.val(update.domain); + if (update.clear) $span.val(''); + }); + + $('.hdomain-create').on('keyup keypress blur change', '.field-hdomain-domain input', function () { + var $scope = $(this).closest('form'); + $scope.find('.field-hdomain-path').trigger('updatePath', {domain: $(this).val()}); + }); +JS +); ?>
@@ -21,10 +103,11 @@
$model->isNewRecord ? Url::to('create') : Url::toRoute([ + 'action' => $model->isNewRecord ? Url::to('create') : Url::toRoute([ 'update', 'id' => $model->id ]), + 'options' => ['class' => 'hdomain-create'] ]); ?> @@ -32,12 +115,50 @@ field($model, 'client')->widget(ClientCombo::className()); print $form->field($model, 'server')->widget(ServerCombo::className()); - print $form->field($model, 'account')->widget(AccountCombo::className()); - + print $form->field($model, 'account')->widget(AccountCombo::className(), [ + 'pluginOptions' => [ + 'onChange' => new \yii\web\JsExpression(<<<'JS' + function (event) { + var $form = event.element.closest('form'); + var data; + if (event.added) { + data = {account: event.added.text}; + } else { + data = {clear: true}; + } + $form.find('.field-hdomain-path').trigger('updatePath', data); + return true; + } +JS + ) + ] + ]); print $form->field($model, 'domain'); - + print $form->field($model, 'path', [ + 'template' => ' + {label} +
+ /home/ + {input} + {hint} + {error} +
+ ' + ]); + print $form->field($model, 'with_www')->checkbox(); + print $form->field($model, 'proxy_enable')->checkbox(); + print $form->field($model, 'ip', ['options' => ['class' => 'field-hdomain-not-proxied_ip']]) + ->widget(HdomainIpCombo::className()); + print $form->field($model, 'ip', ['options' => ['class' => 'field-hdomain-frontend_ip hidden']]) + ->widget(FrontIpCombo::className(), ['inputOptions' => ['id' => 'hdomain-frontend_ip']]) + ->label('Frontend IP'); + print $form->field($model, 'backend_ip', ['options' => ['class' => 'field-hdomain-frontend_ip hidden']]) + ->widget(BackIpCombo::className()); + print $form->field($model, 'backuping_type')->widget(StaticCombo::className(), [ + 'hasId' => true, + 'data' => Ref::getList('type,backuping') + ]); ?> -
'btn btn-primary']) ?>
diff --git a/widgets/combo/AccountCombo.php b/widgets/combo/AccountCombo.php index bf1c56e2..08a14cce 100644 --- a/widgets/combo/AccountCombo.php +++ b/widgets/combo/AccountCombo.php @@ -31,13 +31,14 @@ class AccountCombo extends Combo 'server' => 'server/server', ]; - /** @inheritdoc */ - public $_pluginOptions = [ - 'clearWhen' => ['client/client', 'server/server'], - 'affects' => [ - 'client/seller' => 'seller', - 'client/client' => 'client', - 'server/server' => 'device', - ] - ]; + public function getPluginOptions($config) { + return parent::getPluginOptions([ + 'clearWhen' => ['client/client', 'server/server'], + 'affects' => [ + 'client/seller' => 'seller', + 'client/client' => 'client', + 'server/server' => 'device', + ] + ]); + } } \ No newline at end of file diff --git a/widgets/ip/BackIpCombo.php b/widgets/ip/BackIpCombo.php new file mode 100644 index 00000000..65061849 --- /dev/null +++ b/widgets/ip/BackIpCombo.php @@ -0,0 +1,27 @@ + ['format' => ['apache','apache2']], + ]); + } +} \ No newline at end of file diff --git a/widgets/ip/FrontIpCombo.php b/widgets/ip/FrontIpCombo.php new file mode 100644 index 00000000..9eb9a579 --- /dev/null +++ b/widgets/ip/FrontIpCombo.php @@ -0,0 +1,27 @@ + ['format' => ['nginx']], + ]); + } +} \ No newline at end of file diff --git a/widgets/ip/HdomainIpCombo.php b/widgets/ip/HdomainIpCombo.php new file mode 100644 index 00000000..708c1483 --- /dev/null +++ b/widgets/ip/HdomainIpCombo.php @@ -0,0 +1,89 @@ + 'ip' + ]; + + /** @inheritdoc */ + public $_filter = [ + 'client' => 'client/client', + 'server' => 'server/server', + ]; + + public function getPluginOptions() + { + return parent::getPluginOptions([ + 'activeWhen' => [ + 'hosting/account', + ], + 'select2Options' => [ + 'ajax' => [ + 'results' => new JsExpression(" + function (data) { + var ret = []; + var used_ips = {}; + $.each(data, function (i, v) { + $.each(v.expanded_ips, function (ip, ipval) { + if (used_ips[ip]) return; + + var row = {id: ip}; + used_ips[ip] = true + if (v.links) { + $.each(v.links, function(k,link) { + row.text = link.service + ': ' + ip; + }); + } else { + row.text = ip; + } + ret.push(row); + }); + }); + + return {results: ret}; + } + ") + ], + ] + ]); + } + + /** @inheritdoc */ + public function getFilter() + { + return ArrayHelper::merge(parent::getFilter(), [ + 'soft_type' => ['format' => 'web'], + 'state_in' => ['format' => ['ok']], + 'not_tags' => ['format' => 'aux'], + 'with_links' => ['format' => '1'], + 'show_only_device_link' => ['format' => '1'], + 'with_expanded_ips' => ['format' => '1'], + ]); + } +} \ No newline at end of file