Beispiel #1
0
 public function editArticle()
 {
     $errors = $this->errors;
     //实例化Model
     $articleModel = new ArticleModel();
     $articleTagModel = new ArticleTagModel();
     $articleId = $_GET['articleId'];
     //根据articleId 查询文章内容
     $article = $articleModel->getArticleById($articleId);
     $article['tagId'] = trim($article['tagId'], ',');
     //对tagId进行处理
     $article['tagId'] = explode(',', $article['tagId']);
     //对tagId进行处理
     //echo "<pre>";var_dump($article);echo"</pre>";exit;
     if ($_POST) {
         $pdata = $_POST;
         if (!$pdata['title']) {
             $result['code'] = '038';
             $result['message'] = $errors['038'];
         } elseif (!$pdata['typeId']) {
             $result['code'] = '039';
             $result['message'] = $errors['039'];
         } elseif (!$pdata['content']) {
             $result['code'] = '030';
             $result['message'] = $errors['030'];
         } else {
             if (isset($pdata['content'])) {
                 $start = strpos($pdata['content'], '<img');
                 $end = strpos($pdata['content'], '/>', $start);
                 $img = substr($pdata['content'], $start, $end - $start + 2);
                 $preg = '/src="(.*)"[\\s\\n]*\\/>/';
                 preg_match($preg, $img, $matche);
                 $pdata['picture'] = $matche[1];
             }
             if (isset($pdata['tagId']) && $pdata['tagId']) {
                 $pdata['tagId'] = array_unique($pdata['tagId']);
                 //数组去重
                 $articles = $articleModel->getArticleById($pdata['articleId']);
                 $articles['tagId'] = trim($articles['tagId'], ',');
                 //对tagId进行处理
                 $tagArr1 = explode(',', $articles['tagId']);
                 //对tagId进行处理
                 $tagArr2 = $pdata['tagId'];
                 //要添加的(要加1)
                 $tagArr3 = array_diff($tagArr2, $tagArr1);
                 $tagArr4 = array_diff($tagArr1, $tagArr2);
                 // //要删除的(要减1)
                 if (!empty($tagArr3)) {
                     $tagIds1 = implode(',', $tagArr3);
                     $articleTagModel->setIncByTagIds($tagIds1);
                 }
                 if (!empty($tagArr4)) {
                     $tagIds2 = implode(',', $tagArr4);
                     $articleTagModel->cutIncByTagIds($tagIds2);
                 }
                 $pdata['tagId'] = ',' . implode(',', $pdata['tagId']) . ',';
             }
             //echo "<pre>";var_dump($pdata);echo"</pre>";exit;
             $pdata['adminId'] = $_SESSION['admin']['adminId'];
             $results = $articleModel->updateArticle($pdata['articleId'], $pdata);
             if ($results) {
                 $result['code'] = '0';
                 $result['message'] = $errors['0'];
             } else {
                 $result['code'] = '035';
                 $result['message'] = $errors['035'];
             }
         }
         echo json_encode($result);
         exit;
     }
     //博主所有文章类型
     $allTypes = $this->getAllTypes($_SESSION['admin']['adminId']);
     //所有标签
     $allTags = $this->getAllTags($_SESSION['admin']['adminId']);
     $this->assign('allTypes', $allTypes);
     $this->assign('allTags', $allTags);
     $this->assign('article', $article);
     $this->assign("pageTitle", "编辑文章");
     $this->display();
 }