CategoryUtility.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Utility;
  3. use App\Models\Category;
  4. class CategoryUtility
  5. {
  6. /*when with trashed is true id will get even the deleted items*/
  7. public static function get_immediate_children($id, $with_trashed = false, $as_array = false)
  8. {
  9. $children = $with_trashed ? Category::where('parent_id', $id)->orderBy('order_level', 'desc')->get() : Category::where('parent_id', $id)->orderBy('order_level', 'desc')->get();
  10. $children = $as_array && !is_null($children) ? $children->toArray() : $children;
  11. return $children;
  12. }
  13. public static function get_immediate_children_ids($id, $with_trashed = false)
  14. {
  15. $children = CategoryUtility::get_immediate_children($id, $with_trashed, true);
  16. return !empty($children) ? array_column($children, 'id') : array();
  17. }
  18. public static function get_immediate_children_count($id, $with_trashed = false)
  19. {
  20. return $with_trashed ? Category::where('parent_id', $id)->count() : Category::where('parent_id', $id)->count();
  21. }
  22. /*when with trashed is true id will get even the deleted items*/
  23. public static function flat_children($id, $with_trashed = false, $container = array())
  24. {
  25. $children = CategoryUtility::get_immediate_children($id, $with_trashed, true);
  26. if (!empty($children)) {
  27. foreach ($children as $child) {
  28. $container[] = $child;
  29. $container = CategoryUtility::flat_children($child['id'], $with_trashed, $container);
  30. }
  31. }
  32. return $container;
  33. }
  34. /*when with trashed is true id will get even the deleted items*/
  35. public static function children_ids($id, $with_trashed = false)
  36. {
  37. $children = CategoryUtility::flat_children($id, $with_trashed = false);
  38. return !empty($children) ? array_column($children, 'id') : array();
  39. }
  40. public static function move_children_to_parent($id)
  41. {
  42. $children_ids = CategoryUtility::get_immediate_children_ids($id, true);
  43. $category = Category::where('id', $id)->first();
  44. CategoryUtility::move_level_up($id);
  45. Category::whereIn('id', $children_ids)->update(['parent_id' => $category->parent_id]);
  46. }
  47. public static function move_level_up($id){
  48. if (CategoryUtility::get_immediate_children_ids($id, true) > 0) {
  49. foreach (CategoryUtility::get_immediate_children_ids($id, true) as $value) {
  50. $category = Category::find($value);
  51. $category->level -= 1;
  52. $category->save();
  53. return CategoryUtility::move_level_up($value);
  54. }
  55. }
  56. }
  57. public static function move_level_down($id){
  58. if (CategoryUtility::get_immediate_children_ids($id, true) > 0) {
  59. foreach (CategoryUtility::get_immediate_children_ids($id, true) as $value) {
  60. $category = Category::find($value);
  61. $category->level += 1;
  62. $category->save();
  63. return CategoryUtility::move_level_down($value);
  64. }
  65. }
  66. }
  67. public static function delete_category($id)
  68. {
  69. $category = Category::where('id', $id)->first();
  70. if (!is_null($category)) {
  71. CategoryUtility::move_children_to_parent($category->id);
  72. $category->delete();
  73. }
  74. }
  75. }