Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Model:
- <?php
- class Advertisement extends CActiveRecord
- {
- /**
- * @return string the associated database table name
- */
- public function tableName()
- {
- return 'base_advertisement';
- }
- public $picture;
- /**
- * @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,category_id, title, description, city, phone', 'required'),
- array('user_id, category_id, inactive, blocked', 'numerical', 'integerOnly'=>true),
- array('city, phone', 'length', 'max'=>255),
- array('title', 'length', 'min'=>5, 'max'=>150),
- array('description', 'length', 'min'=>5, 'max'=>1000),
- array('phone', 'match', 'pattern'=>'/^[+][370]{3}[0-9]{8}$/', 'message' => Yii::t('svsite', 'Phone number should contain +370XXXXXXXX format')),
- array('title, description, photo_url, expired_date, deactivated_date', 'safe'),
- array('picture', 'length', 'max' => 255, 'tooLong' => '{attribute} is too long (max {max} chars).'),
- array('picture', 'file', 'types' => 'jpg,jpeg,gif,png', 'maxSize' => 1024 * 1024 * 3, 'tooLarge' => Yii::t('ad', 'Size should be less then 3MB !!!'),'allowEmpty'=>true),
- // The following rule is used by search().
- // @todo Please remove those attributes that should not be searched.
- array('id, user_id, category_id, title, description, city, phone, photo_url, inactive, blocked, expired_date, deactivated_date', '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(
- 'category' => array(self::BELONGS_TO, 'Categoriestree', 'category_id'),
- 'user' => array(self::BELONGS_TO, 'User', 'user_id'),
- 'advertisementAudios' => array(self::HAS_MANY, 'AdvertisementAudio', 'advertisement_id'),
- 'advertisementComments' => array(self::HAS_MANY, 'AdvertisementComments', 'advertisement_id'),
- 'advertisementPhotos' => array(self::HAS_MANY, 'AdvertisementPhoto', 'advertisement_id'),
- 'advertisementPromotions' => array(self::HAS_MANY, 'AdvertisementPromotion', 'advertisement_id'),
- 'advertisementRatings' => array(self::HAS_MANY, 'AdvertisementRating', 'advertisement_id'),
- 'advertisementVideos' => array(self::HAS_MANY, 'AdvertisementVideo', 'advertisement_id'),
- );
- }
- /**
- * @return array customized attribute labels (name=>label)
- */
- public function attributeLabels()
- {
- return array(
- 'id' => 'ID',
- 'user_id' => 'User',
- 'category_id' => Yii::t('ad', 'category'),
- 'title' => Yii::t('ad', 'title'),
- 'description' => Yii::t('ad', 'description'),
- 'city' => Yii::t('ad', 'city'),
- 'phone' => Yii::t('ad', 'phone'),
- 'photo_url' => 'Photo Url',
- 'picture' => Yii::t('ad', 'picture'),
- 'inactive' => Yii::t('ad', 'inactive'),
- 'blocked' => 'Blocked',
- 'expired_date' => 'Expired Date',
- 'deactivated_date' => 'Deactivated Date',
- 'ts' => 'Ts',
- );
- }
- /**
- * Retrieves a list of models based on the current search/filter conditions.
- *
- * Typical usecase:
- * - Initialize the model fields with values from filter form.
- * - Execute this method to get CActiveDataProvider instance which will filter
- * models according to data in model fields.
- * - Pass data provider to CGridView, CListView or any similar widget.
- *
- * @return CActiveDataProvider the data provider that can return the models
- * based on the search/filter conditions.
- */
- public function search()
- {
- // @todo Please modify the following code to remove attributes that should not be searched.
- $criteria=new CDbCriteria;
- $criteria->compare('id',$this->id);
- $criteria->compare('user_id',$this->user_id);
- $criteria->compare('category_id',$this->category_id);
- $criteria->compare('title',$this->title,true);
- $criteria->compare('description',$this->description,true);
- $criteria->compare('city',$this->city,true);
- $criteria->compare('phone',$this->phone,true);
- $criteria->compare('photo_url',$this->photo_url,true);
- $criteria->compare('inactive',$this->inactive);
- $criteria->compare('blocked',$this->blocked);
- $criteria->compare('expired_date',$this->expired_date,true);
- $criteria->compare('deactivated_date',$this->deactivated_date,true);
- $criteria->compare('ts',$this->ts,true);
- return new CActiveDataProvider($this, array(
- 'criteria'=>$criteria,
- ));
- }
- /**
- * Returns the static model of the specified AR class.
- * Please note that you should have this exact method in all your CActiveRecord descendants!
- * @param string $className active record class name.
- * @return Advertisement the static model class
- */
- public static function model($className=__CLASS__)
- {
- return parent::model($className);
- }
- public function beforeValidate() {
- $this->user_id = yii::app()->user->id;
- if (!isset($this->blocked)) $this->blocked = 0;
- $this->expired_date = date('Y-m-d', strtotime('+1 year'));
- if (isset($this->inactive) && $this->inactive == 1)
- $this->deactivated_date = date('Y-m-d', strtotime("+183 day"));
- else $this->deactivated_date = '0000-00-00';
- return parent::beforeValidate();
- }
- public function getImageUrl() {
- if ($this->photo_url != ''){
- return Yii::app()->getBaseUrl().'/images/ads/'.$this->id.'/'.$this->photo_url;
- }
- else return Yii::app()->getBaseUrl().'/images/images.jpg';
- }
- public function getImagethumbUrl() {
- if ($this->photo_url != ''){
- return Yii::app()->getBaseUrl().'/images/ads/'.$this->id.'/thumb_'.$this->photo_url;
- }
- else return Yii::app()->getBaseUrl().'/images/images.jpg';
- }
- }
- Controller action:
- public function actionIndex() {
- $tab = 'advertisement';
- $model = new Advertisement;
- $getModelByUser = User::model()->findByPk(yii::app()->user->id);
- $allAdbyUser = $getModelByUser->advertisement;
- $dataProvider = new CActiveDataProvider('Advertisement', array(
- 'criteria'=>array('condition'=>'user_id=:user_id', 'params'=>array(':user_id'=>Yii::app()->user->id))
- ));
- $this->render('index', array('model' => $model, 'tab' => $tab, 'ads' => $allAdbyUser, 'dataProvider' => $dataProvider));
- }
- View ad.php:
- <?php
- $this->widget('TbButton',array(
- 'buttonType'=>'link',
- 'icon' => 'floppy-save',
- 'url' => $this->createUrl('addnewad'),
- 'label'=>Yii::t('user_profile','Add new ad'),
- 'htmlOptions'=>array('class'=>'adnewad-button', 'style'=>'text-transform: uppercase; border-radius: 0;margin-top:10px;')
- ));
- ?>
- <h4><?php echo Yii::t('user_profile','My ads') ?></h4>
- <?php
- $this->widget('booster.widgets.TbGridView', array(
- 'id' => 'page-creation-grid',
- 'dataProvider' => $dataProvider,
- 'filter' => $model->search(),
- 'columns' => array(
- 'category_id',
- 'title',
- 'expired_date',
- 'deactivated_date',
- array(
- 'class' => 'TbButtonColumn',
- 'buttons' => array(
- //'view' => array('url' => 'Yii::app()->controller->createUrl("pageview",array("id"=>$data->id))'),
- 'update' => array('url' => 'Yii::app()->controller->createUrl("updatead",array("id"=>$data->id, "page"=>1))'),
- 'delete' => array('url' => 'Yii::app()->controller->createUrl("addelete",array("id"=>$data->id))'),
- )
- ),
- ),
- 'htmlOptions' => array(
- //'class' => 'col-xs-12 col-sm-6 col-md-8'
- )
- ));
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement