Skip to content

Commit

Permalink
Merge pull request #148 from iiYii/forecho
Browse files Browse the repository at this point in the history
更新编辑器&&优化代码&&调整列表样式
  • Loading branch information
forecho authored Mar 30, 2017
2 parents 303d3a1 + c95175c commit 4327e83
Show file tree
Hide file tree
Showing 51 changed files with 996 additions and 1,434 deletions.
3 changes: 3 additions & 0 deletions common/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
'class' => 'understeam\slack\Client',
'url' => '',
],
'assetManager' => [
'linkAssets' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
Expand Down
2 changes: 1 addition & 1 deletion common/helpers/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Arr extends ArrayHelper
* 随机筛选$num个数组
* @param array $arr
* @param int $num
* @return array
* @return array|false
*/
public static function arrayRandomAssoc(Array $arr, $num = 1)
{
Expand Down
31 changes: 31 additions & 0 deletions common/helpers/UploadHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Created by PhpStorm.
* User: user
* Date: 2017/1/18
* Time: 17:56
*/

namespace common\helpers;


class UploadHelper
{
public static function getCurlValue($filename, $contentType, $postname)
{
// PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
// See: https://wiki.php.net/rfc/curl-file-upload
if (function_exists('curl_file_create')) {
return curl_file_create($filename, $contentType, $postname);
}

// Use the old style if using an older version of PHP
$value = "@{$filename};filename=" . $postname;
if ($contentType) {
$value .= ';type=' . $contentType;
}

return $value;
}

}
7 changes: 4 additions & 3 deletions common/models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @property string $excerpt
* @property string $image
* @property string $content
* @property string $tags
* @property string|array $tags
* @property string $last_comment_time
* @property string $last_comment_username
* @property integer $view_count
Expand All @@ -34,6 +34,7 @@
* @property integer $updated_at
*
* @property PostMeta $category
* @property User $user
*/
class Post extends ActiveRecord
{
Expand Down Expand Up @@ -88,9 +89,9 @@ public function rules()
[['type'], 'string', 'max' => 32],
[['last_comment_username'], 'string', 'max' => 20],
[['title'], 'string', 'max' => 50, 'min' => 2],
[['excerpt', 'image', 'tags'], 'string', 'max' => 255],
[['excerpt', 'image'], 'string', 'max' => 255],
[['author'], 'string', 'max' => 100],
[['cc'], 'safe']
[['cc', 'tags'], 'safe']
];
}

Expand Down
7 changes: 4 additions & 3 deletions common/models/PostComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use frontend\modules\user\models\UserMeta;
use Yii;
use common\components\db\ActiveRecord;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;
use yii\web\NotFoundHttpException;
Expand All @@ -27,6 +28,7 @@
* @property string $updated_at
*
* @property Topic $topic
* @property Post $post
*/
class PostComment extends ActiveRecord
{
Expand Down Expand Up @@ -131,7 +133,7 @@ public static function findDeletedComment($id)
/**
* 评论列表
* @param $postId
* @return static
* @return Query
*/
public static function findCommentList($postId)
{
Expand All @@ -150,8 +152,7 @@ public function isCurrent()
/**
* @inheritdoc
*/
public
function attributeLabels()
public function attributeLabels()
{
return [
'id' => 'ID',
Expand Down
42 changes: 37 additions & 5 deletions common/models/PostMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace common\models;

use DevGroup\TagDependencyHelper\CacheableActiveRecord;
use DevGroup\TagDependencyHelper\NamingHelper;
use DevGroup\TagDependencyHelper\TagDependencyTrait;
use Yii;
use yii\caching\TagDependency;
use yii\helpers\ArrayHelper;
use common\components\db\ActiveRecord;

Expand All @@ -20,11 +22,13 @@
* @property string $order
* @property string $created_at
* @property string $updated_at
* @property PostMeta[] $children
*/
class PostMeta extends ActiveRecord
{

use TagDependencyTrait;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -97,20 +101,21 @@ public static function topicCategory()

/**
* 返回无人区节点id
* @return mixed|static
* @return PostMeta|int
*/
public static function noManLandId()
{
/** @var PostMeta $postMeta */
$postMeta = self::find()->where(['alias' => 'no-man-land'])->one();
if ($postMeta) {
return $postMeta->id;
}
return $postMeta;
}

public function getParents()
public function getChildren()
{
return ArrayHelper::map(static::find()->where(['parent' => null])->orWhere(['parent' => 0])->all(), 'id', 'name');
return $this->hasMany(self::className(), ['parent' => 'id'])->from(self::tableName() . ' p2');
}

public function getTypes()
Expand All @@ -121,8 +126,35 @@ public function getTypes()
];
}

public static function topic()
/**
* @param array $conditions
* @return array
*/
public static function getNodesMap($conditions = [])
{
return ArrayHelper::map(static::find()->where(['type' => 'topic_category'])->andFilterWhere($conditions)->all(), 'alias', 'name');
}

/**
* 获取父子节点
* @return array
*/
public static function getNodes()
{
return ArrayHelper::map(static::find()->where(['type' => 'topic_category'])->all(), 'alias', 'name');
$cacheKey = md5(__METHOD__);
if (false === $nodes = \Yii::$app->cache->get($cacheKey)) {
$parents = PostMeta::find()->where([PostMeta::tableName() . '.parent' => [0, null]])->joinWith('children')->orderBy(['order' => SORT_ASC])->all();
/*** @var PostMeta $parent */
foreach ($parents as $parent) {
$nodes[$parent->alias] = $parent;
}
//一天缓存
\Yii::$app->cache->set($cacheKey, $nodes, 86400,
new TagDependency([
'tags' => [NamingHelper::getCommonTag(PostMeta::className())]
])
);
}
return $nodes;
}
}
4 changes: 2 additions & 2 deletions common/models/PostSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class PostSearch extends Post
public function rules()
{
return [
[['id', 'post_meta_id', 'user_id', 'view_count', 'comment_count', 'favorite_count', 'like_count', 'thanks_count', 'hate_count', 'status', 'order', 'created_at', 'updated_at'], 'integer'],
[['type', 'title', 'author', 'excerpt', 'image', 'content', 'tags'], 'safe'],
[['user_id', 'view_count', 'comment_count', 'favorite_count', 'like_count', 'thanks_count', 'hate_count', 'status', 'order', 'created_at', 'updated_at'], 'integer'],
[['type', 'title', 'author', 'excerpt', 'image', 'content', 'tags', 'id', 'post_meta_id'], 'safe'],
];
}

Expand Down
15 changes: 2 additions & 13 deletions common/models/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace common\models;

use yii\data\ActiveDataProvider;

/**
* This is the model class for table "topic".
Expand All @@ -15,18 +14,8 @@
*/
class Search extends \hightman\xunsearch\ActiveRecord
{
public function search($keyword)
public static function search($keyword)
{
$query = self::find()->where($keyword)->andWhere(['status' => [1, 2]]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => [
'updated_at' => SORT_DESC,
]
]
]);

return $dataProvider;
return self::find()->where($keyword)->andWhere(['status' => [1, 2]])->asArray()->all();
}
}
21 changes: 17 additions & 4 deletions common/services/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class NotificationService
* @param User $fromUser
* @param Topic $topic
* @param PostComment $comment
* @param string $rawComment
* @throws Exception
* @param $atUsers
*/
public function newReplyNotify(User $fromUser, Topic $topic, PostComment $comment, $atUsers)
{
$users = [];
foreach ($topic->follower as $key => $value) {
$users[$value->user_id] = $value->user_id;
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public function batchNotify($type, User $fromUser, $users, Post $post, PostComme
'user_id' => $key,
'post_id' => $post->id,
'comment_id' => $comment ? $comment->id : 0,
'data' => $comment ? $comment->comment : $post->content,
'data' => self::getNotifyData($type, $comment ? $comment->comment : $post->content),
'type' => $type,
]);
$this->notifiedUsers[] = $key;
Expand All @@ -121,7 +121,7 @@ public function batchNotify($type, User $fromUser, $users, Post $post, PostComme
/**
* 查找用户的动作通知
* @param UserMeta $meta
* @return null|static
* @return null|Notification
*/
public function findUserActionNotify(UserMeta $meta)
{
Expand All @@ -135,4 +135,17 @@ public function findUserActionNotify(UserMeta $meta)
'type' => $meta->target_type . '_' . $meta->type,
] + $condition);
}

/**
* @param $type
* @param $data
* @return string
*/
public static function getNotifyData($type, $data)
{
if (in_array($type, ['topic_like', 'topic_favorite', 'topic_thanks', 'at_topic'])) {
return '';
}
return $data;
}
}
65 changes: 60 additions & 5 deletions common/services/PostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
namespace common\services;


use common\models\PostTag;
use common\models\PostMeta;
use common\models\PostSearch;
use common\models\User;
use common\models\Post;
use DevGroup\TagDependencyHelper\NamingHelper;
use frontend\models\Notification;
use Yii;
use yii\caching\TagDependency;
use yii\helpers\ArrayHelper;
use yii\helpers\Url;

Expand All @@ -31,6 +35,8 @@ public static function delete(Post $post)

/**
* 过滤内容
* @param $content
* @return bool
*/
public function filterContent($content)
{
Expand All @@ -40,10 +46,10 @@ public function filterContent($content)
if (in_array($content, $data)) {
return false;
}
$action = ['+1', '', '很赞', '喜欢', '收藏', 'mark', '写的不错', '不错', '给力'];
if (in_array($content, $action)) {
return false;
}
// $action = ['+1', '赞', '很赞', '喜欢', '收藏', 'mark', '写的不错', '不错', '给力'];
// if (in_array($content, $action)) {
// return false;
// }
return true;
}

Expand Down Expand Up @@ -112,4 +118,53 @@ public static function contentReplaceTag($content)
return $content;
}

/**
* @param $params
* @return array
*/
public static function search($params)
{
$searchModel = new PostSearch();


// 话题或者分类筛选
empty($params['tag']) ?: $params['PostSearch']['tags'] = $params['tag'];
if (isset($params['node'])) {
$postMeta = PostMeta::findOne(['alias' => $params['node']]);
($postMeta) ? $params['PostSearch']['post_meta_id'] = $postMeta->id : null;
}

if (isset($params['tab'])) {
$postMeta = PostMeta::findOne(['alias' => $params['tab']]);
($postMeta) ? $params['PostSearch']['post_meta_id'] = ArrayHelper::getColumn($postMeta->children, 'id') : null;
}

$dataProvider = $searchModel->search($params);
$dataProvider->query->andWhere([Post::tableName() . '.type' => 'topic', 'status' => [Post::STATUS_ACTIVE, Post::STATUS_EXCELLENT]]);
// 排序
$sort = $dataProvider->getSort();
$sort->attributes = array_merge($sort->attributes, [
'hotest' => [
'asc' => [
'view_count' => SORT_DESC,
'created_at' => SORT_DESC
],
],
'excellent' => [
'asc' => [
'status' => SORT_DESC,
'view_count' => SORT_DESC,
'created_at' => SORT_DESC
],
],
'uncommented' => [
'asc' => [
'comment_count' => SORT_ASC,
'created_at' => SORT_DESC
],
]
]);

return ['searchModel' => $searchModel, 'dataProvider' => $dataProvider];
}
}
Loading

0 comments on commit 4327e83

Please sign in to comment.