Category.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $this->formatList[] = $childs[$i];
  69. $this->_searchList($childs[$i][$this->fields['cid']], $space . $pad . " ",$level+1,$childs[$i]['else']);//递归下一级分类
  70. $m++;
  71. }
  72. }
  73. /**
  74. * 不采用数据模型时,可以从外部传递数据,得到递归格式化分类
  75. * @param array,string $condition 条件
  76. * @param int $cid 起始分类
  77. * @param string $orderby 排序
  78. * @return array 返回结构信息
  79. */
  80. public function getList($condition = NULL, $cid = 0, $orderby = NULL) {
  81. unset($this->rawList, $this->formatList);
  82. $this->_findAllCat($condition, $orderby, $orderby);
  83. $this->_searchList($cid);
  84. return $this->formatList? $this->formatList: [];
  85. }
  86. /**
  87. * 获取结构
  88. * @param array $data 二维数组数据
  89. * @param int $cid 起始分类
  90. * @return array 递归格式化分类数组
  91. */
  92. public function getTree($data, $cid = 0) {
  93. unset($this->rawList, $this->formatList);
  94. $this->rawList = $data;
  95. $this->_searchList($cid);
  96. return $this->formatList;
  97. }
  98. /**
  99. * 获取错误信息
  100. * @return string 错误信息字符串
  101. */
  102. public function getError() {
  103. return $this->error;
  104. }
  105. /**
  106. * 检查分类参数$cid,是否为空
  107. * @param int $cid 起始分类
  108. * @return boolean 递归格式化分类数组
  109. */
  110. private function _checkCatID($cid) {
  111. if (intval($cid)) {
  112. return true;
  113. } else {
  114. $this->error = "参数分类ID为空或者无效!";
  115. return false;
  116. }
  117. }
  118. /**
  119. * 检查分类参数$cid,是否为空
  120. * @param int $cid 分类cid
  121. */
  122. private function _searchPath($cid) {
  123. //检查参数
  124. if (!$this->_checkCatID($cid))
  125. return false;
  126. $rs = $this->model->find($cid); //初始化对象,查找上级Id;
  127. $this->formatList[] = $rs; //保存结果
  128. $this->_searchPath($rs[$this->fields['fid']]);
  129. }
  130. /**
  131. * 查询给定分类cid的路径
  132. * @param int $cid 分类cid
  133. * @return array 数组
  134. */
  135. public function getPath($cid) {
  136. unset($this->rawList, $this->formatList);
  137. $this->_searchPath($cid); //查询分类路径
  138. return array_reverse($this->formatList);
  139. }
  140. /**
  141. * 添加分类
  142. * @param array $data 一维数组,要添加的数据,$data需要包含上级分类ID。
  143. * @return boolean 添加成功,返回相应的分类ID,添加失败,返回FALSE;
  144. */
  145. public function add($data) {
  146. if (empty($data))
  147. return false;
  148. return $this->model->data($data)->add();
  149. }
  150. /**
  151. * 修改分类
  152. * @param array $data 一维数组,$data需要包含要修改的分类cid。
  153. * @return boolean 组修改成功,返回相应的分类ID,修改失败,返回FALSE;
  154. */
  155. public function edit($data) {
  156. if (empty($data))
  157. return false;
  158. return $this->model->data($data)->save();
  159. }
  160. /**
  161. * 删除分类
  162. * @param int $cid 分类cid
  163. * @return boolean 删除成功,返回相应的分类ID,删除失败,返回FALSE
  164. */
  165. public function del($cid) {
  166. $cid = intval($cid);
  167. if (empty($cid))
  168. return false;
  169. $conditon[$this->fields['cid']] = $cid;
  170. return $this->model->where($conditon)->delete();
  171. }
  172. }
  173. ?>