View source
<?php
function uc_coupon_purchase_permission() {
return array(
'view purchased coupons' => array(
'title' => t('view purchased coupons'),
'description' => t('View list of the coupons a user has purchased.'),
),
);
}
function uc_coupon_purchase_menu() {
$items = array();
$items['user/%user/coupons'] = array(
'title' => 'Coupons',
'description' => 'View your purchased coupons.',
'page callback' => 'uc_coupon_purchase_view',
'access callback' => 'uc_coupon_purchase_can_view',
'page arguments' => array(
1,
),
'access arguments' => array(
1,
),
'type' => MENU_LOCAL_TASK,
'file' => 'uc_coupon_purchase.pages.inc',
'weight' => 5,
);
$items['user/%user/coupons/%uc_coupon'] = array(
'title' => 'Print preview',
'page callback' => 'uc_coupon_purchase_print',
'access callback' => 'uc_coupon_purchase_can_view',
'page arguments' => array(
1,
3,
),
'access arguments' => array(
1,
3,
),
'type' => MENU_LOCAL_TASK,
'file' => 'uc_coupon_purchase.pages.inc',
'weight' => 5,
);
$items['user/%user/coupons/%uc_coupon/codes'] = array(
'title' => 'Download codes',
'page callback' => 'uc_coupon_codes_csv',
'access callback' => 'uc_coupon_purchase_can_view',
'page arguments' => array(
3,
),
'access arguments' => array(
1,
3,
),
'type' => MENU_CALLBACK,
'file' => 'uc_coupon.admin.inc',
'file path' => drupal_get_path('module', 'uc_coupon'),
'weight' => 5,
);
return $items;
}
function uc_coupon_purchase_can_view($account, $coupon = NULL) {
global $user;
if (user_access('view purchased coupons')) {
if (user_access('administer users') || $account->uid == $user->uid && uc_coupon_purchase_has_coupon($account, $coupon)) {
return TRUE;
}
}
return FALSE;
}
function uc_coupon_purchase_uc_coupon_actions($coupon) {
$actions = array();
if (!user_access('manage store coupons')) {
global $user;
$account = arg(0) == 'user' ? user_load(arg(1)) : $user;
if (uc_coupon_purchase_can_view($account, $coupon)) {
$actions[] = array(
'url' => "user/{$account->uid}/coupons/{$coupon->cid}/view",
'icon' => drupal_get_path('module', 'uc_store') . '/images/order_view.gif',
'title' => t('View coupon: @name', array(
'@name' => $coupon->name,
)),
);
$actions[] = array(
'url' => "user/{$account->uid}/coupons/{$coupon->cid}/print",
'icon' => drupal_get_path('module', 'uc_store') . '/images/print.gif',
'title' => t('Print coupon: @name', array(
'@name' => $coupon->name,
)),
);
if ($coupon->bulk) {
$actions[] = array(
'url' => 'admin/store/coupons/' . $coupon->cid . '/codes',
'icon' => drupal_get_path('module', 'uc_store') . '/images/menu_reports_small.gif',
'title' => t('Download codes as CSV: @name', array(
'@name' => $coupon->name,
)),
);
}
}
}
return $actions;
}
function uc_coupon_purchase_has_coupon($account, $coupon = NULL) {
$query = db_select('uc_coupon_purchase_users', 'u')
->condition('u.uid', $account->uid);
$query
->addExpression('COUNT(*)');
if (!empty($coupon) && !empty($coupon->cid)) {
$query
->condition('u.cid', $coupon->cid, 'AND');
}
return $query
->execute()
->fetchField();
}
function uc_coupon_purchase_uc_product_feature() {
$features[] = array(
'id' => 'coupon',
'title' => t('Coupon creation'),
'callback' => 'uc_coupon_purchase_feature_form',
'delete' => 'uc_coupon_purchase_feature_delete',
'settings' => 'uc_coupon_purchase_feature_settings',
);
return $features;
}
function uc_coupon_purchase_feature_form($form, $form_state, $node, $feature) {
$models = array(
NULL => t('Any'),
$node->model => $node->model,
);
if (module_exists('uc_attribute')) {
$adjustments = db_query("SELECT model FROM {uc_product_adjustments} WHERE nid = :nid", array(
':nid' => $node->nid,
));
foreach ($adjustments as $adjustment) {
if (!in_array($adjustment->model, $models)) {
$models[$adjustment->model] = $adjustment->model;
}
}
}
if (!empty($feature)) {
$data = db_query("SELECT * FROM {uc_coupon_purchase} WHERE pfid = :pfid", array(
':pfid' => $feature['pfid'],
))
->fetchObject();
$form['pfid'] = array(
'#type' => 'value',
'#value' => $feature['pfid'],
);
if (!$data) {
$data = new stdClass();
}
}
else {
$data = new stdClass();
}
$coupons = array();
$result = db_query("SELECT cid, name, code FROM {uc_coupons} WHERE status = :status", array(
':status' => 0,
));
foreach ($result as $coupon) {
$coupons[$coupon->cid] = $coupon->name . ' (' . $coupon->code . ')';
}
if (!$coupons) {
drupal_set_message(t('No inactive coupons found. Please <a href="!url">create an inactive coupon</a> to use as a base coupon for purchases.', array(
'!url' => url('admin/store/coupons/add'),
)), 'error');
drupal_goto('node/' . arg(1) . '/edit/features');
}
$form['title'] = array(
'#type' => 'markup',
'#markup' => '<h2>' . t('Coupon creation') . '</h2>',
);
$form['nid'] = array(
'#type' => 'value',
'#value' => $node->nid,
);
$form['model'] = array(
'#type' => 'select',
'#title' => t('Model/SKU'),
'#default_value' => isset($data->model) ? $data->model : NULL,
'#description' => t('Select the model/SKU of the product that cause a coupon to be created.'),
'#options' => $models,
);
$form['base_cid'] = array(
'#type' => 'select',
'#title' => t('Base coupon'),
'#default_value' => isset($data->base_cid) ? $data->base_cid : reset(array_keys($coupons)),
'#description' => t('Select the coupon that will be cloned when a user purchases this product. Only inactive coupons are listed; the purchased coupon will automatically be activated.'),
'#options' => $coupons,
);
return uc_product_feature_form($form, $form_state, $node, $feature);
}
function uc_coupon_purchase_feature_form_submit($form, &$form_state) {
$coupon = uc_coupon_load($form_state['values']['base_cid']);
$description = empty($form_state['values']['model']) ? t('<strong>SKU:</strong> Any<br/>') : t('<strong>SKU:</strong> !sku<br/>', array(
'!sku' => $form_state['values']['model'],
));
$description .= t('<strong>Base coupon:</strong> !coupon<br/>', array(
'!coupon' => $coupon->name . ' (' . $coupon->code . ')',
));
$data = array(
'pfid' => !empty($form_state['values']['pfid']) ? $form_state['values']['pfid'] : 0,
'nid' => $form_state['values']['nid'],
'fid' => 'coupon',
'description' => $description,
);
$form_state['redirect'] = uc_product_feature_save($data);
if (!empty($form_state['values']['pfid'])) {
db_delete('uc_coupon_purchase')
->condition('pfid', $form_state['values']['pfid'])
->execute();
}
$id = db_insert('uc_coupon_purchase')
->fields(array(
'pfid' => $data['pfid'],
'nid' => $form_state['values']['nid'],
'model' => $form_state['values']['model'],
'base_cid' => $form_state['values']['base_cid'],
))
->execute();
}
function uc_coupon_purchase_feature_delete($pfid) {
db_delete('uc_coupon_purchase')
->condition('pfid', $pfid)
->execute();
}
function uc_coupon_purchase_feature_settings() {
foreach (uc_order_status_list('general') as $status) {
$statuses[$status['id']] = $status['title'];
}
$form['uc_coupon_purchase_order_status'] = array(
'#type' => 'select',
'#title' => t('Order status'),
'#default_value' => variable_get('uc_coupon_purchase_order_status', 'completed'),
'#description' => t('Select the order status that will cause the coupon to be created. Be aware that if payments are processed automatically, this happens before anonymous customers have an account created. This order status should not be reached before the user account exists.'),
'#options' => $statuses,
);
return $form;
}
function uc_coupon_purchase_form_uc_coupon_add_form_alter(&$form) {
$value = isset($form['#uc_coupon']) ? $form['#uc_coupon'] : new stdClass();
$form['uc_coupon_purchase'] = array(
'#type' => 'fieldset',
'#title' => t('Coupon purchase options'),
'#description' => t('These settings alter the generation of purchased coupons based on this coupon.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => -1,
);
$form['uc_coupon_purchase']['purchase_suffix_length'] = array(
'#type' => 'select',
'#title' => t('Purchased coupon code suffix length'),
'#default_value' => isset($value->data['purchase_suffix_length']) ? $value->data['purchase_suffix_length'] : 12,
'#description' => t('A random string of at least this many characters will be appended to the base coupon code to ensure uniqueness.'),
'#options' => drupal_map_assoc(range(4, 30)),
);
$form['uc_coupon_purchase']['purchase_relative'] = array(
'#type' => 'checkbox',
'#title' => t('Alter coupon date restrictions relative to the purchase date.'),
'#description' => t('Checking this box will make purchased coupons valid for the same time span as the base coupon, otherwise coupons will always be valid between the same dates.'),
'#default_value' => isset($value->data['purchase_relative']) ? $value->data['purchase_relative'] : FALSE,
);
$form['status']['#description'] .= '<br />' . t('Only inactive coupons can be selected as the base for a purchased coupon, subsequent purchased coupons will automatically be activated.');
$form['#validate'][] = 'uc_coupon_purchase_uc_coupon_add_form_validate';
}
function uc_coupon_purchase_uc_coupon_add_form_validate($form, &$form_state) {
if ($form_state['values']['purchase_relative'] && !$form_state['values']['use_validity']) {
form_set_error('purchase_relative', t('You must specify date restrictions to use the relative date feature with purchased coupons.'));
}
}
function uc_coupon_purchase_uc_coupon_presave(&$coupon, $edit) {
if (isset($edit['purchase_suffix_length'])) {
$coupon->data['purchase_suffix_length'] = $edit['purchase_suffix_length'];
}
if (isset($edit['purchase_relative']) && $edit['purchase_relative']) {
$coupon->data['purchase_relative'] = TRUE;
}
}
function uc_coupon_purchase_uc_coupon_delete($coupon) {
db_delete('uc_coupon_purchase_users')
->condition('cid', $coupon->cid)
->execute();
}
function uc_coupon_purchase_uc_order($op, $order, $status) {
switch ($op) {
case 'update':
if ($status == variable_get('uc_coupon_purchase_order_status', 'completed') && $order->order_status != $status && $order->uid > 0 && ($order_user = user_load($order->uid)) !== FALSE) {
foreach ($order->products as $product) {
$result = db_query("SELECT * FROM {uc_coupon_purchase} WHERE nid = :nid", array(
':nid' => $product->nid,
));
foreach ($result as $row) {
if ($row->model == $product->model || empty($row->model)) {
$coupon = uc_coupon_load($row->base_cid);
$purchaser = trim(!empty($order->billing_company) ? $order->billing_company : $order->billing_first_name . ' ' . $order->billing_last_name);
if (empty($purchaser)) {
$purchaser = "Anonymous";
}
$coupon->name .= ' ' . t('purchased by !name', array(
'!name' => $purchaser,
));
$coupon->data['order_id'] = $order->order_id;
$new_coupon = uc_coupon_purchase_create($coupon, $product->qty, $order->uid);
uc_order_comment_save($order->order_id, 0, t('Coupon created with code %code.', array(
'%code' => $new_coupon->code,
)));
module_invoke_all('uc_coupon_purchase', $order, $new_coupon);
rules_invoke_event('uc_coupon_purchase', $order, $new_coupon);
}
}
}
}
break;
}
}
function uc_coupon_purchase_create($coupon, $qty, $uid = NULL) {
$suffix_length = isset($coupon->data['purchase_suffix_length']) ? $coupon->data['purchase_suffix_length'] : 12;
if ($qty > 1 || $coupon->bulk) {
$coupon->bulk = TRUE;
if (!isset($coupon->data['bulk_number'])) {
$coupon->data['bulk_number'] = 1;
}
$coupon->data['bulk_number'] *= $qty;
if (!isset($coupon->data['bulk_length'])) {
$coupon->data['bulk_length'] = 8;
}
$suffix_length = max($suffix_length - $coupon->data['bulk_length'], 4);
}
if (isset($coupon->data['purchase_relative'])) {
$diff = $coupon->valid_until - $coupon->valid_from;
$coupon->valid_from = gmmktime();
$coupon->valid_until = $coupon->valid_from + $diff;
}
$coupon->data['base_cid'] = $coupon->cid;
unset($coupon->cid);
$coupon->code .= strtoupper(user_password($suffix_length));
$coupon->status = TRUE;
uc_coupon_save($coupon);
if (!is_null($uid)) {
$id = db_insert('uc_coupon_purchase_users')
->fields(array(
'uid' => $uid,
'cid' => $coupon->cid,
))
->execute();
}
return $coupon;
}
function uc_coupon_purchase_views_api() {
return array(
'api' => '2.0',
'path' => drupal_get_path('module', 'uc_coupon_purchase') . '/views',
);
}