/
dc_registration_waitlist.module
278 lines (224 loc) · 7.29 KB
/
dc_registration_waitlist.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* @file
* Code for the Drupal commerce registration waitlist feature.
*/
include_once 'dc_registration_waitlist.features.inc';
/**
* Implements hook_form_alter().
*
* Adding the "Add to waitlist" link to the "Add to cart" form if the product is
* out of stock.
*/
function dc_registration_waitlist_form_alter(&$form, &$form_state, $form_id) {
// Ignore the form unless its ID starts with "commerce_cart_add_to_cart_form".
if (substr($form_id, 0, 30) != 'commerce_cart_add_to_cart_form') {
return;
}
// Get the product entity.
if (empty($form['product_id']['#value'])) {
return;
}
$product_id = $form['product_id']['#value'];
$wrapper = entity_metadata_wrapper('commerce_product', $product_id);
// Return if there's no stock field on the product.
if (registration_has_room('commerce_product',$product_id,1)) {
return;
}
// If the product is out of stock, disable the "Add to cart" button and add
// an "Add to waitlist" link.
else {
$form['waitlist'] = array(
'#markup' => '<br />'.dc_registration_waitlist_get_link($product_id),
);
}
}
/**
* Retrieve a link for adding/removing a product to the waitlist.
*
* @param int $product_id
* The product for which to issue the link.
*
* @return string
* The link HTML.
*/
function dc_registration_waitlist_get_link($product_id) {
return flag_create_link('waitlist', $product_id);
}
/**
* Implements hook_menu().
*/
function dc_registration_waitlist_menu() {
// custom page that will automatically create an order for the provided product
$items['dc-registration-waitlist/addtocart/%'] = array(
'title' => 'Add to cart',
'page callback' => '_dc_registration_waitlist_addtocart',
'page arguments' => array(2),
'access callback' => '_dc_registration_waitlist_addtocart_access',
'type' => MENU_CALLBACK,
);
$items['node/%node/waitlist'] = array(
'title' => 'Waitlist',
'page callback' => '_dc_registration_waitlist_manage',
'page arguments' => array(1),
'access callback' => '_dc_registration_waitlist_manage_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
); return $items;
}
function _dc_registration_waitlist_manage($node){
drupal_goto('dc-registration-waitlist/'.$node->field_product_event['und'][0]['product_id']);
}
function _dc_registration_waitlist_manage_access($node){
if(!user_access('manage waitlist')){
return FALSE;
}
if($node->type!='panopoly_event'){
return FALSE;
}
$flags_count =flag_get_counts('commerce_product', $node->field_product_event['und'][0]['product_id']);
if (empty($flags_count['waitlist'])){
return FALSE;
}
return TRUE;
}
function _dc_registration_waitlist_addtocart_access(){
$uid = $GLOBALS['user']->uid;
// need registered useres
if($GLOBALS['user']->uid==0){
drupal_set_message(t('Please login first'));
return FALSE;
}
$flags = flag_get_user_flags('commerce_product', arg(2), $uid);
if($flags){
$flagging = flagging_load($flags['waitlist']->flagging_id);
// flag status is set to promoted
if($flagging->field_flag_status['und'][0]['value']==1) {
return TRUE;
} else {
drupal_set_message(t('You have not been promoted off the waitlist yet'));
return FALSE;
}
}
}
/*
* hook_menu callback
*
* creates the order and redirects to checkout
*
*/
function _dc_registration_waitlist_addtocart($product_id){
global $user;
// if there are no slots available, do not create the order
if(!registration_has_room('commerce_product',$product_id,1)){
drupal_set_message(t('Sorry! There are no spots available.'));
drupal_goto('<front>');
}
// Create the new order in checkout; you might also check first to
// see if your user already has an order to use instead of a new one.
$order = commerce_order_new($user->uid, 'checkout_checkout');
// Save the order to get its ID.
commerce_order_save($order);
// Load whatever product represents the item the customer will be
// paying for and create a line item for it.
$product = commerce_product_load($product_id);
$line_item = commerce_product_line_item_new($product, 1, $order->order_id);
// Save the line item to get its ID.
commerce_line_item_save($line_item);
// Add the line item to the order using fago's rockin' wrapper.
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->commerce_line_items[] = $line_item;
// Save the order again to update its line item reference field.
commerce_order_save($order);
drupal_set_message(t('Product %product-name has been added to your cart.',array('%product-name'=>$product->title)));
// Redirect to the order's checkout form. Obviously, if this were a
// form submit handler, you'd just set $form_state['redirect'].
drupal_goto('checkout/' . $order->order_id);
}
/**
* Implements hook_action_info().
*/
function dc_registration_waitlist_action_info() {
return array(
'_dc_registration_waitlist_change_user_flag_and_email' => array(
'type' => 'user',
'label' => t('Promote & message'),
'configurable' => FALSE,
),
'_dc_registration_waitlist_change_user_flag' => array(
'type' => 'user',
'label' => t('Promote'),
'configurable' => FALSE,
),
'_dc_registration_waitlist_reset_user_flag' => array(
'type' => 'user',
'label' => t('Reset to pending'),
'configurable' => FALSE,
),
'_dc_registration_waitlist_remove_flag' => array(
'type' => 'user',
'label' => t('Remove'),
'configurable' => FALSE,
),
);
}
/*
* action definition
* set flag status to promoted & message
*/
function _dc_registration_waitlist_change_user_flag_and_email(&$user, $context = array()){
_dc_registration_waitlist_save_flag($user,1);
// invoke rules component to send email
rules_invoke_component('rules_dc_registration_waitlist_send_email',$user,commerce_product_load(arg(1)));
}
/*
* action definition
* set flag status to promoted
*/
function _dc_registration_waitlist_change_user_flag(&$user, $context = array()){
_dc_registration_waitlist_save_flag($user,1);
}
/*
* action definition
* set flag status to pending
*/
function _dc_registration_waitlist_reset_user_flag(&$user, $context = array()){
_dc_registration_waitlist_save_flag($user,0);
}
/*
* action definition
* remove this flag
*/
function _dc_registration_waitlist_remove_flag(&$user, $context = array()){
flag('unflag', 'waitlist', arg(1), $user);
}
/*
* update the flag entity
* set the status field to $status
*/
function _dc_registration_waitlist_save_flag($user,$status){
$params = array(
'entity_type' =>'commerce_product',
'entity_id' => arg(1),
'uid' => $user->uid,
);
// get the user's flags
$user_flags = flag_get_user_flags($params['entity_type'], $params['entity_id'], $params['uid']);
$flagging_id = $user_flags['waitlist']->flagging_id;
// load the flagging entity
$flagging = flagging_load($flagging_id);
$flagging->field_flag_status['und'][0]['value']=$status;
//save flagging
$flagging = flagging_save($flagging);
}
/**
* Implements hook_permission().
*/
function dc_registration_waitlist_permission() {
return array(
'manage waitlist' => array(
'title' => t('Manage the waitlist'),
'description' => t('Manages the users waitlist for products'),
),
);
}