-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_thumb.module
253 lines (234 loc) · 7.01 KB
/
node_thumb.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
<?php
/**
* Return path to thumbnail directory.
*
* @return
* String. Path to directory with thumbnails.
*/
function node_thumb_directory() {
return file_directory_path() . variable_get('node_thumbs_directory', '');
}
/**
* Return thumb for given node
*
* @param $node
* Node object for which we want to thumbnail
* @return
* Object. Thumbnail for given node.
*/
function node_thumb_load($node) {
$thumbnail = db_fetch_object(db_query('SELECT image FROM {node_thumbs} WHERE nid = %d', $node->nid));
return node_thumb_directory() . '/' . $thumbnail->image;
}
/**
* Save thumb for given node
*
* @param $node
* Node object with thumbnail
* @return
* Object. Thumbnail for given node.
*/
function node_thumb_save($node) {
db_query('INSERT INTO {node_thumbs} (nid, image) VALUES (%d, "%s");', $node->nid, basename($node->thumbnail));
}
/**
* Delete thumb for given node
*
* @param $node
* Node object with thumbnail
* @return
* Object. Thumbnail for given node.
*/
function node_thumb_delete($node) {
db_query('DELETE FROM {node_thumbs} WHERE nid=%d;', $node->nid);
}
/**
* Return all thumbnails
*
* @param $key
* @see file_scan_directory
* @return
* Array of file objects
*/
function node_thumb_all($key = 'filename') {
$options = array();
if (variable_get('node_thumbs_directory', '')) {
$options = file_scan_directory(node_thumb_directory($key), '.*');
}
return $options;
}
/**
* Return promoted thumbnails
*
* @see file_scan_directory()
* @see node_thumb_all()
*/
function node_thumb_promoted($key = 'filename') {
$key = (in_array($key, array('filename', 'basename', 'name')) ? $key : 'filename');
$promoted = (array)variable_get('node_thumbs_promoted', array());
$options = array();
if ($promoted) {
foreach ($promoted as $file) {
$filename = $file;
$basename = basename($file);
$name = substr($basename, 0, strrpos($basename, '.'));
$options[$$key] = new stdClass();
$options[$$key]->filename = $filename;
$options[$$key]->basename = $basename;
$options[$$key]->name = $name;
}
} else {
$options = node_thumb_all();
}
return $options;
}
function node_thumb_form($form_state, $which = 'all', $type = 'radios') {
$files = array();
$options = array();
$default = '';
if ($thumbnail = $form_state->thumbnail) {
$default = $thumbnail;
} elseif ($type == 'checkboxes') {
$default = variable_get('node_thumbs_promoted', array());
}
if ($which == 'all') {
$files = node_thumb_all();
} else {
$files = node_thumb_promoted();
}
foreach ($files as $file) {
$options[$file->filename] = theme('image', $file->filename, check_plain($file->basename), check_plain($file->name));
}
$form = array(
'#type' => $type,
'#title' => t('Proponowane miniaturki.'),
'#options' => $options,
'#default_value' => $default,
'#description' => t('Wybierz miniaturkę najlepiej pasującą do treści wiadomości.'),
'#prefix' => '<div id="node-thumb-gallery">',
'#suffix' => '</div>',
);
return $form;
}
/**
* Builds and returns the thumbnails settings form.
*/
function node_thumb_admin($form_state) {
$form = array();
$form['#after_build'] = array_merge((array)$form['#after_build'], array('node_thumb_add_css'));
$form['directory'] = array(
'#type' => 'textfield',
'#title' => t('Katalog'),
'#description' => t('Ścieżka do katalogu z obrazkami powiązanymi z %directory.', array('%directory' => file_directory_path())),
'#default_value' => variable_get('node_thumbs_directory', '')
);
$form['promoted'] = node_thumb_form(NULL, 'all', 'checkboxes');
$form['content_types'] = array(
'#type' => 'fieldset',
'#title' => t('Typ zawartości')
);
$form['content_types']['nodes'] = array(
'#type' => 'checkboxes',
'#title' => t('Typ zawartości'),
'#default_value' => variable_get('node_thumbs_nodes', array()),
'#options' => array_map('check_plain', node_get_types('names')),
'#description' => t('Wybierz z listy.'),
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Zapisz.'));
return $form;
}
/**
* Validate the node thumbnails settings form.
*/
function node_thumb_admin_validate($form, &$form_state) {
$directory = file_directory_path() . $form_state['values']['directory'];
if (! file_check_directory($directory)) {
form_set_error('directory', t('Katalog %directory nie istnieje.', array('%directory' => $directory)));
}
}
/**
* Handle submission of the thumbnail settings form.
*/
function node_thumb_admin_submit($form, &$form_state) {
function not_empty($val) { return ! empty($val); };
variable_set('node_thumbs_directory', $form_state['values']['directory']);
variable_set('node_thumbs_promoted', array_filter($form_state['values']['promoted'], 'not_empty'));
variable_set('node_thumbs_nodes', $form_state['values']['nodes']);
drupal_set_message(t('Twoje ustawienia zostały zapisane.'));
}
/**
* Adding stylesheet to form
*/
function node_thumb_add_css($form_element, &$form_state) {
drupal_add_css(drupal_get_path('module', 'node_thumb') .'/node_thumb.css');
return $form_element;
}
/**
* Implementation of hook_form_alter(). Append appropriate fields to node form.
*/
function node_thumb_form_alter(&$form, $form_state, $form_id) {
$node = $form['#node'];
$nodes = variable_get('node_thumbs_nodes', array());
if (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id && $nodes[$node->type] === $node->type) {
$form['node_thumb'] = array(
'#type' => 'fieldset',
'#title' => t('Dołącz miniaturkę do treści.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE
);
if ($node->nid) {
$form['node_thumb']['thumbnail'] = node_thumb_form($node, 'all', 'radios');
} else {
$form['node_thumb']['thumbnail'] = node_thumb_form($node, 'promoted', 'radios');
}
$form['#after_build'] = array_merge((array)$form['#after_build'], array('node_thumb_add_css'));
}
}
/**
* Implementation of hook_nodeapi().
*
* Appends thumbnail to node.
*/
function node_thumb_nodeapi(&$node, $op) {
switch ($op) {
case 'insert': {
node_thumb_save($node);
break;
}
case 'update': {
node_thumb_delete($node);
node_thumb_save($node);
break;
}
case 'delete': {
node_thumb_delete($node);
break;
}
case 'load': {
$node->thumbnail = node_thumb_load($node);
break;
}
}
}
/**
* Implementation of hook_perm().
*/
function node_thumb_perm() {
return array('node thumb admin');
}
/**
* Implementation of hook_menu().
*/
function node_thumb_menu() {
$items['admin/settings/node_thumb'] = array(
'title' => t('Powiązane miniaturki.'),
'description' => t('Ustaw miniaturkę.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('node_thumb_admin'),
'access callback' => 'user_access',
'access arguments' => array('node thumb admin'),
'type' => MENU_NORMAL_ITEM
);
return $items;
}
?>