-
Notifications
You must be signed in to change notification settings - Fork 0
/
banner.module
221 lines (196 loc) · 5.84 KB
/
banner.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
<?php
/**
* @file provides a banner node type and block to display banners.
* The banners is display based on there type, so one or
* more may be displayed at the same time.
*/
define('BANNER_SETTINGS_TYPE', 'banner_types');
define('BANNER_ACTIVE_TYPES', 'banner_active_types');
/**
* Implementation of hook_menu
*/
function banner_menu() {
$items['admin/content/banner'] = array(
'title' => 'Banner',
'description' => 'Change how banners behave',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'banner_admin_settings'
),
'access arguments' => array( 'administer site configuration' ),
'type' => MENU_NORMAL_ITEM,
'file' => 'banner.admin.inc',
);
// AHAH callback for adding more types
$items['admin/content/banner/js'] = array(
'title' => 'Javascript Banner Type Form',
'page callback' => 'banner_types_js',
'access arguments' => array( 'administer site configuration' ),
'type' => MENU_CALLBACK,
'file' => 'banner.admin.inc',
);
return $items;
}
/**
* Implementation of hook_perm().
*/
function banner_perm() {
return array('create banner', 'edit banner', 'delete banner');
}
/**
* Implementation of hook_access
*/
function banner_access($op, $node, $account) {
switch ($op) {
case 'create':
return user_access('create banner', $account);
break;
case 'update':
return user_access('edit banner', $account);
break;
case 'delete':
return user_access('delete banner', $account);
break;
}
}
/**
* Implementation of hook_nodeapi
*
* Makes sure that only banners of types that exists are shown
* by adding them to BANNER_ACTIVE_TYPES array.
*
*/
function banner_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($node->type == 'banner') {
switch ($op) {
case "insert":
case "update":
case "delete":
_banner_update_active_types($op, $node->field_banner_type[0]['value']);
break;
}
}
}
/**
* This function updates the array contain banner types, where one or more
* banner exists. It's used to ensure that a banner type without any banner's
* are NOT randomly selected.
*
* @param string $op
* @param string $value
*/
function _banner_update_active_types($op, $value) {
switch ($op) {
case 'insert':
$types = variable_get(BANNER_SETTINGS_TYPE, array());
$active_types = variable_get(BANNER_ACTIVE_TYPES, array());
if (!array_key_exists($value, $active_types)) {
$active_types[$value] = $types[$value];
}
variable_set(BANNER_ACTIVE_TYPES, $active_types);
break;
case 'update':
_banner_update_active_types('insert', $value);
$types = variable_get(BANNER_SETTINGS_TYPE, array());
foreach ($types as $type => $number) {
_banner_update_active_types('delete', $type);
}
break;
case 'delete':
if (banner_number_of_banners($value) == 0) {
$active_types = variable_get(BANNER_ACTIVE_TYPES, array());
unset($active_types[$value]);
variable_set(BANNER_ACTIVE_TYPES, $active_types);
}
break;
}
}
/**
* Find the number of occurences of a banner type in the database.
*
* @param string $type
* @return int
*/
function banner_number_of_banners($type) {
$result = db_query('SELECT count(*) AS count
FROM {content_type_banner}
WHERE field_banner_type_value = \'%s\'', $type);
$result = db_fetch_object($result);
return $result->count;
}
/**
* Implementation of hook_block().
*/
function banner_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case "list":
$blocks[0]['info'] = t('Random banner');
$blocks[0]['cache'] = BLOCK_NO_CACHE;
return $blocks;
case view:
$nodes = _banner_load_random_nodes();
$banners = array();
foreach ($nodes as $node) {
$banners[] = array('url' => $node->field_url[0]['value'],
'img' => $node->field_banner_upload[0]['filepath'],
'rel' => ($node->field_new_window[0]['value'] == 1 ? 'external' : ''),
'alt' => $node->title);
}
$type = str_replace(' ', '-', strtolower($node->field_banner_type[0]['value']));
$block['content'] = theme('banner', $banners, $type);
return $block;
}
}
/**
* Implmentation of hook_theme. Make 'banner' theme function visible.
*
* @return array
*/
function banner_theme() {
return array(
'banner' => array(
'arguments' => array('type' => NULL),
),
);
}
/**
* Theme random banner block content.
*
* @param array $banners
* @return string
*/
function theme_banner($banners, $type) {
$count = 0;
$output = '<div class="banner-random '. $type .'">';
foreach ($banners as $banner) {
if (isset($banner['url'])) {
$output .= '<div id="banner_'. $count .'" class="random-banner"><a href="'. $banner['url'] .'" rel="'. $banner['rel'] .'"><img alt="'. $banner['alt'] .'" src="/'. $banner['img'] .'" /></a></div>';
}
else {
$output .= '<div id="banner_'. $count .'" class="random-banner"><img alt="'. $banner['alt'] .'" src="/'. $banner['img'] .'" /></div>';
}
$count++;
}
$output .= '</div>';
// Add javascript
drupal_add_js(drupal_get_path('module', 'banner') . '/js/banner.js');
return $output;
}
/**
* Finds random banner node.
*/
function _banner_load_random_nodes() {
// Random select type
$types = variable_get(BANNER_ACTIVE_TYPES, array());
$text = array_rand($types);
// Load random banner, based on type
$nodes = array();
$result = db_query('SELECT nid
FROM {content_type_banner}
WHERE field_banner_type_value = \'%s\'
ORDER BY RAND() LIMIT 0,%d', $text, $types[$text]);
while ($data = db_fetch_object($result)) {
$nodes[] = node_load($data->nid);
}
return $nodes;
}