| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id: fieldgroup.module,v 1.9 2006/11/29 12:03:53 fago Exp $
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* Create field groups for CCK fields.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_help().
|
| 12 |
*/
|
| 13 |
function fieldgroup_help($section) {
|
| 14 |
switch ($section) {
|
| 15 |
case 'admin/modules#description':
|
| 16 |
return t('Create field groups for CCK fields. Requires the <em>Content.module</em>.');
|
| 17 |
case 'admin/node/types/'. arg(3) .'/groups':
|
| 18 |
return t('Create and order your groups first. Then assign fields to a group by editing the fields.');
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
|
| 23 |
function fieldgroup_menu($may_cache) {
|
| 24 |
|
| 25 |
if (!$may_cache) {
|
| 26 |
if (arg(0) == 'admin' && arg(1) == 'node' && arg(2) == 'types' && arg(3)) {
|
| 27 |
if ($type = content_types(arg(3))) {
|
| 28 |
$items[] = array(
|
| 29 |
'path' => 'admin/node/types/'. arg(3) .'/groups',
|
| 30 |
'title' => t('manage groups'),
|
| 31 |
'callback' => 'fieldgroup_overview',
|
| 32 |
'access' => user_access('administer content types'),
|
| 33 |
'callback arguments' => array(arg(3)),
|
| 34 |
'type' => MENU_LOCAL_TASK,
|
| 35 |
'weight' => 0,
|
| 36 |
);
|
| 37 |
$items[] = array(
|
| 38 |
'path' => 'admin/node/types/'. arg(3) .'/add_group',
|
| 39 |
'title' => t('add group'),
|
| 40 |
'callback' => 'fieldgroup_edit_group',
|
| 41 |
'access' => user_access('administer content types'),
|
| 42 |
'callback arguments' => array(arg(3), 'add_group'),
|
| 43 |
'type' => MENU_LOCAL_TASK,
|
| 44 |
'weight' => 2,
|
| 45 |
);
|
| 46 |
if (arg(4) == 'groups' && arg(5)) {
|
| 47 |
$items[] = array(
|
| 48 |
'path' => 'admin/node/types/'. arg(3) .'/groups/'. arg(5). '/edit',
|
| 49 |
'title' => t('edit group'),
|
| 50 |
'callback' => 'fieldgroup_edit_group',
|
| 51 |
'access' => user_access('administer content types'),
|
| 52 |
'callback arguments' => array(arg(3), arg(5)),
|
| 53 |
'type' => MENU_CALLBACK_ITEM,
|
| 54 |
);
|
| 55 |
$items[] = array(
|
| 56 |
'path' => 'admin/node/types/'. arg(3) .'/groups/'. arg(5). '/remove',
|
| 57 |
'title' => t('edit group'),
|
| 58 |
'callback' => 'fieldgroup_remove_group',
|
| 59 |
'access' => user_access('administer content types'),
|
| 60 |
'callback arguments' => array(arg(3), arg(5)),
|
| 61 |
'type' => MENU_CALLBACK_ITEM,
|
| 62 |
);
|
| 63 |
}
|
| 64 |
}
|
| 65 |
}
|
| 66 |
}
|
| 67 |
return $items;
|
| 68 |
|
| 69 |
}
|
| 70 |
|
| 71 |
/*
|
| 72 |
* Show a list of available groups for a content type
|
| 73 |
*/
|
| 74 |
function fieldgroup_overview($content_type) {
|
| 75 |
|
| 76 |
$groups = fieldgroup_groups($content_type, TRUE);
|
| 77 |
|
| 78 |
foreach ($groups as $group) {
|
| 79 |
$rows[] = array(check_plain($group['label']), $group['weight'],
|
| 80 |
l(t('configure'), 'admin/node/types/'. $content_type. '/groups/'. $group['group_name'] .'/edit'),
|
| 81 |
l(t('remove'), 'admin/node/types/'. $content_type. '/groups/'. $group['group_name'] .'/remove'));
|
| 82 |
}
|
| 83 |
if (empty($rows)) {
|
| 84 |
$rows[] = array(array('data' => t('No groups available.'), 'colspan' => '4', 'class' => 'message'));
|
| 85 |
}
|
| 86 |
|
| 87 |
$header = array(t('Label'), t('Weight'), array('data' => t('Operations'), 'colspan' => '2'));
|
| 88 |
return theme('table', $header, $rows, array('id' => 'fieldgroup'));
|
| 89 |
}
|
| 90 |
|
| 91 |
function fieldgroup_edit_group($content_type, $group_name) {
|
| 92 |
$groups = fieldgroup_groups($content_type);
|
| 93 |
$group = $groups[$group_name];
|
| 94 |
|
| 95 |
if ($group_name == 'add_group' && arg(6) != 'edit') {
|
| 96 |
//adding a new one
|
| 97 |
$group = array();
|
| 98 |
$form['submit'] = array(
|
| 99 |
'#type' => 'submit',
|
| 100 |
'#value' => t('Add'),
|
| 101 |
'#weight' => 10,
|
| 102 |
);
|
| 103 |
}
|
| 104 |
else if ($group) {
|
| 105 |
$form['submit'] = array(
|
| 106 |
'#type' => 'submit',
|
| 107 |
'#value' => t('Save'),
|
| 108 |
'#weight' => 10,
|
| 109 |
);
|
| 110 |
}
|
| 111 |
else {
|
| 112 |
drupal_not_found();
|
| 113 |
exit;
|
| 114 |
}
|
| 115 |
|
| 116 |
$form['label'] = array(
|
| 117 |
'#type' => 'textfield',
|
| 118 |
'#title' => t('Label'),
|
| 119 |
'#default_value' => $group['label'],
|
| 120 |
'#required' => TRUE,
|
| 121 |
);
|
| 122 |
$form['settings']['collapsible'] = array(
|
| 123 |
'#type' => 'checkbox',
|
| 124 |
'#title' => t('Collapsible'),
|
| 125 |
'#default_value' => $group['settings']['collapsible'],
|
| 126 |
);
|
| 127 |
$form['settings']['collapsed'] = array(
|
| 128 |
'#type' => 'checkbox',
|
| 129 |
'#title' => t('Collapsed'),
|
| 130 |
'#default_value' => $group['settings']['collapsed'],
|
| 131 |
);
|
| 132 |
$form['settings']['#tree'] = TRUE;
|
| 133 |
$form['description'] = array(
|
| 134 |
'#type' => 'textarea',
|
| 135 |
'#title' => t('Help text'),
|
| 136 |
'#default_value' => $group['description'],
|
| 137 |
'#rows' => 5,
|
| 138 |
'#description' => t('Instructions to present to the user on the editing form.'),
|
| 139 |
'#required' => FALSE,
|
| 140 |
);
|
| 141 |
$form['weight'] = array(
|
| 142 |
'#type' => 'weight',
|
| 143 |
'#title' => t('Weight'),
|
| 144 |
'#default_value' => $group['weight'],
|
| 145 |
'#description' => t('In the node editing form, the heavier groups will sink and the lighter groups will be positioned nearer the top.'),
|
| 146 |
);
|
| 147 |
$form['#submit'] = array(fieldgroup_edit_group_submit => array($content_type, $group_name));
|
| 148 |
|
| 149 |
|
| 150 |
return drupal_get_form('fieldgroup_edit_group', $form);
|
| 151 |
}
|
| 152 |
|
| 153 |
function fieldgroup_edit_group_submit($form_id, &$form_values, $content_type, $group_name) {
|
| 154 |
$groups = fieldgroup_groups($content_type);
|
| 155 |
$group = $groups[$group_name];
|
| 156 |
|
| 157 |
if (!$group) {
|
| 158 |
// Find a valid, computer-friendly name.
|
| 159 |
$group_name = trim($form_values['label']);
|
| 160 |
$group_name = drupal_strtolower($group_name);
|
| 161 |
$group_name = str_replace(array(' ', '-'), '_', $group_name);
|
| 162 |
$group_name = preg_replace('/[^a-z0-9_]/', '', $group_name);
|
| 163 |
$group_name = 'group-'. $group_name;
|
| 164 |
$group_name = substr($group_name, 0, 32);
|
| 165 |
if (isset($groups[$group_name])) {
|
| 166 |
$counter = 0;
|
| 167 |
while (isset($groups[$group_name])) {
|
| 168 |
$group_name = substr($group_name, 0, 30) .'_'. $counter++;
|
| 169 |
}
|
| 170 |
}
|
| 171 |
db_query("INSERT INTO {node_group} (type_name, group_name, label, settings, description, weight)
|
| 172 |
VALUES ('%s', '%s', '%s', '%s', '%s', %d)", $content_type, $group_name, $form_values['label'], serialize($form_values['settings']), $form_values['description'], $form_values['weight']);
|
| 173 |
}
|
| 174 |
else {
|
| 175 |
db_query("UPDATE {node_group} SET label = '%s', settings = '%s', description = '%s', weight = %d ".
|
| 176 |
"WHERE type_name = '%s' AND group_name = '%s'",
|
| 177 |
$form_values['label'], serialize($form_values['settings']), $form_values['description'], $form_values['weight'], $content_type, $group_name);
|
| 178 |
}
|
| 179 |
cache_clear_all('fieldgroup_data');
|
| 180 |
return 'admin/node/types/'. $content_type .'/groups';
|
| 181 |
}
|
| 182 |
|
| 183 |
function fieldgroup_remove_group($content_type, $group_name) {
|
| 184 |
$groups = fieldgroup_groups($content_type);
|
| 185 |
$group = $groups[$group_name];
|
| 186 |
|
| 187 |
if (!$group) {
|
| 188 |
drupal_not_found();
|
| 189 |
exit;
|
| 190 |
}
|
| 191 |
|
| 192 |
$form['#submit'] = array(fieldgroup_remove_group_submit => array($content_type, $group_name));
|
| 193 |
return confirm_form('fieldgroup_remove_group', $form,
|
| 194 |
t('Are you sure you want to remove the group %label?',
|
| 195 |
array('%label' => theme('placeholder', $group['label']))),
|
| 196 |
'admin/node/types/'. $content_type .'/groups', t('This action cannot be undone.'),
|
| 197 |
t('Remove'), t('Cancel'));
|
| 198 |
}
|
| 199 |
|
| 200 |
function fieldgroup_remove_group_submit($form_id, &$form_values, $content_type, $group_name) {
|
| 201 |
db_query("DELETE FROM {node_group} WHERE type_name = '%s' AND group_name = '%s'", $content_type, $group_name);
|
| 202 |
db_query("DELETE FROM {node_group_fields} WHERE type_name = '%s' AND group_name = '%s'", $content_type, $group_name);
|
| 203 |
cache_clear_all('fieldgroup_data');
|
| 204 |
|
| 205 |
drupal_set_message('The group has been removed.');
|
| 206 |
return 'admin/node/types/'. $content_type .'/groups';
|
| 207 |
}
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
/*
|
| 212 |
* Returns all groups for a content type
|
| 213 |
*/
|
| 214 |
function fieldgroup_groups($content_type, $sorted = FALSE) {
|
| 215 |
|
| 216 |
static $groups = array();
|
| 217 |
static $groups_sorted = array();
|
| 218 |
|
| 219 |
if (empty($groups)) {
|
| 220 |
if ($cached = cache_get('fieldgroup_data')) {
|
| 221 |
$data = unserialize($cached->data);
|
| 222 |
$groups = &$data['groups'];
|
| 223 |
$groups_sorted = &$data['groups_sorted'];
|
| 224 |
}
|
| 225 |
else {
|
| 226 |
$result = db_query("SELECT g.* FROM {node_group} g ORDER BY g.weight, g.group_name");
|
| 227 |
|
| 228 |
while ($group = db_fetch_array($result)) {
|
| 229 |
$group['settings'] = unserialize($group['settings']);
|
| 230 |
$group['fields'] = array();
|
| 231 |
$groups[$group['type_name']][$group['group_name']] = $group;
|
| 232 |
$groups_sorted[$group['type_name']][] = &$groups[$group['type_name']][$group['group_name']];
|
| 233 |
}
|
| 234 |
//load fields
|
| 235 |
$result = db_query("SELECT fi.*, g.group_name FROM {node_group} g ".
|
| 236 |
"JOIN {node_group_fields} f USING(type_name, group_name) ".
|
| 237 |
"JOIN {node_field_instance} fi USING(field_name, type_name) ".
|
| 238 |
"ORDER BY fi.weight");
|
| 239 |
while ($field = db_fetch_array($result)) {
|
| 240 |
$groups[$field['type_name']][$field['group_name']]['fields'][$field['field_name']] = $field;
|
| 241 |
}
|
| 242 |
cache_set('fieldgroup_data', serialize(array('groups' => $groups, 'groups_sorted' => $groups_sorted)), CACHE_PERMANENT);
|
| 243 |
}
|
| 244 |
}
|
| 245 |
|
| 246 |
if (!$groups[$content_type]) {
|
| 247 |
return array();
|
| 248 |
}
|
| 249 |
return $sorted ? $groups_sorted[$content_type] : $groups[$content_type];
|
| 250 |
}
|
| 251 |
|
| 252 |
|
| 253 |
function _fieldgroup_groups_label($content_type) {
|
| 254 |
$groups = fieldgroup_groups($content_type);
|
| 255 |
|
| 256 |
$labels[0] = t('No group.');
|
| 257 |
foreach ($groups as $group_name => $group) {
|
| 258 |
$labels[$group_name] = $group['label'];
|
| 259 |
}
|
| 260 |
return $labels;
|
| 261 |
}
|
| 262 |
|
| 263 |
function _fieldgroup_field_get_group($content_type, $field_name) {
|
| 264 |
return db_result(db_query("SELECT group_name FROM {node_group_fields} WHERE type_name = '%s' AND field_name = '%s'", $content_type, $field_name));
|
| 265 |
}
|
| 266 |
|
| 267 |
/*
|
| 268 |
* Implementation of hook_form_alter()
|
| 269 |
*/
|
| 270 |
function fieldgroup_form_alter($form_id, &$form) {
|
| 271 |
|
| 272 |
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id &&
|
| 273 |
node_get_base($form['#node']) == 'content') {
|
| 274 |
|
| 275 |
foreach (fieldgroup_groups($form['type']['#value']) as $group_name => $group) {
|
| 276 |
$form[$group_name] = array(
|
| 277 |
'#type' => 'fieldset',
|
| 278 |
'#title' => t($group['label']),
|
| 279 |
'#collapsed' => $group['settings']['collapsed'],
|
| 280 |
'#collapsible' => $group['settings']['collapsible'],
|
| 281 |
'#description' => $group['description'],
|
| 282 |
'#weight' => $group['weight'],
|
| 283 |
);
|
| 284 |
foreach ($group['fields'] as $field_name => $field) {
|
| 285 |
if (isset($form[$field_name])) {
|
| 286 |
$form[$group_name][$field_name] = $form[$field_name];
|
| 287 |
unset($form[$field_name]);
|
| 288 |
}
|
| 289 |
}
|
| 290 |
if (!empty($group['fields']) && !element_children($form[$group_name])) {
|
| 291 |
//hide the fieldgroup, because the fields are hidden too
|
| 292 |
unset($form[$group_name]);
|
| 293 |
}
|
| 294 |
}
|
| 295 |
}
|
| 296 |
else if ($form_id == '_content_admin_field') {
|
| 297 |
$form['widget']['group'] = array(
|
| 298 |
'#type' => 'select',
|
| 299 |
'#title' => t('Display in group'),
|
| 300 |
'#options' => _fieldgroup_groups_label(arg(3)),
|
| 301 |
'#default_value' => _fieldgroup_field_get_group(arg(3), arg(5)),
|
| 302 |
'#description' => t('Select a group, in which the field will be displayed on the editing form.'),
|
| 303 |
'#weight' => 5,
|
| 304 |
);
|
| 305 |
$form['widget']['weight']['#weight'] = 5;
|
| 306 |
$form['widget']['description']['#weight'] = 7;
|
| 307 |
$form['#submit']['fieldgroup_content_admin_form_submit'] = array($form['widget']['group']['#default_value']);
|
| 308 |
}
|
| 309 |
else if ($form_id == '_content_admin_field_remove' || $form_id == '_content_admin_type_delete') {
|
| 310 |
$form['#submit']['fieldgroup_content_admin_remove_submit'] = array();
|
| 311 |
}
|
| 312 |
}
|
| 313 |
|
| 314 |
function fieldgroup_content_admin_form_submit($form_id, &$form_values, $default) {
|
| 315 |
|
| 316 |
if ($default != $form_values['group']) {
|
| 317 |
if ($form_values['group'] && !$default) {
|
| 318 |
db_query("INSERT INTO {node_group_fields} (type_name, group_name, field_name) VALUES ('%s', '%s', '%s')",
|
| 319 |
arg(3), $form_values['group'], arg(5));
|
| 320 |
}
|
| 321 |
else if ($form_values['group']) {
|
| 322 |
db_query("UPDATE {node_group_fields} SET group_name = '%s' WHERE type_name = '%s' AND field_name = '%s'",
|
| 323 |
$form_values['group'], arg(3), arg(5));
|
| 324 |
}
|
| 325 |
else {
|
| 326 |
db_query("DELETE FROM {node_group_fields} WHERE type_name = '%s' AND field_name = '%s'", arg(3), arg(5));
|
| 327 |
}
|
| 328 |
cache_clear_all('fieldgroup_data');
|
| 329 |
}
|
| 330 |
}
|
| 331 |
|
| 332 |
function fieldgroup_content_admin_remove_submit($form_id, &$form_values) {
|
| 333 |
if ($form_values['field_name']) {
|
| 334 |
//a field has been removed
|
| 335 |
db_query("DELETE FROM {node_group_fields} WHERE type_name = '%s' AND field_name = '%s'", $form_values['type_name'], $form_values['field_name']);
|
| 336 |
}
|
| 337 |
else {
|
| 338 |
//the whole content-type has been deleted
|
| 339 |
db_query("DELETE FROM {node_group_fields} WHERE type_name = '%s'", $form_values['type_name']);
|
| 340 |
db_query("DELETE FROM {node_group} WHERE type_name = '%s'", $form_values['type_name']);
|
| 341 |
}
|
| 342 |
}
|
| 343 |
|
| 344 |
|
| 345 |
/*
|
| 346 |
* Gets the group name for a field
|
| 347 |
* If the field isn't in a group, FALSE will be returned.
|
| 348 |
* @return The name of the group, or FALSE.
|
| 349 |
*/
|
| 350 |
function fieldgroup_get_group($content_type, $field_name) {
|
| 351 |
foreach (fieldgroup_groups($content_type) as $group_name => $group) {
|
| 352 |
if (in_array($field_name, array_keys($group['fields']))) {
|
| 353 |
return $group_name;
|
| 354 |
}
|
| 355 |
}
|
| 356 |
return FALSE;
|
| 357 |
}
|