Advertisement
mkdarkness

Untitled

Jun 24th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.08 KB | None | 0 0
  1. //Models
  2. <?php
  3. /**
  4.  * @property integer $cd_user_usr
  5.  * @property string $ds_nick_usr
  6.  * @property string $ds_email_usr
  7.  * @property string $ds_password_usr
  8.  * @property string $dt_add_usr
  9.  * @property string $dt_upd_usr
  10.  * @property integer $fl_deleted_usr
  11.  */
  12.  
  13.  
  14. class User extends CActiveRecord {
  15.  
  16.     public function init(){
  17.         parent::init();
  18.  
  19.     }
  20.  
  21.     public static function model($className=__CLASS__)
  22.     {
  23.         return parent::model($className);
  24.     }
  25.  
  26.     public function tableName()
  27.     {
  28.         return 'tb_user';
  29.     }
  30.  
  31.     public function primaryKey(){
  32.  
  33.         return 'cd_user_usr';
  34.     }
  35.  
  36.     public function  rules(){
  37.         return array(
  38.             array('ds_nick_usr, ds_email_usr, ds_password_usr', 'required'),
  39.             array('usergroup.cd_group_grp', 'required'),
  40.             array('dt_add_usr, dt_upd_usr, fl_deleted_usr', 'safe'),
  41.  
  42.  
  43.         );
  44.  
  45.     }
  46.  
  47.     public function attributeLabels(){
  48.         return array(
  49.             'cd_user_usr'=> 'ID',
  50.             'ds_nick_usr' => 'Usuário:',
  51.             'ds_email_usr' => 'E-Mail:',
  52.             'ds_password_usr' => 'Senha:',
  53.             'dt_add_usr' => 'Data adicinado:',
  54.             'dt_upd_usr' => 'Data atualização:',
  55.             'fl_deleted_usr' => 'Deletado:',
  56.         );
  57.  
  58.     }
  59.  
  60.     public function scopeDeleted(){
  61.         $this->getDbCriteria()->mergeWith(array(
  62.             'condition'=>'fl_deleted_usr = 0',
  63.         ));
  64.         return $this;
  65.  
  66.     }
  67.  
  68.     public function relations()
  69.     {
  70.         return array(
  71.             'usergroup'=>array(self::BELONGS_TO, 'UserGroup', 'cd_user_usr'),
  72.         );
  73.     }
  74.  
  75.     public function afterSave(){
  76.  
  77.  
  78.         if(!$this->isNewRecord){
  79.             $usergroup = UserGroup::model()->scopeUser($this->cd_user_usr)->find();
  80.             $usergroup->cd_group_grp = $this->usergroup->cd_group_grp;
  81.             $usergroup->save();
  82.         }else{
  83.             echo $this->usergroup->cd_group_grp;
  84.             die;
  85.             $usgroup = new UserGroup;
  86.             $usgroup->cd_user_usr = $this->cd_user_usr;
  87.             $usgroup->cd_group_grp = $this->usergroup->cd_group_grp;
  88.             $usgroup->save();
  89.         }
  90.     }
  91.  
  92. }
  93.  
  94. //Controller
  95. <?php
  96.  
  97. class UserController extends Controller
  98. {
  99.     /**
  100.      * Declares class-based actions.
  101.      */
  102.     public function actions()
  103.     {
  104.         return array(
  105.             // captcha action renders the CAPTCHA image displayed on the contact page
  106.             'captcha'=>array(
  107.                 'class'=>'CCaptchaAction',
  108.                 'backColor'=>0xFFFFFF,
  109.             ),
  110.             // page action renders "static" pages stored under 'protected/views/site/pages'
  111.             // They can be accessed via: index.php?r=site/page&view=FileName
  112.             'page'=>array(
  113.                 'class'=>'CViewAction',
  114.             ),
  115.         );
  116.     }
  117.  
  118.     /**
  119.      * This is the default 'index' action that is invoked
  120.      * when an action is not explicitly requested by users.
  121.      */
  122.     public function actionIndex()
  123.     {
  124.         if(Yii::app()->user->isGuest)
  125.             return $this->redirect(array('site/login'));
  126.  
  127.         // renders the view file 'protected/views/site/index.php'
  128.         // using the default layout 'protected/views/layouts/main.php'
  129.         $dataProvider = new CArrayDataProvider(User::model()->with('usergroup')->findAll(),
  130.             array('keyField'=>false,
  131.                   'pagination'=>array(
  132.                       'pageSize'=>30,
  133.                   ),
  134.             ));
  135.  
  136.         $this->render('index', array(
  137.                 'dataProvider'=> $dataProvider,
  138.         ));
  139.     }
  140.  
  141.     public function actionAdd(){
  142.         if(Yii::app()->user->isGuest)
  143.             return $this->redirect(array('site/login'));
  144.  
  145.         $model = new User;
  146.  
  147.         $model->fl_deleted_usr = 0;
  148.         $model->dt_add_usr = time();
  149.         $model->dt_upd_usr = time();
  150.  
  151.         if($model->checkPosted())
  152.         {
  153.             //CVarDumper::dump($model, 10, true);
  154.             //echo $model->usergroup['cd_group_grp'];
  155.                 if($model->execute())
  156.                 {
  157.                     $this->redirect('index');
  158.                 }
  159.             }
  160.  
  161.             $this->render('add',array(
  162.                 'model'=>$model,
  163.             ));
  164.  
  165.     }
  166.  
  167.     public function actionUpdate(){
  168.         if(Yii::app()->user->isGuest)
  169.             return $this->redirect(array('site/login'));
  170.  
  171.         $model = $this->loadUser();
  172.  
  173.         if($model->checkPosted())
  174.         {
  175.             if($model->execute())
  176.             {
  177.                 $this->redirect(array('index'));
  178.             }
  179.         }
  180.  
  181.         $this->render('update',array(
  182.             'model'=>$model,
  183.         ));
  184.  
  185.  
  186.     }
  187.  
  188.     public function actionDelete(){
  189.  
  190.         if(Yii::app()->user->isGuest)
  191.             return $this->redirect(array('site/login'));
  192.  
  193.         $model = $this->loadUser();
  194.         $model->fl_deleted_usr = '1';
  195.         $model->dt_upd_usr = time();
  196.  
  197.         /*if($model->checkPosted())
  198.         {
  199.             if($model->execute())
  200.             {
  201.                 $this->redirect('index');
  202.             }
  203.         }
  204.  
  205.         $this->render('update',array(
  206.             'model'=>$model,
  207.         ));*/
  208.  
  209.     }
  210.  
  211.     /**
  212.      * This is the action to handle external exceptions.
  213.      */
  214.     public function actionError()
  215.     {
  216.         if($error=Yii::app()->errorHandler->error)
  217.         {
  218.             if(Yii::app()->request->isAjaxRequest)
  219.                 echo $error['message'];
  220.             else
  221.                 $this->render('error', $error);
  222.         }
  223.     }
  224.  
  225.     public function loadUser(){
  226.  
  227.         $model = User::model()->with('usergroup')->findByPk(Yii::app()->request->getParam('id', null));
  228.  
  229.         if(!isset($model))
  230.             $model = new User;
  231.  
  232.         return $model;
  233.  
  234.  
  235.     }
  236.  
  237. }
  238.  
  239. //view
  240. <?php /** @var BootActiveForm $form */
  241.  
  242. $this->widget('bootstrap.widgets.TbBreadcrumbs', array(
  243.     'links'=>array('Usuários'=>'#', 'Adicionar'),
  244. ));
  245.  
  246. $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
  247.     'id'=>'horizontalForm',
  248.     'type'=>'horizontal',
  249. )); ?>
  250. <table>
  251.  
  252.     <fieldset>
  253.         <td><legend>Usuários</legend></td>
  254.         <tr>
  255.             <td><?php echo $form->textFieldRow($model, 'ds_nick_usr',
  256.                     array('class'=>'form')); ?></td>
  257.         </tr>
  258.         <tr>
  259.             <td><?php echo $form->textFieldRow($model, 'ds_email_usr',
  260.                     array()); ?></td>
  261.         </tr>
  262.         <tr>
  263.             <td><?php echo $form->passwordFieldRow($model, 'ds_password_usr',
  264.                     array()); ?></td>
  265.         </tr>
  266.         <tr>
  267.             <td><?php
  268.                 $grupo = CHtml::listData(Group::model()->scopeDeleted()->findAll(), 'cd_group_grp', 'ds_name_grp');
  269.                 echo $form->dropDownListRow($model, 'usergroup.cd_group_grp',$grupo); ?>
  270.             </td>
  271.         </tr>
  272.  
  273.  
  274.  
  275.  
  276.  
  277.     </fieldset>
  278.  
  279.     <tr>
  280.         <td>
  281.             <div class="form-buttons">
  282.                 <?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'submit', 'type'=>'inverse', 'label'=>'Salvar')); ?>
  283.                 <?php $this->widget('bootstrap.widgets.TbButton', array('buttonType'=>'primary', 'label'=>'Voltar', 'url'=>Yii::app()->createUrl('/admin/user/index'),)); ?>
  284.             </div>
  285.         </td>
  286.  
  287.     </tr>
  288.  
  289.     <?php $this->endWidget(); ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement