@@ -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+ }
0 commit comments