You are here

commerce_extra_panes.module in Commerce extra panes 7

Module file for Drupal Commerce Extra panes.

File

commerce_extra_panes.module
View source
<?php

/**
 * @file
 * Module file for Drupal Commerce Extra panes.
 */

/**
 * Implements hook_menu().
 */
function commerce_extra_panes_menu() {
  $items = array();
  $items['admin/commerce/config/checkout/extra'] = array(
    'title' => 'Checkout extra panes',
    'description' => 'Add extra panes to the checkout form.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_extra_panes_settings_form',
    ),
    'access arguments' => array(
      'administer checkout',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
    'file' => 'includes/commerce_extra_panes.admin.inc',
  );
  $items['admin/commerce/config/checkout/extra/%/delete'] = array(
    'title' => 'Delete checkout extra panes',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_extra_panes_delete_confirm',
      5,
    ),
    'access arguments' => array(
      'administer checkout',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'includes/commerce_extra_panes.admin.inc',
  );
  $items['admin/commerce/config/checkout/extra/%/change-status/%'] = array(
    'title' => 'Disable/enable checkout extra panes',
    'page callback' => 'commerce_extra_panes_change_status',
    'page arguments' => array(
      5,
      7,
    ),
    'access arguments' => array(
      'administer checkout',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'includes/commerce_extra_panes.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function commerce_extra_panes_theme($existing, $type, $theme, $path) {
  return array(
    'commerce_extra_panes_settings_list' => array(
      'render element' => 'element',
    ),
    'commerce_extra_panes_checkout_form' => array(
      'render element' => 'elements',
      'path' => drupal_get_path('module', 'commerce_extra_panes') . '/theme',
      'template' => 'commerce_extra_panes_checkout_form',
    ),
    'commerce_extra_panes_review' => array(
      'render element' => 'elements',
      'path' => drupal_get_path('module', 'commerce_extra_panes') . '/theme',
      'template' => 'commerce_extra_panes_review',
    ),
  );
}
function theme_commerce_extra_panes_settings_list($element) {
  $operations = array();
  $variables['header'] = array(
    'title' => array(
      'data' => t('Title'),
    ),
    'type' => array(
      'data' => t('Extra pane type'),
    ),
    'status' => array(
      'data' => t('Status'),
    ),
    'op' => array(
      'data' => t('Operations'),
    ),
  );
  $extra_panes = commerce_extra_panes_get_panes();
  foreach ($extra_panes as $extra_pane) {
    $entity = entity_load_single($extra_pane->extra_type, $extra_pane->extra_id);
    $row = array();
    $operations = array();
    $row[] = array(
      'data' => check_plain($entity->title),
    );
    $row[] = array(
      'data' => $extra_pane->extra_type,
    );
    $status = $extra_pane->status ? t('Enabled') : t('Disabled');
    $row[] = array(
      'data' => $status,
    );
    if (node_access('update', $entity)) {
      $operations[] = array(
        'href' => 'node/' . $entity->nid . '/edit',
        'title' => t('edit node'),
        'query' => array(
          'destination' => 'admin/commerce/config/checkout/extra',
        ),
      );
    }
    $operations[] = array(
      'href' => 'admin/commerce/config/checkout/form/pane/extra_pane__' . $extra_pane->extra_type . '__' . $extra_pane->extra_id,
      'title' => t('configure pane'),
      'query' => array(
        'destination' => 'admin/commerce/config/checkout/extra',
      ),
    );
    $operations[] = array(
      'href' => 'admin/commerce/config/checkout/extra/' . $extra_pane->extra_id . '/change-status/' . drupal_get_token($extra_pane->extra_id),
      'title' => $extra_pane->status ? t('disable') : t('enable'),
    );
    $operations[] = array(
      'href' => 'admin/commerce/config/checkout/extra/' . $extra_pane->extra_id . '/delete',
      'title' => t('delete'),
    );
    $ops = theme('links__ctools_dropbutton', array(
      'links' => $operations,
      'attributes' => array(
        'class' => array(
          'links',
          'inline',
        ),
      ),
    ));
    $row[] = array(
      'data' => $ops,
    );
    $variables['rows'][] = $row;
  }
  return theme('table', $variables);
}

/**
 * Helper function for getting one pane or all.
 */
function commerce_extra_panes_get_panes($extra_id = NULL, $extra_type = 'node') {
  $extra_panes = array();
  $query = db_select('commerce_extra_panes', 'cpe')
    ->fields('cpe', array(
    'extra_id',
    'extra_type',
    'status',
  ));
  $query
    ->condition('extra_type', $extra_type);
  if ($extra_id) {
    $query
      ->condition('extra_id', $extra_id);
  }
  $extra_panes = $query
    ->execute()
    ->fetchAllAssoc('extra_id', PDO::FETCH_OBJ);
  return $extra_panes;
}

/**
 * Implements hook_commerce_checkout_pane_info().
 */
function commerce_extra_panes_commerce_checkout_pane_info() {
  global $language;
  $checkout_panes = array();
  $extra_panes = commerce_extra_panes_get_panes();
  foreach ($extra_panes as $extra_pane) {
    $title = '';
    if ($extra_pane->status) {
      $entity = entity_load_single($extra_pane->extra_type, $extra_pane->extra_id);
      if (!empty($entity)) {
        if (module_exists('translation') && !empty($entity->tnid)) {
          $translations = translation_node_get_translations($entity->tnid);
          $title = isset($translations[$language->language]->title) ? $translations[$language->language]->title : $entity->title;
          $title = check_plain($title);
        }
        $checkout_panes['extra_pane__' . $extra_pane->extra_type . '__' . $extra_pane->extra_id] = array(
          'title' => !empty($title) ? $title : check_plain($entity->title),
          'file' => 'includes/commerce_extra_panes.checkout_pane.inc',
          'base' => 'commerce_extra_panes_contents',
        );
      }
    }
  }
  return $checkout_panes;
}

/**
 * Implements hook_entity_info_alter().
 */
function commerce_extra_panes_entity_info_alter(&$entity_info) {
  $entity_info['node']['view modes']['checkout_pane'] = array(
    'label' => t('Checkout pane'),
    'custom settings' => TRUE,
  );
}

/**
 * Implements template_preprocess_node().
 */
function commerce_extra_panes_preprocess_node(&$variables) {
  if ($variables['view_mode'] == 'checkout_pane') {
    $variables['title'] = '';
  }
}

/**
 * Implements hook_entity_update().
 */
function commerce_extra_panes_entity_update($entity, $type) {
  list($id) = entity_extract_ids($type, $entity);
  $panes = commerce_extra_panes_get_panes($id, $type);
  $extra_pane = reset($panes);
  if (!empty($extra_pane) && !empty($extra_pane->extra_id) && !$entity->status) {

    // Disable the pane.
    $affected_rows = db_update('commerce_extra_panes')
      ->fields(array(
      'status' => 0,
    ))
      ->condition('extra_id', $extra_pane->extra_id)
      ->condition('extra_type', $type)
      ->execute();
    if ($affected_rows) {
      drupal_set_message(t('Corresponding Commerce extra pane was disabled.'), 'warning');
    }
  }
}

/**
 * Implements hook_entity_delete().
 */
function commerce_extra_panes_entity_delete($entity, $type) {
  list($id) = entity_extract_ids($type, $entity);
  $panes = commerce_extra_panes_get_panes($id, $type);
  $extra_pane = reset($panes);
  if (!empty($extra_pane) && !empty($extra_pane->extra_id)) {
    $affected_rows = db_delete('commerce_extra_panes')
      ->condition('extra_id', $extra_pane->extra_id)
      ->condition('extra_type', $type)
      ->execute();
    if ($affected_rows) {
      drupal_set_message(t('Corresponding Commerce extra pane was deleted.'), 'warning');
    }
  }
}