#105348 and #105810 - improved INSTALL.txt.
[project/gallery.git] / gallery_block.inc
1 <?php
2 // $Id$
3
4 /**
5 * gallery.module : gallery_block.inc
6 * Block functions
7 */
8
9 /**
10 * Implementation of hook_block
11 *
12 * 0 - gallery imageblock (random, most viewed, etc)
13 * 1 - gallery navigation block (recommended)
14 * 2 - gallery grid block
15 */
16 function _gallery_block($op = 'list', $delta = 0, $edit = array()) {
17 $typeMap = array('none' => t('None'),
18 'randomImage' => t('Random Image'),
19 'recentImage' => t('Newest Image'),
20 'viewedImage' => t('Most Viewed Image'),
21 'randomAlbum' => t('Random Album'),
22 'recentAlbum' => t('Newest Album'),
23 'viewedAlbum' => t('Most Viewed Album'),
24 'dailyImage' => t('Picture of the Day'),
25 'weeklyImage' => t('Picture of the Week'),
26 'monthlyImage' => t('Picture of the Month'),
27 'dailyAlbum' => t('Album of the Day'),
28 'weeklyAlbum' => t('Album of the Week'),
29 'monthlyAlbum' => t('Album of the Month'));
30
31 switch ($op) {
32 case 'list':
33 $blocks[0]['info'] = t('Gallery Block');
34 $blocks[1]['info'] = t('Gallery Navigation');
35 $blocks[2]['info'] = t('Gallery Grid Block');
36 return $blocks;
37
38 case 'view':
39 if (!user_access('access gallery')) {
40 return;
41 }
42 list ($success, $ret) = _gallery_init(true);
43 if (!$success) {
44 $err_msg = t('Unable to initialize embedded Gallery. You need to <a href="%link">
45 configure your embedded Gallery</a>.',
46 array('%link' => url('admin/settings/gallery')));
47 gallery_error($err_msg, $ret);
48 return;
49 }
50 switch ($delta) {
51 // 0 = Image Block
52 case 0:
53 // Allow for multiple image types
54 $param_blocks_array = variable_get('gallery_block_block', array('randomImage'));
55 $params['blocks'] = is_array($param_blocks_array) ? implode('|', $param_blocks_array) : "";
56 $param_show_array = variable_get('gallery_block_show', array());
57 $params['show'] = is_array($param_show_array) ? implode('|', $param_show_array) : "";
58 $params['maxSize'] = variable_get('gallery_maxsize', 160);
59 // Add frames and link target using g2_filter code from MichelleC
60 $params['albumFrame'] = variable_get('gallery_album_frame', 'none');
61 $params['itemFrame'] = variable_get('gallery_item_frame', 'none');
62 $params['linkTarget'] = variable_get('gallery_link_target', '');
63 if (variable_get('gallery_item_id', '') != '') {
64 $params['itemId'] = variable_get('gallery_item_id', '');
65 }
66 $block = array();
67 list($ret, $content, $head) = GalleryEmbed::getImageBlock($params);
68 if ($ret) {
69 gallery_error(t('Unable to get Gallery image block'), $ret);
70 return;
71 } else {
72 if ($content) {
73 // If more than one image type selected then default the subject to 'Gallery'
74 if (count(variable_get('gallery_block_block', 'randomImage')) > 1) {
75 $block['subject'] = t('Gallery');
76 } else {
77 $block['subject'] = $typeMap[$params['blocks']];
78 }
79 // TODO: This should not be hardcoded, but included in the CSS.
80 $block['content'] = '<center>' . $content . '</center>';
81 }
82 }
83 if ($head) {
84 gallery_set_html_head($head);
85 }
86 break;
87 // 1 = Navigation Block
88 case 1:
89 global $gallery_sidebar;
90 if ((arg(0) == 'gallery') && (isset($gallery_sidebar) && !empty($gallery_sidebar))) {
91 $block['subject'] = t('Gallery Navigation');
92 $block['content'] = '<div id="gsSidebar" class="gcBorder1">' . join('', $gallery_sidebar) . '</div>';
93 }
94 break;
95 // 2 = Image Grid Block
96 case 2:
97 $num_cols = variable_get('gallery_grid_num_cols', 2);
98 $num_rows = variable_get('gallery_grid_num_rows', 2);
99 $num_images = $num_cols * $num_rows;
100 // Allow for multiple image types
101 $param_blocks_array = array_fill(0, $num_images, variable_get('gallery_grid_block_block',
102 'randomImage'));
103 $params['blocks'] = is_array($param_blocks_array) ? implode('|', $param_blocks_array) : "";
104 $param_show_array = variable_get('gallery_grid_block_show', array());
105 $params['show'] = is_array($param_show_array) ? implode('|', $param_show_array) : "";
106 $params['maxSize'] = variable_get('gallery_grid_maxsize', 90);
107 $params['albumFrame'] = variable_get('gallery_grid_album_frame', 'none');
108 $params['itemFrame'] = variable_get('gallery_grid_item_frame', 'none');
109 $params['linkTarget'] = variable_get('gallery_grid_link_target', '');
110 if (variable_get('gallery_grid_item_id', '') != '') {
111 $params['itemId'] = variable_get('gallery_grid_item_id', '');
112 }
113 $block = array();
114 list($ret, $content, $head) = GalleryEmbed::getImageBlock($params);
115 if ($ret) {
116 gallery_error(t('Unable to get Gallery image block'), $ret);
117 return;
118 } else {
119 if ($content) {
120 $block['subject'] = t('Gallery');
121 // Split the images from the html so that can put each only in a <td>
122 $images = _gallery_split_imageblock($content);
123 $new_html = '<div class="gallery-grid-block"><div class="image-grid">';
124 $new_html .= '<table>';
125 $col = 0;
126 foreach ($images as $current_image) {
127 if ($col == 0) {
128 $new_html .= '<tr>';
129 }
130 $new_html .= '<td style="text-align:center;">' . $current_image . '</td>';
131 $col++;
132 if ($col >= $num_cols) {
133 $col = 0;
134 $new_html .= '</tr>';
135 }
136 }
137 $new_html .= '</table>';
138 $new_html .= '</div></div>';
139 $block['content'] .= $new_html;
140 }
141 }
142 if ($head) {
143 gallery_set_html_head($head);
144 }
145 break;
146 }
147 $ret = GalleryEmbed::done();
148 if ($ret) {
149 gallery_error(t('Unable to complete Gallery request'), $ret);
150 return;
151 }
152 return $block;
153 }
154 }
155
156 ?>