<?php

/**
 * Implements hook_init().
 */
function mkbh_account_page_init()
{
    module_load_include('inc', 'mkbh_account_page', 'mkbh_account_page.forms');
    module_load_include('inc', 'mkbh_account_page', 'mkbh_account_page.helpers');

    drupal_add_css(drupal_get_path('module', 'mkbh_account_page') . '/assets/less/mkbh_account_page.less');
}

/**
 * Implements hook_block_info().
 */
function mkbh_account_page_block_info()
{
    $blocks['account_form_account'] = [
        'info' => t('Account Page: Preferences'),
        'cache' => DRUPAL_NO_CACHE,
    ];

    $blocks['account_form_newsletter'] = [
        'info' => t('Account Page: Newsletter'),
        'cache' => DRUPAL_NO_CACHE,
    ];

    $blocks['account_form_membership'] = [
        'info' => t('Account Page: Membership'),
        'cache' => DRUPAL_NO_CACHE,
    ];

    $blocks['account_form_business_info'] = [
        'info' => t('Account Page: Business Info'),
        'cache' => DRUPAL_NO_CACHE,
    ];

    return $blocks;
}

/**
 * Implements hook_block_view().
 *
 * @param string $delta
 *
 * @return array
 */
function mkbh_account_page_block_view($delta = '')
{
    $block = [];

    switch ($delta) {
        case 'account_form_account':
            $block = [
                'subject' => 'konto og sikkerhed',
                'content' => drupal_get_form('mkbh_account_page_form_account'),
            ];

            break;

        case 'account_form_newsletter':
            $block = [
                'subject' => 'nyhedsbrev præferencer',
                'content' => drupal_get_form('mkbh_account_page_form_newsletter'),
            ];

            break;

        case 'account_form_membership':
            $block = [
                'subject' => '<span style="white-space: nowrap">abonnement /</span> medlemskab',
                'content' => drupal_get_form('mkbh_account_page_form_membership'),
            ];

            break;

        case 'account_form_business_info':
            $block = [
                'subject' => 'virksomhed',
                'content' => drupal_get_form('mkbh_donate_page_business_info_form'),
            ];

            break;
    }

    return $block;
}

function mkbh_account_page_preprocess_page(&$vars)
{
    if (drupal_match_path(current_path(), 'account*')) {
        $vars['show_messages'] = FALSE;
    }
}

/**
 * Implements hook_form_alter().
 */
function mkbh_account_page_form_user_profile_form_alter(&$form, &$form_state)
{
    global $user;

    if (array_search('administrator', $user->roles) === FALSE) {
        drupal_goto('user');
    }
}

/**
 * Implements hook_form_alter().
 */
function mkbh_account_page_form_alter(&$form, &$form_state) {
  // Removing the little info modals for mobile.
  if ($form['#form_id'] === 'user_register_form') {
    unset($form['account']['pass']['#description']);
  }

  if ($form['#form_id'] === 'user_login') {
    $mobile_detect = module_enable(['mobile_detect']);

    if ($mobile_detect) {
      $detect = mobile_detect_get_object();

      if ($detect->isMobile()) {
        unset($form['name']['#description']);
        unset($form['pass']['#description']);
      }
    }
  }
}
