/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = GrupoUsuarios::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_grupo_usuarios' => $this->id_grupo_usuarios, 'criado_por' => $this->criado_por, 'criado_em' => $this->criado_em, 'modificado_por' => $this->modificado_por, 'modificado_em' => $this->modificado_em, 'ativo' => 1]); $query->andFilterWhere(['like', 'nome', $this->nome])->andFilterWhere(['like', 'descricao', $this->descricao]); return $dataProvider; }
use yii\widgets\ActiveForm; use kartik\widgets\Select2; use app\models\GrupoUsuarios; use yii\helpers\ArrayHelper; use app\models\Usuarios; /* @var $this yii\web\View */ /* @var $model app\models\GrupoUsuarios */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="grupo-usuarios-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'id_grupo_usuarios')->dropdownList(ArrayHelper::map(GrupoUsuarios::find()->where(['ativo' => 1])->orderBy('nome')->all(), 'id_grupo_usuarios', 'nome'), ['prompt' => 'Selecione o grupo', 'disabled' => 'disabled']); ?> <?php echo $form->field($model, 'usuarios')->widget(Select2::classname(), ['data' => ArrayHelper::map(Usuarios::find()->where(['ativo' => 1])->orderBy('nome_completo')->all(), 'id_usuario', 'nome_completo'), 'value' => ArrayHelper::map($model->usuarios, 'id_usuario', 'nome_completo'), 'options' => ['placeholder' => 'Selecione os usuários...'], 'pluginOptions' => ['allowClear' => true, 'multiple' => true]]); ?> <?php echo $form->field($model, 'id_grupo_usuarios')->hiddenInput()->label(false); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? 'Incluir' : 'Salvar', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php
/** * @return \yii\db\ActiveQuery */ public function getGrupoUsuarios() { return $this->hasMany(GrupoUsuarios::className(), ['id_grupo_usuarios' => 'id_grupo_usuarios'])->viaTable('grupo_usuarios_has_usuarios', ['id_usuario' => 'id_usuario']); }
/** * Finds the GrupoUsuarios model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return GrupoUsuarios the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = GrupoUsuarios::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Creates a new Mensagens model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionGroup() { $model = new Mensagens(); if ($model->load(Yii::$app->request->post())) { $model->criado_por = Yii::$app->user->getId(); $group = GrupoUsuarios::findOne($model->id_destinatario); $receivers = $group->usuarios; foreach ($receivers as $destinatario) { $model2 = new Mensagens(); $model2->attributes = $model->attributes; $model2->id_destinatario = $destinatario->id_usuario; $model2->criado_por = Yii::$app->user->getId(); $model2->ativo = 1; $model2->save(); } return $this->redirect(['sent']); } else { $model->scenario = "group"; return $this->render('create', ['model' => $model]); } }
/** * @return \yii\db\ActiveQuery */ public function getIdGrupoUsuarios() { return $this->hasOne(GrupoUsuarios::className(), ['id_grupo_usuarios' => 'id_grupo_usuarios']); }