public function ManagedQuestionAction()
 {
     $request = Project::getRequest();
     $data = array();
     $question_model = new QuestionModel();
     $question_cat_model = new QuestionCatModel();
     $data['question_cat_list'] = $question_cat_model->loadAll();
     $id = (int) $request->getKeyByNumber(0);
     if (!$request->submit) {
         $question_cat_model = new QuestionCatModel();
         if ($id > 0) {
             $data['question'] = $question_model->loadQuestion($id);
             $tags_model = new QuestionTagModel();
             $tags = $tags_model->loadWhere(null, null, $id);
             foreach ($tags as $tag) {
                 $data['question_tag_list'] .= $tag['name'] . ', ';
             }
             $data['question_tag_list'] = rtrim($data['question_tag_list'], ', ');
             $data['tab_manage_question_name'] = "Редактирование вопроса";
         }
         $data['question_cat'] = $question_cat_model->loadAll();
         $this->BaseSiteData($data);
         $this->_view->assign('tab_list', TabController::getOwnTabs(false, false, false, false, false, false, true, false, false, false));
         //$this->_view->assign('tab_list', TabController::getQuestionAnswerTabs(false, false, false, false, true));
         $this->_view->ManagedQuestion($data);
         $this->_view->parse();
     } else {
         if ($id > 0) {
             $data['question'] = $question_model->load($id);
             if ($question_model->user_id != Project::getUser()->getDbUser()->id) {
                 if ($request->desktop_question) {
                     Project::getResponse()->redirect($request->createUrl('QuestionAnswer', 'UserQuestions'));
                 } else {
                     Project::getResponse()->redirect($request->createUrl('QuestionAnswer', 'List'));
                 }
             }
         }
         if ($request->question_text == null) {
             //TODO validator
             $data['error'][] = "Заполните текст вопроса";
             $question_cat_model = new QuestionCatModel();
             $data['question_cat'] = $question_cat_model->loadAll();
             $data['question_tag_list'] = $request->tags;
             $data['question']['q_text'] = $request->question_text;
             $data['question']['question_cat_id'] = (int) $request->cat_id;
             $data['question']['user_id'] = Project::getUser()->getDbUser()->id;
             $data['question']['creation_date'] = date("Y-m-d H:i:s");
             $this->BaseSiteData($data);
             $this->_view->ManagedQuestion($data);
             $this->_view->parse();
             return;
         }
         $question_model->q_text = $request->question_text;
         $question_model->questions_cat_id = (int) $request->cat_id;
         $question_model->user_id = Project::getUser()->getDbUser()->id;
         $question_model->creation_date = date("Y-m-d H:i:s");
         $q_id = $question_model->save();
         $tag_model = new QuestionTagModel();
         $question_tag_model = new QTagModel();
         $question_tag_model->deleteByQuestionId($id);
         //TODO: revamp
         $tags_ar = array();
         $tags_ar = explode(",", $request->tags);
         foreach ($tags_ar as $tag) {
             $tag = trim($tag);
             if (count($tag_model->loadByName($tag)) <= 0) {
                 $tag_model->name = $tag;
                 $tag_id = $tag_model->save();
                 /*if(count($question_tag_model->loadWhere($q_id, $tag_model->id)) <= 0) {
                 			$question_tag_model->question_id = $q_id;
                 			$question_tag_model->question_tag_id = $tag_model->id;
                 			$question_tag_model->save();
                 			$question_tag_model->clear();	
                 		}*/
             } else {
                 $tag_id = $tag_model->id;
             }
             $tag_model->clear();
             $question_tag_model->question_id = $q_id;
             $question_tag_model->question_tag_id = $tag_id;
             $question_tag_model->save();
             $question_tag_model->clear();
         }
         if ($request->desktop_question) {
             Project::getResponse()->redirect($request->createUrl('QuestionAnswer', 'UserQuestions'));
         } else {
             Project::getResponse()->redirect($request->createUrl('QuestionAnswer', 'List'));
         }
     }
 }
 public function EditQuestionAction()
 {
     $request = Project::getRequest();
     $id = $request->getKeyByNumber(0);
     $data = array();
     if (!$request->getKeyByNumber(1)) {
         if ($id > 0) {
             $model = new QuestionModel();
             $cat_model = new QuestionCatModel();
             $tag_model = new QuestionTagModel();
             $data['question'] = $model->load($id);
             $data['cat_list'] = $cat_model->loadAll();
             $tags_model = new QuestionTagModel();
             $tags = $tags_model->loadWhere(null, null, $id);
             foreach ($tags as $tag) {
                 $data['tags'] .= $tag['name'] . ', ';
             }
             $data['tags'] = rtrim($data['tags'], ', ');
             $this->BaseAdminData();
             $this->_view->EditQuestion($data);
             $this->_view->ajax();
         }
     } else {
         if ($id > 0) {
             $model = new QuestionModel();
             $model->load($id);
             $model->q_text = $request->question_text;
             $model->questions_cat_id = (int) $request->cat_id;
             $id = $model->save();
             $tag_model = new QuestionTagModel();
             $question_tag_model = new QTagModel();
             $tags_ar = array();
             $tags_ar = explode(",", $request->tags);
             foreach ($tags_ar as $tag) {
                 $tag = trim($tag);
                 if (count($tag_model->loadByName($tag)) > 0) {
                     if (count($question_tag_model->loadWhere($id, $tag_model->id)) <= 0) {
                         $question_tag_model->question_id = $id;
                         $question_tag_model->question_tag_id = $tag_model->id;
                         $question_tag_model->save();
                         $question_tag_model->clear();
                     }
                 } else {
                     $tag_model->name = $tag;
                     $tag_id = $tag_model->save();
                     $tag_model->clear();
                     $question_tag_model->question_id = $id;
                     $question_tag_model->question_tag_id = $tag_id;
                     $question_tag_model->save();
                     $question_tag_model->clear();
                 }
             }
         }
         Project::getResponse()->Redirect($request->createUrl('AdminQuestionAnswer', 'QuestionList'));
     }
 }