-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinapi.module
326 lines (293 loc) · 10.9 KB
/
pinapi.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* @file
* Provides an API for Pin functionality.
*/
require_once dirname(__FILE__) . '/includes/core.inc';
require_once dirname(__FILE__) . '/includes/pin.inc';
require_once dirname(__FILE__) . '/includes/collection.inc';
require_once dirname(__FILE__) . '/includes/txn.inc';
require_once dirname(__FILE__) . '/includes/cache.inc';
require_once dirname(__FILE__) . '/includes/test.inc';
/**
* Implements hook_permission().
*/
function pinapi_permission() {
return array(
'administer pin api' => array(
'title' => t('Administer pins'),
'description' => t('Access the pin administration pages.')
),
'use pins' => array(
'title' => t('Use pins'),
'description' => t('Access to use pins.'),
),
'use test pins' => array(
'title' => t('Use test pins'),
'description' => t('Access to use test pins.'),
),
);
}
/**
* Implements hook_menu().
*/
function pinapi_menu() {
$items = array();
$items['admin/pinapi'] = array(
'title' => 'Pins',
'description' => 'Administer your pins.',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file path' => drupal_get_path('module', 'system'),
'file' => 'system.admin.inc',
'weight' => -7,
);
$items['admin/pinapi/collection'] = array(
'title' => 'Collections',
'description' => 'Manage your pin collections.',
'page callback' => 'pinapi_admin_collection_view',
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/collection/add'] = array(
'title' => 'Create Collection',
'page callback' => 'drupal_get_form',
'page arguments' => array('pinapi_form_collection'),
'access arguments' => array('administer pin api'),
'type' => MENU_LOCAL_ACTION,
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/collection/%pinapi_collection/edit'] = array(
'title' => 'Edit collection',
'page callback' => 'drupal_get_form',
'page arguments' => array('pinapi_form_collection', 3),
'access arguments' => array('administer pin api'),
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/collection/%pinapi_collection/delete'] = array(
'title' => 'Delete collection',
'page callback' => 'drupal_get_form',
'page arguments' => array('pinapi_form_collection_delete', 3),
'access arguments' => array('administer pin api'),
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/collection/%pinapi_collection/calculate'] = array(
'title' => 'Calculate collection',
'page callback' => 'drupal_get_form',
'page arguments' => array('pinapi_form_collection_calculate', 3),
'access arguments' => array('administer pin api'),
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/collection/%pinapi_collection/reset'] = array(
'title' => 'Reset collection',
'page callback' => 'drupal_get_form',
'page arguments' => array('pinapi_form_collection_reset', 3),
'access arguments' => array('administer pin api'),
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/collection/%pinapi_collection/clear'] = array(
'title' => 'Clear collection',
'page callback' => 'drupal_get_form',
'page arguments' => array('pinapi_form_collection_clear', 3),
'access arguments' => array('administer pin api'),
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/transaction'] = array(
'title' => 'Transactions',
'description' => 'Manage your pin transactions.',
'page callback' => 'pinapi_admin_transaction_overview',
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/transaction/%pinapi_txn'] = array(
'title' => 'View transaction',
'description' => 'View transaction',
'page callback' => 'pinapi_transaction_view',
'page arguments' => array(3),
'access arguments' => array('administer pin api'),
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/transaction/%pinapi_txn/edit'] = array(
'title' => 'Edit transaction',
'page callback' => 'drupal_get_form',
'page arguments' => array('pinapi_form_transaction', 3),
'access arguments' => array('administer pin api'),
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/transaction/%pinapi_txn/delete'] = array(
'title' => 'Delete transaction',
'description' => 'Delete transaction',
'page callback' => 'drupal_get_form',
'page arguments' => array('pinapi_form_transaction_delete', 3),
'access arguments' => array('administer pin api'),
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/pin'] = array(
'title' => 'Pins',
'description' => 'Manage your pins.',
'page callback' => 'pinapi_admin_pin_overview',
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/pin/%pinapi_pin/edit'] = array(
'title' => 'Edit pin',
'description' => 'Edit pin',
'page callback' => 'drupal_get_form',
'page arguments' => array('pinapi_form_pin', 3),
'access arguments' => array('administer pin api'),
'file' => 'pinapi.admin.inc',
);
$items['admin/pinapi/pin/%pinapi_pin/delete'] = array(
'title' => 'Delete pin',
'description' => 'Delete pin',
'page callback' => 'drupal_get_form',
'page arguments' => array('pinapi_form_pin_delete', 3),
'access arguments' => array('administer pin api'),
'file' => 'pinapi.admin.inc',
);
return $items;
}
/**
* Implements hook_views_api().
*/
function pinapi_views_api() {
return array('api' => 3.0);
}
/**
* Implements hook_block_info().
*/
function pinapi_block_info() {
$blocks = array();
foreach ( pinapi_collection_load_all() as $collection ) {
$blocks['collection_' . $collection->cid] = array(
'info' => t('Pin API Collection Form (@name)', array('@name' => $collection->name)),
'cache' => DRUPAL_NO_CACHE,
);
$blocks['txn-total_' . $collection->cid] = array(
'info' => t('Pin API Transaction Total (@name)', array('@name' => $collection->name)),
'cache' => DRUPAL_NO_CACHE,
);
}
return $blocks;
}
/**
* Implements hook_block_view().
*/
function pinapi_block_view($delta = '') {
list($type, $id) = explode('_', $delta);
$block = array();
if ( isset($type) && isset($id) ) {
switch ( $type ) {
case 'collection':
if ( user_access('use pins') && $collection = pinapi_collection_load($id) ) {
$block['subject'] = t('Pin API Collection Form (@name)', array('@name' => $collection->name));
$block['content'] = drupal_get_form('pinapi_collection_block_form', $collection);
}
break;
case 'txn-total':
if ( user_access('use pins') && $collection = pinapi_collection_load($id) ) {
$block['subject'] = t('Pin API Transaction Total (@name)', array('@name' => $collection->name));
$block['content'] = pinapi_cache_get($GLOBALS['user']->uid, $collection->cid, 'txn_total');
}
break;
}
}
return $block;
}
/**
* Implements hook_forms().
*/
function pinapi_forms() {
return array(
'pinapi_collection_block_form' => array(
'callback' => 'pinapi_collection_block_form',
'callback arguments' => array('pinapi_collection_block_form'),
),
);
}
/**
* Form: Collection Block Form.
*/
function pinapi_collection_block_form($form, &$form_state, $collection) {
$form['code'] = array(
'#type' => 'textfield',
'#title' => t('Code'),
'#default_value' => '',
'#size' => 20,
'#required' => TRUE,
);
$form['collection'] = array(
'#type' => 'value',
'#value' => $collection,
);
$form['actions'] = array(
'#type' => 'actions',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Enter'),
);
$form['#id'] = 'pinapi-collection-block-form-' . $collection->machine_name;
$form['#attributes']['class'][] = 'pinapi-collection-block-form';
return $form;
}
/**
* Form Validation Handler: Collection Block Form.
*/
function pinapi_collection_block_form_validate($form, &$form_state) {
global $user;
if ( !empty($form_state['values']['code']) ) {
// Create a transaction object.
$txn = pinapi_txn($form_state['values']['code'], $form_state['values']['collection']->cid);
// Invoke hook_pinapi_txn with validate operation.
pinapi_invoke('pinapi_txn', 'validate', $txn);
// Show appripriate error message based on transaction status.
switch ( $txn->txn_status ) {
case PINAPI_STATUS_UNAVAILABLE:
form_set_error('code', t('The code you have entered has already been used.'));
watchdog('pinapi', 'Failed pin transaction with @code (already used)', array('@code' => $txn->code), WATCHDOG_NOTICE, l('view user', 'user/' . $txn->uid));
break;
case PINAPI_STATUS_CLOSED:
form_set_error('code', t('Codes are not being accepted at this time.'));
break;
case PINAPI_STATUS_NOT_FOUND:
form_set_error('code', t('The code you have entered is not valid.'));
watchdog('pinapi', 'Failed pin transaction with @code (not found)', array('@code' => $txn->code), WATCHDOG_NOTICE, l('view user', 'user/' . $txn->uid));
break;
}
// Only users with "use test pins" permissions can redeem test pins.
if ( isset($txn->pin->test) && $txn->pin->test && !user_access('use test pins') ) {
form_set_error('code', t('Your account cannot redeem test pins.'));
watchdog('pinapi', 'Failed pin transaction with @code (test pin)', array('@code' => $txn->code), WATCHDOG_WARNING, l('view user', 'user/' . $txn->uid));
}
// Show message if not redeemable.
if ( !$txn->redeemable ) {
// @TODO: Does this actually get to do anything?
form_set_error('code', t('The code you have entered is not redeemable at this time.'));
//watchdog('pinapi', 'Failed pin transaction with @code (not redeemable)', array('@code' => $txn->code), WATCHDOG_NOTICE, l('view user', 'user/' . $txn->uid));
}
// Pass transaction to submit handler.
$form_state['values']['txn'] = $txn;
}
}
/**
* Form Submit Handler: Collection Block Form.
*/
function pinapi_collection_block_form_submit($form, &$form_state) {
global $user;
$txn = $form_state['values']['txn'];
if ( $txn->redeemable ) {
// Finalize and redeem transaction.
pinapi_txn_redeem($txn);
// @TODO: Find a better way to show messages.
//drupal_set_message(t('Congratulations! Your code has been entered successfully.'));
watchdog('pinapi', 'Redeemed pin transaction with @code', array('@code' => $txn->pin->code), WATCHDOG_NOTICE, l('edit', 'admin/pinapi/transaction/' . $txn->txn_id . '/edit'));
// Invoke hook_pinapi_txn with validate operation.
pinapi_invoke('pinapi_txn', 'submit', $txn);
// @TODO: Find a better way to redirect.
//drupal_goto('<front>');
}
}