Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Model
- class Residential extends CActiveRecord
- {
- /**
- * Returns the static model of the specified AR class.
- * @param string $className active record class name.
- * @return Residential 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 'residential';
- }
- /**
- * @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('locationID, type, price_rangeID, description, active', 'required'),
- array('locationID,price_rangeID', 'my_required'),
- //array('artID, locationID, zip, size, lot, price_rangeID, active', 'numerical', 'integerOnly'=>true),
- array('type, state, price', 'length', 'max'=>10),
- array('name, street', 'length', 'max'=>100),
- array('city', 'length', 'max'=>25),
- array('feature', 'safe'),
- // The following rule is used by search().
- // Please remove those attributes that should not be searched.
- array('id, artID, locationID, type, name, street, city, state, zip, size, lot, price, price_rangeID, description, feature, active', '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(
- 'art' => array(self::BELONGS_TO, 'Art', 'artID'),
- 'location' => array(self::BELONGS_TO, 'Location', 'locationID'),
- 'priceRange' => array(self::BELONGS_TO, 'PriceRange', 'price_rangeID'),
- );
- }
- /**
- * @return array customized attribute labels (name=>label)
- */
- public function attributeLabels()
- {
- return array(
- 'id' => 'ID',
- 'artID' => 'Art',
- 'locationID' => 'Location',
- 'type' => 'Type',
- 'name' => 'Name',
- 'street' => 'Street',
- 'city' => 'City',
- 'state' => 'State',
- 'zip' => 'Zip',
- 'size' => 'Size',
- 'lot' => 'Lot',
- 'price' => 'Price',
- 'price_rangeID' => 'Price Range',
- 'description' => 'Description',
- 'feature' => 'Feature',
- 'active' => 'Active',
- );
- }
- /**
- * 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('id',$this->id);
- $criteria->compare('artID',$this->artID);
- $criteria->compare('locationID',$this->locationID);
- $criteria->compare('type',$this->type,true);
- $criteria->compare('name',$this->name,true);
- $criteria->compare('street',$this->street,true);
- $criteria->compare('city',$this->city,true);
- $criteria->compare('state',$this->state,true);
- $criteria->compare('zip',$this->zip);
- $criteria->compare('size',$this->size);
- $criteria->compare('lot',$this->lot);
- $criteria->compare('price',$this->price,true);
- $criteria->compare('price_rangeID',$this->price_rangeID);
- $criteria->compare('description',$this->description,true);
- $criteria->compare('feature',$this->feature,true);
- $criteria->compare('active',$this->active);
- return new CActiveDataProvider($this, array(
- 'criteria'=>$criteria,
- ));
- }
- public function my_required($attribute_name, $params)
- {
- if (empty($this->locationID)
- && empty($this->price_rangeID)
- // && empty($this->sizeID)
- ) {
- $this->locationID=-1;
- //$this->price_rangeID=-1;
- $this->addError($attribute_name, Yii::t('user', 'Please choose at least one option form either one or both list menus.'));
- return false;
- }
- return true;
- }
- }
- Controller
- /**
- * Show all active apartment listings.
- */
- public function actionListings()
- {
- $model=new Residential;
- // Uncomment the following line if AJAX validation is needed
- //$this->performAjaxValidation($model);
- $condition='active=1';
- //if(Yii::app()->user->hasState('residential'))
- //$_POST['Residential']=Yii::app()->user->getState('residential');//get session variable
- if(isset($_POST['Residential'])){
- //Yii::app()->user->setState('residential',$_POST['Residential']);//save session variable
- $model->attributes=$_POST['Residential'];
- if (strlen($model->locationID>0))
- $condition='locationID='.$model->locationID.' AND '.$condition;
- if (strlen($model->price_rangeID>0))
- $condition='price_rangeID='.$model->price_rangeID.' AND '.$condition;
- $dataProvider=new CActiveDataProvider('Residential', array(
- 'criteria'=>array(
- 'condition'=>$condition,
- 'order'=>'locationID ASC,price ASC',
- ),
- 'pagination'=>array(
- 'pageSize'=>5,
- ),
- ));
- if($model->save()){
- $this->render('listings',array(
- 'model'=>$model,
- 'dataProvider'=>$dataProvider,
- ));
- exit;
- }
- }else{
- $dataProvider=new CActiveDataProvider('Residential', array(
- 'criteria'=>array(
- 'condition'=>$condition,
- 'order'=>'locationID ASC,price ASC',
- ),
- 'pagination'=>array(
- 'pageSize'=>5,
- ),
- ));
- }
- $this->render('listings',array(
- 'model'=>$model,
- 'dataProvider'=>$dataProvider,
- ));
- }
- View
- <?php $this->widget('zii.widgets.CListView', array(
- 'dataProvider'=>$dataProvider,
- 'itemView'=>'_listingsResults',
- 'emptyText'=>$emptyText,
- 'summaryText'=>"{count} Listings",
- 'template'=>"{pager}\n{items}\n{pager}",
- 'pager'=> array(
- 'cssFile'=>false,
- 'header'=>'',
- 'firstPageLabel' => '<< First',
- 'prevPageLabel' => '< Back',
- 'nextPageLabel' => 'Next >',
- 'lastPageLabel' => 'Last >>',
- ),
- 'afterAjaxUpdate'=>'js:function(id, data) {a[rel^=\'prettyPhoto\']").prettyPhoto();}',
- )); ?>
Advertisement
Add Comment
Please, Sign In to add comment