/** * 将演讲集迁移到新的资讯里面 * * @return void * @author **/ public function actionIndex() { echo "========== start processing ==========\n\n"; $i = 1; $info = Speech::find()->where(['is_del' => 0])->all(); foreach ($info as $speech) { preg_match('/[\\w][\\w-]*\\.(?:com\\.cn|com|cn|co|net|org|gov|cc|biz|info)/isU', $speech->video_url, $match); if (isset($match[0]) && $match[0] == 'youku.com' && strlen($speech->video_url) > 5) { // if (file_exists($src_file = Yii::getAlias("@frontend/web/{$speech->photo_url}"))) { // 复制图片 $ext = '.jpg'; if (strlen($speech->photo_url) > 0) { $src_file = Yii::getAlias("@frontend/web/{$speech->photo_url}{$ext}"); $new_file = 'speech' . substr($speech->photo_url, strpos($speech->photo_url, '/')); $dist_file = Yii::getAlias("@frontend/web/source/{$new_file}{$ext}"); if (!is_dir(dirname($dist_file))) { mkdir(dirname($dist_file), 0777, true); } $copied = @copy($src_file, $dist_file); } // 写入数据 $post = Post::findOne(['migrate_id' => md5($speech->id)]); if (!$post) { $post_model = new Post(); $post_model->news_title = $speech->title; $post_model->news_type = 'news_video'; $post_model->news_des = $speech->short_intro; $post_model->news_content = empty($speech->detailed_intro) ? ' ' : $speech->detailed_intro; $post_model->news_author = $speech->author; $post_model->news_cover_url = $new_file; $post_model->news_video_url = $speech->video_url; $post_model->news_active = 0; $post_model->migrate_id = md5($speech->id); $saved = $post_model->save(); // Msg $saved_msg = $saved ? '成功' : '失败'; $copied_msg = $copied ? '成功' : '失败'; echo "Process_{$i}: {$src_file} -> {$dist_file} 复制{$copied_msg} \n"; echo "{$src_file} -> {$dist_file} 迁移{$saved_msg} \n"; $i++; } } } echo "Process Over\n"; }
/** * Creates data provider instance with search query applied * @return ActiveDataProvider */ public function search($params = [], $pageSize = 16) { // print_r($params);exit; // $params['attrs.tag.tag_name'] = 'zhanglu'; // print_r($params);exit; $query = self::find()->select(['i_news.*', "GROUP_CONCAT(tag.tag_name SEPARATOR ',') as tag_names"])->where(['news_active' => 0])->joinWith(['attrs' => function ($query) { $query->from(['attrs' => '{{%news_attr_value}}'])->onCondition(['attrs.attr_id' => PostAttribute::TAG])->joinWith(['tag' => function ($query) { $query->from(['tag' => '{{%tag}}']); }]); // ->select([ // PostAttrVal::tableName() . '.news_id', // PostAttrVal::tableName() . '.attr_id', // PostAttrVal::tableName() . '.attr_value_text' // ]); }])->groupBy("{{%news}}.news_id"); // ->where(['and', 'poll_active=0', ['or', 'parent_id is null', 'parent_id = 0']]); // if (count($params) == 0) { // $query->orderBy(['news_id' => SORT_DESC])->limit(3); // } $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => $pageSize], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC, 'news_id' => SORT_DESC]]]); // $dataProvider->sort->attributes['attrs.attr_value_text'] = [ // 'asc' => ['attr_value_text' => SORT_ASC], // 'desc' => ['attr_value_text' => SORT_DESC], // ]; $dataProvider->sort->attributes['attrs.tag.tag_name'] = ['asc' => ['tag.tag_name' => SORT_ASC], 'desc' => ['tag.tag_name' => SORT_DESC]]; // print_r($params);exit; // return $dataProvider; if (!($this->load($params) && $this->validate())) { // echo "string";exit; return $dataProvider; } $query->andFilterWhere(['like', Post::tableName() . '.news_id', $this->news_id])->andFilterWhere(['like', 'news_title', $this->news_title])->andFilterWhere(['like', 'news_author', $this->news_author])->andFilterWhere(['like', 'news_from', $this->news_from])->andFilterWhere(['like', 'tag.tag_name', $this->getAttribute('attrs.tag.tag_name')]); // $query->orderBy(['news_id' => SORT_DESC])->limit(3); // if (count($params) == 0) { // } return $dataProvider; }
/** * 获取资讯的接口 * * @return void * @author **/ public function actionAjaxRecommend() { $like = Yii::$app->request->get('s', null); // JSON $post_model = new Post(); $news_result = $post_model->getAllRecommendResult($like); echo json_encode($news_result); }
/** * Action Tag * @param $id int 标签id */ public function actionTag($id, $page = 1) { //获取资讯列表信息 $pagesize = Post::PAGE_SIZE; $start = ($page - 1) * $pagesize; $mNewsMes = new Post(); $mNewsMessage = $mNewsMes->getTagnews($id, '', $pagesize, $start); $count = $mNewsMes->getTagnews($id, "true"); $totalpage = ceil($count / $pagesize); // $pages = new Pagination([ // 'totalCount' => $mNewsMessage->count(), // 'pageSize' => Post::PAGE_SIZE // ]); // $mNewsMessage = $mNewsMessage->asArray()->all(); // 获取首页标签信息 $mTag = new Tag(); $tags = $mTag->getTags(); $condition = array('tag_id' => $id); $tag_name = $mTag->getTagsBycond($condition); $tag_result = $mTag->findOne($id); $tag_des = isset($tag_result->tag_des) && $tag_result->tag_des ? $tag_result->tag_des : '让我们一起发现美丽与你共创美好的生活'; // 获取热门活动 $mPageBlock = new PageCustomBlock(); $slideOption['customId'] = PageCustomBlock::NEWS_PAGE_ACTIVITIES; $activity = $mPageBlock->block($slideOption); // 获取热门产品 $mProductStat = new IProductStat(); $HotProducts = $mProductStat->getHotProduct(); // 获取热门文章 $mPostStat = new PostAttrStat(); $HotArticles = $mPostStat->getHotArticle(); // 获取关注用户 $mFollow = new FollowTag(); $followUser = $mFollow->getFollowUser($id); $follow = ['tag_follow' => false]; if ($user = Yii::$app->util->isLogin()) { // 获取用户是否喜欢/Like $mFollow = new Follow(); $follow['tag_follow'] = $mFollow->getUserTagFollow($id, $user['uid']); } return $this->render('tag', ['data' => $mNewsMessage, 'tags' => $tags, 'activity' => $activity, 'products' => $HotProducts, 'article' => $HotArticles, 'tag_name' => $tag_name[0], 'tag_des' => $tag_des, 'tag_id' => $id, 'follow_user' => $followUser, 'totalpage' => $totalpage, 'follow' => $follow]); }
/** * 定义与Post的关联关系 */ public function getPost() { return $this->hasMany(Post::className(), ['news_id' => 'news_id']); }