/**
  * Кабинет пользователя
  */
 public function actionUser_cabinet()
 {
     if (Yii::app()->request->isAjaxRequest) {
         if (isset($_POST['Users'])) {
             $model = Users::model()->findByPk($_POST['Users']['idUser']);
             $oldFile = null;
             $fileName = null;
             // Генерим имя фото
             if ('' != $_FILES['Users']['name']['image']) {
                 $fileName = AuxiliaryFunctions::getUniquNamePhoto($_FILES['Users']['name']['image']);
                 $_POST['Users']['photo'] = $fileName;
                 $oldFile = $model->photo;
             }
             $model->attributes = $_POST['Users'];
             if ($model->save(false)) {
                 if ($fileName) {
                     AuxiliaryFunctions::savePhoto($model, $fileName, $oldFile);
                 }
                 if (Yii::app()->request->isAjaxRequest) {
                     echo CJSON::encode(array('result' => 'ok'));
                     exit;
                 }
             } else {
                 if (Yii::app()->request->isAjaxRequest) {
                     echo CJSON::encode(array('error' => 'save'));
                     exit;
                 }
             }
         }
     }
     if (Yii::app()->user->isGuest || 'Admins' == Yii::app()->session->get("typeAuthorize") || !isset($_GET['idUser']) || $_GET['idUser'] != Yii::app()->user->id) {
         $this->redirect(Yii::app()->homeUrl);
     } else {
         $model = Users::model()->findByPk($_GET['idUser']);
     }
     $artWaitingModeration = Articles::model()->count('idUser = :idUser AND moderationAppruv = 0', array(":idUser" => $_GET['idUser']));
     $artPublication = Articles::model()->count('idUser = :idUser AND public = 1', array(":idUser" => $_GET['idUser']));
     $menuInBookmarks = Bookmarks::model()->getUserBookmarks(Yii::app()->user->id);
     $this->render('user_cabinet', array('model' => $model, 'tabArticles' => array('artWaitingModeration' => $artWaitingModeration, 'artPublication' => $artPublication, 'menuInBookmarks' => $menuInBookmarks)));
 }
 /**
  * Добавление статьи пользователем
  */
 public function actionNew_article()
 {
     if (Yii::app()->request->isAjaxRequest) {
         if (isset($_POST['idMenu'])) {
             $category1 = array('' => 'Без категории');
             $category2 = Categorys::getAllCategories($_POST['idMenu']);
             $category = $category1 + $category2;
             $filteredCategory = array();
             $i = 0;
             foreach ($category as $key => $val) {
                 $filteredCategory[$i]['id'] = $key;
                 $filteredCategory[$i]['title'] = $val;
                 $i++;
             }
             echo CJSON::encode(array('categorys' => $filteredCategory));
             exit;
         }
     }
     // Проверки на доступ к странице
     if (Yii::app()->user->isGuest) {
         $this->redirect(Yii::app()->homeUrl);
     }
     if (isset($_POST['cancel'])) {
         $this->redirect(Yii::app()->homeUrl);
     }
     $model = new Articles('create');
     // Нажата кнопка "Редактировать" или "Добавить"
     if (isset($_POST['Articles'])) {
         $oldFile = null;
         $oldFile = $model->photo;
         // Генерим имя фото
         if ('' != $_FILES['Articles']['name']['image']) {
             $fileName = AuxiliaryFunctions::getUniquNamePhoto($_FILES['Articles']['name']['image']);
             $_POST['Articles']['photo'] = $fileName;
             $oldFile = $model->photo;
         }
         $model->attributes = $_POST['Articles'];
         $model->idUser = Yii::app()->user->id;
         if ($model->validate()) {
             if ($model->save()) {
                 if ($fileName) {
                     AuxiliaryFunctions::savePhoto($model, $fileName, $oldFile);
                 }
                 if (1 == $model->public && 1 == $model->moderationAppruv) {
                     $this->redirect($this->createAbsoluteUrl('articles/view_article', array('idArticle' => $model->idArticle, 'idMenu' => $model->idMenu)));
                 }
                 $this->redirect($this->createAbsoluteUrl('base/index', array('referrer' => 'new_article')));
             }
         }
     }
     $menus = Mainmenu::model()->getDropDownMenu();
     $idMenu = !empty($model->idMenu) ? $model->idMenu : key($menus);
     $category1 = array('' => 'Без категории');
     $category2 = Categorys::getAllCategories($idMenu);
     $category = $category1 + $category2;
     $this->render('new_article', array('model' => $model, 'menus' => $menus, 'category' => $category));
 }
 public function actionSave_article()
 {
     $idArticle = null;
     // Проверки на доступ к странице
     if (Yii::app()->user->isGuest) {
         $this->redirect($this->createAbsoluteUrl('default/index'));
     }
     if (isset($_POST['cancel'])) {
         $this->redirect($this->createAbsoluteUrl('default/list_articles', $_GET));
     }
     if (isset($_POST['delete'])) {
         $this->actionDelete_article();
         $this->redirect($this->createAbsoluteUrl('default/list_articles', $_GET));
     }
     // Автокомплитер тегов
     if (isset($_GET['q'])) {
         $lastTag = end(explode(",", $_GET['q']));
         $criteria = new CDbCriteria();
         $criteria->condition = 'textTag LIKE :tag';
         $criteria->params = array(':tag' => '%' . trim(htmlspecialchars($lastTag)) . '%');
         if (isset($_GET['limit']) && is_numeric($_GET['limit'])) {
             $criteria->limit = $_GET['limit'];
         }
         $tags = Tags::model()->findAll($criteria);
         if ($tags) {
             $resStr = '';
             foreach ($tags as $tag) {
                 $resStr .= $tag->textTag . "\n";
             }
             echo $resStr;
         }
         exit;
     }
     // Редактирование или добавление новой
     if (isset($_GET['idArticle'])) {
         $idArticle = $_GET['idArticle'];
         $model = Articles::model()->findByPk($_GET['idArticle']);
         $model['tagArray'] = AuxiliaryFunctions::getTagsList($model->tagstoarticles, false);
     } else {
         $model = new Articles('create');
     }
     // Нажата кнопка "Редактировать" или "Добавить"
     if (isset($_POST['Articles'])) {
         $oldFile = null;
         $oldFile = $model->photo;
         $fileName = null;
         // Генерим имя фото
         if ('' != $_FILES['Articles']['name']['image']) {
             $fileName = AuxiliaryFunctions::getUniquNamePhoto($_FILES['Articles']['name']['image']);
             $_POST['Articles']['photo'] = $fileName;
             $oldFile = $model->photo;
         }
         $model->attributes = $_POST['Articles'];
         if (empty($model->idUser)) {
             $model->idUser = Users::getIdUserForAdmin();
         }
         if ($model->validate()) {
             if ($model->save()) {
                 $idArticle = $model->idArticle;
                 if ($fileName) {
                     AuxiliaryFunctions::savePhoto($model, $fileName, $oldFile);
                 }
                 // Теги для статьи
                 if (isset($_POST['textTag'])) {
                     $tagsArray = explode(",", $_POST['textTag']);
                     $idArray = array();
                     foreach ($tagsArray as $item) {
                         $tagId = null;
                         if (!empty($item)) {
                             $item = trim(htmlspecialchars($item));
                         } else {
                             continue;
                         }
                         $tagResult = Tags::model()->findByAttributes(array('textTag' => $item));
                         if (null === $tagResult) {
                             $newTag = new Tags();
                             $newTag->textTag = $item;
                             if ($newTag->save()) {
                                 $tagId = $newTag->idTag;
                             }
                         } else {
                             $tagId = $tagResult->idTag;
                         }
                         $idArray[] = $tagId;
                     }
                     if ($idArray) {
                         foreach ($idArray as $item) {
                             $item = (int) $item;
                             $idArticle = (int) $idArticle;
                             if (!TagsToArticles::model()->exists('idArticle = :idArticle AND idTag = :idTag', array(':idArticle' => $idArticle, ':idTag' => $item))) {
                                 $newTags2Art = new TagsToArticles();
                                 $newTags2Art->idArticle = $idArticle;
                                 $newTags2Art->idTag = (int) $item;
                                 $newTags2Art->save();
                             }
                         }
                     }
                 }
                 $this->redirect($this->createAbsoluteUrl('default/list_articles', $_GET));
             }
         }
     }
     $menus = Mainmenu::model()->getDropDownMenu();
     $idMenu = !empty($model->idMenu) ? $model->idMenu : key($menus);
     $category1 = array('' => 'Без категории');
     $category2 = Categorys::getAllCategories($idMenu);
     $category = $category1 + $category2;
     $tags = new Tags();
     $this->render('//articles/save_article', array('model' => $model, 'menus' => $menus, 'category' => $category, 'tags' => $tags));
 }