You are here

function tb_megamenu_update_megamenus in The Better Mega Menu 7

Same name and namespace in other branches
  1. 8 tb_megamenu.module \tb_megamenu_update_megamenus()
  2. 2.x tb_megamenu.module \tb_megamenu_update_megamenus()

Update items in tb_megamenus table.

1 string reference to 'tb_megamenu_update_megamenus'
tb_megamenu_form_menu_overview_form_alter in ./tb_megamenu.module
Implements hook_form_FORM_ID_alter().

File

./tb_megamenu.functions.inc, line 666

Code

function tb_megamenu_update_megamenus(&$form, &$form_state) {

  // Capture all values in $form_state representing a menu item.
  $mlid_values = array_filter($form_state['values'], function ($key) {
    return strpos($key, 'mlid:') !== FALSE;
  }, ARRAY_FILTER_USE_KEY);

  // Extract the mlid for each item from the keys of the array of menu items.
  $mlid_keys = array_keys($mlid_values);
  $valid_mlids = [];

  // Build an array of current/valid mlids.
  foreach ($mlid_keys as $key => $value) {
    $valid_mlids[] = substr($value, 5);
  }
  if (isset($form_state['build_info']['args'][0]['menu_name'])) {
    $menu_name = $form_state['build_info']['args'][0]['menu_name'];
    if ($menu_config = tb_megamenu_get_menu_config($menu_name)) {

      // Confirm the previously saved config doesn't contain any items that no
      // longer exist by comparing the mlids in config to the mlids of the
      // submitted form values.
      $invalid_mlids = array_diff(array_keys($menu_config), $valid_mlids);

      // Remove any invalid menu items from the config object.
      if (!empty($invalid_mlids)) {
        foreach ($invalid_mlids as $invalid_mlid) {
          unset($menu_config[$invalid_mlid]);
        }
      }
      $items = tb_megamenu_get_tree($menu_name);
      if (function_exists('i18n_menu_localize_tree')) {
        $items = i18n_menu_localize_tree($items);
      }
      $block_config = tb_megamenu_get_block_config($menu_name);
      tb_megamenu_sync_config_all($items, $menu_config, 'backend');
      tb_megamenu_sync_order_menus($menu_config);
      global $language;
      $tb_megamenu = db_select('tb_megamenus', 't')
        ->fields('t')
        ->condition('menu_name', $menu_name)
        ->condition('language', $language->language)
        ->execute()
        ->fetchObject();
      if ($tb_megamenu) {
        db_update('tb_megamenus')
          ->fields(array(
          'menu_config' => json_encode($menu_config),
          'block_config' => json_encode($block_config),
          'language' => $language->language,
        ))
          ->condition('menu_name', $menu_name)
          ->condition('language', $language->language)
          ->execute();
      }
      else {
        db_insert('tb_megamenus')
          ->fields(array(
          'menu_name' => $menu_name,
          'block_config' => json_encode($block_config),
          'menu_config' => json_encode($menu_config),
          'language' => $language->language,
        ))
          ->execute();
      }
    }
  }
}