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

Contents of /contributions/modules/multiblock/multiblock.module

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


Revision 1.1 - (show annotations) (download) (as text)
Fri Feb 29 20:26:31 2008 UTC (20 months, 4 weeks ago) by andrewlevine
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--1
File MIME type: text/x-php
multiblock module allows you to create multiple instances of blocks so you can put them at different regions, weights, etc. Contributed by PageSix.com.
1 <?php
2 // $Id$
3 /*
4 * Implementation of hook_menu
5 */
6 function multiblock_menu($may_cache) {
7 $items = array();
8 if ($may_cache) {
9 $items[] = array(
10 'path' => 'admin/build/multiblock',
11 'title' => t('Block Instances'),
12 'description' => t('Create and delete instances of blocks.'),
13 'callback' => 'multiblock_general',
14 'access' => user_access('administer blocks'),
15 'type' => MENU_NORMAL_ITEM,
16 );
17 $items[] = array(
18 'path' => 'multiblock/delete',
19 'title' => t('Delete Block Instance'),
20 'callback' => 'multiblock_delete',
21 'access' => user_access('administer blocks'),
22 'type' => MENU_CALLBACK,
23 );
24 }
25 else {
26 }
27 return $items;
28 }
29
30 /*
31 * Implementation of hook_block
32 */
33 function multiblock_block($op = 'list', $delta = 0, $edit = array()) {
34 if ($op == 'list') {
35 //get all of the block instances that exist
36 $result = db_query("SELECT delta, title FROM {multiblock}");
37 $blocks = array();
38 //escape the titles and return an array of them keyed by their NEW deltas
39 while ($row = db_fetch_object($result)) {
40 $blocks[$row->delta] = array( 'info' => check_plain($row->title), );
41 }
42 return $blocks;
43 }
44 //any op besides list we want to dispatch the call to its respective module
45 else if ($op == 'view' || $op == 'configure' || $op == 'save') {
46 return multiblock_call_block($delta, $op, $edit);
47 }
48 }
49
50 /*
51 * Dispatch a hook_block call to it's respective module. Paramater $delta
52 * is the new multiblock delta that we're using and $op is the op we are
53 * dispatching.
54 */
55 function multiblock_call_block($delta, $op, $edit) {
56 $result = db_query("SELECT module, orig_delta, delta, multi_settings FROM {multiblock} WHERE delta='%s'", $delta);
57 if ($block_info = db_fetch_object($result)) {
58 //if this block is multiblock enabled, send it the delta of the block we're using
59 if ($block_info->multi_settings == 1 ) {
60 $edit['multiblock_delta'] = array(
61 '#type' => 'value',
62 '#value' => $block_info->delta,
63 );
64 }
65 return module_invoke($block_info->module, 'block', $op, $block_info->orig_delta, $edit);
66 }
67 //no such multiblock, shouldn't ever happen
68 return;
69 }
70
71 /*
72 * Page callback for the "Manage Block Instances page"
73 */
74 function multiblock_general() {
75 //fetch blocks directly from modules using block.module function
76 $blocks = _block_rehash();
77 //sort blocks how we want them
78 usort($blocks, 'multiblock_block_sort');
79
80 //fetch "Add Instance" form
81 $form = drupal_get_form('multiblock_add_block', $blocks);
82
83 //get an array of existing blocks
84 $result = db_query('SELECT * FROM {multiblock} ORDER BY title');
85 $multiblocks = array();
86 while ($multiblock = db_fetch_object($result)) {
87 $multiblocks[] = $multiblock;
88 }
89
90 return theme('multiblock_general', $form, $multiblocks);
91 }
92
93 /*
94 * "Add Instance" form
95 */
96 function multiblock_add_block($blocks) {
97 $form = array();
98 $form['title'] = array(
99 '#type' => 'textfield',
100 '#title' => t('Instance Title'),
101 '#maxlength' => 256,
102 '#required' => TRUE,
103 );
104
105 //turn $blocks into form options of block types
106 //remember we need the module and delta to be able to tell what kind of blocks
107 //we're talking about.
108 $options = array();
109 foreach ($blocks as $block) {
110 //don't include multiblock module blocks in the list
111 if ($block['module'] != 'multiblock') {
112 $options[$block['module'].'***MB***'.$block['delta']] = $block['info'];
113 }
114 }
115
116 $form['block'] = array(
117 '#type' => 'select',
118 '#title' => t('Block type'),
119 '#options' => $options,
120 '#required' => TRUE,
121 );
122 $form['submit'] = array(
123 '#type' => 'submit',
124 '#value' => t('Add Instance'),
125 );
126 return $form;
127 }
128
129 /*
130 * Call back that deletes the block instance with a delta of arg(2)
131 * and redirects the user back to the "Manage Block Instances" form
132 */
133 function multiblock_delete() {
134 $multiblock_delta = arg(2);
135 $result = db_query('DELETE FROM {multiblock} WHERE delta=%d', (int)$multiblock_delta);
136 //if we actually deleted something
137 if (ctype_digit($multiblock_delta) && db_affected_rows() == 1) {
138 drupal_set_message(t('Block successfully deleted!'));
139 _block_rehash();
140 }
141 else {
142 drupal_set_message(t('There was a problem deleting the block'));
143 }
144 drupal_goto('admin/build/multiblock');
145 exit;
146 }
147
148 /*
149 * Validate "Add Block Instance" form
150 */
151 function multiblock_add_block_validate($form_id, $form_values) {
152 //make sure we are getting a valid block to add
153 if (!preg_match('/^.+\*\*\*MB\*\*\*.+$/', $form_values['block'])) {
154 form_set_error('block', t('Bad block module input, contact administrator'));
155 return;
156 }
157 //make sure the block and delta exist
158 $orig_block = multiblock_blockinfo_from_form($form_values['block']);
159 if (!module_hook($orig_block['module'], 'block') ||
160 !array_key_exists($orig_block['delta'], module_invoke($orig_block['module'], 'block', 'list'))) {
161 form_set_error('block', t('Module or doesn\t exist, contact administrator'));
162 }
163 }
164
165 /*
166 * Add block instance to database from "Add Block Instance" form
167 */
168 function multiblock_add_block_submit($form_id, $form_values) {
169 //create new delta for block instance
170 $delta = db_next_id('{multiblock}_delta');
171 //get the original block info
172 $orig_block = multiblock_blockinfo_from_form($form_values['block']);
173 //check whether this module is multiblock enabled
174 $mb_enabled = (int)(module_invoke($orig_block['module'], 'block', 'mb_enabled') == 'mb_enabled');
175 $sql = "INSERT INTO {multiblock}
176 (delta, title, module, orig_delta, multi_settings)
177 VALUES (%d, '%s', '%s', '%s', %d)";
178 db_query($sql, $delta, $form_values['title'], $orig_block['module'], $orig_block['delta'], $mb_enabled);
179 drupal_set_message(t('Block instance %instance created.', array('%instance' => $form_values['title'])));
180 }
181
182 /*
183 * Custom sort based on info element of array
184 */
185 function multiblock_block_sort($a, $b) {
186 return strcmp($a['info'], $b['info']);
187 }
188
189 /*
190 * Get the module and delta from the "Add Block Instance" block form element
191 */
192 function multiblock_blockinfo_from_form($form_value) {
193 $matches = array();
194 preg_match('/^(.+)\*\*\*MB\*\*\*(.+)$/', $form_value, $matches);
195 return array ('module' => $matches[1], 'delta' => $matches[2]);
196 }
197
198 /*
199 * Get title of a block by its module and delta
200 */
201 function multiblock_get_block_title($module, $delta) {
202 $block_info = module_invoke($module, 'block', 'list');
203 return $block_info[$delta]['info'];
204 }
205
206 /*
207 * Theme function for the "Manage Block Instances" page
208 */
209 function theme_multiblock_general($add_block_form, $multiblocks) {
210 $output = '';
211
212 $output .= '<p><h3>' . t('Add Instance') . '</h3>' . $add_block_form . '</p>';
213
214 $header = array (t('Title'), t('Original Block Title'), t('Original Module'), t('Original Delta'), t('Action'));
215
216 foreach ($multiblocks as $row) {
217 $delete_link = l(t('Delete'), 'multiblock/delete/'. $row->delta);
218 $title = multiblock_get_block_title($row->module, $row->orig_delta);
219 $rows[] = array(check_plain($row->title), $title, $row->module, $row->orig_delta, $delete_link);
220 }
221
222 $output .= '<p><h3>' . t('Manage Instances') . '</h3>' . theme('table', $header, $rows) . '</p>';
223
224 return $output;
225 }

  ViewVC Help
Powered by ViewVC 1.1.2