You are here

basic_cart.module in Basic cart 7

Basic cart module file.

File

basic_cart.module
View source
<?php

/**
 * @file
 * Basic cart module file.
 */

/**
 * Implements hook_permission().
 */
function basic_cart_permission() {
  return array(
    'administer basic cart' => array(
      'title' => t('Administer basic cart'),
      'description' => t('Perform administration tasks for basic cart.'),
    ),
    'use basic cart' => array(
      'title' => t('Use basic cart'),
      'description' => t('Use basic cart (add to cart, remove from cart, checkout).'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function basic_cart_menu() {
  $items = array();
  $items['admin/config/basic_cart'] = array(
    'title' => 'Basic cart',
    'description' => 'Basic cart content type selection.',
    'position' => 'left',
    'weight' => -10,
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array(
      'administer basic cart',
    ),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
  );
  $items['admin/config/basic_cart/settings'] = array(
    'title' => 'Basic cart',
    'description' => 'Basic cart content type selection.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'basic_cart_admin_content_type',
    ),
    'access arguments' => array(
      'administer basic cart',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'basic_cart.admin.inc',
  );
  $items['cart'] = array(
    'title' => 'Your shopping cart',
    'description' => 'The shopping cart page',
    'page callback' => 'basic_cart_cart',
    'access arguments' => array(
      'use basic cart',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'basic_cart.cart.inc',
  );
  $items['cart/add'] = array(
    'title' => 'Add to cart',
    'description' => 'Add to cart url.',
    'page callback' => 'basic_cart_add_to_cart',
    'access arguments' => array(
      'use basic cart',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'basic_cart.cart.inc',
  );
  $items['cart/remove'] = array(
    'title' => 'Remove from cart',
    'description' => 'Remove from cart url.',
    'page callback' => 'basic_cart_remove_from_cart',
    'access arguments' => array(
      'use basic cart',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'basic_cart.cart.inc',
  );
  $items['checkout'] = array(
    'title' => 'Checkout',
    'description' => 'Checkout.',
    'page callback' => 'basic_cart_checkout',
    'access arguments' => array(
      'use basic cart',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'basic_cart.cart.inc',
  );
  $items['checkout/thank-you'] = array(
    'title' => 'Thank you',
    'description' => 'Checkout thank you page.',
    'page callback' => 'basic_cart_checkout_thank_you',
    'access arguments' => array(
      'use basic cart',
    ),
    'type' => MENU_CALLBACK,
    'file' => 'basic_cart.cart.inc',
  );
  return $items;
}

/**
 * Implements hook_node_view().
 */
function basic_cart_node_view($node, $view_mode, $langcode) {

  // Check if the current user has access.
  if (user_access('use basic cart')) {

    // Getting node types.
    $node_types = variable_get('basic_cart_content_types');
    if (!is_array($node_types) || empty($node_types)) {
      return;
    }
    if (!empty($node_types[$node->type]) && in_array($node->type, $node_types)) {
      $node->content['basic_cart_add_to_cart'] = array(
        '#markup' => theme('basic_cart_add_to_cart', array(
          'nid' => $node->nid,
        )),
        '#weight' => 50,
      );
    }
  }
}

/**
 * Implements hook_theme().
 */
function basic_cart_theme() {
  return array(
    'basic_cart_add_to_cart' => array(
      'function' => 'basic_cart_add_to_cart_button',
      'variables' => array(
        'nid' => NULL,
      ),
    ),
    'basic_cart_cart_flat' => array(
      'template' => 'basic_cart_cart_flat',
      'variables' => array(
        'cart' => NULL,
      ),
    ),
    'basic_cart_cart_render_block' => array(
      'template' => 'basic_cart_cart_render_block',
      'variables' => array(
        'cart' => NULL,
      ),
    ),
    'basic_cart_render_cart_element' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * Theme function implementation.
 */
function basic_cart_add_to_cart_button($vars) {
  $nid = $vars['nid'];
  return '<div class="basic-cart-add-to-cart">
            ' . l(t('Add to cart'), 'cart/add/' . $nid, array(
    'attributes' => array(
      'class' => array(
        'button basic-cart-add-to-cart-link',
      ),
    ),
  )) . '
          </div>';
}

/**
 * Implements hook_block_info().
 */
function basic_cart_block_info() {
  $blocks['shopping_cart'] = array(
    'info' => t('Shopping cart'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function basic_cart_block_view($delta = '') {

  // Check if the current user has access.
  switch ($delta) {
    case 'shopping_cart':
      if (user_access('use basic cart')) {
        include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'basic_cart') . '/basic_cart.cart.inc';
        $block['subject'] = t('Your cart');
        $block['content'] = theme('basic_cart_cart_render_block', array(
          'cart' => basic_cart_get_cart(),
        ));
        return $block;
      }
      break;
  }
}

Functions