| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/***********************************************************
|
| 5 |
* LOCAL TASK *
|
| 6 |
************************************************************/
|
| 7 |
|
| 8 |
function composite_general_select_page($type, $node) {
|
| 9 |
$type_def = composite_get_types($type);
|
| 10 |
drupal_set_title(t('@label for @title', array('@label' => $type_def['label']['plural'], '@title' => $node->title)));
|
| 11 |
return drupal_get_form('composite_general_select_form', $type_def, $node);
|
| 12 |
}
|
| 13 |
|
| 14 |
function composite_general_select_form(&$form_state, $type, $node) {
|
| 15 |
// If there is no potentials callback defined, then there's no point continuing.
|
| 16 |
$potentials_callback = $type['potentials callback'];
|
| 17 |
composite_include_file($type);
|
| 18 |
if (!$potentials_callback || !function_exists($potentials_callback)) {
|
| 19 |
return;
|
| 20 |
}
|
| 21 |
|
| 22 |
// Save for later use
|
| 23 |
$form['#node'] = $node;
|
| 24 |
$form['composite_type'] = array('#type' => 'value', '#value' => $type['type']);
|
| 25 |
|
| 26 |
// Retrieve select list of potentials
|
| 27 |
$options = $potentials_callback($node);
|
| 28 |
|
| 29 |
// Fill in our selected values
|
| 30 |
$selected = array();
|
| 31 |
foreach ($node->composite_references['#'. $type['type'] .'_references'] as $id => $unused) {
|
| 32 |
// Make sure this block still exists
|
| 33 |
if (array_key_exists($id, $options)) {
|
| 34 |
$selected[$id] = $id;
|
| 35 |
}
|
| 36 |
}
|
| 37 |
|
| 38 |
$form['composite_items'] = array(
|
| 39 |
'#type' => 'checkboxes',
|
| 40 |
'#title' => t('Items to display in @title', array('@title' => $node->title)),
|
| 41 |
'#options' => $options,
|
| 42 |
'#multiple' => TRUE,
|
| 43 |
'#description' => t('Please select the items you would like to display as part of this node.'),
|
| 44 |
'#default_value' => $selected,
|
| 45 |
);
|
| 46 |
|
| 47 |
$form['submit'] = array(
|
| 48 |
'#type' => 'submit',
|
| 49 |
'#value' => t('Save'),
|
| 50 |
);
|
| 51 |
return $form;
|
| 52 |
}
|
| 53 |
|
| 54 |
function composite_general_select_form_submit($form, &$form_state) {
|
| 55 |
$node = $form['#node'];
|
| 56 |
$type = composite_get_types($form_state['values']['composite_type']);
|
| 57 |
|
| 58 |
// Construct a list of items to delete, and a list of new items to insert
|
| 59 |
$delete_items = array();
|
| 60 |
$insert_items = array();
|
| 61 |
foreach ($form_state['values']['composite_items'] as $id => $flag) {
|
| 62 |
if ($flag && !$node->composite_references['#'. $type['type'] .'_references'][$id]) {
|
| 63 |
// This is an added item, add to in the insert list
|
| 64 |
$insert_items[$id] = $id;
|
| 65 |
}
|
| 66 |
else if (!$flag && $node->composite_references['#'. $type['type'] .'_references'][$id]) {
|
| 67 |
// This is a to-be-deleted item, add to the delete list
|
| 68 |
$delete_items[$id] = $id;
|
| 69 |
}
|
| 70 |
}
|
| 71 |
|
| 72 |
// Delete old items
|
| 73 |
foreach ($delete_items as $id => $flag) {
|
| 74 |
db_query("DELETE FROM {node_composite_references} WHERE vid = %d AND id = '%s'", $node->vid, $id);
|
| 75 |
}
|
| 76 |
|
| 77 |
// Add new entries
|
| 78 |
$args = array();
|
| 79 |
$query_parts = array();
|
| 80 |
foreach ($insert_items as $id => $flag) {
|
| 81 |
$query_parts[] = " (%d, %d, '%s', %d, '%s', '%s', '%s')";
|
| 82 |
$args[] = $node->nid;
|
| 83 |
$args[] = $node->vid;
|
| 84 |
$args[] = $type['type'];
|
| 85 |
$args[] = 0;
|
| 86 |
$args[] = $id;
|
| 87 |
$args[] = '';
|
| 88 |
$args[] = COMPOSITE_ZONE_NONE;
|
| 89 |
}
|
| 90 |
|
| 91 |
if (count($query_parts)) {
|
| 92 |
$query = "INSERT INTO {node_composite_references} (nid, vid, type, weight, id, data, zone) VALUES" . implode(', ', $query_parts);
|
| 93 |
db_query($query, $args);
|
| 94 |
}
|
| 95 |
drupal_set_message(t('The selected items have been saved.'));
|
| 96 |
}
|
| 97 |
|
| 98 |
/***********************************************************
|
| 99 |
* ZONES *
|
| 100 |
************************************************************/
|
| 101 |
|
| 102 |
function composite_zones_page($node) {
|
| 103 |
drupal_set_title(t('Zones for @title', array('@title' => $node->title)));
|
| 104 |
if ($node->composite_layout) {
|
| 105 |
$output .= drupal_get_form('composite_zones_form', $node);
|
| 106 |
// $output .= theme('composite_zones_preview', node_view($node, FALSE, FALSE, FALSE));
|
| 107 |
return $output;
|
| 108 |
}
|
| 109 |
else {
|
| 110 |
return t('You need to choose a layout first.');
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
function composite_zones_form(&$form_state, $node) {
|
| 115 |
if ($form_state['operation'] == 'delete') {
|
| 116 |
return composite_zones_multiple_delete_confirm($form_state, $node);
|
| 117 |
}
|
| 118 |
|
| 119 |
drupal_add_css(drupal_get_path('module', 'block') .'/block.css', 'module', 'all');
|
| 120 |
|
| 121 |
$form['#tree'] = TRUE;
|
| 122 |
|
| 123 |
if (isset($form_state['node'])) {
|
| 124 |
// Use the node override instead of the default
|
| 125 |
$node = $form_state['node'];
|
| 126 |
}
|
| 127 |
// Insert preview if it exists
|
| 128 |
if (isset($form_state['node_preview'])) {
|
| 129 |
$form['#prefix'] = $form_state['node_preview'];
|
| 130 |
}
|
| 131 |
|
| 132 |
// Save for later
|
| 133 |
$form['#node'] = $node;
|
| 134 |
|
| 135 |
$layout = composite_get_layout($node->composite_layout);
|
| 136 |
$layout_zones = $layout['zones'] + array(COMPOSITE_ZONE_NONE => t('<none>'));
|
| 137 |
// Need to pass this through to template function
|
| 138 |
$form['#zones'] = $layout_zones;
|
| 139 |
|
| 140 |
$references = _composite_references_preprocess($node->composite_references, $layout_zones);
|
| 141 |
_composite_compare('reset-zones', $layout_zones);
|
| 142 |
usort($references, '_composite_compare');
|
| 143 |
|
| 144 |
foreach ($references as $id => $reference) {
|
| 145 |
$form[$id]['id'] = array('#type' => 'value', '#value' => $reference['id']);
|
| 146 |
$form[$id]['type'] = array('#type' => 'value', '#value' => $reference['type']);
|
| 147 |
$form[$id]['nid'] = array('#type' => 'value', '#value' => $node->nid);
|
| 148 |
$form[$id]['vid'] = array('#type' => 'value', '#value' => $node->vid);
|
| 149 |
$form[$id]['info'] = array('#value' => $reference['info']);
|
| 150 |
$form[$id]['weight'] = array(
|
| 151 |
'#type' => 'weight',
|
| 152 |
'#default_value' => $reference['weight'],
|
| 153 |
);
|
| 154 |
$form[$id]['zone'] = array(
|
| 155 |
'#type' => 'select',
|
| 156 |
// If the reference is in a zone that no longer exists, place it in the disabled list.
|
| 157 |
'#default_value' => array_key_exists($reference['zone'], $layout_zones) ? $reference['zone'] : '-1',
|
| 158 |
'#options' => $layout_zones,
|
| 159 |
);
|
| 160 |
$extra_fields = composite_invoke_referenceapi($reference, 'settings', $node);
|
| 161 |
if ($extra_fields) {
|
| 162 |
$form[$id]['data'] = $extra_fields;
|
| 163 |
// Kludgey
|
| 164 |
$form[$id]['data']['#prefix'] = '<div class="container-inline">';
|
| 165 |
$form[$id]['data']['#suffix'] = '</div>';
|
| 166 |
}
|
| 167 |
$form[$id]['checkbox'] = array(
|
| 168 |
'#type' => 'checkbox',
|
| 169 |
'#default_value' => 0,
|
| 170 |
);
|
| 171 |
}
|
| 172 |
|
| 173 |
$form['update_options'] = array(
|
| 174 |
'#type' => 'fieldset',
|
| 175 |
'#title' => t('Update options'),
|
| 176 |
'#prefix' => '<div class="container-inline">',
|
| 177 |
'#suffix' => '</div>',
|
| 178 |
);
|
| 179 |
$form['update_options']['operation'] = array(
|
| 180 |
'#type' => 'select',
|
| 181 |
'#options' => array('delete' => t('Delete checked items')),
|
| 182 |
);
|
| 183 |
$form['update_options']['update'] = array(
|
| 184 |
'#type' => 'submit',
|
| 185 |
'#value' => t('Update'),
|
| 186 |
'#submit' => array('composite_zones_form_operations'),
|
| 187 |
);
|
| 188 |
|
| 189 |
$form['submit'] = array(
|
| 190 |
'#type' => 'submit',
|
| 191 |
'#value' => t('Save zones'),
|
| 192 |
'#submit' => array('composite_zones_form_submit'),
|
| 193 |
);
|
| 194 |
$form['save_and_view'] = array(
|
| 195 |
'#type' => 'submit',
|
| 196 |
'#value' => t('Save zones and view'),
|
| 197 |
'#submit' => array('composite_zones_form_submit'),
|
| 198 |
);
|
| 199 |
$form['buttons']['preview'] = array(
|
| 200 |
'#type' => 'submit',
|
| 201 |
'#value' => t('Preview'),
|
| 202 |
'#submit' => array('composite_zones_form_preview'),
|
| 203 |
);
|
| 204 |
return $form;
|
| 205 |
}
|
| 206 |
|
| 207 |
function composite_zones_form_submit($form, &$form_state) {
|
| 208 |
foreach ($form_state['values'] as $reference) {
|
| 209 |
if (is_array($reference) && isset($reference['id'])) {
|
| 210 |
$data = $reference['data'] ? serialize($reference['data']) : '';
|
| 211 |
db_query("UPDATE {node_composite_references} SET weight = %d, data = '%s', zone = '%s' WHERE vid = %d AND id = '%s'", $reference['weight'], $data, $reference['zone'], $reference['vid'], $reference['id']);
|
| 212 |
}
|
| 213 |
}
|
| 214 |
drupal_set_message(t('The selected zones have been saved.'));
|
| 215 |
if ($form_state['values']['op'] == t('Save zones and view')) {
|
| 216 |
$node = $form['#node'];
|
| 217 |
$form_state['redirect'] = 'node/' . $node->nid;
|
| 218 |
}
|
| 219 |
}
|
| 220 |
|
| 221 |
function composite_zones_form_preview($form, &$form_state) {
|
| 222 |
$node = $form['#node'];
|
| 223 |
|
| 224 |
// Rebuild composite references with the submitted values
|
| 225 |
$original_references = $node->composite_references;
|
| 226 |
$node->composite_references = array();
|
| 227 |
|
| 228 |
foreach ($form_state['values'] as $reference) {
|
| 229 |
if (is_array($reference) && isset($reference['id'])) {
|
| 230 |
// Fill in values which were not transferred with the form (and
|
| 231 |
// implicitly, remained unchanged).
|
| 232 |
$reference['type'] = $original_references[$reference['id']]['type'];
|
| 233 |
composite_invoke_referenceapi($reference, 'load');
|
| 234 |
|
| 235 |
$node->composite_references[$reference['id']] = $reference;
|
| 236 |
}
|
| 237 |
}
|
| 238 |
|
| 239 |
// Save values for the next form iteration
|
| 240 |
$form_state['node'] = $node;
|
| 241 |
$form_state['node_preview'] = theme('composite_zones_preview', drupal_clone($node));
|
| 242 |
|
| 243 |
drupal_set_title(t('Preview'));
|
| 244 |
drupal_set_message('This is a preview. Changes will not be saved until the Save zones button is clicked.');
|
| 245 |
|
| 246 |
// Setting this flag seems to propagate ['node_preview'] back to the form builder
|
| 247 |
$form_state['rebuild'] = TRUE;
|
| 248 |
}
|
| 249 |
|
| 250 |
function composite_zones_form_operations($form, &$form_state) {
|
| 251 |
if ($form_state['values']['update_options']['operation']) {
|
| 252 |
$form_state['operation'] = $form_state['values']['update_options']['operation'];
|
| 253 |
// Cycle this form
|
| 254 |
$form_state['rebuild'] = TRUE;
|
| 255 |
}
|
| 256 |
}
|
| 257 |
|
| 258 |
function composite_zones_multiple_delete_confirm(&$form_state, $node) {
|
| 259 |
$form['#node'] = $node;
|
| 260 |
$form['references'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
|
| 261 |
$checklist = array();
|
| 262 |
foreach ($form_state['values'] as $reference) {
|
| 263 |
if (is_array($reference) && isset($reference['id']) && $reference['checkbox']) {
|
| 264 |
composite_invoke_referenceapi($reference, 'load');
|
| 265 |
composite_invoke_referenceapi($reference, 'info');
|
| 266 |
|
| 267 |
$form['references'][$reference['id']] = array(
|
| 268 |
'#type' => 'hidden',
|
| 269 |
'#value' => $reference['id'],
|
| 270 |
'#prefix' => '<li>',
|
| 271 |
'#suffix' => $reference['info'] ."</li>\n",
|
| 272 |
);
|
| 273 |
}
|
| 274 |
}
|
| 275 |
$form['operation'] = array('#type' => 'hidden', '#value' => 'delete');
|
| 276 |
$form['#submit'][] = 'composite_zones_multiple_delete_confirm_submit';
|
| 277 |
return confirm_form($form,
|
| 278 |
t('Are you sure you want to delete these items?'),
|
| 279 |
'node/' . $node->nid . '/composite_zones', '',
|
| 280 |
t('Delete all'), t('Cancel'));
|
| 281 |
}
|
| 282 |
|
| 283 |
function composite_zones_multiple_delete_confirm_submit($form, &$form_state) {
|
| 284 |
$node = $form['#node'];
|
| 285 |
if ($form_state['values']['confirm']) {
|
| 286 |
foreach ($form_state['values']['references'] as $id => $reference) {
|
| 287 |
db_query("DELETE FROM {node_composite_references} WHERE vid = %d AND id = '%s'", $node->vid, $id);
|
| 288 |
}
|
| 289 |
drupal_set_message(t('The items have been deleted.'));
|
| 290 |
}
|
| 291 |
$form_state['redirect'] = 'node/' . $node->nid . '/composite_zones';
|
| 292 |
return;
|
| 293 |
}
|
| 294 |
|
| 295 |
|
| 296 |
// This function borrows some code from template_preprocess_block_admin_display_form
|
| 297 |
function template_preprocess_composite_zones_form(&$variables) {
|
| 298 |
$layout_zones = $variables['form']['#zones'];
|
| 299 |
$layout_zones[COMPOSITE_ZONE_NONE] = t('Disabled');
|
| 300 |
$variables['layout_zones'] = $layout_zones;
|
| 301 |
|
| 302 |
foreach ($layout_zones as $key => $value) {
|
| 303 |
// Highlight regions on page to provide visual reference.
|
| 304 |
// drupal_set_content($key, '<div class="block-region">'. $value .'</div>');
|
| 305 |
// Initialize an empty array for the region.
|
| 306 |
$variables['zone_listing'][$key] = array();
|
| 307 |
}
|
| 308 |
$variables['zone_listing'][COMPOSITE_ZONE_NONE] = array();
|
| 309 |
|
| 310 |
foreach (element_children($variables['form']) as $i) {
|
| 311 |
$reference = &$variables['form'][$i];
|
| 312 |
|
| 313 |
// Only take form elements that are references.
|
| 314 |
if (isset($reference['id'])) {
|
| 315 |
// Fetch zone for current block.
|
| 316 |
$zone = $reference['zone']['#default_value'];
|
| 317 |
|
| 318 |
// Set special classes needed for table drag and drop.
|
| 319 |
$variables['form'][$i]['zone']['#attributes']['class'] = 'block-region-select block-region-'. $zone;
|
| 320 |
$variables['form'][$i]['weight']['#attributes']['class'] = 'block-weight block-weight-'. $zone;
|
| 321 |
|
| 322 |
$variables['zone_listing'][$zone][$i]->row_class = isset($reference['#attributes']['class']) ? $reference['#attributes']['class'] : '';
|
| 323 |
$variables['zone_listing'][$zone][$i]->reference_modified = isset($reference['#attributes']['class']) && strpos($reference['#attributes']['class'], 'block-modified') !== FALSE ? TRUE : FALSE;
|
| 324 |
$variables['zone_listing'][$zone][$i]->reference_title = drupal_render($reference['info']);
|
| 325 |
$variables['zone_listing'][$zone][$i]->zone_select = drupal_render($reference['zone']); //. drupal_render($block['theme']);
|
| 326 |
$variables['zone_listing'][$zone][$i]->weight_select = drupal_render($reference['weight']);
|
| 327 |
$variables['zone_listing'][$zone][$i]->data_widget = drupal_render($reference['data']);
|
| 328 |
$variables['zone_listing'][$zone][$i]->checkbox = drupal_render($reference['checkbox']);
|
| 329 |
$variables['zone_listing'][$zone][$i]->printed = FALSE;
|
| 330 |
}
|
| 331 |
}
|
| 332 |
|
| 333 |
$variables['form_submit'] = drupal_render($variables['form']);
|
| 334 |
}
|