Role ID */ return $role_mapping = array( 'MEMBER' => 8, 'DELEGATE' => 7, '1ST-DAY' => 9, '2ND-DAY' => 10, 'BOTH-DAYS' => 11, /* */ 'Companies'=>12, 'Micro-company/freelancers'=>13, 'Organisations/public sector'=>14, 'Students'=>15 ); } /** * Implementation of hook_cron() * * - searches past ticket purchases which are pending * - each email address (attribute) is compared against user list * - if exists, add a role based on the product model * - else create the user and add role * - custom email based on tickets CCK text fields */ function uc_ticket_cron() { $role_mapping = _uc_ticket_get_mapping(); $email_templates = array(); /* Get a list of pending orders */ $sql = "SELECT o.order_id, o.primary_email " . "FROM {uc_orders} AS o " . "WHERE o.`order_status` = 'processing' "; $result = db_query($sql); /* Loop through the pending orders */ while( $order = db_fetch_array( $result ) ) { $complete = TRUE; /* Get a list of line items */ $sql = "SELECT p.data, p.model, p.nid, p.order_id FROM {uc_order_products} AS p " . "WHERE p.order_id = %d"; $item_result = db_query($sql, $order['order_id']); /* Loop through the line items */ while( $item = db_fetch_array( $item_result ) ){ /* get data and needed vars */ $data = unserialize($item['data']); $email = $data['attributes']['Ticket holders email address']; $ticket_class = $data['attributes']['Ticket class']; $tmp_password = substr( str_shuffle( $email . 'bTWEEN08'), 0 , 5); $add_roles = array( $role_mapping['MEMBER'] => 'RID', $role_mapping['DELEGATE'] => 'RID', $role_mapping[ $item['model'] ] => 'RID', $role_mapping[ $ticket_class ] => 'RID' ); /* load in confirmation email based on the ticket purchased */ if(!isset($email_templates[ $item['nid'] ])){ $sql = "SELECT field_new_user_email_value, field_exsisting_user_email_value FROM {content_type_product} WHERE nid = '%d'"; $email_templates[ $item['nid'] ] = db_fetch_array( db_query( $sql, $item['nid'] ) ); } /* check if the email already has an account */ $user = user_load(array('mail'=>$email)); if( $user->uid != null ){ /* User exsists, just add roles */ $changed = false; /* Add roles to user, when they dont already have them */ foreach( $add_roles as $role => $junk ) { if( !isset( $user->roles[$role] ) ) { $changed = true; db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $user->uid, $role); } } /* only update users who's roles have changed */ if($changed){ /* customise the email for this user */ $template = $email_templates[ $item['nid'] ]['field_exsisting_user_email_value']; $template = str_replace('[%home%]', 'http://www.'.$_SERVER['SERVER_NAME'], $template); $template = str_replace('[%purchase_email%].', $order['primary_email'], $template); mimemail('tickets@'.$_SERVER['SERVER_NAME'], $user->mail, 'bTWEEN08', $template); watchdog('ticket-system', 'Order '.$order['order_id'].' : User ' . $user->name . ' updated
' . $user->name . ' (model : ' . $item['model'] . ' | order_id : ' . $item['order_id'] . ')', WATCHDOG_NOTICE, l('Edit User','user/' . $user->uid . '/edit')); } } else { /* No user, make one */ $details = array( 'name' => substr( $email, 0, strpos( $email, '@' ) ), 'pass' => $tmp_password, 'mail' => $email, 'init' => $email, 'access' => 0, 'status' => 1, 'roles' => $add_roles ); if($user = user_save(null, $details)){ /* customise the email for this user */ $template = $email_templates[ $item['nid'] ]['field_new_user_email_value']; $template = str_replace('[%user%]', $user->name, $template); $template = str_replace('[%password%]', $tmp_password, $template); $template = str_replace('[%home%]', 'http://www.'.$_SERVER['SERVER_NAME'], $template); $template = str_replace('[%purchase_email%].', $order['primary_email'], $template); mimemail('tickets@'.$_SERVER['SERVER_NAME'], $user->mail, 'bTWEEN08', $template); watchdog('ticket-system', 'Order '.$order['order_id'].' : New user created
' . $user->name . ' (model : ' . $item['model'] . ' | order_id : ' . $item['order_id'] . ')', WATCHDOG_NOTICE, l('Edit User','user/' . $user->uid . '/edit')); } else { watchdog('ticket-system', 'Order '.$order['order_id'].' : Problems saving new user

' . 'order_id : ' . $item['order_id'] . '
' . 'buyer : ' . $order['primary_email'] . '
' . 'model : ' . $item['model'] . '
' . '
' . print_r( $details, true ) . '
', WATCHDOG_ERROR); $complete = $item['order_id']; } } } // while items if($complete === TRUE){ /* update order status */ uc_order_update_status($order['order_id'], 'completed'); watchdog('ticket-system', 'Order '.$order['order_id'].' : Completed', WATCHDOG_NOTICE); } else { /* part or all of the order was not completed */ watchdog('ticket-system', 'Order '.$order['order_id'].' : failed to complete

' . 'order_id : ' . $order['order_id'] . '
' . 'buyer : ' . $order['primary_email'] . '
' . 'failed_item_id : ' . $complete . '
' . '
' . print_r( $order, true ) . '
', WATCHDOG_ERROR); } } // while orders } /** * Implementation of hook_add_to_cart() * * * Checks a ticket with this email address is not already in the cart * Checks user with that email address does not already have a ticket for that day */ function uc_ticket_add_to_cart($nid, $qty, $data) { $message = ''; /* Checking the cart doesnt already contain a ticket with this email address */ $cart = uc_cart_get_contents(); foreach( $cart as $item ){ if( in_array( $data['attributes'][1], $item->data['attributes'] ) ) { $message = 'You already have a ticket for '.$data['attributes'][1].' in your cart'; } } /* Checking the user isnt already attending on that day, only if no error */ if($message == '') { $role_mapping = _uc_ticket_get_mapping(); $product = db_fetch_array(db_query('SELECT model FROM {uc_products} WHERE nid = %d', $nid)); $condition = ''; if( $product['model'] == 'BOTH-DAYS' ){ $condition = sprintf( 'r.rid = %d OR r.rid = %d OR r.rid = %d', $role_mapping['1ST-DAY'], $role_mapping['2ND-DAY'], $role_mapping['BOTH-DAYS'] ); } else { $condition = sprintf( 'r.rid = %d OR r.rid = %d', $role_mapping[ $product['model'] ], $role_mapping['BOTH-DAYS'] ); } $sql = 'SELECT mail FROM {users} as u, {users_roles} as r WHERE u.uid = r.uid AND u.mail = "%s" AND '.$condition; $result = db_fetch_array(db_query($sql, $data['attributes'][1])); if(isset($result['mail']) AND $result['mail'] != ''){ $message = $data['attributes'][1].' already has a ticket for this day or both days'; } } if( $message != '' ){ $return[] = array( 'success' => false, 'message' => $message ); } else { $return[] = array( 'success' => true, 'message' => 'Ticket added to your ticket cart' ); } return $return; }