<?php

function _block_inserate_my_account() {
  global $user;
  $user = user_load($user->uid);

  $ichbinhandwerker = false;
  if ($field_anbieter = field_get_items('user', $user, 'field_anbieter')) {
    if ($field_anbieter[0]['value']) {
      $ichbinhandwerker = true;
    }
  }

  $abo_status = _inserate_abo_status($uid = NULL);

  $view = views_get_view('meine_branchen');
  $view->set_arguments(array($user->uid));
  $view->execute();

  $branchenlinks = array();
  $branchenlinks[0] = l(t('Extended Profile'), 'profile-anbieterprofil/' . $user->uid . '/edit');
  $branchenlinks[1] = l('Erweiterte Benachrichtigungen', 'profile-benachrichtigungen_anfragen/' . $user->uid . '/edit');

  foreach ($view->result as $result) {
    $branche_link_classes = array();

    $branche = node_load($result->nid);
    $alias = drupal_get_path_alias('node/' . $result->nid, NULL);

    if ($field_hide_branche = field_get_items('node', $branche, 'field_profil_verbergen')) {
      if ($field_hide_branche[0]['value'] == 1) {
        $branche_link_classes[] = 'branche-hidden';
      }
    }

    $link_options = array('alias' => TRUE, 'html' => TRUE, 'attributes' => array());
    if (!empty($branche_link_classes)) {
      $link_options['attributes'] += array('class' => implode(' ', $branche_link_classes));
    }

    $link = l($result->node_title, $alias, $link_options);

    $branchenlinks[] = $link . ' (' . l(t('edit'), 'node/' . $result->nid . '/edit') . ')';
  }
  $branchenlinks[] = l(t('Create new profile'), 'node/add/branche');


  // Company profile
  if (!empty($abo_status) && $abo_status[0]->field_field_anzahl_tokens[0]['raw']['value'] == 999999999) {
    $company_profile = user_has_companyprofile($user->uid);
    if ($company_profile) {
      $company_links = array(
        l(t('Edit company profile'), 'node/' . $company_profile[0]->nid . '/edit'),
      );
    } else {
      $company_links = array(
        l(t('Add company profile'), 'node/add/company-profile'),
      );
    }
    $company_links[] = l(t('My relationships'), 'relationships');
  }

  // Employee's Relationship
  $view = views_get_view('company_profile_block');
  $view->set_display('block_2');
  $view->set_arguments(array($user->uid));
  $view->execute();
  if (count($view->result)) {
    $employee_links = array(
      l(t('Company contact'), 'firmen-relationships'),
    );
  }

  $contentlinks = array(
    l(t('Meine Inserate'), 'meine-inserate/aktuell'),
  );

  if ($ichbinhandwerker) {
    $contentlinks[] = l(t('Meine Anfragen'), 'meine-anfragen/aktiv');
  }

  $paymentlinks = array(
    l(t('Buy contact packages'), 'node/add/token-paket'),
    l(t('Contact packages overview'), 'meine-token/aktuell'),
    l(t('My orders'), 'user/' . $user->uid . '/orders'),

  );

  $sublinks1 = array(
    l(t('General Data'), 'user/' . $user->uid . '/edit'),
    l(t('Change Password'), 'user/' . $user->uid . '/change_password'),
    l(t('Benachrichtigungen'), 'profile-notifications/' . $user->uid . '/edit'),

  );

  $links = array(
    '0' => array(
      'data' => 'Kontoinformationen',
      'id' => 'item_1',
      'class' => array('menu my_account_menu'),
      'children' => $sublinks1
    ),
  );

  if ($ichbinhandwerker) {
    $links[1] = array(
      'data' => t('Anbieterprofil'),
      'id' => 'item_2',
      'class' => array('menu my_account_menu'),
      'children' => $branchenlinks
    );
  }
  if (!empty($abo_status) && $abo_status[0]->field_field_anzahl_tokens[0]['raw']['value'] == 999999999) {
    $links[2] = array(
      'data' => t('Firmenprofil'),
      'id' => 'item_3',
      'class' => array('menu my_account_menu'),
      'children' => $company_links
    );
  }

  if (isset($employee_links) && count($employee_links)) {
    $links[3] = array(
      'data' => t('Firmenkontakt'),
      'id' => 'item_4',
      'class' => array('menu my_account_menu'),
      'children' => $employee_links
    );
  }

  $links[4] = array(
    'data' => t('Inserate & Anfragen'),
    'id' => 'item_4',
    'class' => array('menu my_account_menu'),
    'children' => $contentlinks
  );

  $links[] = array(
    'data' => t('Contact packages and subscription'),
    'id' => 'item_5',
    'class' => array('menu my_account_menu'),
    'children' => $paymentlinks
  );

  return theme('item_list', array('items' => $links, 'attributes' => array('class' => 'menu my_account_menu')));

}