示例#1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = MeritTemplate::find();
     $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;
     }
     $query->andFilterWhere(['id' => $this->id, 'type' => $this->type, 'method' => $this->method, 'event' => $this->event, 'action_type' => $this->action_type, 'rule_key' => $this->rule_key, 'rule_value' => $this->rule_value, 'increment' => $this->increment, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'unique_id', $this->unique_id]);
     return $dataProvider;
 }
示例#2
0
 /**
  * @param MeritTemplate $meritTemplate
  * @throws Exception
  */
 public function update(MeritTemplate $meritTemplate)
 {
     $meritLog = new MeritLog();
     $user = \Yii::$app->user->identity;
     $transaction = \Yii::$app->db->beginTransaction();
     try {
         /** @var Merit $userMerit */
         $userMerit = Merit::findOne(['user_id' => $user->getId(), 'type' => $meritTemplate->type]);
         // is sub 判断是否是减法
         $actionSub = $meritTemplate->action_type == MeritTemplate::ACTIVE_TYPE_SUB;
         if ($userMerit) {
             $merit = call_user_func($actionSub ? 'bcsub' : 'bcadd', $userMerit->merit, $meritTemplate->increment);
             $userMerit->setAttributes(['merit' => (int) $merit]);
         } else {
             $userMerit = new Merit();
             $userMerit->setAttributes(['merit' => ($actionSub ? '-' : '') . $meritTemplate->increment, 'user_id' => $user->getId(), 'username' => $user->username, 'type' => $meritTemplate->type]);
         }
         if (!$userMerit->save()) {
             Yii::error('Merit 操作失败' . json_encode(array_values($userMerit->getFirstErrors())), 'error');
             throw new Exception(array_values($userMerit->getFirstErrors())[0]);
         }
         $description = $meritTemplate->title . ': ' . MeritTemplate::getActionTypes()[$meritTemplate->action_type] . $meritTemplate->increment . MeritTemplate::getTypes()[$meritTemplate->type];
         $meritLog->setAttributes(['user_id' => $user->getId(), 'username' => $user->username, 'merit_template_id' => $meritTemplate->id, 'type' => $meritTemplate->type, 'description' => $description, 'action_type' => $meritTemplate->action_type, 'increment' => $meritTemplate->increment, 'created_at' => time()]);
         if (!$meritLog->save()) {
             throw new Exception(array_values($meritLog->getFirstErrors())[0]);
         }
         $transaction->commit();
     } catch (Exception $e) {
         $transaction->rollBack();
     }
 }
示例#3
0
<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yiier\merit\models\MeritTemplate;
/* @var $this yii\web\View */
/* @var $searchModel yiier\merit\models\MeritLogSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('app', 'Merit Logs');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="merit-log-index">

    <h1><?php 
echo Html::encode($this->title);
?>
</h1>
    <?php 
// echo $this->render('_search', ['model' => $searchModel]);
?>

    <?php 
echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => ['user_id', 'username', 'merit_template_id', ['attribute' => 'type', 'value' => function ($data) {
    return MeritTemplate::getTypes()[$data->type];
}], 'description', ['attribute' => 'action_type', 'value' => function ($data) {
    return MeritTemplate::getActionTypes()[$data->action_type];
}], 'increment', 'created_at:datetime', ['class' => 'yii\\grid\\ActionColumn', 'template' => '{view} {delete}']]]);
?>

</div>
示例#4
0
文件: view.php 项目: yiier/yii2-merit
use yii\helpers\Html;
use yii\widgets\DetailView;
use yiier\merit\models\MeritTemplate;
/* @var $this yii\web\View */
/* @var $model yiier\merit\models\MeritTemplate */
$this->title = $model->title;
$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Merit Templates'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="merit-template-view">

    <h1><?php 
echo Html::encode($this->title);
?>
</h1>

    <p>
        <?php 
echo Html::a(Yii::t('app', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']);
?>
        <?php 
echo Html::a(Yii::t('app', 'Delete'), ['delete', 'id' => $model->id], ['class' => 'btn btn-danger', 'data' => ['confirm' => Yii::t('app', 'Are you sure you want to delete this item?'), 'method' => 'post']]);
?>
    </p>

    <?php 
echo DetailView::widget(['model' => $model, 'attributes' => ['id', 'title', ['attribute' => 'type', 'value' => MeritTemplate::getTypes()[$model->type]], 'unique_id', ['attribute' => 'method', 'value' => MeritTemplate::getMethods()[$model->method]], ['attribute' => 'action_type', 'value' => MeritTemplate::getActionTypes()[$model->action_type]], 'increment', ['attribute' => 'rule_key', 'value' => MeritTemplate::getRuleKeys()[$model->rule_key]], 'rule_value', ['attribute' => 'status', 'value' => MeritTemplate::getStatuses()[$model->status]], 'created_at:datetime', 'updated_at:datetime']]);
?>

</div>
示例#5
0
echo $form->field($model, 'action_type')->dropDownList(MeritTemplate::getActionTypes());
?>

    <?php 
echo $form->field($model, 'increment')->textInput();
?>

    <?php 
echo $form->field($model, 'rule_key')->dropDownList(MeritTemplate::getRuleKeys());
?>

    <?php 
echo $form->field($model, 'rule_value')->textInput();
?>

    <?php 
echo $form->field($model, 'status')->dropDownList(MeritTemplate::getStatuses());
?>

    <div class="form-group">
        <?php 
echo Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']);
?>
    </div>

    <?php 
ActiveForm::end();
?>

</div>
示例#6
0
    <h1><?php 
echo Html::encode($this->title);
?>
</h1>
    <?php 
// echo $this->render('_search', ['model' => $searchModel]);
?>

    <p>
        <?php 
echo Html::a(Yii::t('app', 'Create Merit Template'), ['create'], ['class' => 'btn btn-success']);
?>
    </p>

    <?php 
echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => ['title', ['attribute' => 'type', 'value' => function ($data) {
    return MeritTemplate::getTypes()[$data->type];
}], 'unique_id', ['attribute' => 'method', 'value' => function ($data) {
    return MeritTemplate::getMethods()[$data->method];
}], ['attribute' => 'action_type', 'value' => function ($data) {
    return MeritTemplate::getActionTypes()[$data->action_type];
}], 'increment', ['attribute' => 'rule_key', 'value' => function ($data) {
    return MeritTemplate::getRuleKeys()[$data->rule_key];
}], 'rule_value', ['attribute' => 'status', 'value' => function ($data) {
    return MeritTemplate::getStatuses()[$data->status];
}], 'created_at:datetime', 'updated_at:datetime', ['class' => 'yii\\grid\\ActionColumn']]]);
?>

</div>
示例#7
0
<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yiier\merit\models\MeritTemplate;
/* @var $this yii\web\View */
/* @var $searchModel yiier\merit\models\MeritSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('app', 'Merits');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="merit-index">

    <h1><?php 
echo Html::encode($this->title);
?>
</h1>
    <?php 
// echo $this->render('_search', ['model' => $searchModel]);
?>

    <?php 
echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => ['user_id', 'username', ['attribute' => 'type', 'value' => function ($data) {
    return MeritTemplate::getTypes()[$data->type];
}], 'merit', 'created_at:datetime', 'updated_at:datetime', ['class' => 'yii\\grid\\ActionColumn', 'template' => '{view} {delete}']]]);
?>

</div>
 /**
  * Finds the MeritTemplate model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return MeritTemplate the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = MeritTemplate::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }