/[drupal]/contributions/modules/recent_blocks/recent_blocks.module
ViewVC logotype

Contents of /contributions/modules/recent_blocks/recent_blocks.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.10 - (show annotations) (download) (as text)
Wed Aug 29 21:44:36 2007 UTC (2 years, 2 months ago) by cotto
Branch: MAIN
CVS Tags: DRUPAL-5--1-3, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.9: +21 -7 lines
File MIME type: text/x-php
username display, fix for bug 140200 (unpublished comments displayed, feature #100125 (teaser display)
1 <?php
2 // $Id$
3
4 function recent_blocks_perm() {
5 return array('administer recent blocks');
6 }
7
8 function recent_blocks_menu($may_cache) {
9 global $user;
10 $items = array();
11
12 if ($may_cache) {
13 $items[] = array(
14 'path' => 'admin/build/recent_blocks/add',
15 'title' => t('Add a new block'),
16 'access' => user_access('administer recent blocks'),
17 'description' => t('add blocks for specific content types'),
18 'callback' => 'drupal_get_form',
19 'callback arguments' => array('_recent_blocks_add'),
20 'type' => MENU_NORMAL_ITEM,
21 );
22 $items[] = array(
23 'path' => 'admin/build/recent_blocks/delete',
24 'title' => t('Delete a block'),
25 'access' => user_access('administer recent blocks'),
26 'description' => t('remove blocks for specific content types'),
27 'callback' => 'drupal_get_form',
28 'callback arguments' => array('_recent_blocks_delete'),
29 'type' => MENU_NORMAL_ITEM,
30 );
31 $items[] = array(
32 'path' => 'admin/build/recent_blocks/edit',
33 'title' => t('Edit a block'),
34 'access' => user_access('administer recent blocks'),
35 'description' => t('edit blocks for specific content types'),
36 'callback' => 'recent_blocks_edit_page',
37 'type' => MENU_NORMAL_ITEM,
38 );
39 $items[] = array(
40 'path' => 'admin/build/recent_blocks',
41 'title' => t('Recent Blocks'),
42 'description' => t('Administer recent blocks'),
43 'access' => user_access('administer recent blocks'),
44 'callback' => '_recent_blocks_admin',
45 'type' => MENU_NORMAL_ITEM,
46 );
47 }
48 return $items;
49 }
50
51 function _recent_blocks_get_default_settings($delta = 0) {
52 return array(
53 'subject' => t("Recent block @delta", array('@delta' => $delta)),
54 'info' => t("Recent block @delta", array('@delta' => $delta)),
55 'items_no' => 5,
56 'types' => array_keys(node_get_types()),
57 'url_href' => '',
58 'url_text' => '',
59 'comments_only' => '0',
60 'mode_co_show' => array('mark', 'comment date'),
61 'mode_full_show' => array('mark', 'node date', 'comments number'),
62 'mode_full_wow_comments' => 'with or without',
63 'mode_full_sort' => 'last_change',
64 'username_show' => true,
65 );
66 }
67
68 function recent_blocks_block($op = 'list', $delta = 0, $edit = array()) {
69 $block_deltas = variable_get('recent_blocks_deltas', array('0' => TRUE));
70 $settings = variable_get("recent_blocks_$delta", _recent_blocks_get_default_settings($delta));
71
72 switch ($op) {
73 case 'list':
74 foreach (array_keys($block_deltas) as $delta) {
75 $settings = variable_get("recent_blocks_$delta", _recent_blocks_get_default_settings($delta));
76 $blocks[$delta]['info'] = $settings['info'];
77 }
78 return $blocks;
79
80 case 'view':
81 $block['subject'] = $settings['subject'];
82 $block['content'] = _recent_blocks_block($settings);
83 return $block;
84 break;
85 }
86 }
87
88 function _recent_blocks_block($s) {
89 global $user;
90 $types = $s['types'];
91 $common_where = "n.status = 1 AND n.type IN ('". implode("', '", $types) ."')";
92 switch ($s['mode_full_wow_comments']) {
93 case 'with': $wow_comments = 'comment_count != 0'; break;
94 case 'without': $wow_comments = 'comment_count = 0'; break;
95 default: $wow_comments = '1=1'; break;
96 }
97 $items = array();
98
99 if ($s['comments_only']) {
100 // TODO: when bug with empty comment subject is fixed, remove c.comment
101 $sql = "SELECT n.nid, c.cid, c.subject, c.comment, c.timestamp, c.format, u.name, u.uid".
102 " FROM {node} n LEFT JOIN {comments} c USING(nid)".
103 " INNER JOIN {users} u ON n.uid = u.uid ".
104 " WHERE $common_where AND c.cid IS NOT NULL AND c.status = 0". // c.cid is null iff the node has no comment
105 " ORDER BY c.timestamp DESC";
106 $result = db_query_range(db_rewrite_sql($sql), 0, (int) $s['items_no']);
107
108 while ($comment = db_fetch_object($result)) {
109 if (($subject = trim($comment->subject)) == '') {
110 $subject = truncate_utf8(decode_entities(strip_tags(check_markup($comment->comment, $comment->format, FALSE))), 29, TRUE);
111 }
112 $items[] = l($subject, "node/$comment->nid", NULL, NULL, 'comment-'. $comment->cid) .
113 ($s['mode_co_show']['mark'] ? ' '. theme('mark', node_mark($comment->nid, $comment->timestamp)) : '') .
114 ($s['username_show'] ? " (".l($comment->name,'user/'.$comment->uid).')' : '').
115 ($s['mode_co_show']['comment date'] ? '<br />'. t('%time ago', array('%time' => format_interval(time() - $comment->timestamp))) : '');
116 }
117 }
118 else {
119 $sql = "SELECT n.nid, n.title, r.teaser, n.uid, n.changed, n.created, l.last_comment_timestamp, GREATEST(n.changed, l.last_comment_timestamp) AS last_change, l.comment_count, u.name".
120 " FROM {node} n ".
121 " INNER JOIN {node_comment_statistics} l ON n.nid = l.nid ".
122 " INNER JOIN {users} u ON n.uid = u.uid ".
123 " INNER JOIN {node_revisions} r ON n.vid = r.vid ".
124 " WHERE n.nid = l.nid AND $common_where AND $wow_comments".
125 " ORDER BY $s[mode_full_sort] DESC";
126 $result = db_query_range(db_rewrite_sql($sql), 0, (int) $s['items_no']);
127
128 $show = $s['mode_full_show'];
129 while ($node = db_fetch_object($result)) {
130
131 $comments_no = '';
132 if ($show['comments number'] && $node->comment_count) {
133 $comments_no = format_plural($node->comment_count, '1 reply', '@count replies');
134 if (module_exists('comment') && $new = comment_num_new($node->nid)) {
135 $comments_no .= ' '. l(format_plural($new, '1 new', '@count new'), "node/$node->nid", NULL, NULL, 'new');
136 }
137 }
138
139 $item = l($node->title, "node/$node->nid") .
140 ($show['teaser'] ? ' '. t(strip_tags($node->teaser,'<p><b><i>')) : '') .
141 ($show['mark'] ? ' '. theme('mark', node_mark($node->nid, $node->changed)) : '') .
142 ($s['username_show'] ? " (".l($node->name,'user/'.$node->uid).')' : '').
143 ($show['node date'] ? '<br />'. t('%time ago', array('%time' => format_interval(time() - $node->$s['mode_full_sort']))) : '') .
144 ($comments_no ? '<br />'. $comments_no : '') .
145 ($comments_no && $show['comment date'] ? '<br />'. t('%time ago', array('%time' => format_interval(time() - $node->last_comment_timestamp))) : '');
146
147 $items[] = $item;
148 }
149 }
150
151 if (empty($items)) {
152 return;
153 }
154
155 $content = theme('item_list', $items);
156 if (trim($s['url_href']) != '') {
157 $content .= "<div class='more-link'><a href='$s[url_href]'>$s[url_text]</a></div>";
158 }
159
160 return $content;
161 }
162
163 function _recent_blocks_add() {
164 $block_deltas = variable_get('recent_blocks_deltas', array('0' => TRUE));
165 for ($i = 0; isset($block_deltas[$i]); $i++) { } // Find first free delta (block id)
166 $block_deltas[$i] = TRUE;
167 variable_set('recent_blocks_deltas', $block_deltas);
168 drupal_set_message(t("Added new block - 'Recent block %delta'", array('%delta' => $i)));
169 drupal_goto('admin/build/recent_blocks');
170 }
171
172 function _recent_blocks_delete($delta) {
173 $block_deltas = variable_get('recent_blocks_deltas', array('0' => TRUE));
174 if (!isset($block_deltas[$delta])) {
175 drupal_set_message(t('Recent block %delta not found', array('%delta' => $delta)), 'error');
176 drupal_goto('admin/build/recent_blocks');
177 }
178 unset($block_deltas[$delta]);
179 variable_set('recent_blocks_deltas', $block_deltas);
180 variable_del("recent_blocks_$delta");
181 drupal_set_message(t("Deleted recent block %delta", array('%delta' => $delta)));
182 drupal_goto('admin/build/recent_blocks');
183 }
184
185 function recent_blocks_edit_form($delta) {
186
187 $block_deltas = variable_get('recent_blocks_deltas', array('0' => TRUE));
188 $types = array_keys(node_get_types());
189 $settings = variable_get("recent_blocks_$delta", _recent_blocks_get_default_settings($delta));
190
191 #change types to something the forms API can deal with
192 #types is in the form 0 => book
193 foreach ($types as $t_key => $t_val) {
194 #settings[types] is book => book
195 if (isset($settings['types'][$t_val])) {
196 $settings['types'][$t_val] = $t_key;
197 }
198 }
199
200 if (!isset($block_deltas[$delta])) {
201 drupal_set_message(t('Recent block %delta not found', array('%delta' => $delta)), 'error');
202 drupal_goto('admin/build/recent_blocks');
203 }
204 if (!isset($block_deltas[$delta])) {
205 drupal_set_message(t("You're requesting a non-existing block, changes won't be saved."), 'error');
206 return;
207 }
208 $form["recent_blocks_$delta"]["general"] = array(
209 '#type' => 'fieldset',
210 '#title' => t('Common settings'),
211 '#collapsible' => TRUE,
212 '#collapsed' => FALSE,
213 );
214 $form["recent_blocks_$delta"]["delta"] = array(
215 '#type' => 'hidden',
216 '#value' => "$delta",
217 );
218 $form["recent_blocks_$delta"]["general"]["general_description"] = array(
219 '#type' => 'markup',
220 '#value' => '<p>'.t('These are general options, applicable to all work modes.').'</p>',
221 );
222 $form["recent_blocks_$delta"]["general"]["subject"] = array(
223 '#type' => 'textfield',
224 '#title' => t('Block subject (title)'),
225 '#description' => t('This is block title to be displayed to the user'),
226 '#default_value' => $settings["subject"],
227 );
228 $form["recent_blocks_$delta"]["general"]["info"] = array(
229 '#type' => 'textfield',
230 '#title' => t('Block information'),
231 '#description' => t('This is text to be displayed to the admin on blocks overview page'),
232 '#default_value' => $settings["info"],
233 );
234 $form["recent_blocks_$delta"]["general"]["items_no"] = array(
235 '#type' => 'textfield',
236 '#title' => t('How many items'),
237 '#description' => t('How many items to display'),
238 '#default_value' => $settings["items_no"],
239 );
240 $form["recent_blocks_$delta"]["general"]["username_show"] = array(
241 '#type' => 'checkbox',
242 '#title' => t('Display usernames'),
243 '#description' => t('Display usernames beside comment/node titles.'),
244 '#default_value' => $settings["username_show"],
245 );
246 $form["recent_blocks_$delta"]["general"]["types"] = array(
247 '#type' => 'select',
248 '#title' => t('Display following content types'),
249 '#description' => t('Only posts with type which is selected will be displayed'),
250 '#default_value' => $settings["types"],
251 '#options' => $types,
252 '#multiple' => TRUE,
253 );
254 $form["recent_blocks_$delta"]["general"]["url"] = array(
255 '#type' => 'fieldset',
256 '#title' => t('Custom url'),
257 '#collapsible' => FALSE,
258 '#collapsed' => FALSE,
259 );
260 $form["recent_blocks_$delta"]["general"]["url"]["url_href"] = array(
261 '#type' => 'textfield',
262 '#title' => t('Address'),
263 '#description' => t('The path, e.g. forum or user/login or http://google.com. Leave blank if you do not want it.'),
264 '#default_value' => $settings["url_href"],
265 );
266 $form["recent_blocks_$delta"]["general"]["url"]["url_text"] = array(
267 '#type' => 'textfield',
268 '#title' => t('Address text'),
269 '#description' => t('The text to display, e.g. more'),
270 '#default_value' => $settings["url_text"],
271 );
272
273 $form["recent_blocks_$delta"]["mode_selection"] = array(
274 '#type' => 'fieldset',
275 '#title' => t('Work mode selection'),
276 '#collapsible' => TRUE,
277 '#collapsed' => TRUE,
278 );
279 $form["recent_blocks_$delta"]["mode_selection"]["selection_description"] = array(
280 '#type' => 'markup',
281 '#value' => t('
282 <p>Each block can work in two modes:
283 <em>comment only</em> mode, in which only comments (replies) will be searched and displayed, and
284 <em>full</em> mode, in which both nodes and comments will be searched and displayed.
285 Depending which mode you choose, you should configure the settings for that mode.</p>'),
286 );
287 $form["recent_blocks_$delta"]["mode_selection"]["comments_only"] = array(
288 '#type' => 'radios',
289 '#title' => t('Work mode'),
290 '#description' => t('Choose this block work mode'),
291 '#default_value' => $settings["comments_only"],
292 '#options' => array(
293 '1' => t('Comments only mode'),
294 '0' => t('Full mode')),
295 );
296
297 $form["recent_blocks_$delta"]["comments_only_settings"] = array(
298 '#type' => 'fieldset',
299 '#title' => t('Comments only mode settings'),
300 '#collapsible' => TRUE,
301 '#collapsed' => TRUE,
302 );
303 $form["recent_blocks_$delta"]["comments_only_settings"]["mode_co_show"] = array(
304 '#type' => 'checkboxes',
305 '#title' => t('Show what?'),
306 '#default_value' => $settings["mode_co_show"],
307 '#options' => array(
308 'mark' => t('Show "new"/"actualized" mark'),
309 'comment date' => t('Show comment date')),
310 );
311
312 $form["recent_blocks_$delta"]["mode_full_settings"] = array(
313 '#type' => 'fieldset',
314 '#title' => t('Full mode settings'),
315 '#collapsible' => TRUE,
316 '#collapsed' => TRUE,
317 );
318 $form["recent_blocks_$delta"]["mode_full_settings"]["mode_full_show"] = array(
319 '#type' => 'checkboxes',
320 '#title' => t('Show what?'),
321 '#default_value' => $settings["mode_full_show"],
322 '#options' => array(
323 'teaser' => t('Show node teaser'),
324 'mark' => t('Show "new"/"actualized" mark'),
325 'node date' => t('Show node date'),
326 'comments number' => t('Show number of comments'),
327 'comment date' => t('Show last comment date')),
328 );
329 $form["recent_blocks_$delta"]["mode_full_settings"]["mode_full_wow_comments"] = array(
330 '#type' => 'radios',
331 '#title' => t('Show only nodes'),
332 '#description' => t('Show only nodes with or without comments?'),
333 '#default_value' => $settings["mode_full_wow_comments"],
334 '#options' => array(
335 'with' => t('with comments'),
336 'without' => t('without comments'),
337 'with or without' => t('with or without comments')),
338 );
339 $form["recent_blocks_$delta"]["mode_full_settings"]["mode_full_sort"] = array(
340 '#type' => 'radios',
341 '#title' => t('Sort by'),
342 '#description' => t('Sort by what?'),
343 '#default_value' => $settings["mode_full_sort"],
344 '#options' => array(
345 'last_change' => t('Modification or last post date'),
346 'created' => t('Node creation date'),
347 'changed' => t('Node modification date (either create or last edit date)'),
348 'last_comment_timestamp' => t('Last post date')),
349 );
350 $form["submit"] = array(
351 '#type' => 'submit',
352 '#value' => t('Save changes'),
353 );
354 return $form;
355 }
356
357 function recent_blocks_edit_page($delta) {
358 return drupal_get_form('recent_blocks_edit_form',$delta);
359 }
360
361 function recent_blocks_edit_form_submit($form_id, $form_values) {
362 $delta = $form_values['delta'];
363
364 #put node types in the format expected by _recent_blocks_block
365 $types = array_keys(node_get_types());
366 $rb_types = $form_values['types'];
367 foreach ($rb_types as $val) {
368 unset($form_values['types']["$val"]);
369 $form_values['types'][ $types[$val] ] = $types[$val];
370 }
371
372 variable_set("recent_blocks_$delta",$form_values);
373 variable_set("recent_blocks_broken",print_r($form_values,true));
374 drupal_set_message(t('Your changes have been saved.'));
375 return 'admin/build/recent_blocks';
376 }
377
378 function _recent_blocks_admin() {
379
380 $sort_by_options = array('created' => t('Created date'), 'changed' => t('Updated date'), 'last_comment_timestamp' => t('Last post date'), 'last_change' => t('Updated or last post date'));
381 $content = t("<p>This is a list of your recent blocks.</p>");
382 $content .= theme('item_list', array(
383 t("<a href='@add'>add new recent block</a>", array('@add' => url('admin/build/recent_blocks/add'))),
384 t("<a href='@blocks'>configure all blocks</a> (you can enable your blocks here)", array('@blocks' => url('admin/build/block'))),
385 ));
386
387 $block_deltas = variable_get('recent_blocks_deltas', array('0' => TRUE));
388 $destination = drupal_get_destination();
389 $header = array(
390 t('block title'),
391 t('block info'),
392 t('max items'),
393 t('node types'),
394 t('url'),
395 t('mode'),
396 t('show'),
397 t('comments'),
398 t('sort'),
399 t('action'),
400 );
401 $rows = array();
402 foreach (array_keys($block_deltas) as $delta) {
403 $settings = variable_get("recent_blocks_$delta", _recent_blocks_get_default_settings($delta));
404 switch($settings['mode_full_sort']) {
405 case 'last_change' : $sort = 'last change'; break;
406 case 'last_comment_timestamp': $sort = 'last post'; break;
407 default: $sort = $settings['mode_full_sort']; break;
408 }
409 $rows[] = array(
410 $settings['subject'],
411 $settings['info'],
412 $settings['items_no'],
413 implode(', ', $settings['types']),
414 $settings['url_href'] != '' ? "<a href='$settings[url_href]'>$settings[url_text]</a>" : t('n/a'),
415 $settings['comments_only'] ? t('Comments') : t('Full'),
416 $settings['comments_only'] ? implode(', ', $settings['mode_co_show']) : implode(', ', array_keys(array_filter($settings['mode_full_show']))),
417 $settings['comments_only'] ? t('n/a') : $settings['mode_full_wow_comments'],
418 $settings['comments_only'] ? t('n/a') : $sort,
419 l(t('delete'), "admin/build/recent_blocks/delete/$delta") .' '. l(t('edit'), "admin/build/recent_blocks/edit/$delta"),
420 );
421 }
422 $content .= theme('table', $header, $rows);
423 return $content;
424 }
425
426 function recent_blocks_uninstall(){
427 db_query("DELETE FROM {variable} WHERE name LIKE 'recent_blocks_%'");
428 }

  ViewVC Help
Powered by ViewVC 1.1.2