CatalogModel.class.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Api\Model;
  3. use Api\Model\BaseModel;
  4. /**
  5. *
  6. * @author star7th
  7. */
  8. class CatalogModel extends BaseModel {
  9. //获取目录列表。如果isGroup参数为true,则按分组返回
  10. public function getList($item_id,$isGroup = false ){
  11. if ($item_id > 0 ) {
  12. $ret = $this->where(" item_id = '%d' ",array($item_id))->order(" s_number, cat_id asc ")->select();
  13. }
  14. if ($ret) {
  15. foreach ($ret as $key => $value) {
  16. $ret[$key]['addtime'] = date("Y-m-d H:i:s",$value['addtime']) ;
  17. }
  18. if ($isGroup) {
  19. $ret2 = array() ;
  20. foreach ($ret as $key => $value) {
  21. if ($value['parent_cat_id']) {
  22. //跳过
  23. //
  24. }else{
  25. $value['sub'] = $this->_getChlid($value['cat_id'],$ret);
  26. $ret2[] = $value ;
  27. }
  28. }
  29. $ret = $ret2 ;
  30. }
  31. return $ret ;
  32. }else{
  33. return array();
  34. }
  35. }
  36. //获取某个目录的子 (如果存在的话) 此private方法只给本类内调用
  37. private function _getChlid($cat_id,$item_data){
  38. $return = array() ;
  39. if ($item_data && $cat_id) {
  40. foreach ($item_data as $key => $value) {
  41. if ($value['parent_cat_id'] == $cat_id ) {
  42. $value['sub'] = $this->_getChlid($value['cat_id'],$item_data);
  43. $return[] = $value ;
  44. }
  45. }
  46. }
  47. return $return;
  48. }
  49. //获取某id下的子目录列表(此public方法暴露出去给其他地方调用)
  50. public function getChlid($item_id,$cat_id){
  51. $return = array() ;
  52. $ret = $this->getList($item_id , true) ;
  53. if ($ret) {
  54. foreach ($ret as $key => $value) {
  55. if ($value['cat_id'] == $cat_id) {
  56. $return = $value['sub'] ;
  57. }
  58. if ($value['sub']) {
  59. foreach ($value['sub'] as $key2 => $value2) {
  60. if ($value2['cat_id'] == $cat_id) {
  61. $return = $value2['sub'] ;
  62. }
  63. if ($value2['sub']) {
  64. foreach ($value2['sub'] as $key3 => $value3) {
  65. if ($value3['cat_id'] == $cat_id) {
  66. $return = $value3['sub'] ;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. return $return ;
  75. }
  76. //获取某个层级的目录列表。例如 获取二级目录列表
  77. public function getListByLevel($item_id , $level = 2){
  78. $return = array() ;
  79. $ret = $this->getList($item_id) ;
  80. if ($ret) {
  81. foreach ($ret as $key => $value) {
  82. if ($value['level'] == $level) {
  83. $return[] = $value ;
  84. }
  85. }
  86. }
  87. return $return ;
  88. }
  89. }