예제 #1
0
 public function actionCreate()
 {
     if (!Yii::app()->user->isGuest) {
         if (Yii::app()->user->checkAccess('backend_access')) {
             $this->redirect(Yii::app()->createUrl('/apartments/backend/main/create'));
         } else {
             $this->redirect(Yii::app()->createUrl('/userads/main/create'));
         }
     }
     if (param('user_registrationMode') == 'without_confirm') {
         $user = new User('register_without_confirm');
     } else {
         $user = new User('register');
     }
     $login = new LoginForm();
     $model = new Apartment();
     $model->active = Apartment::STATUS_DRAFT;
     $model->period_activity = param('apartment_periodActivityDefault', 'always');
     $model->references = $model->getFullInformation($model->id, $model->type);
     $isAdmin = false;
     $activeTab = 'tab_register';
     $isUpdate = Yii::app()->request->getPost('is_update');
     if (!$isUpdate && isset($_POST['LoginForm']) && ($_POST['LoginForm']['username'] || $_POST['LoginForm']['password'])) {
         if (Yii::app()->user->getState('attempts-login') >= LoginForm::ATTEMPTSLOGIN) {
             $login->scenario = 'withCaptcha';
         }
         $activeTab = 'tab_login';
         $login->attributes = $_POST['LoginForm'];
         if ($login->validate() && $login->login()) {
             Yii::app()->user->setState('attempts-login', 0);
             User::updateUserSession();
             $isAdmin = Yii::app()->user->checkAccess('backend_access');
             $user = User::model()->findByPk(Yii::app()->user->id);
         } else {
             Yii::app()->user->setState('attempts-login', Yii::app()->user->getState('attempts-login', 0) + 1);
             if (Yii::app()->user->getState('attempts-login') >= LoginForm::ATTEMPTSLOGIN) {
                 $login->scenario = 'withCaptcha';
             }
         }
     }
     if (isset($_POST['Apartment'])) {
         $model->attributes = $_POST['Apartment'];
         if (!$isUpdate) {
             $adValid = $model->validate();
             $userValid = false;
             if ($activeTab == 'tab_register' && param('useUserRegistration')) {
                 $user->attributes = $_POST['User'];
                 $userValid = $user->validate();
                 if ($adValid && $userValid) {
                     $user->activatekey = User::generateActivateKey();
                     $userData = User::createUser($user->attributes);
                     if ($userData) {
                         $user = $userData['userModel'];
                         $user->password = $userData['password'];
                         $user->activatekey = $userData['activatekey'];
                         $user->activateLink = $userData['activateLink'];
                         $notifier = new Notifier();
                         $notifier->raiseEvent('onNewUser', $user, array('forceEmail' => $user->email));
                     }
                 }
             }
             if ($user->id && ($activeTab == 'tab_login' && $adValid || $activeTab == 'tab_register' && param('useUserRegistration') && $adValid && $userValid)) {
                 if (param('useUseradsModeration', 1)) {
                     $model->active = Apartment::STATUS_MODERATION;
                 } else {
                     $model->active = Apartment::STATUS_ACTIVE;
                 }
                 $model->owner_active = Apartment::STATUS_ACTIVE;
                 $model->owner_id = $user->id;
                 if ($model->save(false)) {
                     if (!$isAdmin && param('useUseradsModeration', 1)) {
                         Yii::app()->user->setFlash('success', tc('The listing is succesfullty added and is awaiting moderation'));
                     } else {
                         Yii::app()->user->setFlash('success', tc('The listing is succesfullty added'));
                     }
                     if ($activeTab == 'tab_register') {
                         if (param('user_registrationMode') == 'without_confirm') {
                             $login = new LoginForm();
                             $login->setAttributes(array('username' => $user['username'], 'password' => $user['password']));
                             if ($login->validate() && $login->login()) {
                                 User::updateUserSession();
                                 User::updateLatestInfo(Yii::app()->user->id, Yii::app()->controller->currentUserIp);
                                 $this->redirect(array('/usercpanel/main/index'));
                             } else {
                                 /*echo 'getErrors=<pre>';
                                 		print_r($login->getErrors());
                                 		echo '</pre>';
                                 		exit;*/
                                 showMessage(Yii::t('common', 'Registration'), Yii::t('common', 'You were successfully registered.'));
                             }
                         } else {
                             showMessage(Yii::t('common', 'Registration'), Yii::t('common', 'You were successfully registered. The letter for account activation has been sent on {useremail}', array('{useremail}' => $user['email'])));
                         }
                     } else {
                         if ($isAdmin) {
                             NewsProduct::getProductNews();
                             $this->redirect(array('/apartments/backend/main/update', 'id' => $model->id));
                             Yii::app()->end();
                         } else {
                             $this->redirect(array('/userads/main/update', 'id' => $model->id));
                         }
                     }
                 }
             }
         }
     } else {
         $objTypes = array_keys(Apartment::getObjTypesArray());
         $model->setDefaultType();
         $model->obj_type_id = reset($objTypes);
         $user->unsetAttributes(array('verifyCode'));
     }
     $this->render('create', array('model' => $model, 'user' => $user, 'login' => $login, 'activeTab' => $activeTab));
 }
예제 #2
0
    public static function getCategories($id = null, $type = Apartment::TYPE_DEFAULT, $selected = array())
    {
        $addWhere = '';
        $addWhere .= Apartment::TYPE_RENT == $type ? ' AND reference_values.for_rent=1' : '';
        $addWhere .= Apartment::TYPE_SALE == $type ? ' AND reference_values.for_sale=1' : '';
        $sql = '
			SELECT	style,
					reference_values.title_' . Yii::app()->language . ' as value_title,
					reference_categories.title_' . Yii::app()->language . ' as category_title,
					reference_category_id, reference_values.id
			FROM	{{apartment_reference_values}} reference_values,
					{{apartment_reference_categories}} reference_categories
			WHERE	reference_category_id = reference_categories.id AND reference_categories.type=1
			' . $addWhere . '
			ORDER BY reference_categories.sorter, reference_values.sorter';
        $dependency = new CDbCacheDependency('
			SELECT MAX(val) FROM
				(SELECT MAX(date_updated) as val FROM {{apartment_reference_values}}
				UNION
				SELECT MAX(date_updated) as val FROM {{apartment_reference_categories}}) as t
		');
        $results = Yii::app()->db->cache(param('cachingTime', 1209600), $dependency)->createCommand($sql)->queryAll();
        $return = array();
        if ($id) {
            $selected = Apartment::getFullInformation($id, $type);
        } else {
            // При добавлении объявления
            if ($selected && count($selected)) {
                $tmp = array();
                foreach ($selected as $selKey => $selVal) {
                    $tmp[$selKey]['values'] = $selVal;
                }
                $selected = $tmp;
            }
        }
        if ($results) {
            foreach ($results as $result) {
                $return[$result['reference_category_id']]['title'] = $result['category_title'];
                $return[$result['reference_category_id']]['style'] = $result['style'];
                $return[$result['reference_category_id']]['values'][$result['id']]['title'] = $result['value_title'];
                if (isset($selected[$result['reference_category_id']]['values'][$result['id']])) {
                    $return[$result['reference_category_id']]['values'][$result['id']]['selected'] = true;
                } else {
                    $return[$result['reference_category_id']]['values'][$result['id']]['selected'] = false;
                }
            }
        }
        return $return;
    }