Example #1
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'update' page.
  */
 public function actionCreate()
 {
     $model = new FaqElements();
     $modelAuthor = new FaqAuthor();
     $root = FaqRubrics::getRoot(new FaqRubrics());
     $catalog = $root->descendants()->findAll($root->id);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     // set attributes from get
     if (isset($_GET['FaqElements'])) {
         $model->attributes = $_GET['FaqElements'];
     }
     if (isset($_POST['FaqElements'])) {
         $model->attributes = $_POST['FaqElements'];
         if (isset($_POST['FaqAuthor'])) {
             $modelAuthor->attributes = $_POST['FaqAuthor'];
             $modelAuthor->save();
             $model->author_id = $modelAuthor->id;
         }
         if ($model->save()) {
             $url = isset($_POST['go_to_list']) ? $this->listUrl('index') : $this->itemUrl('update', $model->id);
             $this->redirect($url);
         }
     }
     $this->render('create', array('model' => $model, 'root' => $root, 'catalog' => $catalog, 'modelAuthor' => $modelAuthor));
 }
 public function actionElement($param)
 {
     //Если прищли данные с формы
     if (isset($_POST['AddQuestion'])) {
         if (!empty($_POST['AddQuestion']['name']) && !empty($_POST['AddQuestion']['email']) && !empty($_POST['AddQuestion']['rubrics']) && !empty($_POST['AddQuestion']['question'])) {
             preg_match('|([a-z0-9_\\.\\-]{1,20})@([a-z0-9\\.\\-]{1,20})\\.([a-z]{2,4})|is', $_POST['AddQuestion']['email'], $match);
             if (!empty($match)) {
                 $model = FaqAuthor::model()->find('email LIKE "' . $_POST['AddQuestion']['email'] . '"');
                 if (!$model) {
                     $model = new FaqAuthor();
                 }
                 $model->name = $_POST['AddQuestion']['name'];
                 $model->email = $_POST['AddQuestion']['email'];
                 if ($model->save()) {
                     $modelElement = new FaqElements();
                     $modelElement->parent_id = (int) $_POST['AddQuestion']['rubrics'];
                     $modelElement->author_id = $model->id;
                     $modelElement->question = '<p>' . $_POST['AddQuestion']['question'] . '</p>';
                     $modelElement->save();
                 }
             }
         }
     }
     //Если параметр текст - это каталог, если число - элемент
     $paramArr = explode("/", $param);
     $paramArr = array_pop($paramArr);
     if (is_numeric($paramArr)) {
         //Число - это элемент
         $model = array();
         $model['elements'] = FaqElements::model()->find('id = ' . (int) $paramArr);
         $model['rubrics'] = FaqRubrics::model()->find('id = ' . $model['elements']->parent_id . ' AND level>1');
         //Титл и SEO
         $this->setSEO(Yii::app()->request->requestUri, 'Вопрос-ответ', $model['rubrics']);
         $render = 'view';
     } else {
         //Список  категории
         $model = array();
         $root = FaqRubrics::model()->find('url LIKE "' . $paramArr . '"');
         //Титл и SEO
         $this->setSEO(Yii::app()->request->requestUri, 'Вопрос-ответ', $root);
         if (empty($root)) {
             throw new CHttpException(404, 'The page can not be found.');
         }
         $model['rubrics'] = $root->descendants(1)->findAll($root->id);
         $model['elements'] = FaqElements::model()->findAll(array("condition" => "status!=0 AND parent_id = " . $root->id, "order" => "id DESC"));
         $render = 'list';
     }
     if (empty($model)) {
         throw new CHttpException(404, 'The page can not be found.');
     }
     $this->render($render, array('model' => $model));
 }