Skip to content

Commit 56969ad

Browse files
committed
tag
1 parent 601abf5 commit 56969ad

File tree

10 files changed

+136
-39
lines changed

10 files changed

+136
-39
lines changed

src/Redwood/Service/Note/Impl/JswidgetServiceImpl.php

Lines changed: 85 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,46 @@ public function createJswidget($jswidget)
1313
$jswidget['userId'] = $this->getCurrentUser()->id;
1414
$jswidget['createTime'] = time();
1515
$this->filterJswidgetFields($jswidget);
16-
return $this->getJswidgetDao()->addJswidget($jswidget);
16+
$newJswidget = $this->getJswidgetDao()->addJswidget(JswidgetSerialize::serialize($jswidget));
17+
18+
$newJswidget = $this->getJswidget($newJswidget['id']);
19+
return $newJswidget;
1720
}
1821

19-
public function updateJswidget($id, $jswidget)
22+
public function updateJswidget($id, $fields)
23+
{
24+
$jswidget = $this->getJswidgetDao()->getJswidget($id);
25+
if (empty($jswidget)) {
26+
throw $this->createServiceException('组件不存在,更新失败!');
27+
}
28+
29+
$this->filterJswidgetFields($fields);
30+
31+
$fields['updateTime'] = time();
32+
// 存储前过滤一下 tags
33+
$fields = JswidgetSerialize::serialize($fields);
34+
35+
return JswidgetSerialize::unserialize(
36+
$this->getJswidgetDao()->updateJswidget($id, $fields)
37+
);
38+
}
39+
40+
private function filterJswidgetFields(array &$fields)
2041
{
21-
$jswidget['updateTime'] = time();
22-
return $this->getJswidgetDao()->updateJswidget($id, $jswidget);
42+
// @todo
43+
// if (!empty($fields['content'])) {
44+
// $fields['about'] = $this->purifyHtml($fields['about']);
45+
// }
46+
47+
if (!empty($fields['tags'])) {
48+
$fields['tags'] = explode(',', $fields['tags']);
49+
$fields['tags'] = $this->getTagService()->getTagsByNames($fields['tags']);
50+
51+
array_walk($fields['tags'], function(&$item, $key) {
52+
$item = (int) $item['id'];
53+
});
54+
}
55+
return $fields;
2356
}
2457

2558
public function getJswidget($id)
@@ -33,18 +66,6 @@ public function getJswidget($id)
3366
}
3467
}
3568

36-
37-
private function filterJswidgetFields(&$fields)
38-
{
39-
40-
if (!empty($fields['tags'])) {
41-
$tempTags = explode(',', $fields['tags']);
42-
$fields['tags'] = implode("|", $tempTags);
43-
}
44-
45-
return $fields;
46-
}
47-
4869
private function filterJswidgetTags(&$fields)
4970
{
5071

@@ -56,9 +77,6 @@ private function filterJswidgetTags(&$fields)
5677
return $fields;
5778
}
5879

59-
60-
61-
6280
public function deleteJswidget($id){
6381
$jswidget = $this->getJswidget($id);
6482
if (empty($jswidget)) {
@@ -119,4 +137,51 @@ private function getJswidgetDao()
119137
return $this->createDao('Note.JswidgetDao');
120138
}
121139

122-
}
140+
protected function getTagService()
141+
{
142+
return $this->createService('Taxonomy.TagService');
143+
}
144+
145+
}
146+
147+
148+
149+
150+
class JswidgetSerialize
151+
{
152+
// 数组 -> 字符串
153+
public static function serialize(array &$jswidget)
154+
{
155+
if (isset($jswidget['tags'])) {
156+
if (is_array($jswidget['tags']) and !empty($jswidget['tags'])) {
157+
$jswidget['tags'] = '|' . implode('|', $jswidget['tags']) . '|';
158+
} else {
159+
$jswidget['tags'] = '';
160+
}
161+
}
162+
163+
return $jswidget;
164+
}
165+
166+
public static function unserialize(array $jswidget = null)
167+
{
168+
if (empty($jswidget)) {
169+
return $jswidget;
170+
}
171+
172+
if(empty($jswidget['tags'] )) {
173+
$jswidget['tags'] = array();
174+
} else {
175+
$jswidget['tags'] = explode('|', trim($jswidget['tags'], '|'));
176+
}
177+
178+
return $jswidget;
179+
}
180+
181+
public static function unserializes(array $jswidgets)
182+
{
183+
return array_map(function($jswidget) {
184+
return JswidgetSerialize::unserialize($jswidget);
185+
}, $jswidgets);
186+
}
187+
}

src/Redwood/Service/Note/JswidgetService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ interface JswidgetService
55
{
66
public function createJswidget($jswidget);
77

8-
public function updateJswidget($id, $jswidget);
8+
public function updateJswidget($id, $fields);
99

1010
public function deleteJswidget($id);
1111

src/Redwood/Service/Taxonomy/Dao/Impl/TagDaoImpl.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ public function getTagByLikeName($name)
2828
return $this->getConnection()->fetchAll($sql, array($name));
2929
}
3030

31+
public function getTagsByNames(array $names)
32+
{
33+
$conditions = array();
34+
if (empty($names)) {
35+
return array();
36+
}else{
37+
$conditions['names'] = $names;
38+
}
39+
40+
$builder = $this->createTagQueryBuilder($conditions)
41+
->select('*');
42+
43+
return $builder->execute()->fetchAll() ? : array();
44+
}
45+
3146
public function getTagsByIds(array $ids)
3247
{
3348
$conditions = array();
@@ -43,6 +58,8 @@ public function getTagsByIds(array $ids)
4358
return $builder->execute()->fetchAll() ? : array();
4459
}
4560

61+
62+
4663
public function addTag($tag)
4764
{
4865
$affected = $this->getConnection()->insert($this->table, $tag);
@@ -97,6 +114,7 @@ private function createTagQueryBuilder($conditions)
97114
->from($this->table, 'tag')
98115
->andWhere('name LIKE :name')
99116
->andWhere('id IN (:tagIds)')
117+
->andWhere('name IN (:names)')
100118
->andWhere('id = :id');
101119
}
102120

src/Redwood/Service/Taxonomy/Dao/TagDao.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public function getTagByLikeName($name);
1818

1919
public function getTagsByIds(array $ids);
2020

21+
public function getTagsByNames(array $names);
22+
2123
public function searchTagCount(array $conditions);
2224

2325
public function searchTags($conditions, $orderBy, $start, $limit);

src/Redwood/Service/Taxonomy/Impl/TagServiceImpl.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ public function getTagByLikeName($name)
7575
return $this->getTagDao()->getTagByLikeName($name);
7676
}
7777

78+
public function getTagsByNames(array $names)
79+
{
80+
return $this->getTagDao()->getTagsByNames($names);
81+
}
82+
7883

7984
public function getTagByName($name)
8085
{

src/Redwood/Service/Taxonomy/TagService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public function getTag($id);
1414

1515
public function getTagByName($name);
1616

17+
public function getTagsByNames(array $names);
18+
1719
public function getTagByLikeName($name);
1820

1921
public function getTagsByIds($ids);

src/Redwood/WebBundle/Controller/JswidgetController.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,25 @@ public function editAction(Request $request, $id)
4141
return $this->redirect($this->generateUrl('jswidget_show'));
4242
}
4343

44+
if(empty($jswidget)){
45+
return $this->createMessageResponse('info', "非常抱歉,组件id:{$id} 未找到, 15秒后将自动跳转到组件首页.",'', 15,$this->generateUrl('jswidget_show'));
46+
}
47+
4448
if ($request->getMethod() == 'POST') {
4549

46-
// try {
50+
try {
4751
$updateJswidget = $request->request->all();
4852

49-
var_dump($updateJswidget);exit;
5053
$jswidget = $this->getJswidgetService()->updateJswidget($jswidget['id'], $updateJswidget);
5154

5255
// 提交成功跳转到 组件页面
5356
return $this->redirect($this->generateUrl('jswidget_content', array(
5457
'id' => $jswidget['id']
5558
)));
5659

57-
// } catch (\Exception $e) {
58-
// return $this->createMessageResponse('error', '更新组件出错');
59-
// }
60+
} catch (\Exception $e) {
61+
return $this->createMessageResponse('error', '更新组件出错');
62+
}
6063

6164
}
6265

src/Redwood/WebBundle/Resources/public/js/controller/jswidget/create.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ define(function(require, exports, module) {
4646
$.each(data, function(index, item) {
4747

4848
results.push({
49-
id: item.id,
49+
id: item.name,
5050
name: item.name
5151
});
5252
});
@@ -59,10 +59,9 @@ define(function(require, exports, module) {
5959
},
6060
initSelection: function(element, callback) {
6161
var data = [];
62-
var idsArr = element.data('tagids').split('|');
63-
$(element.val().split(",")).each(function(s0, name) {
62+
$(element.val().split(",")).each(function (s0, name) {
6463
data.push({
65-
id: idsArr[s0],
64+
id: name,
6665
name: name
6766
});
6867
});
@@ -76,22 +75,21 @@ define(function(require, exports, module) {
7675
},
7776
width: 'off',
7877
multiple: true,
79-
maximumSelectionSize: 20,
8078
placeholder: "请输入标签",
8179
width: 'off',
8280
multiple: true,
8381
createSearchChoice: function() {
8482
return null;
8583
},
86-
maximumSelectionSize: 20
84+
maximumSelectionSize: 17
8785
});
8886

8987

9088

9189

9290
// tag 转为 后台可识别的字符
9391
// $('#jswidgetForm').submit(function(e) {
94-
92+
// debugger;
9593
// e.preventDefault();
9694
// return;
9795
// });

src/Redwood/WebBundle/Resources/views/Jswidget/create.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<div class="form-group">
6666
<label class="col-md-2 control-label">{{'标签'}}</label>
6767
<div class="col-md-7 controls">
68-
<input type="text" id="jswidget_tags" name="tags" class="width-full select2-offscreen form-control" data-tagids="{{ jswidget.tags }}" tabindex="-1" value="{{ tags }}">
68+
<input type="text" id="jswidget_tags" name="tags" class="width-full select2-offscreen form-control" tabindex="-1" value="{{ tags|default('') }}">
6969
<div class="help-block">选填,管理员后台统一管理标签</div>
7070
</div>
7171
</div>

src/Redwood/WebBundle/Twig/Extension/WebExtension.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php
22
namespace Redwood\WebBundle\Twig\Extension;
33

4+
use Redwood\Service\Common\ServiceKernel;
5+
6+
use Redwood\Common\ArrayToolkit;
7+
48
class WebExtension extends \Twig_Extension
59
{
610
protected $container;
@@ -13,8 +17,8 @@ public function __construct ($container)
1317
public function getFilters ()
1418
{
1519
return array(
16-
'smart_time' => new \Twig_Filter_Method($this, 'smarttimeFilter') ,
17-
'tags_join' => new \Twig_SimpleFilter($this, 'tagsJoinFilter')
20+
'smart_time' => new \Twig_Filter_Method($this, 'smarttimeFilter'),
21+
'tags_join' => new \Twig_Filter_Method($this, 'tagsJoinFilter')
1822
);
1923
}
2024

@@ -50,13 +54,13 @@ public function sqlUriConvertWebUri($sqlUri)
5054
}
5155
}
5256

57+
// @todo bug
5358
public function tagsJoinFilter($tagIds)
54-
{
59+
{
5560
if (empty($tagIds) || !is_array($tagIds)) {
5661
return '';
5762
}
58-
59-
$tags = ServiceKernel::instance()->createService('Taxonomy.TagService')->findTagsByIds($tagIds);
63+
$tags = ServiceKernel::instance()->createService('Taxonomy.TagService')->getTagsByIds($tagIds);
6064
$names = ArrayToolkit::column($tags, 'name');
6165

6266
return join($names, ',');

0 commit comments

Comments
 (0)