Topic Text   Topic Comments (0)   Topic Properties   Topic Information leiwei8509@126...
Topic title: test111 Tuesday August 4, 2015 13:10:36

Download topic text | View in monospace font | Tab width set to 8 (change to 4)

Files in topic:  
[Jump to] CategoryService.php   {+153,-0}

[Add General Comment] to topic.

File CategoryService.php (Revision 1.0) [Add File Comment] [Top]
 
1 <?php
2 /**
3 * ?????
4 * @author lei wei <leiwei8509@126.com> 2015-1-31
5 * @link www.firefly.com
6 * @copyright Copyright &copy; 2003-2015 www.firefly.com
7 * @license
8 */
9
10 class CategoryService extends FireService{
11
12 const APP_NEWS = 'news'; //????
13 const APP_BOOK = 'book'; //????
14 const APP_PICTURE = 'picture'; //????
15
16 private static $categoryModel;
17
18 public function __construct(){
19 if(self::$categoryModel == null)
20 return self::$categoryModel = new CategoryModel();
21 }
22
23 /**
24 * ??????
25 * @param array $category ????
26 * @return int
27 */
28 public function saveCategory(array $category){
29 if(empty($category))
30 return $this->setError(Yii::t('common','Parameter is empty'),false);
31 if(isset($category['cat_id'])){
32 $orgCategoryModel = self::$categoryModel->findByPk($category['id']);
33 if(!$orgCategoryModel)
34 return $this->setNotice(Yii::t('common','Data not exists'),false);
35 $this->attachAttribute($orgCategoryModel,$category);
36 if(!$orgCategoryModel->validate()){
37 return $this->setNotices($orgCategoryModel->getErrors(),false);
38 }
39 $orgCategoryModel->save();
40 $insertId = $orgCategoryModel->getPrimaryKey();
41 }else{
42 $this->attachAttribute(self::$categoryModel,$category);
43 if(!self::$categoryModel->validate()){
44 return $this->setNotices(self::$categoryModel->getErrors(),false);
45 }
46 $insertId = self::$categoryModel->save();
47 }
48 return $insertId;
49 }
50
51 /**
52 * @param int|array $catIds
53 * @return array
54 */
55 public function getCategoryByCatIds($catIds){
56 if(empty($catIds))
57 return $this->setError(Yii::t('common','Parameter is empty'),array());
58 $catIds = is_array($catIds) ? $ids : array($catIds);
59 $data = self::$categoryModel->getCategoryByCatIds($catIds);
60 return $data ? $this->arToArray($data) : array();
61 }
62
63 public function getCategoryByCondition(array $condition=array()){
64 $data = self::$categoryModel->getCategoryByCondition($condition);
65 return $data ? $this->arToArray($data) : array();
66 }
67
68
69
70 /**
71 * ????????
72 * @return array
73 */
74 public function getCategoryTree(){
75 $category =$this->getCategoryByCondition();
76 return $this->getItemsArray($category);
77 }
78
79
80 /**
81 * ????????
82 * @param string $code ????
83 * @return string|array
84 */
85 public function getAppName($code = ''){
86 $apps=array(
87 self::APP_NEWS => '??',
88 self::APP_BOOK => '??',
89 self::APP_PICTURE => '??',
90 );
91 return isset($apps[$code]) ? $apps[$code] : $apps;
92 }
93
94 private function getItemsArray(&$data, $pid = 0 ,$level=0){
95 $arrcat = array();
96 $level++;
97 foreach($data as $k => $row){
98 if($row['fid'] == $pid){
99 $row['level']=$level;
100 $row['children'] = $this->getItemsArray($data, $row['cat_id'],$level);
101 $arrcat[] = $row;
102 }
103 }
104 return $arrcat;
105 }
106
107 public function getItemsTree($data, $pid = 0){
108 //???
109 static $tree = array();
110 if(!empty($data)){
111 foreach ($data as $v){
112 $tree[$v['cat_id']] =str_repeat('|-',1).str_repeat('-', ($v['level']-1)) . $v['name'];
113 if(!empty($v['children'])){
114 $this->getItemsTree($v['children']);
115 }
116
117 }
118 }
119 return $tree;
120 }
121
122 private function getJsonData($jsonData) {
123 $output = json_decode($jsonData,true);
124 $list = array();
125 foreach ($output as $row) {
126 $list[] = array(
127 'id' => $row['id'],
128 'parent_id' => 0,
129 );
130 if (isset($row['children'])) {
131 $this->getChildren($list, $row['id'], $row['children']);
132 }
133 }
134
135 return $list;
136 }
137
138
139 private function getChildren(&$list, $id, $child) {
140 foreach ($child as $c) {
141 $list[] = array(
142 'id' => $c['id'],
143 'parent_id' => $id,
144 );
145 if (isset($c['children']) ) {
146 $this->getChildren($list, $c['id'], $c['children']);
147 }
148 }
149 return ;
150 }
151
152
153 }
 
  
Legend:
Removed 
Changed
 Added

[Add General Comment] to topic.