Guest User

Untitled

a guest
May 6th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. Model
  2.  
  3. class Residential extends CActiveRecord
  4. {
  5. /**
  6. * Returns the static model of the specified AR class.
  7. * @param string $className active record class name.
  8. * @return Residential the static model class
  9. */
  10. public static function model($className=__CLASS__)
  11. {
  12. return parent::model($className);
  13. }
  14.  
  15. /**
  16. * @return string the associated database table name
  17. */
  18. public function tableName()
  19. {
  20. return 'residential';
  21. }
  22.  
  23. /**
  24. * @return array validation rules for model attributes.
  25. */
  26. public function rules()
  27. {
  28. // NOTE: you should only define rules for those attributes that
  29. // will receive user inputs.
  30. return array(
  31. //array('locationID, type, price_rangeID, description, active', 'required'),
  32. array('locationID,price_rangeID', 'my_required'),
  33. //array('artID, locationID, zip, size, lot, price_rangeID, active', 'numerical', 'integerOnly'=>true),
  34. array('type, state, price', 'length', 'max'=>10),
  35. array('name, street', 'length', 'max'=>100),
  36. array('city', 'length', 'max'=>25),
  37. array('feature', 'safe'),
  38. // The following rule is used by search().
  39. // Please remove those attributes that should not be searched.
  40. array('id, artID, locationID, type, name, street, city, state, zip, size, lot, price, price_rangeID, description, feature, active', 'safe', 'on'=>'search'),
  41. );
  42. }
  43.  
  44. /**
  45. * @return array relational rules.
  46. */
  47. public function relations()
  48. {
  49. // NOTE: you may need to adjust the relation name and the related
  50. // class name for the relations automatically generated below.
  51. return array(
  52. 'art' => array(self::BELONGS_TO, 'Art', 'artID'),
  53. 'location' => array(self::BELONGS_TO, 'Location', 'locationID'),
  54. 'priceRange' => array(self::BELONGS_TO, 'PriceRange', 'price_rangeID'),
  55. );
  56. }
  57.  
  58. /**
  59. * @return array customized attribute labels (name=>label)
  60. */
  61. public function attributeLabels()
  62. {
  63. return array(
  64. 'id' => 'ID',
  65. 'artID' => 'Art',
  66. 'locationID' => 'Location',
  67. 'type' => 'Type',
  68. 'name' => 'Name',
  69. 'street' => 'Street',
  70. 'city' => 'City',
  71. 'state' => 'State',
  72. 'zip' => 'Zip',
  73. 'size' => 'Size',
  74. 'lot' => 'Lot',
  75. 'price' => 'Price',
  76. 'price_rangeID' => 'Price Range',
  77. 'description' => 'Description',
  78. 'feature' => 'Feature',
  79. 'active' => 'Active',
  80. );
  81. }
  82.  
  83. /**
  84. * Retrieves a list of models based on the current search/filter conditions.
  85. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
  86. */
  87. public function search()
  88. {
  89. // Warning: Please modify the following code to remove attributes that
  90. // should not be searched.
  91.  
  92. $criteria=new CDbCriteria;
  93.  
  94. $criteria->compare('id',$this->id);
  95. $criteria->compare('artID',$this->artID);
  96. $criteria->compare('locationID',$this->locationID);
  97. $criteria->compare('type',$this->type,true);
  98. $criteria->compare('name',$this->name,true);
  99. $criteria->compare('street',$this->street,true);
  100. $criteria->compare('city',$this->city,true);
  101. $criteria->compare('state',$this->state,true);
  102. $criteria->compare('zip',$this->zip);
  103. $criteria->compare('size',$this->size);
  104. $criteria->compare('lot',$this->lot);
  105. $criteria->compare('price',$this->price,true);
  106. $criteria->compare('price_rangeID',$this->price_rangeID);
  107. $criteria->compare('description',$this->description,true);
  108. $criteria->compare('feature',$this->feature,true);
  109. $criteria->compare('active',$this->active);
  110.  
  111. return new CActiveDataProvider($this, array(
  112. 'criteria'=>$criteria,
  113. ));
  114. }
  115.  
  116. public function my_required($attribute_name, $params)
  117. {
  118. if (empty($this->locationID)
  119. && empty($this->price_rangeID)
  120. // && empty($this->sizeID)
  121. ) {
  122. $this->locationID=-1;
  123. //$this->price_rangeID=-1;
  124. $this->addError($attribute_name, Yii::t('user', 'Please choose at least one option form either one or both list menus.'));
  125.  
  126. return false;
  127. }
  128.  
  129. return true;
  130. }
  131. }
  132.  
  133. Controller
  134.  
  135. /**
  136. * Show all active apartment listings.
  137. */
  138. public function actionListings()
  139. {
  140. $model=new Residential;
  141.  
  142. // Uncomment the following line if AJAX validation is needed
  143. //$this->performAjaxValidation($model);
  144.  
  145. $condition='active=1';
  146.  
  147. //if(Yii::app()->user->hasState('residential'))
  148. //$_POST['Residential']=Yii::app()->user->getState('residential');//get session variable
  149.  
  150. if(isset($_POST['Residential'])){
  151. //Yii::app()->user->setState('residential',$_POST['Residential']);//save session variable
  152. $model->attributes=$_POST['Residential'];
  153. if (strlen($model->locationID>0))
  154. $condition='locationID='.$model->locationID.' AND '.$condition;
  155. if (strlen($model->price_rangeID>0))
  156. $condition='price_rangeID='.$model->price_rangeID.' AND '.$condition;
  157. $dataProvider=new CActiveDataProvider('Residential', array(
  158. 'criteria'=>array(
  159. 'condition'=>$condition,
  160. 'order'=>'locationID ASC,price ASC',
  161. ),
  162. 'pagination'=>array(
  163. 'pageSize'=>5,
  164. ),
  165. ));
  166. if($model->save()){
  167. $this->render('listings',array(
  168. 'model'=>$model,
  169. 'dataProvider'=>$dataProvider,
  170. ));
  171. exit;
  172.  
  173. }
  174. }else{
  175. $dataProvider=new CActiveDataProvider('Residential', array(
  176. 'criteria'=>array(
  177. 'condition'=>$condition,
  178. 'order'=>'locationID ASC,price ASC',
  179. ),
  180. 'pagination'=>array(
  181. 'pageSize'=>5,
  182. ),
  183. ));
  184. }
  185.  
  186. $this->render('listings',array(
  187. 'model'=>$model,
  188. 'dataProvider'=>$dataProvider,
  189. ));
  190. }
  191.  
  192. View
  193.  
  194. <?php $this->widget('zii.widgets.CListView', array(
  195. 'dataProvider'=>$dataProvider,
  196. 'itemView'=>'_listingsResults',
  197. 'emptyText'=>$emptyText,
  198. 'summaryText'=>"{count} Listings",
  199. 'template'=>"{pager}\n{items}\n{pager}",
  200. 'pager'=> array(
  201. 'cssFile'=>false,
  202. 'header'=>'',
  203. 'firstPageLabel' => '&lt;&lt;&nbsp;First',
  204. 'prevPageLabel' => '&lt;&nbsp;Back',
  205. 'nextPageLabel' => 'Next&nbsp;&gt;',
  206. 'lastPageLabel' => 'Last&nbsp;&gt;&gt;',
  207. ),
  208. 'afterAjaxUpdate'=>'js:function(id, data) {a[rel^=\'prettyPhoto\']").prettyPhoto();}',
  209. )); ?>
Advertisement
Add Comment
Please, Sign In to add comment