<?php
/**
 * @file
 * Code for the Commons Site Homepage feature.
 */

include_once 'commons_site_homepage.features.inc';

/**
* Implements hook_form_alter().
*/
function commons_site_homepage_form_alter(&$form, &$form_state, $form_id) {
  // Redirect users to the site homepage after login or registration.
  if ($form_id == 'user_register_form' || $form_id == 'user_login') {
    $form['#submit'][] = 'commons_homepage_user_register_login_submit';
  }
  if ($form_id == 'views_exposed_form' && $form['#id'] == 'views-exposed-form-commons-homepage-content-panel-pane-1') {
    // We customize the text inside the exposed filter form elements
    // to allow for them to form a sentence, such as:
    // "Show Groups I'm following sorted by last updated first"
    $form['following']['#options'][1] = t('following');
    $form['following']['#options'][0] = t('not following');
    // Users should never be able to show groups in this listing.
    unset($form['type']['#options']['group']);
    $form['type']['#options']['All'] = t('all content');
    $form['sort_by']['#title'] = t('sorted by');
    foreach ($form['type']['#options'] as $type => $name) {
      if ($type != 'All') {
        $form['type']['#options'][$type] = t(strtolower(substr($name, 0, 1)) . substr($name,1) . 's');
      }
    }
  }
}

function commons_homepage_user_register_login_submit($form, &$form_state) {
  $form_state['redirect'] = variable_get('site_frontpage', 'home');
}

/**
 * Implements hook_block_info().
 */
function commons_site_homepage_block_info() {
  $blocks['commons_site_homepage'] = array(
    'info' => t('Commons anonymous homepage sign up/log in buttons'),
    'cache' => DRUPAL_NO_CACHE,
  );

  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function commons_site_homepage_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'commons_site_homepage':
      // The block does not have a default title.
      $block['subject'] = NULL;

      $links = array();

      // Display a 'sign up' link if visitors can create accounts.
      if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)) {
        $links[] = array(
          'title' => t('Sign up'),
          'href' => 'user/register',
          'attributes' => array(
            'class' => array('commons-sign-up'),
          ),
        );
      }

      // Always display a 'log in' link.
      $links[] = array(
        'title' => t('Log in'),
        'href' => 'user/login',
        'attributes' => array(
          'class' => array('commons-login'),
        ),
      );

      $block['content'] = array(
        '#theme' => 'links',
        '#links' => $links,
        '#attributes' => array(
          'class' => array('links', 'inline'),
        ),
      );
      break;
  }

  return $block;
}

/**
 * Implements hook_default_page_manager_pages_alter().
 */
function commons_site_homepage_default_page_manager_pages_alter(&$items) {
  if (isset($items['commons_home'])) {
    $items['commons_home']->default_handlers['page_commons_home_panel_context_2']->conf['display']->content['new-2ab570f0-8498-44f4-e9d0-9977e3d8d4df']->configuration['title'] = variable_get('commons_anonymous_welcome_title', t('The open-source alternative for enterprise-scale business software'));
    $items['commons_home']->default_handlers['page_commons_home_panel_context_2']->conf['display']->content['new-2ab570f0-8498-44f4-e9d0-9977e3d8d4df']->configuration['body'] = variable_get('commons_anonymous_welcome_body', t('Enable social networking to create productive interactions. Whether your community is public or private, Commons does it!'));

    $pane = new stdClass();
    $pane->pid = 'new-94106ddd-1278-0954-c5ba-b59ff1b9bf49';
    $pane->panel = 'three_33_first';
    $pane->type = 'views';
    $pane->subtype = 'commons_events_upcoming';
    $pane->shown = TRUE;
    $pane->access = array();
    $pane->configuration = array(
      'override_pager_settings' => 0,
      'use_pager' => 0,
      'nodes_per_page' => '5',
      'pager_id' => '0',
      'offset' => '0',
      'more_link' => 0,
      'feed_icons' => 0,
      'panel_args' => 0,
      'link_to_view' => 0,
      'args' => '',
      'url' => '',
      'display' => 'commons_events_upcoming_global_block',
      'override_title' => 0,
      'override_title_text' => '',
    );
    $pane->cache = array();
    $pane->style = array(
      'settings' => NULL,
    );
    $pane->css = array();
    $pane->extras = array();
    $pane->position = 1;
    $pane->locks = array();
    $pane->uuid = '94106ddd-1278-0954-c5ba-b59ff1b9bf49';
    $items['commons_home']->default_handlers['page_commons_home_panel_context_2']->conf['display']->content['new-94106ddd-1278-0954-c5ba-b59ff1b9bf49'] = $pane;
    $items['commons_home']->default_handlers['page_commons_home_panel_context_2']->conf['display']->panels['three_33_first'][1] = 'new-94106ddd-1278-0954-c5ba-b59ff1b9bf49';
  }
}
