You are here

function tb_megamenu_sync_config in The Better Mega Menu 7

1 call to tb_megamenu_sync_config()
tb_megamenu_sync_config_all in ./tb_megamenu.functions.inc

File

./tb_megamenu.functions.inc, line 268

Code

function tb_megamenu_sync_config($items, &$item_config, $_mlid, $section) {
  if (empty($item_config['rows_content'])) {
    $item_config['rows_content'] = array(
      0 => array(
        0 => array(
          'col_content' => array(),
          'col_config' => array(),
        ),
      ),
    );
    foreach ($items as $item) {
      $mlid = $item['link']['mlid'];
      if (!$item['link']['hidden']) {
        $item_config['rows_content'][0][0]['col_content'][] = array(
          'type' => 'menu_item',
          'mlid' => $mlid,
          'tb_item_config' => array(),
          'weight' => $item['link']['weight'],
        );
      }
    }
    if (empty($item_config['rows_content'][0][0]['col_content'])) {
      unset($item_config['rows_content'][0]);
    }
  }
  else {
    $hash = array();
    foreach ($item_config['rows_content'] as $i => $row) {
      foreach ($row as $j => $col) {
        foreach ($col['col_content'] as $k => $tb_item) {
          if ($tb_item['type'] == 'menu_item') {
            $hash[$tb_item['mlid']] = array(
              'row' => $i,
              'col' => $j,
            );
            $existed = false;
            foreach ($items as $item) {
              if (!$item['link']['hidden'] && $tb_item['mlid'] == $item['link']['mlid']) {
                $item_config['rows_content'][$i][$j]['col_content'][$k]['weight'] = $item['link']['weight'];
                $existed = true;
                break;
              }
            }
            if (!$existed) {
              unset($item_config['rows_content'][$i][$j]['col_content'][$k]);
              if (empty($item_config['rows_content'][$i][$j]['col_content'])) {
                unset($item_config['rows_content'][$i][$j]);
              }
              if (empty($item_config['rows_content'][$i])) {
                unset($item_config['rows_content'][$i]);
              }
            }
          }
          else {
            if (!tb_megamenu_block_content_exists($tb_item['block_key'], $section)) {
              unset($item_config['rows_content'][$i][$j]['col_content'][$k]);
              if (empty($item_config['rows_content'][$i][$j]['col_content'])) {
                unset($item_config['rows_content'][$i][$j]);
              }
              if (empty($item_config['rows_content'][$i])) {
                unset($item_config['rows_content'][$i]);
              }
            }
          }
        }
      }
    }
    $row = -1;
    $col = -1;
    foreach ($items as $item) {
      $mlid = $item['link']['mlid'];
      if (!$item['link']['hidden']) {
        if (isset($hash[$mlid])) {
          $row = $hash[$mlid]['row'];
          $col = $hash[$mlid]['col'];
          continue;
        }
        if ($row > -1) {
          tb_megamenu_insert_tb_item($item_config, $row, $col, $item);
        }
        else {
          $row = 0;
          $col = 0;
          while (isset($item_config['rows_content'][$row][$col]['col_content']) && isset($item_config['rows_content'][$row][$col]['col_content'][0]) && $item_config['rows_content'][$row][$col]['col_content'][0]['type'] == 'block') {
            $row++;
          }
          tb_megamenu_insert_tb_item($item_config, $row, $col, $item);
          if (!isset($item_config['rows_content'][$row][$col]['col_config'])) {
            $item_config['rows_content'][$row][$col]['col_config'] = array();
          }
        }
      }
    }
  }
}