user2301881
user2301881

Reputation: 350

How can I add new admin pages to YII?

I have a completed YII and I have tried to copy an admin page to make another one. The admin.php inclue this:

<?php
/* @var $this UsersController */
/* @var $model banned */
//$model = 'Users';
$this->breadcrumbs=array(
    'Banned users'=>array('index'),
    'Manage',
);

/*
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
    $('.search-form').toggle();
    return false;
});
$('.search-form form').submit(function(){
    $('#banned-grid').yiiGridView('update', {
        data: $(this).serialize()
    });
    return false;
});
");*/
?>

<h1>Manage Banned Users</h1>

<p>
You may optionally enter a comparison operator (<b>&lt;</b>, <b>&lt;=</b>, <b>&gt;</b>, <b>&gt;=</b>, <b>&lt;&gt;</b>
or <b>=</b>) at the beginning of each of your search values to specify how the comparison should be done.
</p>

<?php //echo CHtml::link('Advanced Search','#',array('class'=>'search-button')); ?>
<div class="search-form" style="display:none">

<?php $this->renderPartial('_search1',array(
    'model'=>$model,
)); ?>
</div><!-- search-form -->

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'banned-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'uid',
        //'name',
        //'email',
        //'password',
        'datejoined',
        //'picture',
        /*
        'interestingquestionnotify',
        'myquestionnotify',
        'myanswernotify',*/
        'role',
        'status',
        //'newsletternotify',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

  <?php
 // print_r($model);
  ?>

My problem is: When I open that menu point what show me this admin php, than I can see the other menu point's zii.widgets.grid.CGridView . I need to be used the Banned.php from models folder, but this one use Users.php. Any idea how could I debugg it?

I have Banned.php in models. And I have Class Banned. I have banned in views.

This is my protected/controllers/BannedController.php

<?php

class BannedController extends Controller
{
    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout='//layouts/column1';

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions'=>array('index','view','admin','delete','create','update'),
                'users'=>array('admin'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

    /**
     * Displays a particular model.
     * @param integer $id the ID of the model to be displayed
     */
/*  public function actionView($id)
    {
        $model = $this->loadModel($id);
        $questions = new Questions('search');
        $ans = new Answers('search');

        // válaszok
        if(isset($_GET['Answers'])){
            $ans->attributes = $_GET['Answers'];
            $ans->uid = $model->uid;
            $answersDataProvider = $ans->searchOnUser();
        } else {
            $answersDataProvider = new CActiveDataProvider('Answers', array(
                'criteria'=>array('condition'=>'uid='.$model->uid),
                'pagination'=>array('pageSize'=>10),
            ));
        }

        //kérdések
        if(isset($_GET['Questions'])){
            $questions->attributes = $_GET['Questions'];
            $questions->uid = $model->uid;
            $questionsDataProvider = $questions->searchOnUser();
        } else {
            $questionsDataProvider = new CActiveDataProvider('Questions', array(
                'criteria'=>array('condition'=>'uid='.$model->uid),
                'pagination'=>array('pageSize'=>10),
            ));
        }

        $this->render('view',array(
            'model'=>$model,
            'answersDataProvider'=>$answersDataProvider,
            'questionsDataProvider'=>$questionsDataProvider,
            'ans'=>$ans,
            'questions'=>$questions
        ));
    } */
    public function actionView($id)
    {
        $this->render('view',array(
            'model'=>$this->loadModel($id),
        ));
    }       

    /**
     * Creates a new model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate()
    {
        $model=new Banned;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Users']))
        {
            $model->attributes=$_POST['Users'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->uid));
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }

    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Users']))
        {
            $model->attributes=$_POST['Users'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->uid));
        }

        $this->render('update',array(
            'model'=>$model,
        ));
    }

    /**
     * Deletes a particular model.
     * If deletion is successful, the browser will be redirected to the 'admin' page.
     * @param integer $id the ID of the model to be deleted
     */
    public function actionDelete($id)
    {
        DMongo::get()->selectCollection('users')->remove(array('_id' => intval($id)));
        $this->loadModel($id)->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if(!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
    }

    /**
     * Lists all models.
     */
    public function actionIndex()
    {
        $dataProvider=new CActiveDataProvider('Users');
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));
    }

    /**
     * Manages all models.
     */
    public function actionAdmin()
    {
        $model=new Users('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Users']))
            $model->attributes=$_GET['Banned'];
                                                    // Users
        $this->render('admin',array(
            'model'=>$model,
        ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer $id the ID of the model to be loaded
     * @return Users the loaded model
     * @throws CHttpException
     */ 
    public function loadModel($id)
    {
        $model=Banned::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist1.');
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param Users $model the model to be validated
     */
    protected function performAjaxValidation($model)
    {
        if(isset($_POST['ajax']) && $_POST['ajax']==='users-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}

And this is: protected/models/Banned.php

<?php

/**
 * This is the model class for table "tbl_users".
 *
 * The followings are the available columns in table 'tbl_users':
 * @property integer $uid
 * @property string $name
 * @property string $email
 * @property string $password
 * @property string $datejoined
 * @property string $picture
 * @property integer $interestingquestionnotify
 * @property integer $myquestionnotify
 * @property integer $myanswernotify
 * @property string $role
 * @property integer $status
 * @property integer $newsletternotify
 */
class Banned extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Banned the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'tbl_banned_users';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('user_id, user_mail, user_name'),
            array('interestingquestionnotify, myquestionnotify, myanswernotify, status, newsletternotify', 'numerical', 'integerOnly'=>true),
            array('name, email, password, picture', 'length', 'max'=>50),
            array('role', 'length', 'max'=>10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('uid, name, email, password, datejoined, picture, interestingquestionnotify, myquestionnotify, myanswernotify, role, status, newsletternotify', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'uid' => 'Azonosító1',
            'name' => 'Név',
            'email' => 'E-mail',
            'password' => 'Jelszó',
            'datejoined' => 'Dátum',
            'picture' => 'Profilkép',
            'interestingquestionnotify' => 'Megfigyelt kérdésekről értesítés',
            'myquestionnotify' => 'Kérdésekről értesítés',
            'myanswernotify' => 'Válaszokról értesítés',
            'role' => 'Jog',
            'status' => 'Állapot',
            'newsletternotify' => 'Hírekről értesítés',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('uid',$this->uid);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('email',$this->email,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('datejoined',$this->datejoined,true);
        $criteria->compare('picture',$this->picture,true);
        $criteria->compare('interestingquestionnotify',$this->interestingquestionnotify);
        $criteria->compare('myquestionnotify',$this->myquestionnotify);
        $criteria->compare('myanswernotify',$this->myanswernotify);
        $criteria->compare('role',$this->role,true);
        $criteria->compare('status',$this->status);
        $criteria->compare('newsletternotify',$this->newsletternotify);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    public function beforeSave() {
        if ($this->isNewRecord)
            $this->password = md5($this->password);

        return parent::beforeSave();
    }
}

And my BannedController dont get datas from Banned.php, it gets from Users.php Now I replaced all Users to Banned, but it writes: Banned has an invalid validation rule. The rule must specify attributes to be validated and the validator name.

Upvotes: 0

Views: 2196

Answers (3)

Ninad
Ninad

Reputation: 1871

Ok lets check it

In your actionCreate method do like shown

public function actionCreate()
    {
        $model=new Banned;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Banned']))
        {
            $model->attributes=$_POST['Banned'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->uid));
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }

and in your actionAdmin method do like as shown

 public function actionAdmin()
    {
        $model=new Banned('search');
        $model->unsetAttributes();  // clear any default values
        if(isset($_GET['Banned']))
            $model->attributes=$_GET['Banned'];
                                                    // Users
        $this->render('admin',array(
            'model'=>$model,
        ));
    }

Just Try it and see

Upvotes: 2

topher
topher

Reputation: 14860

As @Ninad has said above the $model you are rendering several of the views with is of type Users and not Banned as required. Replace all instances of Users with Banned to correct this. The admin method should be

public function actionAdmin()
{
    $model=new Banned('search'); 
    $model->unsetAttributes();  // clear any default values
    if(isset($_GET['Banned']))
        $model->attributes=$_GET['Banned'];
                                                // Users
    $this->render('admin',array(
        'model'=>$model,
    ));
}

Upvotes: 1

Ninad
Ninad

Reputation: 1871

Check the controller method which displays this view U may have sent User as model to the view as shown for example

$model=new User();
$this->render('UrViewFileName',array('model'=>$model);

Instead u send your Banned model

$banned=new Banned();
$this->render('UrViewFileName',array('model'=>$banned);

I am not sure but i think this may be ur problem

Upvotes: 1

Related Questions