public function beforeAction($action) { //$css_file_id=''; $test = 11; $ref = Yii::$app->request->get('ref', ''); $id = Yii::$app->request->get('id', ''); $user_id = 0; if (!\Yii::$app->user->isGuest) { $user_id = \Yii::$app->user->identity->getId(); } if (!empty($ref)) { $user_id = AlphaId::id($ref, true); } $user_setting = UserSetting::findByUserId($user_id); if ($user_setting) { $this->getView()->params['css_file_id'] = $user_setting->css_file_id; $this->getView()->params['logo_url'] = $user_setting->logo_url; $this->getView()->params['ref_user_id'] = $user_id; $this->getView()->params['id'] = $id; $this->getView()->params['ref'] = $ref; } else { $this->getView()->params['css_file_id'] = ''; $this->getView()->params['logo_url'] = ''; } if ($user_id != 0) { $cartForm = new CartForm(); $rows = $cartForm->getUserCart($user_id, true); $this->getView()->params['cart_count'] = count($rows); $invoicies = Invoice::find()->where(['user_id' => $user_id, 'status' => Invoice::STATUS_NEW])->all(); $invoice = Invoice::findOne(['user_id' => $user_id, 'status' => Invoice::STATUS_NEW]); $invoice_id = 0; if ($invoice) { $invoice_id = $invoice->id; } $this->getView()->params['invoice_count'] = count($invoicies); $this->getView()->params['invoice_id'] = $invoice_id; if (!Yii::$app->user->isGuest && Yii::$app->user && Yii::$app->user->identity) { $this->getView()->params['demo'] = Yii::$app->user->identity->role == User::ROLE_DEMO ? true : false; } } else { $this->getView()->params['demo'] = 0; } return parent::beforeAction($action); }
public function save() { $old_setting = UserSetting::findByUserId($this->user_id); if (empty($old_setting)) { $setting = new UserSetting(); $setting->user_id = $this->user_id; $setting->color_1 = $this->color_1; $setting->color_2 = $this->color_2; $setting->primary_text_color = $this->primary_text_color; $setting->primary_back_color = $this->primary_back_color; $setting->primary_border_color = $this->primary_border_color; $setting->primary_active_border_color = $this->primary_active_border_color; $setting->primary_active_back_color = $this->primary_active_back_color; $setting->primary_active_text_color = $this->primary_active_text_color; $setting->default_text_color = $this->default_text_color; $setting->default_back_color = $this->default_back_color; $setting->default_border_color = $this->default_border_color; $setting->default_active_back_color = $this->default_active_back_color; $setting->default_active_text_color = $this->default_active_text_color; $setting->default_active_border_color = $this->default_active_border_color; $setting->active_link_color = $this->active_link_color; $setting->link_color = $this->link_color; $setting->css = $this->css; if ($this->logo_url) { $setting->logo_url = $this->logo_url; } else { unset($setting->logo_url); } $setting->css_file_id = $this->saveCss($setting->css); $setting->save(); return $setting; } else { $old_setting->color_1 = $this->color_1; $old_setting->color_2 = $this->color_2; $old_setting->primary_text_color = $this->primary_text_color; $old_setting->primary_back_color = $this->primary_back_color; $old_setting->primary_border_color = $this->primary_border_color; $old_setting->primary_active_border_color = $this->primary_active_border_color; $old_setting->primary_active_back_color = $this->primary_active_back_color; $old_setting->primary_active_text_color = $this->primary_active_text_color; $old_setting->default_text_color = $this->default_text_color; $old_setting->default_back_color = $this->default_back_color; $old_setting->default_border_color = $this->default_border_color; $old_setting->default_active_back_color = $this->default_active_back_color; $old_setting->default_active_text_color = $this->default_active_text_color; $old_setting->default_active_border_color = $this->default_active_border_color; $old_setting->active_link_color = $this->active_link_color; $old_setting->link_color = $this->link_color; $old_setting->css = $this->css; if ($this->logo_url) { $old_setting->logo_url = $this->logo_url; } else { unset($old_setting->logo_url); } $old_css_file_id = $old_setting->css_file_id; $old_setting->css_file_id = $this->saveCss($old_setting->css); $old_setting->update(); if (!empty($old_css_file_id) && $old_css_file_id !== 'pb-theme-color-default.css') { $css_file_path = UserUrl::css() . DIRECTORY_SEPARATOR . $old_css_file_id . '.css'; if (file_exists($css_file_path)) { unlink($css_file_path); } } return $old_setting; } return null; }
public function actionSettings() { if (Yii::$app->user->isGuest) { $this->redirect(Url::toRoute(['intro/index'])); return; } if (Yii::$app->user->identity->role == User::ROLE_DEMO) { $this->redirect(Url::toRoute(['user/signup-demo'])); return; } $this->layout = 'default'; $less_content = file_get_contents(Yii::getAlias('@webroot') . DIRECTORY_SEPARATOR . 'less' . DIRECTORY_SEPARATOR . 'pb-color-template.less'); $model = new UserSettingForm(); if ($model->load(Yii::$app->request->post(), 'UserSettingForm')) { if ($model->validate()) { $model->user_id = Yii::$app->user->identity->getId(); $file = UploadedFile::getInstance($model, 'logo_url'); if ($file) { if ($file->size !== 0) { $model->logo_url = $file; } else { $model->logo_url = null; } if ($file->size !== 0) { $old_file_id = ''; $user_setting = UserSetting::findByUserId($model->user_id); if ($user_setting) { $old_file_id = $user_setting->logo_url; } $file_id = AlphaId::id(rand(10000000000, 9999999999999)); $file_path = UserUrl::logo() . DIRECTORY_SEPARATOR . UserUrl::imageFile($file_id, UserUrl::IMAGE_ORIGINAL, 'png'); $file->saveAs($file_path); $image = Yii::$app->image->load($file_path); $image->resize(0, 49, Yii\image\drivers\Image::HEIGHT); $image->save(UserUrl::logo() . DIRECTORY_SEPARATOR . UserUrl::imageFile($file_id, UserUrl::IMAGE_SMALL, 'png')); $model->logo_url = $file_id; if (!empty($old_file_id)) { if (file_exists(UserUrl::logo() . DIRECTORY_SEPARATOR . UserUrl::imageFile($old_file_id, UserUrl::IMAGE_ORIGINAL, 'png'))) { unlink(UserUrl::logo() . DIRECTORY_SEPARATOR . UserUrl::imageFile($old_file_id, UserUrl::IMAGE_ORIGINAL, 'png')); } if (file_exists(UserUrl::logo() . DIRECTORY_SEPARATOR . UserUrl::imageFile($old_file_id, UserUrl::IMAGE_SMALL, 'png'))) { unlink(UserUrl::logo() . DIRECTORY_SEPARATOR . UserUrl::imageFile($old_file_id, UserUrl::IMAGE_SMALL, 'png')); } } } } else { $model->logo_url = null; } if (Yii::$app->request->post('defaultLogo', 0) == 1) { $model->logo_url = 'default-logo'; } if ($setting = $model->save()) { Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Настройки успешно сохранены.')); return $this->redirect(Url::toRoute('user/settings')); } } } else { if (!$model->loadByUserId(Yii::$app->user->identity->getId())) { } } return $this->render('settings', ['model' => $model, 'less_content' => $less_content]); }