Category.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace com;
  3. /**
  4. * 分类管理
  5. */
  6. class Category {
  7. private $model; //分类的数据表模型
  8. private $rawList = array(); //原始的分类数据
  9. private $formatList = array(); //格式化后的分类
  10. private $error = ""; //错误信息
  11. private $icon = array('', '', ''); //格式化的字符
  12. private $fields = array(); //字段映射,分类id,上级分类fid,分类名称name,格式化后分类名称fullname
  13. /**
  14. * 构造函数,对象初始化
  15. * @param array,object $model 数组或对象,基于TP5.0的数据表模型名称,若不采用TP,可传递空值。
  16. * @param array $field 字段映射,分类cid,上级分类fid,分类名称,格式化后分类名称fullname
  17. */
  18. public function __construct($model = '', $fields = array()) {
  19. if (is_string($model) && (!empty($model))) {
  20. if (!$this->model = db($model))
  21. $this->error = $model . "模型不存在!";
  22. }
  23. if (is_object($model))
  24. $this->model = &$model;
  25. $this->fields['cid'] = $fields['0'] ? $fields['0'] : 'cid';
  26. $this->fields['fid'] = $fields['1'] ? $fields['1'] : 'fid';
  27. $this->fields['name'] = $fields['2'] ? $fields['2'] : 'name';
  28. $this->fields['fullname'] = $fields['3'] ? $fields['3'] : 'fullname';
  29. }
  30. /**
  31. * 获取分类信息数据
  32. * @param array,string $condition 查询条件
  33. * @param string $orderby 排序
  34. */
  35. private function _findAllCat($condition, $orderby = NULL) {
  36. $this->rawList = empty($orderby) ? $this->model->where($condition)->select() : $this->model->where($condition)->order($orderby)->select();
  37. }
  38. /**
  39. * 返回给定上级分类$fid的所有同一级子分类
  40. * @param int $fid 传入要查询的fid
  41. * @return array 返回结构信息
  42. */
  43. public function getChild($fid) {
  44. $childs = array();
  45. foreach ($this->rawList as $Category) {
  46. if ($Category[$this->fields['fid']] == $fid)
  47. $childs[] = $Category;
  48. }
  49. return $childs;
  50. }
  51. /**
  52. * 递归格式化分类前的字符
  53. * @param int $cid 分类cid
  54. * @param string $space
  55. */
  56. private function _searchList($cid = 0, $space = "",$level=1,$p_name='') {
  57. $childs = $this->getChild($cid);
  58. //下级分类的数组
  59. //如果没下级分类,结束递归
  60. if (!($n = count($childs)))
  61. return;
  62. $m = 1;
  63. //循环所有的下级分类
  64. for ($i = 0; $i < $n; $i++) {
  65. $pad = '';
  66. $childs[$i]['level'] = $level;
  67. $childs[$i]['label'] = $childs[$i]['name'];
  68. $childs[$i]['structure_id'] = $childs[$i]['id'];
  69. $this->formatList[] = $childs[$i];
  70. $this->_searchList($childs[$i][$this->fields['cid']], $space . $pad . " ",$level+1,$childs[$i]['else']);//递归下一级分类
  71. $m++;
  72. }
  73. }
  74. /**
  75. * 不采用数据模型时,可以从外部传递数据,得到递归格式化分类
  76. * @param array,string $condition 条件
  77. * @param int $cid 起始分类
  78. * @param string $orderby 排序
  79. * @return array 返回结构信息
  80. */
  81. public function getList($condition = NULL, $cid = 0, $orderby = NULL) {
  82. unset($this->rawList, $this->formatList);
  83. $this->_findAllCat($condition, $orderby, $orderby);
  84. $this->_searchList($cid);
  85. return $this->formatList? $this->formatList: [];
  86. }
  87. /**
  88. * 获取结构
  89. * @param array $data 二维数组数据
  90. * @param int $cid 起始分类
  91. * @return array 递归格式化分类数组
  92. */
  93. public function getTree($data, $cid = 0) {
  94. unset($this->rawList, $this->formatList);
  95. $this->rawList = $data;
  96. $this->_searchList($cid);
  97. return $this->formatList;
  98. }
  99. /**
  100. * 获取错误信息
  101. * @return string 错误信息字符串
  102. */
  103. public function getError() {
  104. return $this->error;
  105. }
  106. /**
  107. * 检查分类参数$cid,是否为空
  108. * @param int $cid 起始分类
  109. * @return boolean 递归格式化分类数组
  110. */
  111. private function _checkCatID($cid) {
  112. if (intval($cid)) {
  113. return true;
  114. } else {
  115. $this->error = "参数分类ID为空或者无效!";
  116. return false;
  117. }
  118. }
  119. /**
  120. * 检查分类参数$cid,是否为空
  121. * @param int $cid 分类cid
  122. */
  123. private function _searchPath($cid) {
  124. //检查参数
  125. if (!$this->_checkCatID($cid))
  126. return false;
  127. $rs = $this->model->find($cid); //初始化对象,查找上级Id;
  128. $this->formatList[] = $rs; //保存结果
  129. $this->_searchPath($rs[$this->fields['fid']]);
  130. }
  131. /**
  132. * 查询给定分类cid的路径
  133. * @param int $cid 分类cid
  134. * @return array 数组
  135. */
  136. public function getPath($cid) {
  137. unset($this->rawList, $this->formatList);
  138. $this->_searchPath($cid); //查询分类路径
  139. return array_reverse($this->formatList);
  140. }
  141. /**
  142. * 添加分类
  143. * @param array $data 一维数组,要添加的数据,$data需要包含上级分类ID。
  144. * @return boolean 添加成功,返回相应的分类ID,添加失败,返回FALSE;
  145. */
  146. public function add($data) {
  147. if (empty($data))
  148. return false;
  149. return $this->model->data($data)->add();
  150. }
  151. /**
  152. * 修改分类
  153. * @param array $data 一维数组,$data需要包含要修改的分类cid。
  154. * @return boolean 组修改成功,返回相应的分类ID,修改失败,返回FALSE;
  155. */
  156. public function edit($data) {
  157. if (empty($data))
  158. return false;
  159. return $this->model->data($data)->save();
  160. }
  161. /**
  162. * 删除分类
  163. * @param int $cid 分类cid
  164. * @return boolean 删除成功,返回相应的分类ID,删除失败,返回FALSE
  165. */
  166. public function del($cid) {
  167. $cid = intval($cid);
  168. if (empty($cid))
  169. return false;
  170. $conditon[$this->fields['cid']] = $cid;
  171. return $this->model->where($conditon)->delete();
  172. }
  173. }
  174. ?>