/**
  * Create article
  * @author Devin Jin
  */
 public function actionCreate()
 {
     $attributes = $this->getParams();
     $article = new Article();
     $article->load($attributes, '');
     //short url generation
     $article->_id = new \MongoId();
     $originUrl = Yii::$app->request->hostInfo . '/msite/article/' . $article->_id;
     $urlArr = Yii::$app->urlService->shortenUrl($originUrl);
     $article->url = $urlArr['Short'];
     $accountId = $this->getAccountId();
     $article->accountId = $accountId;
     if (false === $article->save()) {
         $errors = array_keys($article->errors);
         if ($errors[0] == 'name') {
             $errors[0] = 'title';
         }
         throw new InvalidParameterException([$errors[0] => Yii::t("microSite", $errors[0] . '_field_not_empty')]);
     } else {
         return $article;
     }
 }
 public function actionTitle()
 {
     $url = $this->getQuery('url');
     if (empty($url)) {
         throw BadRequestHttpException('common', 'parameters_missing');
     }
     $url = urldecode($url);
     $page = Page::getByShortUrl($url);
     if (!empty($page->title)) {
         return ['title' => $page->title];
     }
     $article = Article::getByUrl($url);
     return ['title' => empty($article->name) ? '' : $article->name];
 }
示例#3
0
 /**
  * Render article page
  */
 public function actionArticle($id = '')
 {
     $id = new \MongoId($id);
     $article = Article::findByPk($id);
     if (empty($article) || empty($article->url)) {
         return $this->redirect(self::NOT_FOUND_PAGE_PATH);
     }
     $actionView = $this->getView();
     $sdk = Yii::$app->wechatSdk;
     $sdk->refererUrl = $sdk->refererDomain . 'msite/article/' . $id;
     $signPackage = $sdk->getSignPackage();
     $actionView->registerJsFile('https://res.wx.qq.com/open/js/jweixin-1.0.0.js');
     $actionView->registerJsFile(self::MICROSITE_PATH . 'article/index.js');
     $actionView->registerJsFile(self::VENDOR_PATH . 'moment/min/moment.min.js');
     $this->layout = self::ARTICLE_PATH;
     return $this->render(self::ARTICLE_PATH, ['signPackage' => $signPackage]);
 }
 public function actionArticle()
 {
     $allArticles = Article::findAll([]);
     if ($allArticles) {
         foreach ($allArticles as $article) {
             if (isset($article->url) && !empty($article->url)) {
                 $url = $article->url;
                 $statResult = Yii::$app->urlService->statistics($url);
                 $longUrl = $statResult['Long'];
                 $newUrl = ereg_replace('microsite', 'msite', $longUrl);
                 $shortenResult = Yii::$app->urlService->shortenUrl($newUrl);
                 $shortUrl = $shortenResult['Short'];
                 $article->url = $shortUrl;
                 if (!$article->update(false)) {
                     Yii::error('Update article ' . (string) $article->_id . ' fail', 'application');
                 }
             }
         }
     }
 }
 public function actionIndex()
 {
     $query = Article::find();
     $channelId = $this->getQuery('channel');
     $where = ['isDeleted' => false, 'channel' => new \MongoId($channelId)];
     $orderBy = $this->getQuery('orderBy');
     if (!empty($orderBy)) {
         $orderBy = Json::decode($orderBy, true);
         foreach ($orderBy as $key => $value) {
             if ('asc' === strtolower($value)) {
                 $orderBy[$key] = SORT_ASC;
             } else {
                 $orderBy[$key] = SORT_DESC;
             }
         }
     } else {
         $orderBy = ['createdAt' => SORT_DESC];
     }
     return new ActiveDataProvider(['query' => $query->where($where)->orderBy($orderBy)]);
 }
 /**
  * Delete channel
  */
 public function actionDelete($id)
 {
     $channelId = new \MongoId($id);
     $articleCount = Article::countByChannel($channelId);
     if ($articleCount > 0) {
         return $articleCount;
     }
     if (!ArticleChannel::deleteAll(['_id' => $channelId])) {
         throw new ServerErrorHttpException("failed to delete article channel for unknown reason");
     }
     Yii::$app->getResponse()->setStatusCode(204);
 }