Skip to content

Commit

Permalink
Service - added objects count
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire committed Nov 20, 2015
1 parent b177286 commit 41427fc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/controllers/ServiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function actions()
/** @var \hipanel\actions\SearchAction $action */
$action = $event->sender;
$dataProvider = $action->getDataProvider();
$dataProvider->query->joinWith('ips');
$dataProvider->query->joinWith('ips')->joinWith('objects_count');
},

],
Expand Down
31 changes: 28 additions & 3 deletions src/grid/ServiceGridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

use hipanel\grid\ActionColumn;
use hipanel\grid\MainColumn;
use hipanel\modules\hosting\models\Service;
use hipanel\modules\server\grid\ServerColumn;
use hipanel\widgets\ArraySpoiler;
use hipanel\widgets\Label;
use kartik\helpers\Html;
use Yii;

Expand All @@ -20,17 +22,40 @@ static public function defaultColumns()
{
return [
'service' => [
'class' => MainColumn::className(),
'attribute' => 'name',
'filterAttribute' => 'service_like',
'class' => MainColumn::className(),
'attribute' => 'name',
'filterAttribute' => 'service_like',
],
'server_id' => [
'class' => ServerColumn::className(),
],
'object' => [
'format' => 'raw',
'value' => function ($model) {
$html = $model->name;
if ($model->objects_count > 0) {
$html .= ' ';
if ($model->soft_type === Service::SOFT_TYPE_DB) {
$labelOptions = [
'label' => Yii::t('hipanel/hosting', '{0, plural, one{# DB} other{# DBs}}', $model->objects_count),
'color' => 'default',
];
} elseif ($model->soft_type === Service::SOFT_TYPE_WEB) {
$labelOptions = [
'label' => Yii::t('hipanel/hosting', '{0, plural, one{# domain} other{# domains}}', $model->objects_count),
'color' => 'default',
];
} else {
$labelOptions = [
'label' => Yii::t('hipanel/hosting', '{0}', $model->objects_count),
'color' => 'default'
];
}

$html .= Label::widget($labelOptions); /// TODO create search url to search related
}

return $html;
},
],
'ip' => [
Expand Down
9 changes: 8 additions & 1 deletion src/models/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
namespace hipanel\modules\hosting\models;

use Yii;
use yii\base\InvalidConfigException;

class Service extends \hipanel\base\Model
{
const SOFT_TYPE_WEB = 'web';
const SOFT_TYPE_DB = 'db';

use \hipanel\base\ModelTrait;

/** @inheritdoc */
public function rules () {
return [
[['id', 'server_id', 'device_id', 'client_id', 'seller_id', 'soft_id'], 'integer'],
[['id', 'server_id', 'device_id', 'client_id', 'seller_id', 'soft_id', 'objects_count'], 'integer'],
[['name', 'server', 'device', 'client', 'seller', 'soft'], 'safe'],
[['ips', 'bin', 'etc'], 'safe'],
[['soft_type', 'soft_type_label', 'state', 'state_label'], 'safe'],
Expand All @@ -38,4 +41,8 @@ public function attributeLabels () {
public function getIps() {
return $this->hasMany(Ip::className(), ['service_id', 'id']);
}

public function getObjects_count() {
return null; // This relation is not available separately from search query.
}
}
2 changes: 1 addition & 1 deletion src/views/service/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
'checkbox',
'seller_id',
'client_id',
'service',
'object',
'ip',
'bin',
'etc',
Expand Down

0 comments on commit 41427fc

Please sign in to comment.