Commit d03a501d authored by Олег Гиммельшпах's avatar Олег Гиммельшпах

Merge branch 'master' of git.task-on.com:ktask/task-on.com

parents fe8b7952 2231c98f
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
*/ */
namespace common\modules\triggers\components\conditions; namespace common\modules\triggers\components\conditions;
use common\modules\triggers\controllers\TriggerAdminController;
use yii\web\ServerErrorHttpException; use yii\web\ServerErrorHttpException;
class Conditions { class Conditions {
...@@ -88,11 +89,43 @@ class Conditions { ...@@ -88,11 +89,43 @@ class Conditions {
return null; return null;
} }
public static function getConditionHtml($id, $key, $values=array()){
$condition = Conditions::init()->getConditionById($id);
$condition_key = rand(100,999);
$result = '<div class="alert alert-info alert-dismissible" role="alert">';
$result.= '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
$result.= '<strong>Условие: '.$condition->getName().'</strong>';
$result.= '<input type="hidden" name="Conditions['.$key.']['.$condition->getId().$condition_key.'][id]" value="'.$condition->getId().'"/>';
foreach($condition->getParams($key, $condition_key, $values) as $param) {
$result.=$param;
}
$result.= '</div>';
return $result;
}
/** /**
* @param array $condition_array
* @param null $key
* @return string * @return string
*/ */
public function getControlArea(){ public static function getControlButtons($condition_array=array(), $key=null){
$html = '<div class="conditions-block"></div>'; $key = ($key!==null) ? $key : rand(100000,999999);
return '<tr data-key='.$key.'><td>'.Conditions::init()->getControlArea($condition_array, $key).'</td></tr>';
}
/**
* @param array $condition_array
* @param $key
* @return string
*/
public function getControlArea($condition_array=array(), $key=null){
$html = '<div class="conditions-block">';
if (count($condition_array)>0) {
foreach($condition_array as $condition) {
$html .= self::getConditionHtml($condition['id'], ($key!==null) ? $key : rand(100000,999999), (array_key_exists('params', $condition)) ? $condition['params'] : array());
}
}
$html .= '</div>';
$html .= '<div class="btn-group" role="group">'; $html .= '<div class="btn-group" role="group">';
$html .= '<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="glyphicon glyphicon-plus"></i></button>'; $html .= '<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="glyphicon glyphicon-plus"></i></button>';
$html .= '<ul class="dropdown-menu">'; $html .= '<ul class="dropdown-menu">';
......
...@@ -49,9 +49,10 @@ class ConditionBase { ...@@ -49,9 +49,10 @@ class ConditionBase {
/** /**
* @param $group_key * @param $group_key
* @param $condition_key * @param $condition_key
* @param array $values
* @return array * @return array
*/ */
public function getParams($group_key, $condition_key, $value=null){ public function getParams($group_key, $condition_key, $values=array()){
$result = []; $result = [];
if (isset($this->config['params'])) { if (isset($this->config['params'])) {
foreach($this->config['params'] as $name=>$param) { foreach($this->config['params'] as $name=>$param) {
...@@ -59,7 +60,7 @@ class ConditionBase { ...@@ -59,7 +60,7 @@ class ConditionBase {
switch($type) { switch($type) {
case 'text': case 'text':
default: default:
$result[] = '<input type="text" value="'.((!is_null($value)) ? $value : '').'" name="Conditions['.$group_key.']['.$this->getId().$condition_key.']['.$name.']" class="form-control" style="margin-top: 5px;" placeholder="'.((isset($param['placeholder'])) ? $param['placeholder'] : '').'"/>'; $result[] = '<input type="text" value="'.((array_key_exists($name, $values)) ? $values[$name] : '').'" name="Conditions['.$group_key.']['.$this->getId().$condition_key.'][params]['.$name.']" class="form-control" style="margin-top: 5px;" placeholder="'.((isset($param['placeholder'])) ? $param['placeholder'] : '').'"/>';
break; break;
} }
} }
......
...@@ -35,23 +35,15 @@ class TriggerAdminController extends AdminController { ...@@ -35,23 +35,15 @@ class TriggerAdminController extends AdminController {
} }
public function actionGetandconditionhtml(){ public function actionGetandconditionhtml(){
$result = '<tr data-key='.rand(100000,999999).'><td>'.Conditions::init()->getControlArea().'</td></tr>'; $result = Conditions::getControlButtons();
return (Yii::$app->request->isAjax) ? Json::encode($result) : $result; return (Yii::$app->request->isAjax) ? Json::encode($result) : $result;
} }
public function actionGetconditionhtml($id, $key){ public function actionGetconditionhtml($id, $key){
$condition = Conditions::init()->getConditionById($id); $result = Conditions::getConditionHtml($id, $key);
$result = $this->renderPartial(
'condition-html',
[
'object' => $condition,
'key' => $key
]
);
return Json::encode($result); return Json::encode($result);
} }
public function actionTesting(){ public function actionTesting(){
return $this->render( return $this->render(
'testing' 'testing'
...@@ -109,8 +101,10 @@ class TriggerAdminController extends AdminController { ...@@ -109,8 +101,10 @@ class TriggerAdminController extends AdminController {
public function actionUpdate($id){ public function actionUpdate($id){
/** @var TriggerTrigger $model */ /** @var TriggerTrigger $model */
$model = TriggerTrigger::findOne($id); $model = TriggerTrigger::findOne($id);
$model->setScenario('update');
if ($model->load(Yii::$app->request->post())) { if ($model->load(Yii::$app->request->post())) {
$model->conditions = Yii::$app->request->post('Conditions');
$datetime=new \DateTime(); $datetime=new \DateTime();
$model->owner_id = Yii::$app->user->getId(); $model->owner_id = Yii::$app->user->getId();
$model->date_create=$datetime->format('Y-m-d H:i:s'); $model->date_create=$datetime->format('Y-m-d H:i:s');
......
<?php <?php
/** @var \common\components\BaseForm $this */ /** @var \common\components\BaseForm $this */
/** @var \common\modules\triggers\models\TriggerTrigger $model */
use \common\modules\triggers\components\conditions\Conditions; use \common\modules\triggers\components\conditions\Conditions;
Yii::$app->controller->view->registerJsFile('/js/triggers/conditions/admin-block.js'); $model = $this->model;
// Формируем html будущей таблицы, открыли тег Yii::$app->controller->view->registerJsFile('/js/triggers/conditions/admin-block.js');
$table = '<table class="table table-bordered">';
// Формируем тело будущей таблицы
$table .= '<tbody>';
// Если есть ошибка
if ($this->model->getErrors('conditions')) {
$error = $this->model->getErrors('conditions');
$table .= '<tr><td style="color: red;">'.$error[0].'</td></tr>';
}
// Тут добавляем кнопки управления по умолчанию
$table .= Yii::$app->controller->actionGetandconditionhtml();
$table .= '</tbody>';
// Формируем футер
$table .= '<tfoot>';
$table .= '<td><button type="button" class="btn btn-primary add-and-condition">Добавить условие "И"</button></td>';
$table .= '</tfoot>';
// Закрываем таблицу
$table .= '</table>';
return [ return [
'activeForm' => [ 'activeForm' => [
'id' => 'trigger-form' 'id' => 'trigger-form'
], ],
'elements' => [ 'elements' => [
'table' => $table, 'table' => $model->getConditionTable(),
'active' => [ 'active' => [
'type' => 'checkbox' 'type' => 'checkbox'
], ],
......
...@@ -10,10 +10,12 @@ use Yii; ...@@ -10,10 +10,12 @@ use Yii;
* @property integer $id * @property integer $id
* @property integer $group_number * @property integer $group_number
* @property integer $trigger_id * @property integer $trigger_id
* @property integer $condition_id
* *
* @property TriggerTrigger $trigger * @property TriggerTrigger $trigger
* @property TriggerParam[] $triggerParams
*/ */
class TriggerCondition extends \yii\db\ActiveRecord class TriggerCondition extends \common\components\ActiveRecordModel
{ {
/** /**
* @inheritdoc * @inheritdoc
...@@ -23,18 +25,27 @@ class TriggerCondition extends \yii\db\ActiveRecord ...@@ -23,18 +25,27 @@ class TriggerCondition extends \yii\db\ActiveRecord
return 'trigger_condition'; return 'trigger_condition';
} }
public function name()
{
return 'Условия триггера';
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function rules() public function rules()
{ {
return [ return [
[['group_number', 'trigger_id'], 'required'], [['group_number', 'trigger_id', 'condition_id'], 'required'],
[['group_number', 'trigger_id'], 'integer'], [['group_number', 'trigger_id', 'condition_id'], 'integer'],
[['trigger_id'], 'exist', 'skipOnError' => true, 'targetClass' => TriggerTrigger::className(), 'targetAttribute' => ['trigger_id' => 'id']], [['trigger_id'], 'exist', 'skipOnError' => true, 'targetClass' => TriggerTrigger::className(), 'targetAttribute' => ['trigger_id' => 'id']],
]; ];
} }
public function behaviors(){
return [];
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
...@@ -44,6 +55,7 @@ class TriggerCondition extends \yii\db\ActiveRecord ...@@ -44,6 +55,7 @@ class TriggerCondition extends \yii\db\ActiveRecord
'id' => 'ID', 'id' => 'ID',
'group_number' => 'Group Number', 'group_number' => 'Group Number',
'trigger_id' => 'Trigger ID', 'trigger_id' => 'Trigger ID',
'condition_id' => 'Идентификатор условия'
]; ];
} }
...@@ -54,4 +66,12 @@ class TriggerCondition extends \yii\db\ActiveRecord ...@@ -54,4 +66,12 @@ class TriggerCondition extends \yii\db\ActiveRecord
{ {
return $this->hasOne(TriggerTrigger::className(), ['id' => 'trigger_id']); return $this->hasOne(TriggerTrigger::className(), ['id' => 'trigger_id']);
} }
/**
* @return \yii\db\ActiveQuery
*/
public function getTriggerParams()
{
return $this->hasMany(TriggerParam::className(), ['condition_id' => 'id']);
}
} }
<?php
namespace common\modules\triggers\models;
use Yii;
/**
* This is the model class for table "trigger_param".
*
* @property integer $id
* @property integer $condition_id
* @property string $key
* @property string $value
*
* @property TriggerCondition $condition
*/
class TriggerParam extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'trigger_param';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['condition_id'], 'required'],
[['condition_id'], 'integer'],
[['key', 'value'], 'string'],
[['condition_id'], 'exist', 'skipOnError' => true, 'targetClass' => TriggerCondition::className(), 'targetAttribute' => ['condition_id' => 'id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'condition_id' => 'Condition ID',
'key' => 'Key',
'value' => 'Value',
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCondition()
{
return $this->hasOne(TriggerCondition::className(), ['id' => 'condition_id']);
}
}
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace common\modules\triggers\models; namespace common\modules\triggers\models;
use common\modules\triggers\components\conditions\Conditions;
use Yii; use Yii;
use common\modules\users\models\User; use common\modules\users\models\User;
...@@ -32,6 +33,80 @@ class TriggerTrigger extends \common\components\ActiveRecordModel ...@@ -32,6 +33,80 @@ class TriggerTrigger extends \common\components\ActiveRecordModel
return 'Триггеры'; return 'Триггеры';
} }
public function afterFind(){
/** @var TriggerCondition $condition */
foreach($this->triggerConditions as $condition) {
$this->conditions[$condition->group_number][$condition->getPrimaryKey()] = [
'id' => $condition->condition_id
];
foreach($condition->triggerParams as $param) {
$this->conditions[$condition->group_number][$condition->getPrimaryKey()]['params'][$param->key] = $param->value;
}
}
}
public function afterSave($insert,$changedAttributes){
parent::afterSave($insert,$changedAttributes);
if ($this->scenario == 'update') {
foreach($this->triggerConditions as $condition) {
TriggerParam::deleteAll(['condition_id' => $condition->getPrimaryKey()]);
TriggerCondition::deleteAll(['id' => $condition->getPrimaryKey()]);
}
}
// Сбросили ключи
$this->conditions = array_values($this->conditions);
// Перебираем условия в цикле
foreach($this->conditions as $group_index=>$condition_array) {
foreach($condition_array as $condition) {
$conditionModel = new TriggerCondition();
$conditionModel->group_number = $group_index;
$conditionModel->trigger_id = $this->id;
$conditionModel->condition_id = $condition['id'];
if ($conditionModel->save()) {
if (array_key_exists('params', $condition)) {
foreach($condition['params'] as $key=>$param) {
$paramModel = new TriggerParam();
$paramModel->condition_id = $conditionModel->getPrimaryKey();
$paramModel->key = $key;
$paramModel->value = $param;
if (!$paramModel->save())
echo current(current($paramModel->getErrors()));
}
}
}
}
}
}
public function getConditionTable(){
// Формируем html будущей таблицы, открыли тег
$table = '<table class="table table-bordered">';
// Формируем тело будущей таблицы
$table .= '<tbody>';
// Если есть ошибка
if ($this->getErrors('conditions')) {
$error = $this->getErrors('conditions');
$table .= '<tr><td style="color: red;">'.$error[0].'</td></tr>';
}
// Тут добавляем кнопки управления
if (count($this->conditions)>0) {
foreach($this->conditions as $condition_array) {
$table .= self::getControlButtons($condition_array);
}
} else
$table .= self::getControlButtons();
$table .= '</tbody>';
// Формируем футер
$table .= '<tfoot>';
$table .= '<td><button type="button" class="btn btn-primary add-and-condition">Добавить условие "И"</button></td>';
$table .= '</tfoot>';
// Закрываем таблицу
$table .= '</table>';
return $table;
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
...@@ -106,4 +181,11 @@ class TriggerTrigger extends \common\components\ActiveRecordModel ...@@ -106,4 +181,11 @@ class TriggerTrigger extends \common\components\ActiveRecordModel
{ {
return $this->hasOne(User::className(), ['id' => 'owner_id']); return $this->hasOne(User::className(), ['id' => 'owner_id']);
} }
public function scenarios(){
return [
'default' => ['active','name','description','owner_id','date_create','timeout','message_template_id','conditions'],
'update' => ['active','name','description','owner_id','date_create','timeout','message_template_id','conditions'],
];
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment