Guest User

Untitled

a guest
Mar 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. <?php
  2. /**
  3. * Clean the menu table from deleted groups
  4. */
  5. private function cleanMenuFromDeletedGroup()
  6. {
  7. /**
  8. * Find menus which have custom(groups) visible_by level
  9. */
  10. $customMenus = Yii::app()->db->createCommand()
  11. ->select('*')
  12. ->from('menu')
  13. ->where('JSON_CONTAINS(visible_by, CAST(false as JSON), "$.all")')
  14. ->query();
  15. /**
  16. * If we have such custom menus loop through them
  17. * and search for deleted group ($this->idst)
  18. */
  19. if ( $customMenus && is_array($customMenus)){
  20. // Hold groups ids here answer will be [menuId => groupId]
  21. $groupsToDelete = array();
  22. foreach ( $customMenus as $menu){
  23. $groups = json_decode($menu['visible_by'])->groups;
  24. $groupIndex = array_search($this->idst, $groups);
  25. if ( $groupIndex !== false){
  26. $groupsToDelete[$menu['id']] = $groupIndex;
  27. }
  28. }
  29. }
  30. // If we have such groups delete just the id from menu table
  31. if ( !empty($groupsToDelete)){
  32. foreach ( $groupsToDelete as $id => $index){
  33. /**
  34. * $id - menuId
  35. * $index - position in the JSON field "visible_by->groups"
  36. */
  37. Yii::app()->db
  38. ->createCommand('UPDATE menu SET visible_by = JSON_REMOVE(visible_by, "$.groups['.$index.']") WHERE menu.id = '.$id)->query();
  39. }
  40. }
  41. }
Add Comment
Please, Sign In to add comment