Category.class.php 10 KB

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