Merge pull request #25 from apuc/NotesAdd

Notes add
This commit is contained in:
kavalar 2019-12-06 08:55:56 +02:00 committed by GitHub
commit ded92c5d3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 591 additions and 12 deletions

View File

@ -38,6 +38,9 @@ return [
'holiday' => [
'class' => 'backend\modules\holiday\Holiday',
],
'notes' => [
'class' => 'backend\modules\notes\Notes',
],
],
'components' => [
'request' => [

View File

@ -71,18 +71,19 @@ class UserCard extends \common\models\UserCard
if ($post['fields']) {
FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_PROFILE]);
foreach ($post['fields'] as $item) {
$fildsValue = new FieldsValueNew();
$fildsValue->field_id = $item['field_id'];
$fildsValue->value = $item['value'];
$fildsValue->type_file = 'text';
if (substr($item['value'], 0, 1) == '/') {
$fildsValue->type_file = 'file';
$fieldsValue = new FieldsValueNew();
$fieldsValue->field_id = $item['field_id'];
$fieldsValue->value = $item['value'];
$fieldsValue->order = $item['order'];
$fieldsValue->item_id = $this->id;
$fieldsValue->item_type = FieldsValueNew::TYPE_PROFILE;
if(is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])){
$fieldsValue->type_file = 'file';
}else{
$fieldsValue->type_file = 'text';
}
$fildsValue->order = $item['order'];
$fildsValue->item_id = $this->id;
$fildsValue->item_type = FieldsValueNew::TYPE_PROFILE;
$fildsValue->save();
$fieldsValue->save();
}
}

24
backend/modules/notes/Notes.php Executable file
View File

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

View File

@ -0,0 +1,116 @@
<?php
namespace backend\modules\notes\controllers;
use backend\modules\notes\models\NoteSearch;
use Yii;
use backend\modules\notes\models\Note;
use common\models\FieldsValueNew;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
/**
* Default controller for the `notes` module
*/
class NotesController extends Controller
{
/**
* Renders the index view for the module
* @return string
*/
public function actionIndex()
{
$searchModel = new NoteSearch();
$dataProvider = $searchModel->search();
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider
]);
}
/**
* Creates a new note model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Note();
if ($model->load(\Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model
]);
}
/**
* Displays a single note model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
$additionalDataProvider = new ActiveDataProvider([
'query' => FieldsValueNew::find()
->where(['item_id' => $id, 'item_type' => FieldsValueNew::TYPE_NOTE])
->orderBy('order'),
'pagination' => [
'pageSize' => 200,
],
]);
return $this->render('view', [
'model' => Note::findOne($id),
'additionalDataProvider' => $additionalDataProvider
]);
}
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 Note 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 note model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Note the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Note::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace backend\modules\notes\models;
use Yii;
use common\models\FieldsValueNew;
class Note extends \common\models\Note
{
public $fields;
public function init()
{
parent::init();
$fieldValue = FieldsValueNew::find()
->where(
[
'item_id' => \Yii::$app->request->get('id'),
'item_type' => FieldsValueNew::TYPE_NOTE,
])
->all();
$array = [];
if(!empty($fieldValue)){
foreach ($fieldValue as $item){
array_push($array,
['field_id' => $item->field_id,
'value' => $item->value,
'order' => $item->order,
'field_name' => $item->field->name]);
}
$this->fields = $array;
}
else{
$this->fields = [
[
'field_id' => null,
'value' => null,
'order' => null,
'field_name' => null,
],
];
}
}
public function afterSave($insert, $changedAttributes)
{
$post = \Yii::$app->request->post('Note');
FieldsValueNew::deleteAll(['item_id' => $this->id, 'item_type' => FieldsValueNew::TYPE_NOTE]);
foreach ( $post['fields'] as $item) {
$fieldsValue = new FieldsValueNew();
$fieldsValue->field_id = $item['field_id'];
$fieldsValue->item_id = $this->id;
$fieldsValue->item_type = FieldsValueNew::TYPE_NOTE;
$fieldsValue->order = $item['order'];
$fieldsValue->value = $item['value'];
if(is_file(Yii::getAlias('@frontend') . '/web/' . $item['value'])){
$fieldsValue->type_file = 'file';
}else{
$fieldsValue->type_file = 'text';
}
$fieldsValue->save();
}
parent::afterSave($insert, $changedAttributes);
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace backend\modules\notes\models;
use Yii;
use yii\data\ActiveDataProvider;
class NoteSearch extends Note
{
public function rules()
{
return [
[['id'], 'integer'],
[['name', 'description', 'created_at', 'updated_at'], 'safe'],
];
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search()
{
$params = Yii::$app->request->queryParams;
$query = Note::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,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'description', $this->description]);
$query->orderBy('created_at DESC');
return $dataProvider;
}
}

View File

@ -0,0 +1,76 @@
<?php
use backend\modules\settings\models\AdditionalFields;
use mihaildev\elfinder\InputFile;
use unclead\multipleinput\MultipleInput;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\notes\models\Note */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="notes-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
<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(AdditionalFields::find()
->joinWith('useFields')
->where(['`use_field`.`use`' => \common\models\UseStatus::USE_NOTE])
->all(),
'id', 'name'),
'options' => ['prompt' => 'Выберите']
],
[
'name' => 'value',
'title' => 'Значение',
'type' => InputFile::className(),
'options' => [
'language' => 'ru',
'controller' => 'elfinder',
// вставляем название контроллера, по умолчанию равен elfinder
// фильтр файлов, можно задать массив фильтров https://github.com/Studio-42/elFinder/wiki/Client-con..
'name' => 'fields[value]',
'id' => 'fields-value',
'options' => ['class' => 'form-control itemImg', 'maxlength' => '255'],
'buttonOptions' => ['class' => 'btn btn-primary'],
'value' => $model->fields[0]['value'],
'buttonName' => 'Выбрать файл',
],
],
[
'name' => 'order',
'title' => 'Приоритет',
'enableError' => true,
'options' => [
'class' => 'input-priority'
]
]
]
])->label('Дополнительно');
?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>

View File

@ -0,0 +1,19 @@
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model backend\modules\notes\models\Note */
$this->title = 'Создать заметку';
$this->params['breadcrumbs'][] = ['label' => 'Заметки', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="notes-create">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,33 @@
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\notes\models\NoteSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Заметки';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="notes-index">
<p>
<?= Html::a('Добавить', ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
'name',
'description',
'created_at',
// 'updated_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>

View File

@ -0,0 +1,17 @@
<?php
/* @var $this yii\web\View */
/* @var $model backend\modules\notes\models\Note */
$this->title = 'Редактировать заметку: ' . $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Заметки', 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = 'Редактировать';
?>
<div class="notes-update">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>

View File

@ -0,0 +1,58 @@
<?php
use yii\grid\GridView;
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $dataProviderF \yii\data\ActiveDataProvider
/* @var $model backend\modules\notes\models\Note */
$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => 'Заметки', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="notes-view">
<p>
<?= Html::a('Список', ['index'], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Редактировать', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Удалить', ['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',
'created_at',
'updated_at',
],
]) ?>
<h2>Дополнительные сведения</h2>
<?= GridView::widget([
'dataProvider' => $additionalDataProvider,
'layout' => "{items}",
'columns' => [
'field.name:text:Поле',
[
'attribute' => 'value',
'format' => 'raw',
'value' => function ($model) {
if ($model->type_file == 'file') {
return $model->value . ' (' . Html::a('Скачать', $model->value, ['target' => '_blank', 'download' => 'download']) . ')';
}
return $model->value;
}
],
],
]); ?>
</div>

View File

@ -46,6 +46,7 @@
['label' => 'Баланс', 'icon' => 'dollar', 'url' => ['/balance/balance'], 'active' => \Yii::$app->controller->id == 'balance'],
['label' => 'Отпуска', 'icon' => 'plane', 'url' => ['/holiday/holiday'], 'active' => \Yii::$app->controller->id == 'holiday'],
['label' => 'Доступы', 'icon' => '', 'url' => ['/accesses/accesses'], 'active' => \Yii::$app->controller->id == 'accesses'],
['label' => 'Заметки', 'icon' => '', 'url' => ['/notes/notes'], 'active' => \Yii::$app->controller->id == 'notes'],
/*['label' => 'Gii', 'icon' => 'file-code-o', 'url' => ['/gii']],
['label' => 'Debug', 'icon' => 'dashboard', 'url' => ['/debug']],

View File

@ -22,6 +22,7 @@ class FieldsValueNew extends \yii\db\ActiveRecord
const TYPE_PROJECT = 1;
const TYPE_COMPANY = 2;
const TYPE_BALANCE = 3;
const TYPE_NOTE = 4;
/**
* {@inheritdoc}
*/

66
common/models/Note.php Executable file
View File

@ -0,0 +1,66 @@
<?php
namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
/**
* This is the model class for table "note".
*$fildsValue
* @property int $id
* @property string $name
* @property string $description
* @property string $created_at
* @property string $updated_at
*/
class Note extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'note';
}
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
],
];
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['name'], 'required'],
[['description'], 'string'],
[['created_at', 'updated_at'], 'safe'],
[['name'], 'string', 'max' => 255],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'name' => 'Название',
'description' => 'Описание',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
}

View File

@ -22,6 +22,7 @@ class UseField extends \yii\db\ActiveRecord
const USE_PROJECT = 1;
const USE_COMPANY = 2;
const USE_BALANCE = 3;
const USE_NOTE = 4;
/**
@ -70,7 +71,8 @@ class UseField extends \yii\db\ActiveRecord
self::USE_PROFILE => 'Профиль',
self::USE_PROJECT => 'Проект',
self::USE_COMPANY => 'Компания',
self::USE_BALANCE => 'Баланс'
self::USE_BALANCE => 'Баланс',
self::USE_NOTE => 'Заметка'
];
}

View File

@ -22,6 +22,7 @@ class UseStatus extends \yii\db\ActiveRecord
const USE_PROJECT = 1;
const USE_COMPANY = 2;
const USE_BALANCE = 3;
const USE_NOTE= 4;
/**
* {@inheritdoc}
@ -69,7 +70,8 @@ class UseStatus extends \yii\db\ActiveRecord
self::USE_PROFILE => 'Профиль',
self::USE_PROJECT => 'Проект',
self::USE_COMPANY => 'Компания',
self::USE_BALANCE => 'Баланс'
self::USE_BALANCE => 'Баланс',
self::USE_NOTE => 'Заметка'
];
}

View File

@ -0,0 +1,31 @@
<?php
use yii\db\Migration;
/**
* Handles the creation of table `{{%notes}}`.
*/
class m191205_092846_create_note_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('note', [
'id' => $this->primaryKey(),
'name' => $this->string()->notNull(),
'description' => $this->text(),
'created_at' => $this->dateTime(),
'updated_at' => $this->dateTime(),
]);
}
/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropTable('note');
}
}