-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtopspin.module
More file actions
619 lines (520 loc) · 18.2 KB
/
topspin.module
File metadata and controls
619 lines (520 loc) · 18.2 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
<?php
// $Id: $
/**
*
* @file
* Integration with a TopSpin Media account. API for communication with their API, a default store view to display offers from TopSpin and offers provided as blocks.
*
* - Connection API.
* - A store page.
* - A store block.
* - Basic configurations.
* - TopSpin offers as blocks.
*
*/
/**
* Implementation of hook_perm().
*/
function topspin_perm() {
return array(
'access topspin store',
'access topspin widgets',
'administer topspin'
);
}
/**
* Implementation of hook_menu().
*/
function topspin_menu() {
$items = array();
$items['admin/settings/topspin'] = array(
'title' => 'TopSpin',
'description' => t('Administer the TopSpin Media configuration.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('topspin_settings_form'),
'access arguments' => array('administer topspin'),
'file' => 'topspin.admin.inc',
);
// Only include a store page if a path is configured.
$store_path = variable_get('topspin_store_path', '');
if ($store_path != '') {
$items[$store_path] = array(
'title' => variable_get('topspin_store_title', 'Store'),
'page callback' => 'topspin_store_view',
'access arguments' => array('access topspin store'),
);
}
return $items;
}
/**
* Implementation of hook_theme().
*/
function topspin_theme($existing, $type, $theme, $path) {
return array(
'topspin_grid' => array(
'arguments' => array('result' => NULL, 'grid_size' => NULL),
),
'topspin_offer' => array(
'arguments' => array('offer' => NULL),
),
'topspin_offer_buy_button' => array(
'arguments' => array('offer' => NULL),
),
'topspin_offer_email_for_media' => array(
'arguments' => array('offer' => NULL),
),
'topspin_offer_fb_for_media' => array(
'arguments' => array('offer' => NULL),
),
'topspin_offer_bundle_widget' => array(
'arguments' => array('offer' => NULL),
),
'topspin_offer_single_track_player_widget' => array(
'arguments' => array('offer' => NULL),
),
'topspin_offer_block' => array(
'arguments' => array('delta' => NULL),
),
);
}
function topspin_init() {
$topspin_settings = array(
'topspin' => array(
'showEmptyCart' => variable_get('topspin_show_empty_cart', 1),
),
);
drupal_add_js($topspin_settings, 'setting');
drupal_add_js('Drupal.behaviors.topspin = function(context) {
if (Drupal.settings.topspin.showEmptyCart && !("admin" in Drupal) && TSCart) {
TSCart.showWhenEmpty = true;
}
};
', 'inline');
}
/**
* Implementation of hook_block().
*/
function topspin_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks = array();
// Enabled offers as blocks.
$enabled_offers = topspin_enabled_offer_blocks();
foreach ($enabled_offers as $index => $offer) {
$blocks[$index]['info'] = t('TopSpin - !offer', array('!offer' => check_plain($offer->name)));
}
// Default store block.
$blocks['store']['info'] = t('TopSpin - Store');
// Social media links block.
$blocks['social_media']['info'] = t('Social Media links');
// Generic logos block.
$blocks['generic_logos']['info'] = t('Generic logos');
return $blocks;
case 'configure':
switch ($delta) {
// Allow the defaults configured in admin/settings/topspin to be overwritten for the block display.
case 'store':
$form['grid_size'] = array(
'#type' => 'select',
'#title' => t('Total Per Row'),
'#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)),
'#description' => t('The total number of offers to display per row.'),
'#default_value' => variable_get('topspin_store_block_grid_size', variable_get('topspin_store_grid_size', 4)),
'#required' => TRUE,
);
$form['page_limit'] = array(
'#type' => 'select',
'#title' => t('Total Per Page'),
'#options' => drupal_map_assoc(array(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)),
'#description' => t('The total number of offers to display per page.'),
'#default_value' => variable_get('topspin_store_block_page_limit', variable_get('topspin_store_page_limit', 4)),
'#required' => TRUE,
);
$form['product_type'] = array(
'#type' => 'select',
'#title' => t('Product Type'),
'#description' => t('Which product type to display.'),
'#options' => array(
'' => t('All'),
'image' => t('Image'),
'video' => t('Video'),
'track' => t('Track'),
'album' => t('Album'),
'package' => t('Package'),
'other_media' => t('Other media'),
'merchandise' => t('Merchandise'),
),
'#default_value' => variable_get('topspin_store_block_filter_product_type', variable_get('topspin_store_filter_product_type', '')),
);
$form['sort_column'] = array(
'#type' => 'select',
'#title' => t('Sorting Field'),
'#description' => t('Which field to sort the offers by.'),
'#options' => array(
'created_at' => t('Created at'),
'name' => t('Name'),
'offer_type' => t('Offer type'),
'artist_id' => t('Artist ID'),
),
'#default_value' => variable_get('topspin_store_block_sort_column', variable_get('topspin_store_sort_column', 'created_at')),
);
$form['sort_direction'] = array(
'#type' => 'select',
'#title' => t('Sort Direction'),
'#description' => t('Which direction to sort the offers.'),
'#options' => array(
'asc' => t('Ascending'),
'desc' => t('Descending'),
),
'#default_value' => variable_get('topspin_store_block_sort_direction', variable_get('topspin_store_sort_direction', 'asc')),
);
case 'social_media':
case 'generic_logos':
$form['body'] = array(
'#type' => 'textarea',
'#title' => t('Block body'),
'#description' => t('Tokens available: !base-path, !theme-path, !current-page-url, !current-page-path, !current-nid'),
'#default_value' => variable_get('topspin_store_block_'. $delta .'_body', ''),
);
break;
}
return $form;
case 'save':
switch ($delta) {
case 'store':
variable_set('topspin_store_block_grid_size', $edit['grid_size']);
variable_set('topspin_store_block_page_limit', $edit['page_limit']);
variable_set('topspin_store_block_filter_product_type', $edit['product_type']);
variable_set('topspin_store_block_sort_column', $edit['sort_column']);
variable_set('topspin_store_block_sort_direction', $edit['sort_direction']);
break;
case 'social_media':
case 'generic_logos':
variable_set('topspin_store_block_'. $delta .'_body', $edit['body']);
break;
}
break;
case 'view':
switch ($delta) {
case 'store':
if (user_access('access topspin store')) {
$block['content'] = topspin_store_view();
}
break;
case 'social_media':
case 'generic_logos':
$block['content'] = t(variable_get('topspin_store_block_'. $delta .'_body', ''),
array('!base-path' => base_path(), '!theme-path' => base_path() . drupal_get_path('theme', $GLOBALS['theme_key']),
'!current-page-url' => url($_GET['q'], array('absolute' => TRUE)),
'!current-page-path' => $_GET['q'],
'!current-nid' => is_numeric(arg(1)) ? arg(1) : ''));
break;
default:
if (user_access('access topspin widgets')) {
$block['content'] = theme('topspin_offer_block', $delta);
}
break;
}
return $block;
}
}
/**
* Helper function to retrieve or set an array of enabled offer blocks.
*
* @param array
* - Offers.
*
* @return array
* - Enabled offers.
*/
function topspin_enabled_offer_blocks($offers = NULL) {
if (isset($offers)) {
// Don't need to save all offer data - pick out important stuff.
$offers_trimmed = array();
foreach ($offers as $index => $offer) {
$offers_trimmed[$index]->url = $offer->url;
$offers_trimmed[$index]->artist_name = $offer->artist_name;
$offers_trimmed[$index]->artist_id = $offer->artist_id;
$offers_trimmed[$index]->name = $offer->name;
}
variable_set('topspin_enabled_offer_blocks', $offers_trimmed);
} else {
return variable_get('topspin_enabled_offer_blocks', array());
}
}
/**
* Helper function to retrieve an offer object by an offer_id value from an array of offers.
*
* @param int
* - Offer id.
*
* @param array
* - An array of offers.
*
* @return object
* - An offer object.
*/
function topspin_get_offer_by_id($id, $offers) {
foreach ($offers as $index => $offer) {
if ($id == $offer->id) {
return $offer;
}
}
}
/**
* A helper function to parse a querystring to a struct.
*
* @param string
* - Querystring.
*
* @return struct
* - Structured array of parameters.
*/
function topspin_parse_querystring($querystring) {
if (!$querystring) {
return array();
}
$vars = explode('&', str_replace('&', '&', $querystring));
$items = array();
foreach ($vars as $var) {
$item = explode('=', $var);
list($name, $value) = $item;
if (trim(name)) {
$items[trim($name)] = trim($value);
}
}
return $items;
}
/**
* Request information from the TopSpin API.
*
* @param string
* - The API method we are requesting. Can be just the API name (folder path in the URL i.e. offers, order/update, orders, sku/update, artist, etc..) or a full URL.
*
* @param struct
* - A structured array of parameters to pass to the API.
*
* @return struct
* - A structured array as a response from the API.
*/
function topspin_request($method = 'offers', $parameters = array()) {
$key = variable_get('topspin_api_key', '');
$user = variable_get('topspin_api_user', '');
// Basic error control on credentials.
if (!$key || !$user) {
$error = t('TopSpin is not configured properly. Please make sure your credentials are correct.');
watchdog('topspin', $error);
if (user_access('administer topspin')) {
drupal_set_message($error, 'error');
}
return array('error' => $error);
}
// Build the querystring for the request.
$query_string = '';
foreach ($parameters as $name => $value) {
$query_string .= drupal_urlencode($name) .'='. drupal_urlencode($value) .'&';
}
// Build the REST URL if one is not provided.
if (!strpos($method, '://')) {
$url = variable_get('topspin_api_url', 'https://app.topspin.net/api/v1') .'/'. $method;
} else {
$url = $method;
}
$request = $url . ($query_string ? '?'. $query_string : '');
if (variable_get('topspin_debug', FALSE)) {
drupal_set_message('<pre>Sending TopSpin Request >>> '. print_r($request, 1) .'</pre>');
}
// Start the request.
$curl = curl_init($request);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $user .':'. $key);
$output = curl_exec($curl);
$info = curl_getinfo($curl);
// Handle errors.
if ($info['http_code'] !== 200) {
watchdog('topspin', t('A request failed with the following response: <pre>!response</pre>.', array('!response' => print_r($info, 1))));
return array('error' => $info['http_code'], 'response' => $info);
}
curl_close($curl);
return json_decode($output);
}
/**
* General store view.
*
* @param struct
* - Structured array of parameters to pass through the API. Using this will void the default configured settings.
*
* @param int
* - The amount of items to display in a column of the grid view.
*
* @return string
* - HTML formatted output.
*/
function topspin_store_view($parameters = array(), $grid_size = 4) {
// If no parameters provided, use default configurations.
if (empty($parameters)) {
// Check for advanced filter mode.
if (variable_get('topspin_store_search_parameters', '')) {
$parameters = topspin_parse_querystring(variable_get('topspin_store_search_parameters', ''));
}
// Build configured parameters.
else {
$product_type = variable_get('topspin_store_filter_product_type', '');
$sort_column = variable_get('topspin_store_sort_column', 'created_at');
$sort_direction = variable_get('topspin_store_sort_direction', 'asc');
if ($product_type) {
$parameters['product_type'] = $product_type;
}
$parameters['sortcol'] = $sort_column;
$parameters['sortdir'] = $sort_direction;
$parameters['per_page'] = variable_get('topspin_store_page_limit', 16);
}
// Always populate a page value unless already populated.
if (!$parameters['page']) {
$parameters['page'] = ($_GET['page'] ? $_GET['page'] + 1 : 1);
}
}
$result = topspin_request('offers', $parameters);
$grid_size = variable_get('topspin_store_grid_size', 4);
return theme('topspin_grid', $result, $grid_size);
}
/**
* Store grid themed output.
*
* @param struct
* - A raw structured array returned by the TopSpin API.
*
* @param int
* - Size of the grid.
*/
function theme_topspin_grid($result, $grid_size = 4) {
drupal_add_css(drupal_get_path('module', 'topspin') .'/topspin.css');
$output = '';
$row_output = '';
$cell_output = '';
// Build main product grid.
$cell_count = 0;
foreach ($result->offers as $index => $offer) {
++$cell_count;
$cell_output .= theme('topspin_offer', $offer);
// End the row & reset cell count - we've either hit the max cells-per-row or this is the last cell of data.
if ($cell_count == $grid_size || $index == (count($result->offers) -1)) {
$row_output .= '<div class="topspin-row">'. $cell_output .'</div>';
// Reset cell output.
$cell_output = '';
$cell_count = 0;
}
}
// Add row data to output.
$output .= $row_output;
// Add pager controls to output, if needed.
if ($result->total_pages > 1) {
// Ugly pager code lifted from pager_query() function in pager.inc.
global $pager_page_array, $pager_total, $pager_total_items;
$page = isset($_GET['page']) ? $_GET['page'] : '';
$pager_page_array = explode(',', $page);
$limit = $result->per_page;
// If there's more than 1 pager per page (ie: blocks), need to make sure element is unique.
static $element = -1;
++$element;
// We calculate the total of pages as ceil(items / limit).
$pager_total_items[$element] = $result->total_entries;
$pager_total[$element] = ceil($pager_total_items[$element] / $limit);
$pager_page_array[$element] = max(0, min((int)$pager_page_array[$element], ((int)$pager_total[$element]) - 1));
$output .= theme('pager', array(), $limit, $element, array(), $result->total_pages);
}
return $output;
}
/**
* Offer container themed output.
*
* @param object
* - Offer object.
*/
function theme_topspin_offer($offer) {
$output = '<div class="topspin-wrapper '. $offer->offer_type .'">';
$output .= theme('topspin_offer_'. $offer->offer_type, $offer);
$output .= '</div>';
return $output;
}
/**
* Buy Button Offer themed output.
*
* @param object
* - Offer object.
*/
function theme_topspin_offer_buy_button($offer) {
// Grab a thumbnail view of the first item if it is available and display it.
if (!empty($offer->campaign->product->cover_art_image)) {
$url = $offer->campaign->product->cover_art_image->medium_url;
$alt = $offer->campaign->product->cover_art_image->name;
$output .= '<div class="topspin-thumbnail"><img src="'. $url .'" alt="'. $alt .'" title="'. $alt .'" /></div>';
}
$output .= '<div class="topspin-name">'. check_plain($offer->name) .'</div>';
//$output .= '<div class="topspin-description">'. check_plain($offer->description) .'</div>';
$output .= '<div class="topspin-price">$'. $offer->price .' '. $offer->currency .'</div>';
$output .= '<div class="topspin-embed">'. $offer->embed_code .'</div>';
return $output;
}
/**
* Email For Media Offer themed output.
*
* @param object
* - Offer object.
*/
function theme_topspin_offer_email_for_media($offer) {
$output = '<div class="topspin-name">'. check_plain($offer->name) .'</div>';
$output .= '<div class="topspin-embed">'. $offer->embed_code .'</div>';
return $output;
}
/**
* Facebook For Media Offer themed output.
*
* @param object
* - Offer object.
*/
function theme_topspin_offer_fb_for_media($offer) {
$output = '<div class="topspin-name">'. check_plain($offer->name) .'</div>';
$output .= '<div class="topspin-embed">'. $offer->embed_code .'</div>';
return $output;
}
/**
* Bundle Widget themed output.
*
* @param object
* - Offer object.
*/
function theme_topspin_offer_bundle_widget($offer) {
$output = '<div class="topspin-name">'. check_plain($offer->name) .'</div>';
$output .= '<div class="topspin-embed">'. $offer->embed_code .'</div>';
return $output;
}
/**
* Single Track Player Widget Offer themed output.
*
* @param object
* - Offer object.
*/
function theme_topspin_offer_single_track_player_widget($offer) {
$output = '<div class="topspin-name">'. check_plain($offer->name) .'</div>';
$output .= '<div class="topspin-embed">'. $offer->embed_code .'</div>';
return $output;
}
/**
* Offer block themed output.
*
* @param string
* - Block delta, which is actually the offer id.
*/
function theme_topspin_offer_block($delta) {
$output = '';
// Retrieve the individual offer.
$saved_offer = topspin_get_offer_by_id($delta, topspin_enabled_offer_blocks());
$offer = topspin_request($saved_offer->url);
if (is_object($offer)) {
$output .= $offer->embed_code;
}
return $output;
}