Skip to content

Commit

Permalink
tag
Browse files Browse the repository at this point in the history
  • Loading branch information
yidashi committed Jul 24, 2016
1 parent cc158e9 commit 3548324
Show file tree
Hide file tree
Showing 20 changed files with 843 additions and 116 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,26 @@
前台访问地址: `http:https://localhost:8080/`

后台访问地址: `http:https://localhost:8080/admin/` 管理员 帐号 `hehe` 密码 `111111`


## 目录结构

```
api api
backend 后台
common 核心
console 命令
database 数据库(迁移 填充)
frontend 前台
plugins 插件
runtime 运行时(日志 缓存等)
tests 测试
vendor 扩展
web web目录(web服务器可只开放该目录,保证安全)
wechat 微信
.env 基本配置文件
helpers 基本工具函数(已自动加载)
```

## 现有功能:

* rbac权限管理
Expand Down
1 change: 1 addition & 0 deletions backend/controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function actionView($id)
public function actionCreate($module = 'base')
{
$model = new Article();
$model->loadDefaultValues();
$dataModel = new ArticleData();
if ($module != 'base') {
$moduleModelClass = $this->findModule($module);
Expand Down
9 changes: 9 additions & 0 deletions backend/controllers/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ public function behaviors()
];
}

public function actions()
{
return [
'search' => [
'class' => 'common\\actions\\TagSearchAction'
]
];
}

/**
* Lists all Tag models.
* @return mixed
Expand Down
4 changes: 3 additions & 1 deletion backend/modules/rbac/controllers/MenuController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Yii;
use mdm\admin\models\Menu;
use mdm\admin\models\searchs\Menu as MenuSearch;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
Expand Down Expand Up @@ -43,7 +44,8 @@ public function actions()
'findModel' => [$this, 'findModel']
],
'position' => [
'class' => 'yii2tech\\admin\\actions\\Position'
'class' => 'yii2tech\\admin\\actions\\Position',
'returnUrl' => Url::current()
]
];
}
Expand Down
10 changes: 5 additions & 5 deletions backend/views/article/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use yii\helpers\Html;
use backend\widgets\ActiveForm;
use backend\widgets\meta\MetaForm;
use common\widgets\tag\TagsInput;
use common\behaviors\TagBehavior;

/* @var $this yii\web\View */
/* @var $model common\models\Article */
Expand Down Expand Up @@ -57,21 +59,19 @@
]
]
) ?>
<?= $form->boxField($model, 'cover')->widget(\common\widgets\upload\SingleWidget::className()) ?>
<?= $form->boxField($model, 'cover', ['collapsed' => true])->widget(\common\widgets\upload\SingleWidget::className()) ?>

<?= $form->field($model, 'is_top')->checkbox() ?>

<?= $form->field($model, 'is_hot')->checkbox() ?>

<?= $form->field($model, 'is_best')->checkbox() ?>

<?= $form->field($model, 'status')->radioList(\common\models\Article::getStatusList()) ?>
<?= $form->field($model, 'status')->checkbox() ?>

<?= $form->field($model, 'view')->textInput() ?>

<?= $form->field($model, 'tagNames')->widget(\common\widgets\tag\Tag::className(), [
'clientOptions' => ['width' => '230px']
]) ?>
<?= $form->boxField($model, TagBehavior::$formName)->widget(TagsInput::className())->header(TagBehavior::$formLable); ?>

<?= $form->field($model, 'source')->textInput() ?>

Expand Down
35 changes: 35 additions & 0 deletions common/actions/TagSearchAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Created by PhpStorm.
* User: yidashi
* Date: 16/7/24
* Time: 下午2:12
*/

namespace common\actions;

use common\models\Tag;
use yii\base\Action;

class TagSearchAction extends Action
{
/**
* @var bool
*/
public $skipEmpty = false;

public function run()
{
\Yii::$app->response->format = 'json';
$q = \Yii::$app->request->get('q');

if (empty($q) && $this->skipEmpty) {
$data = ['id' => '', 'text' => ''];
} else {
$data = Tag::find()->where(['like', 'name', $q])->select('id,name text')->asArray()->all();
}
return [
'results' => $data
];
}
}
105 changes: 105 additions & 0 deletions common/behaviors/TagBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Created by PhpStorm.
* User: yidashi
* Date: 16/7/24
* Time: 下午1:26
*/

namespace common\behaviors;


use common\models\ArticleTag;
use common\models\Tag;
use yii\base\Behavior;
use yii\db\ActiveRecord;

class TagBehavior extends Behavior
{
private $_tags;

public static $formName = "tagItems";

public static $formLable = '标签';

public function events()
{
return [
ActiveRecord::EVENT_INIT => 'initInternal',
ActiveRecord::EVENT_AFTER_INSERT => 'afterSave',
ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave',
ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete',
];
}

public function initInternal($event)
{

}
public function getTags()
{
return $this->owner->hasMany(Tag::className(), ['id' => 'tag_id'])
->viaTable('{{%article_tag}}', ['article_id' => 'id']);
}

public function getTagItems()
{
if($this->_tags === null){
$this->_tags = [];
foreach($this->owner->tags as $tag) {
$this->_tags[] = $tag->name;
}
}
return $this->_tags;
}

public function getTagNames()
{
return join(' ', $this->getTagItems());
}

public function afterSave()
{
if(!$this->owner->isNewRecord) {
$this->beforeDelete();
}

$data = \Yii::$app->request->post($this->owner->formName());
// 更新原标签文章数
$oldTags = $this->getTagItems();
Tag::updateAllCounters(['article' => -1], ['name' => $oldTags]);
// 先清除文章所有标签
ArticleTag::deleteAll(['article_id' => $this->owner->id]);
if(isset($data[static::$formName]) && !empty($data[static::$formName])) {
$tags = $data[static::$formName];
foreach($tags as $tag) {
$tagModel = Tag::findOne(['name' => $tag]);
if (empty($tagModel)) {
$tagModel = new Tag();
$tagModel->name = $tag;
$tagModel->save();
}
$articleTag = new ArticleTag();
$articleTag->article_id = $this->owner->id;
$articleTag->tag_id = $tagModel->id;
$articleTag->save();
}
Tag::updateAllCounters(['article' => 1], ['name' => $tags]);
}
}

public function beforeDelete()
{
$pks = [];

foreach($this->owner->tags as $tag){
$pks[] = $tag->primaryKey;
}

if (count($pks)) {
Tag::updateAllCounters(['article' => -1], ['in', 'id', $pks]);
}
Tag::deleteAll(['article' => 0]);
ArticleTag::deleteAll(['article_id' => $this->owner->id]);
}
}
45 changes: 17 additions & 28 deletions common/models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use common\behaviors\MetaBehavior;
use common\behaviors\PushBehavior;
use common\behaviors\SoftDeleteBehavior;
use common\behaviors\TagBehavior;
use common\behaviors\UserBehavior;
use common\behaviors\VoteBehavior;
use common\models\behaviors\ArticleBehavior;
Expand Down Expand Up @@ -115,6 +116,12 @@ public function attributeHints()
'tagNames' => '(空格分隔多个标签)'
];
}
public function loadDefaultValues($skipIfSet = true)
{
parent::loadDefaultValues($skipIfSet);
$this->status = self::STATUS_ACTIVE;
return $this;
}

/**
* {@inheritdoc}
Expand All @@ -134,6 +141,7 @@ public function behaviors()
'class' => VoteBehavior::className(),
'type' => 'article'
],
['class' => TagBehavior::className()],
UserBehavior::className()
];
if (!Yii::$app->request->isConsoleRequest) {
Expand Down Expand Up @@ -181,12 +189,15 @@ public function getData()
return $this->hasOne(ArticleData::className(), ['id' => 'id']);
}

public function getTags()
public function getExtend()
{
return $this->hasMany(Tag::className(), ['id' => 'tag_id'])
->viaTable('{{%article_tag}}', ['article_id' => 'id']);
if ($this->module != 'base') {
$module = ArticleModule::find()->where(['name' => $this->module])->one();
return $this->hasOne($module->model, ['id' => 'id']);
}
}


/**
* 真实浏览量
*/
Expand Down Expand Up @@ -215,26 +226,6 @@ public function addView()
$cache->set($key, 1);
}
}
private $_tagNames;
/**
* 获取所有标签,默认空格分隔
* @param string $seperator 分隔符
* @return string
*/
public function getTagNames($seperator = ' ')
{
$tags = $this->tags;
if (!empty($tags)) {
$tagNames = [];
foreach($tags as $tag) {
$tagNames[] = $tag->name;
}
$tagNames = join($seperator, $tagNames);
} else {
$tagNames = '';
}
return $tagNames;
}

/**
* 当前用户是否收藏
Expand All @@ -252,11 +243,9 @@ public function getIsFavourite()
return false;
}

public function getExtend()
public function getIsReprint()
{
if ($this->module != 'base') {
$module = ArticleModule::find()->where(['name' => $this->module])->one();
return $this->hasOne($module->model, ['id' => 'id']);
}
return !empty($this->source);
}

}
4 changes: 2 additions & 2 deletions common/models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ public function getCategoryNameById($id)
return isset($list[$id]) ? $list[$id] : null;
}

public function getCategoryIdByName($name)
public static function getIdByName($name)
{
$list = $this->lists();
$list = self::lists();

return array_search($name, $list);
}
Expand Down
1 change: 1 addition & 0 deletions common/models/Nav.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static function tableName()
public function rules()
{
return [
[['key', 'title'], 'required'],
[['key', 'title'], 'string', 'max' => 128],
];
}
Expand Down
27 changes: 0 additions & 27 deletions common/models/behaviors/ArticleBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public function afterReductionInternal($event)
public function afterInsertInternal($event)
{
Category::updateAllCounters(['article' => 1], ['id' => $event->sender->category_id]);
$this->setTagNames($event->sender);
}
/**
* 修改文章后(如果修改了分类,更新分类文章数)
Expand All @@ -78,31 +77,5 @@ public function afterUpdateInternal($event) {
Category::updateAllCounters(['article' => 1], ['id' => $event->sender->category_id]);
Category::updateAllCounters(['article' => -1], ['id' => $changedAttributes['category_id']]);
}
$this->setTagNames($event->sender);
}
public function setTagNames($model)
{
$data = \Yii::$app->request->post($model->formName());
// 更新原标签文章数
$oldTags = explode(' ', $model->getTagNames());
Tag::updateAllCounters(['article' => -1], ['name' => $oldTags]);
// 先清除文章所有标签
ArticleTag::deleteAll(['article_id' => $model->id]);
if (isset($data['tagNames']) && !empty($data['tagNames'])) {
$tags = explode(' ', $data['tagNames']);
foreach($tags as $tag) {
$tagModel = Tag::findOne(['name' => $tag]);
if (empty($tagModel)) {
$tagModel = new Tag();
$tagModel->name = $tag;
$tagModel->save();
}
$articleTag = new ArticleTag();
$articleTag->article_id = $model->id;
$articleTag->tag_id = $tagModel->id;
$articleTag->save();
}
Tag::updateAllCounters(['article' => 1], ['name' => $tags]);
}
}
}
Loading

0 comments on commit 3548324

Please sign in to comment.