uc_coupon.entity.inc in Ubercart Discount Coupons 7.2
File
uc_coupon.entity.inc
View source
<?php
function uc_coupon_load($cid) {
$coupon = db_query("SELECT * FROM {uc_coupons} WHERE cid = :cid", array(
':cid' => $cid,
))
->fetchObject();
$coupon = is_object($coupon) ? $coupon : new stdClass();
$coupon->data = isset($coupon->data) ? unserialize($coupon->data) : array();
if (!isset($coupon->data['apply_to'])) {
if (isset($coupon->data['max_applicable_products_value']) && isset($coupon->data['max_applicable_products']) && $coupon->data['max_applicable_products']) {
$coupon->data['apply_to'] = $coupon->data['max_applicable_products_value'];
$coupon->data['apply_count'] = $coupon->data['max_applicable_products'];
}
elseif (isset($coupon->data['products']) || isset($coupon->data['skus']) || isset($coupon->data['terms']) || isset($coupon->data['product_types'])) {
$coupon->data['apply_to'] = 'products';
}
else {
$coupon->data['apply_to'] = 'subtotal';
}
}
unset($coupon->data['max_applicable_products']);
unset($coupon->data['max_applicable_products_value']);
drupal_alter('uc_coupon', $coupon);
return $coupon;
}
function uc_coupon_save(&$coupon, $edit = array()) {
foreach (module_implements('uc_coupon_presave') as $module) {
$callback = $module . '_uc_coupon_presave';
$callback($coupon, $edit);
}
if (isset($coupon->cid)) {
drupal_write_record('uc_coupons', $coupon, 'cid');
}
else {
$coupon->created = REQUEST_TIME;
$coupon->bulk_seed = md5(uniqid());
drupal_write_record('uc_coupons', $coupon);
}
module_invoke_all('uc_coupon_save', $coupon);
}
function uc_coupon_delete($cid) {
$coupon = uc_coupon_load($cid);
module_invoke_all('uc_coupon_delete', $coupon);
db_delete('uc_coupons')
->condition('cid', $cid)
->execute();
db_delete('uc_coupons_orders')
->condition('cid', $cid)
->execute();
return $coupon;
}