-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinapi_tester.module
132 lines (116 loc) · 3.35 KB
/
pinapi_tester.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
<?php
/**
* @file
* Provides a helpful testing block for testing pins
*/
/**
* Implements hook_theme().
*/
function pinapi_tester_theme() {
return array(
'pinapi_tester' => array(
'variables' => array('collection' => NULL, 'pins' => NULL),
'template' => 'pinapi-tester',
'path' => drupal_get_path('module', 'pinapi_tester') . '/theme',
),
);
}
/**
* Implements hook_form_form_id_alter().
*/
function pinapi_tester_form_pinapi_collection_block_form_alter(&$form, &$form_state) {
if ( user_access('use test pins') ) {
$form['pinapi_tester'] = array(
'#type' => 'fieldset',
'#title' => t('Insert Test Pin'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 100,
'#attributes' => array(
'class' => array('fieldset-pinapi-tester'),
),
'pins' => array(
'#theme' => 'pinapi_tester',
'#collection' => $form['collection']['#value'],
'#pins' => pinapi_tester_load_test_pins($form['collection']['#value']->cid),
),
);
}
}
/**
* Implements hook_block_info().
*/
function pinapi_tester_block_info() {
$blocks = array();
foreach ( pinapi_collection_load_all() as $collection ) {
$blocks['tester_' . $collection->cid] = array(
'info' => t('Pin API Collection Tester (@name)', array('@name' => $collection->name)),
'cache' => DRUPAL_NO_CACHE,
);
}
return $blocks;
}
/**
* Implements hook_block_view().
*/
function pinapi_tester_block_view($delta = '') {
list($type, $id) = explode('_', $delta);
$block = array();
if ( isset($type) && isset($id) ) {
switch ( $type ) {
case 'tester':
if ( user_access('use test pins') && $collection = pinapi_collection_load($id) ) {
$pins = pinapi_tester_load_test_pins($collection->cid);
$block['subject'] = t('Pin API Collection Tester (@name)', array('@name' => $collection->name));
$block['content'] = theme('pinapi_tester', array('collection' => $collection, 'pins' => $pins));
}
break;
}
}
return $block;
}
/**
* Load available test pins.
*/
function pinapi_tester_load_test_pins($cid, $limit = 10) {
$result = db_select('pinapi_pin', 'p')
->fields('p', array())
->condition('cid', $cid)
->condition('test', 1)
->condition('status', 1)
->range(0, $limit)
->execute();
$pins = array();
foreach ( $result as $pin ) {
$pins[] = $pin;
}
return $pins;
}
/**
* Implements hook_preprocess_pinapi_tester().
*/
function template_preprocess_pinapi_tester(&$vars) {
$vars['id'] = 'pinapi-tester-' . $vars['collection']->machine_name;
drupal_add_js(drupal_get_path('module', 'pinapi_tester') . '/pinapi_tester.js');
drupal_add_js(array(
'pinapi_tester' => array(
'#' . $vars['id'] => array(
'form' => '#pinapi-collection-block-form-' . $vars['collection']->machine_name,
),
),
), 'setting');
// Gather test pin codes.
$codes = array();
foreach ( $vars['pins'] as $pin ) {
$codes[] = array(
'data' => $pin->code . ' <span class="quantity">x' . $pin->quantity . '</span>',
'data-code' => $pin->code,
);
}
if ( count($codes) > 0 ) {
$vars['codes'] = theme('item_list', array('items' => $codes, 'attributes' => array('class' => array('pinapi-tester-codes', 'clearfix'))));
}
else {
$vars['codes'] = t('No test pins available');
}
}