add company

This commit is contained in:
king199025 2018-10-11 17:24:47 +03:00
parent 3e10a51c3a
commit 5e2bc02bb5
21 changed files with 720 additions and 10 deletions

View File

@ -25,6 +25,9 @@ return [
'project' => [ 'project' => [
'class' => 'backend\modules\project\Project', 'class' => 'backend\modules\project\Project',
], ],
'company' => [
'class' => 'backend\modules\company\Company',
],
], ],
'components' => [ 'components' => [
'request' => [ 'request' => [

View File

@ -0,0 +1,24 @@
<?php
namespace backend\modules\company;
/**
* company module definition class
*/
class Company extends \yii\base\Module
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'backend\modules\company\controllers';
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
// custom initialization code goes here
}
}

View File

@ -0,0 +1,127 @@
<?php
namespace backend\modules\company\controllers;
use Yii;
use backend\modules\company\models\Company;
use backend\modules\company\models\CompanySearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* CompanyController implements the CRUD actions for Company model.
*/
class CompanyController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Company models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new CompanySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Company model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Company model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Company();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Company model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('update', [
'model' => $model,
]);
}
/**
* Deletes an existing Company model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Company model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Company the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Company::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace backend\modules\company\models;
use common\models\FieldsValue;
class Company extends \common\models\Company
{
public $fields;
public function init()
{
parent::init();
$fieldValue = FieldsValue::find()->where(
[
'company_id' => \Yii::$app->request->get('id'),
'project_id' => null,
'card_id' => null,
])
->all();
$array = [];
if(!empty($fieldValue)){
foreach ($fieldValue as $item){
array_push($array, ['field_id' => $item->field_id, 'value' => $item->value, 'order' => $item->order]);
}
$this->fields = $array;
}
else{
$this->fields = [
[
'field_id' => null,
'value' => null,
'order' => null,
],
];
}
}
public function afterSave($insert, $changedAttributes)
{
$post = \Yii::$app->request->post('Company');
FieldsValue::deleteAll(['company_id' => $this->id]);
foreach ( $post['fields'] as $item) {
$fildsValue = new FieldsValue();
$fildsValue->field_id = $item['field_id'];
$fildsValue->value = $item['value'];
$fildsValue->order = $item['order'];
$fildsValue->company_id = $this->id;
$fildsValue->save();
}
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
}

View File

@ -0,0 +1,73 @@
<?php
namespace backend\modules\company\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\company\models\Company;
/**
* CompanySearch represents the model behind the search form of `backend\modules\company\models\Company`.
*/
class CompanySearch extends Company
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id', 'status_id'], 'integer'],
[['name', 'description', 'created_at', 'updated_at'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Company::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'status_id' => $this->status_id,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'description', $this->description]);
return $dataProvider;
}
}

View File

@ -0,0 +1,76 @@
<?php
use unclead\multipleinput\MultipleInput;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\company\models\Company */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="company-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'status_id')
->dropDownList(\yii\helpers\ArrayHelper::map(
\common\models\Status::find()
->joinWith('useStatuses')
->where(['`use_status`.`use`' => \common\models\UseStatus::USE_COMPANY])->all(), 'id', 'name'),
[
'prompt' => 'Выберите'
]
) ?>
<div class="row">
<div class="col-xs-12">
<?= $form->field($model, 'fields')->widget(MultipleInput::class, [
'columns' => [
[
'name' => 'field_id',
'type' => 'dropDownList',
'title' => 'Поле',
'defaultValue' => null,
'items' => \yii\helpers\ArrayHelper::map(\backend\modules\fields\models\AdditionalFields::find()
->joinWith('useFields')
->where(['`use_field`.`use`' => \common\models\UseStatus::USE_COMPANY])
->all(),
'id', 'name'),
'options' => ['prompt' => 'Выберите']
],
[
'name' => 'value',
'title' => 'Значение',
'enableError' => true,
'options' => [
'class' => 'input-priority'
]
],
[
'name' => 'order',
'title' => 'Приоритет',
'enableError' => true,
'options' => [
'class' => 'input-priority'
]
]
]
])->label('Дополнительно');
?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,37 @@
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\company\models\CompanySearch */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="company-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'name') ?>
<?= $form->field($model, 'description') ?>
<?= $form->field($model, 'status_id') ?>
<?= $form->field($model, 'created_at') ?>
<?php // echo $form->field($model, 'updated_at') ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,20 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\company\models\Company */
$this->title = 'Create Company';
$this->params['breadcrumbs'][] = ['label' => 'Companies', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="company-create">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,37 @@
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\company\models\CompanySearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Companies';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="company-index">
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a('Create Company', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'name',
'description:ntext',
'status_id',
//'created_at',
//'updated_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>

View File

@ -0,0 +1,20 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\company\models\Company */
$this->title = 'Update Company: ' . $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Companies', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Update';
?>
<div class="company-update">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,37 @@
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model backend\modules\company\models\Company */
$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Companies', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="company-view">
<p>
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => 'Are you sure you want to delete this item?',
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'name',
'description:ntext',
'status_id',
'created_at',
'updated_at',
],
]) ?>
</div>

View File

@ -68,7 +68,9 @@ class StatusController extends Controller
$model = new Status(); $model = new Status();
if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]); Yii::$app->session->addFlash('success', 'Статус добавлен');
return $this->redirect(['index']);
} }
return $this->render('create', [ return $this->render('create', [
@ -88,7 +90,8 @@ class StatusController extends Controller
$model = $this->findModel($id); $model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]); Yii::$app->session->addFlash('success', 'Статус обновлен');
return $this->redirect(['index']);
} }
$model->use = ArrayHelper::getColumn( $model->use = ArrayHelper::getColumn(
\common\models\UseStatus::find()->where(['status_id' => $model->id])->asArray()->all(), \common\models\UseStatus::find()->where(['status_id' => $model->id])->asArray()->all(),

View File

@ -12,11 +12,9 @@ $this->params['breadcrumbs'][] = $this->title;
?> ?>
<div class="status-view"> <div class="status-view">
<h1><?= Html::encode($this->title) ?></h1>
<p> <p>
<?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?> <?= Html::a('Редактировать', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['delete', 'id' => $model->id], [ <?= Html::a('Удалить', ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger', 'class' => 'btn btn-danger',
'data' => [ 'data' => [
'confirm' => 'Are you sure you want to delete this item?', 'confirm' => 'Are you sure you want to delete this item?',

View File

@ -9,6 +9,7 @@
['label' => 'Доп. поля', 'icon' => 'file-text-o', 'url' => ['/fields/additional-fields']], ['label' => 'Доп. поля', 'icon' => 'file-text-o', 'url' => ['/fields/additional-fields']],
['label' => 'Профили', 'icon' => 'users', 'url' => ['/card/user-card']], ['label' => 'Профили', 'icon' => 'users', 'url' => ['/card/user-card']],
['label' => 'Пректы', 'icon' => 'files-o', 'url' => ['/project/project']], ['label' => 'Пректы', 'icon' => 'files-o', 'url' => ['/project/project']],
['label' => 'Компании', 'icon' => 'files-o', 'url' => ['/company/company']],
/*['label' => 'Gii', 'icon' => 'file-code-o', 'url' => ['/gii']], /*['label' => 'Gii', 'icon' => 'file-code-o', 'url' => ['/gii']],
['label' => 'Debug', 'icon' => 'dashboard', 'url' => ['/debug']], ['label' => 'Debug', 'icon' => 'dashboard', 'url' => ['/debug']],

View File

@ -19,3 +19,11 @@
margin: 3px; margin: 3px;
object-fit: cover; object-fit: cover;
} }
#status-use, #additionalfields-use {
display: flex;
flex-wrap: wrap;
width: 300px;
max-height: 100px;
flex-direction: column;
}

75
common/models/Company.php Normal file
View File

@ -0,0 +1,75 @@
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "company".
*
* @property int $id
* @property string $name
* @property string $description
* @property int $status_id
* @property string $created_at
* @property string $updated_at
*
* @property Status $status
* @property FieldsValue[] $fieldsValues
*/
class Company extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'company';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name'], 'required'],
[['description'], 'string'],
[['status_id'], 'integer'],
[['created_at', 'updated_at'], 'safe'],
[['name'], 'string', 'max' => 255],
[['status_id'], 'exist', 'skipOnError' => true, 'targetClass' => Status::className(), 'targetAttribute' => ['status_id' => 'id']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Название',
'description' => 'Описание',
'status_id' => 'Статус',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getStatus()
{
return $this->hasOne(Status::className(), ['id' => 'status_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getFieldsValues()
{
return $this->hasMany(FieldsValue::className(), ['company_id' => 'id']);
}
}

View File

@ -9,12 +9,15 @@ use Yii;
* *
* @property int $id * @property int $id
* @property int $card_id * @property int $card_id
* @property int $project_id
* @property int $field_id * @property int $field_id
* @property string $value * @property string $value
* @property int $order * @property int $order
* @property int $project_id
* @property int $company_id
* *
* @property AdditionalFields $field * @property AdditionalFields $field
* @property Company $company
* @property Project $project
* @property UserCard $card * @property UserCard $card
*/ */
class FieldsValue extends \yii\db\ActiveRecord class FieldsValue extends \yii\db\ActiveRecord
@ -34,9 +37,10 @@ class FieldsValue extends \yii\db\ActiveRecord
{ {
return [ return [
[['field_id', 'value'], 'required'], [['field_id', 'value'], 'required'],
[['card_id', 'field_id', 'order', 'project_id'], 'integer'], [['card_id', 'field_id', 'order', 'project_id', 'company_id'], 'integer'],
[['value'], 'string', 'max' => 255], [['value'], 'string', 'max' => 255],
[['field_id'], 'exist', 'skipOnError' => true, 'targetClass' => AdditionalFields::class, 'targetAttribute' => ['field_id' => 'id']], [['field_id'], 'exist', 'skipOnError' => true, 'targetClass' => AdditionalFields::class, 'targetAttribute' => ['field_id' => 'id']],
[['company_id'], 'exist', 'skipOnError' => true, 'targetClass' => Company::class, 'targetAttribute' => ['company_id' => 'id']],
[['project_id'], 'exist', 'skipOnError' => true, 'targetClass' => Project::class, 'targetAttribute' => ['project_id' => 'id']], [['project_id'], 'exist', 'skipOnError' => true, 'targetClass' => Project::class, 'targetAttribute' => ['project_id' => 'id']],
[['card_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserCard::class, 'targetAttribute' => ['card_id' => 'id']], [['card_id'], 'exist', 'skipOnError' => true, 'targetClass' => UserCard::class, 'targetAttribute' => ['card_id' => 'id']],
]; ];
@ -53,6 +57,7 @@ class FieldsValue extends \yii\db\ActiveRecord
'field_id' => 'Field ID', 'field_id' => 'Field ID',
'value' => 'Value', 'value' => 'Value',
'project_id' => 'Project ID', 'project_id' => 'Project ID',
'company_id' => 'Company ID',
]; ];
} }
@ -64,6 +69,14 @@ class FieldsValue extends \yii\db\ActiveRecord
return $this->hasOne(AdditionalFields::class, ['id' => 'field_id']); return $this->hasOne(AdditionalFields::class, ['id' => 'field_id']);
} }
/**
* @return \yii\db\ActiveQuery
*/
public function getCompany()
{
return $this->hasOne(Company::className(), ['id' => 'company_id']);
}
/** /**
* @return \yii\db\ActiveQuery * @return \yii\db\ActiveQuery
*/ */

View File

@ -20,6 +20,7 @@ class UseField extends \yii\db\ActiveRecord
{ {
const USE_PROFILE = 0; const USE_PROFILE = 0;
const USE_PROJECT = 1; const USE_PROJECT = 1;
const USE_COMPANY = 2;
/** /**
@ -66,7 +67,8 @@ class UseField extends \yii\db\ActiveRecord
{ {
return [ return [
self::USE_PROFILE => 'Профиль', self::USE_PROFILE => 'Профиль',
self::USE_PROJECT => 'Проект' self::USE_PROJECT => 'Проект',
self::USE_COMPANY => 'Компания'
]; ];
} }

View File

@ -20,6 +20,7 @@ class UseStatus extends \yii\db\ActiveRecord
{ {
const USE_PROFILE = 0; const USE_PROFILE = 0;
const USE_PROJECT = 1; const USE_PROJECT = 1;
const USE_COMPANY = 2;
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -65,7 +66,8 @@ class UseStatus extends \yii\db\ActiveRecord
{ {
return [ return [
self::USE_PROFILE => 'Профиль', self::USE_PROFILE => 'Профиль',
self::USE_PROJECT => 'Проект' self::USE_PROJECT => 'Проект',
self::USE_COMPANY => 'Компания'
]; ];
} }

View File

@ -0,0 +1,44 @@
<?php
use yii\db\Migration;
/**
* Handles the creation of table `company`.
*/
class m181011_140628_create_company_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('company', [
'id' => $this->primaryKey(),
'name' => $this->string()->notNull(),
'description' => $this->text(),
'status_id' => $this->integer(),
'created_at' => $this->dateTime(),
'updated_at' => $this->dateTime(),
]);
$this->addForeignKey(
'company_ibfk_status',
'company',
'status_id',
'status',
'id',
'RESTRICT',
'CASCADE'
);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropForeignKey('company_ibfk_status', 'company');
$this->dropTable('company');
}
}

View File

@ -0,0 +1,52 @@
<?php
use yii\db\Migration;
/**
* Class m181011_140904_add_column_to_fields_value
*/
class m181011_140904_add_column_to_fields_value extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('fields_value', 'company_id', $this->integer(11));
$this->addForeignKey(
'fields_value_ibfk_company',
'fields_value',
'company_id',
'company',
'id',
'RESTRICT',
'CASCADE'
);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropForeignKey('fields_value_ibfk_company', 'fields_value');
$this->dropColumn('fields_value', 'project_id');
}
/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m181011_140904_add_column_to_fields_value cannot be reverted.\n";
return false;
}
*/
}