'user_access', 'access arguments' => array(EC_PERM_REPORT), 'type' => MENU_CALLBACK, 'page callback' => 'ec_anon_blank', ); $items['ec_anon/confirm'] = array( 'access callback' => 'user_access', 'access arguments' => array(EC_PERM_REPORT), 'title' => 'email confirmation', 'type' => MENU_CALLBACK, 'page callback' => 'ec_anon_confirmation', ); $items['ec_anon/regsent'] = array( 'access callback' => 'user_access', 'access arguments' => array(EC_PERM_REPORT), 'title' => 'confirmation sent', 'type' => MENU_CALLBACK, 'page callback' => 'ec_anon_registration_confirm_sent', ); return $items; } /** * Implements hook_form_alter(). */ function ec_anon_form_ec_product_admin_ptypes_settings_alter(&$form, &$form_state) { $form['ec_anon_policy'] = array( '#type' => 'select', '#title' => t('Anonymous purchasing policy'), '#default_value' => variable_get('ec_anon_policy', ECANON_POLICY_DEFAULT), '#options' => ec_anon_policy_list(FALSE), '#description' => theme('advanced_help_topic', array('module' => 'ec_anon', 'topic' => 'policy-description')) . ' ' . t('This sets the site-wide policy for anonymous purchasing. This setting can be overridden by product settings.'), '#weight' => -20, ); } /** * Implements hook_product_form_alter(). */ function ec_anon_product_form_alter(&$pform, &$node_form) { if (ec_product_feature_exists($node_form['#node'], 'anonymous')) { $plist = ec_anon_policy_list(); $sitewide = $plist[variable_get('ec_anon_policy', ECANON_POLICY_DEFAULT)]; $pform['anon_policy'] = array( '#type' => 'select', '#title' => t('Anonymous purchasing policy for this product'), '#default_value' => !empty($node_form['#node']->anon_policy) ? $node_form['#node']->anon_policy : NULL, '#options' => ec_anon_policy_list(), '#description' => theme('advanced_help_topic', array('module' => 'ec_anon', 'topic' => 'policy-description')) . ' ' . t('This sets the policy for user registrations during checkout for this product. This setting overrides the !link, which is currently set to !setting.', array('!link' => l(t('site-wide setting'), 'admin/config/store/products'), '!setting' => $sitewide)), '#weight' => 0, ); } } /** * Implements hook_user_insert(). */ function ec_anon_user_insert(&$edit, $account, $category) { // Migrate cart of anonymous user over to new user. if (isset($_GET['reg']) && $_GET['reg'] == 'standard') { db_update('ec_cart') ->fields(array( 'cookie_id' => $account->uid, )) ->condition('cookie_id', session_id()) ->execute(); } } /** * Used to display a blank page. */ function ec_anon_blank() { return ''; } /** * Displays a message to the user after registration has completed. * Accepts $reg_type of 'standard' or 'express' (logintoboggan). * * @return * String, the confirmation message. */ function ec_anon_registration_confirm_sent() { return t('Please follow the link contained in that email then proceed to the checkout to complete your purchase.'); } /** * Returns an associative array of policies for use on form select elements. * * @return Array of policies. */ function ec_anon_policy_list($allow_unset = TRUE) { $plist = array( ECANON_POLICY_NEVER => t('Registered only'), ECANON_POLICY_NEVER_CART => t('Registered only, Allow anonymous selection'), ECANON_POLICY_OPTIONAL => t('Flexible'), ECANON_POLICY_ALWAYS => t('Anonymous only'), ); if (!empty($allow_unset)) { return array(ECANON_POLICY_UNSET => t('Unset')) + $plist; } return $plist; } /** * Implements hook_customer_info(). */ function ec_anon_customer_info() { return array( 'anonymous' => array( 'name' => t('Anonymous'), 'description' => t('Handle customer information for anonymous customers'), 'module' => 'ec_anon', 'store_addresses' => FALSE, ), ); } /** * Implements hook_customer_info_alter(). * * Make sure that the anonymous customer type is always last. */ function ec_anon_customer_info_alter(&$types) { $weights = array_map('ec_common_get_weights', $types); unset($weights['anonymous']); $types['anonymous']->weight = empty($weights) ? 1 : max($weights) + 1; } /** * Implements hook_customer_get_id(). * * FIXME: This needs to be a unique sequencial id. exid is NOT a serial * field, so I can no longer use the db to generate one. */ function ec_anon_customer_get_id($info) { if (!isset($_SESSION['ec_customer_anonymous'])) { $_SESSION['ec_customer_anonymous'] = drupal_get_token(ip_address()); } if (isset($_SESSION['ec_customer_anonymous'])) { return array( 'type' => 'anonymous', 'exid' => $_SESSION['ec_customer_anonymous'], ); } return array( 'type' => 'anonymous', ); } /** * @todo Please document this function. * @see http://drupal.org/node/1354 */ function ec_anon_customer_get_info($customer) { $info = new StdClass; $info->name = variable_get('anonymous', t('Anonymous')); return $info; } /** * FIXME: The name should not be gotten from here as ec_transaction may not * exist. */ function ec_anon_customer_get_name($customer) { if (!module_exists('ec_store')) { return; } if (isset($customer->ecid)) { $info = db_query_range('SELECT sta.fullname, sta.firstname, sta.lastname FROM {ec_customer} ec INNER JOIN {ec_transaction} st ON ec.ecid = st.ecid INNER JOIN {ec_transaction_address} sta ON st.txnid = sta.txnid WHERE ec.ecid = :ec.ecid AND (sta.fullname IS NOT NULL OR sta.firstname IS NOT NULL OR sta.lastname IS NOT NULL) ORDER BY st.txnid DESC, sta.type ASC', array(':ec.ecid' => $customer->ecid)) ->fetchObject(); } else { $info = NULL; } $name = ''; if (!empty($info->fullname)) { $name = $info->fullname; } elseif (!empty($info->firstname) || !empty($info->lastname)) { $name = !empty($info->firstname) ? $info->firstname : ''; $name = !empty($info->lastname) ? trim($name . ' ' . $info->lastname) : $name; } return $name; } /** * Returns the policy for the given product. If the product has no policy set * this function returns the site-wide policy. * * @param $vid * Product version ID. * * @return * Registration policy for the given product or, if one isn't set, the * site-wide policy. */ function ec_anon_policy_get($vid) { $policy = db_query('SELECT anon_policy FROM {ec_product} WHERE vid = :vid', array(':vid' => $vid))->fetchField(); if (empty($policy) || $policy == ECANON_POLICY_UNSET) { $policy = variable_get('ec_anon_policy', ECANON_POLICY_DEFAULT); } return $policy; } /** * Implements hook_feature_info(). */ function ec_anon_feature_info() { return array( 'anonymous' => array( 'name' => t('Anonymous purchases'), 'module' => 'ec_anon', 'description' => t('Handles how anonymous customers will interact with your store'), 'weight' => -10, 'allow_edit' => FALSE, 'allow_disable' => FALSE, 'hidden' => TRUE, ), ); } /** * Implements hook_ec_checkout_validate_item(). */ function ec_anon_ec_checkout_validate_item($node, $type, $qty, $data, $severity, $return) { global $user; $registered = $user->uid != 0; if (!$registered && $type == 'ec_cart' && $items = ec_cart_current()) { foreach ($items as $item) { if (in_array(ec_anon_get_product_policy($item), array(ECANON_POLICY_NEVER, ECANON_POLICY_NEVER_CART))) { $registered = TRUE; break; } } } if ($registered) { $disallowed = array(ECANON_POLICY_ALWAYS); $msg = t('Only non-registered users can purchase this product'); } else { $disallowed = array(ECANON_POLICY_NEVER); if ($type == 'ec_cart') { // check for any anonymous only items to make exclude registed items which can be put into the cart by // anonymous users. foreach (ec_cart_current() as $item) { if (ec_anon_get_product_policy($item) == ECANON_POLICY_ALWAYS) { $disallowed[] = ECANON_POLICY_NEVER_CART; break; } } } $msg = t('Only registered users can purchase this product'); } if (in_array(ec_anon_get_product_policy($node), $disallowed)) { if ($severity >= EC_VALIDATE_ITEM_SEVERITY_MEDIUM) { drupal_set_message($msg); } return FALSE; } return TRUE; } /** * Returns TRUE if the given product is allowed in the cart of the given * user. * * This function enforces that a non-creational and recurring product * cannot be purchased by an anonymous user. */ function ec_anon_product_allowed_in_cart(&$node, $uid, $verbose = TRUE) { $disallowed = array(); if (!empty($uid)) { $disallowed = array(ECANON_POLICY_ALWAYS); $msg = t('Only non-registered users can purchase this product'); } elseif (!isset($node->is_recurring) || empty($node->is_recurring) || !isset($node->useracc['create']) || empty($node->useracc['create'])) { $disallowed = array(ECANON_POLICY_NEVER); $msg = t('Only registered users can purchase this product'); } if (in_array(ec_anon_get_product_policy($node), $disallowed)) { if ($verbose) { drupal_set_message($msg); } return FALSE; } return TRUE; } /** * Implements hook_checkout_info(). */ function ec_anon_checkout_info() { return array( 'anonymous' => array( 'name' => t('Anonymous'), 'description' => t('Provides support for anonymous customers and checking that anonymous customers can purchase products.'), 'module' => 'ec_anon', 'file' => 'ec_anon.checkout.inc', 'weight' => -10, ), ); } /** * @todo Please document this function. * @see http://drupal.org/node/1354 */ function ec_anon_validate_mail($element) { if (!valid_email_address($element['#value'])) { form_error($element, t('Valid email address required')); } } /** * Generates a token that gives anonymous customers direct access to their * order status etc. * * @return Token string to be inserted into ec_transaction. */ function ec_anon_generate_token() { do { $token = user_password(ECANON_TOKEN_LENGTH); } while (db_query("SELECT token FROM {ec_transaction} WHERE token = :token", array(':token' => $token))->fetchField()); return $token; } /** * This function returns TRUE if the customer can either register or login * for the given transaction. The decision is made according to the anonymous * purchasing policies on products and the site-wide anonymous purchasing * policy. * * @param $txn * Object, Transaction to check. * @return * Boolean, TRUE if the registration link can be displayed. */ function ec_anon_login_allowed(&$txn) { $unsetcount = $nitems = 0; foreach ($txn->items as $key => $item) { if ($items[$key]->anon_policy == ECANON_POLICY_ALWAYS) { return FALSE; } if ($items[$key]->anon_policy == ECANON_POLICY_UNSET) { ++$unsetcount; } ++$nitems; } if ($unsetcount == $nitems && variable_get('ec_anon_policy', ECANON_POLICY_DEFAULT) == ECANON_POLICY_ALWAYS) { return FALSE; } return TRUE; } /** * Implements hook_ec_ptypes_alter(). */ function ec_anon_ec_ptypes_alter(&$ptypes) { $feature = ec_product_feature_get('type', 'anonymous'); foreach ($ptypes as $type => $ptype) { $feature->ptype = $type; $feature->weight = array_reduce($ptype->features, 'ec_reduce_weight_lightest') - 2; $ptypes[$type]->features['anonymous'] = clone $feature; } } /** * Implements hook_link(). */ function ec_anon_link($type, $object, $teaser = FALSE) { global $user; $$type = & $object; if ($type == 'node' && isset($node->ptype)) { if (variable_get('ec_product_cart_addition_by_link', TRUE) && (($teaser && variable_get('ec_product_cart_on_teaser', 1)) || !$teaser)) { $l = array(); if ($user->uid && ec_anon_get_product_policy($node) == ECANON_POLICY_ALWAYS) { $l['ec_anon_reg_not_allowed'] = array( 'title' => t('Product can only be purchased by anonymous customers'), ); } elseif (!$user->uid && ec_anon_get_product_policy($node) == ECANON_POLICY_NEVER) { $l['ec_anon_anon_not_allowed'] = array( 'title' => t('Product can only be purchased by registered customers. Please !login or !register to purchase', array('!login' => l(t('login'), 'user/login', array('query' => drupal_get_destination())), '!register' => l(t('register'), 'user/register', array('query' => drupal_get_destination())))), 'html' => TRUE, ); } return $l; } } } /** * Get the products actual purchasing policy * * @param $item * Product item which you want to get to the product policy for. * * @return * Return products actual product policy. */ function ec_anon_get_product_policy($item) { if (!isset($item->anon_policy) || $item->anon_policy == ECANON_POLICY_UNSET) { return variable_get('ec_anon_policy', ECANON_POLICY_DEFAULT); } return $item->anon_policy; }