You are here

commerce_extra_panes_termsofservice.module in Commerce extra panes 7

Terms of service plugin for Extra panes in checkout phase.

File

modules/commerce_extra_panes_termsofservice/commerce_extra_panes_termsofservice.module
View source
<?php

/**
 * @file
 * Terms of service plugin for Extra panes in checkout phase.
 */

/**
 * Implements hook_form_FORM_ID_alter().
 */
function commerce_extra_panes_termsofservice_form_commerce_checkout_pane_settings_form_alter(&$form, &$form_state, $form_id) {
  if ($form['checkout_pane']['#value']['base'] == 'commerce_extra_panes_contents') {
    $pane_id = $form['checkout_pane']['#value']['pane_id'];
    $form['termsofservice'] = array(
      '#type' => 'fieldset',
      '#title' => t('Extra panes Terms of Service'),
    );
    $form['termsofservice']['pane-tos'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable this pane as Terms of Service.'),
      '#default_value' => variable_get('cep_tos_' . $pane_id, 0),
    );
    $form['termsofservice']['pane-tos-required'] = array(
      '#type' => 'checkbox',
      '#title' => t('Make the terms of service required.'),
      '#default_value' => variable_get('cep_tos_required_' . $pane_id, 0),
      '#states' => array(
        'visible' => array(
          ':input[name=pane-tos]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['termsofservice']['pane-tos-as-link'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display Terms of service as link.'),
      '#default_value' => variable_get('cep_tos_as_link_' . $pane_id, 0),
      '#states' => array(
        'visible' => array(
          ':input[name=pane-tos]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['termsofservice']['pane-tos-position'] = array(
      '#type' => 'radios',
      '#title' => t('Position of checkbox'),
      '#default_value' => variable_get('cep_tos_position_' . $pane_id, 'below'),
      '#options' => array(
        'above' => t('Above'),
        'below' => t('Below'),
      ),
      '#states' => array(
        'visible' => array(
          ':input[name=pane-tos]' => array(
            'checked' => TRUE,
          ),
          ':input[name=pane-tos-as-link]' => array(
            'checked' => FALSE,
          ),
        ),
      ),
    );
    $form['submit']['#weight'] = 1;
    $form['reset']['#weight'] = 2;
    $form['submit']['#submit'][] = 'commerce_extra_panes_termsofservice_form_settings_submit';
  }
}
function commerce_extra_panes_termsofservice_form_settings_submit(&$form, &$form_state) {
  $pane_id = $form['checkout_pane']['#value']['pane_id'];
  variable_set('cep_tos_' . $pane_id, $form_state['values']['pane-tos']);
  variable_set('cep_tos_required_' . $pane_id, $form_state['values']['pane-tos-required']);
  variable_set('cep_tos_as_link_' . $pane_id, $form_state['values']['pane-tos-as-link']);
  variable_set('cep_tos_position_' . $pane_id, $form_state['values']['pane-tos-position']);
}

/**
 * Implements hook_form_alter().
 */
function commerce_extra_panes_termsofservice_form_alter(&$form, &$form_state, $form_id) {
  if (strpos($form_id, 'commerce_checkout_form_') === 0) {
    $extra_panes = commerce_extra_panes_get_panes();
    $enabled_panes = commerce_checkout_panes(array(
      'enabled' => TRUE,
    ));
    $page_id = $form_state['checkout_page']['page_id'];
    foreach ($extra_panes as $extra_pane) {
      if ($extra_pane->status) {
        $pane_id = 'extra_pane__' . $extra_pane->extra_type . '__' . $extra_pane->extra_id;
        if ($enabled_panes[$pane_id]['page'] == $page_id) {
          if (variable_get('cep_tos_' . $pane_id, 0)) {
            if (variable_get('cep_tos_as_link_' . $pane_id, 0)) {
              $form[$pane_id][$pane_id]["#access"] = FALSE;
              unset($form[$pane_id]["#title"]);
              $link = l(t('Terms of Service'), 'node/' . $extra_pane->extra_id, array(
                'attributes' => array(
                  'target' => '_blank',
                ),
              ));
              $title = t('I agree with the !link', array(
                '!link' => $link,
              ));
            }
            else {
              $title = t('I agree with the Terms of Service');
            }
            $form[$pane_id]['termsofservice'] = array(
              '#type' => 'checkbox',
              '#title' => $title,
              '#required' => variable_get('cep_tos_required_' . $pane_id, 0),
              '#weight' => variable_get('cep_tos_position_' . $pane_id, 'below') == 'below' ? 1 : -1,
            );

            // Add a generic class.
            $form[$pane_id]['#attributes'] = array(
              'class' => array(
                'terms-of-service',
              ),
              '#weight' => 0,
            );
          }
        }
      }
    }
  }
}