Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Models
- <?php
- /**
- * @property integer $cd_user_usr
- * @property string $ds_nick_usr
- * @property string $ds_email_usr
- * @property string $ds_password_usr
- * @property string $dt_add_usr
- * @property string $dt_upd_usr
- * @property integer $fl_deleted_usr
- */
- class User extends CActiveRecord {
- public function init(){
- parent::init();
- }
- public static function model($className=__CLASS__)
- {
- return parent::model($className);
- }
- public function tableName()
- {
- return 'tb_user';
- }
- public function primaryKey(){
- return 'cd_user_usr';
- }
- public function rules(){
- return array(
- array('ds_nick_usr, ds_email_usr, ds_password_usr', 'required'),
- array('usergroup.cd_group_grp', 'required'),
- array('dt_add_usr, dt_upd_usr, fl_deleted_usr', 'safe'),
- );
- }
- public function attributeLabels(){
- return array(
- 'cd_user_usr'=> 'ID',
- 'ds_nick_usr' => 'Usuário:',
- 'ds_email_usr' => 'E-Mail:',
- 'ds_password_usr' => 'Senha:',
- 'dt_add_usr' => 'Data adicinado:',
- 'dt_upd_usr' => 'Data atualização:',
- 'fl_deleted_usr' => 'Deletado:',
- );
- }
- public function scopeDeleted(){
- $this->getDbCriteria()->mergeWith(array(
- 'condition'=>'fl_deleted_usr = 0',
- ));
- return $this;
- }
- public function relations()
- {
- return array(
- 'usergroup'=>array(self::BELONGS_TO, 'UserGroup', 'cd_user_usr'),
- );
- }
- public function afterSave(){
- if(!$this->isNewRecord){
- $usergroup = UserGroup::model()->scopeUser($this->cd_user_usr)->find();
- $usergroup->cd_group_grp = $this->usergroup->cd_group_grp;
- $usergroup->save();
- }else{
- echo $this->usergroup->cd_group_grp;
- die;
- $usgroup = new UserGroup;
- $usgroup->cd_user_usr = $this->cd_user_usr;
- $usgroup->cd_group_grp = $this->usergroup->cd_group_grp;
- $usgroup->save();
- }
- }
- }
- //Controller
- <?php
- class UserController extends Controller
- {
- /**
- * Declares class-based actions.
- */
- public function actions()
- {
- return array(
- // captcha action renders the CAPTCHA image displayed on the contact page
- 'captcha'=>array(
- 'class'=>'CCaptchaAction',
- 'backColor'=>0xFFFFFF,
- ),
- // page action renders "static" pages stored under 'protected/views/site/pages'
- // They can be accessed via: index.php?r=site/page&view=FileName
- 'page'=>array(
- 'class'=>'CViewAction',
- ),
- );
- }
- /**
- * This is the default 'index' action that is invoked
- * when an action is not explicitly requested by users.
- */
- public function actionIndex()
- {
- if(Yii::app()->user->isGuest)
- return $this->redirect(array('site/login'));
- // renders the view file 'protected/views/site/index.php'
- // using the default layout 'protected/views/layouts/main.php'
- $dataProvider = new CArrayDataProvider(User::model()->with('usergroup')->findAll(),
- array('keyField'=>false,
- 'pagination'=>array(
- 'pageSize'=>30,
- ),
- ));
- $this->render('index', array(
- 'dataProvider'=> $dataProvider,
- ));
- }
- public function actionAdd(){
- if(Yii::app()->user->isGuest)
- return $this->redirect(array('site/login'));
- $model = new User;
- $model->fl_deleted_usr = 0;
- $model->dt_add_usr = time();
- $model->dt_upd_usr = time();
- if($model->checkPosted())
- {
- //CVarDumper::dump($model, 10, true);
- //echo $model->usergroup['cd_group_grp'];
- if($model->execute())
- {
- $this->redirect('index');
- }
- }
- $this->render('add',array(
- 'model'=>$model,
- ));
- }
- public function actionUpdate(){
- if(Yii::app()->user->isGuest)
- return $this->redirect(array('site/login'));
- $model = $this->loadUser();
- if($model->checkPosted())
- {
- if($model->execute())
- {
- $this->redirect(array('index'));
- }
- }
- $this->render('update',array(
- 'model'=>$model,
- ));
- }
- public function actionDelete(){
- if(Yii::app()->user->isGuest)
- return $this->redirect(array('site/login'));
- $model = $this->loadUser();
- $model->fl_deleted_usr = '1';
- $model->dt_upd_usr = time();
- /*if($model->checkPosted())
- {
- if($model->execute())
- {
- $this->redirect('index');
- }
- }
- $this->render('update',array(
- 'model'=>$model,
- ));*/
- }
- /**
- * This is the action to handle external exceptions.
- */
- public function actionError()
- {
- if($error=Yii::app()->errorHandler->error)
- {
- if(Yii::app()->request->isAjaxRequest)
- echo $error['message'];
- else
- $this->render('error', $error);
- }
- }
- public function loadUser(){
- $model = User::model()->with('usergroup')->findByPk(Yii::app()->request->getParam('id', null));
- if(!isset($model))
- $model = new User;
- return $model;
- }
- }
- //view
- <?php /** @var BootActiveForm $form */
- $this->widget('bootstrap.widgets.TbBreadcrumbs', array(
- 'links'=>array('Usuários'=>'#', 'Adicionar'),
- ));
- $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
- 'id'=>'horizontalForm',
- 'type'=>'horizontal',
- )); ?>
- <table>
- <fieldset>
- <td><legend>Usuários</legend></td>
- <tr>
- <td><?php echo $form->textFieldRow($model, 'ds_nick_usr',
- array('class'=>'form')); ?></td>
- </tr>
- <tr>
- <td><?php echo $form->textFieldRow($model, 'ds_email_usr',
- array()); ?></td>
- </tr>
- <tr>
- <td><?php echo $form->passwordFieldRow($model, 'ds_password_usr',
- array()); ?></td>
- </tr>
- <tr>
- <td><?php
- $grupo = CHtml::listData(Group::model()->scopeDeleted()->findAll(), 'cd_group_grp', 'ds_name_grp');
- echo $form->dropDownListRow($model, 'usergroup.cd_group_grp',$grupo); ?>
- </td>
- </tr>
- </fieldset>
- <tr>
- <td>
- <div class="form-buttons">
- <?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'inverse', 'label'=>'Salvar')); ?>
- <?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'primary', 'label'=>'Voltar', 'url'=>Yii::app()->createUrl('/admin/user/index'),)); ?>
- </div>
- </td>
- </tr>
- <?php $this->endWidget(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement