| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: date_tools.wizard.inc,v 1.1.2.4 2010/08/12 14:14:57 karens Exp $ 3 4 function date_tools_wizard_form() { 5 6 $form = array(); 7 $form['type'] = array( 8 '#type' => 'fieldset', 9 '#title' => t('Content type'), 10 ); 11 $form['type']['type_name'] = array( 12 '#type' => 'textfield', 13 '#default_value' => 'date', 14 '#title' => t('Content type name'), 15 '#description' => t('Machine-readable name. Allowed values: (a-z, 0-9, _). If this is not an existing content type, the content type will be created.'), 16 ); 17 $form['type']['name'] = array( 18 '#type' => 'textfield', 19 '#default_value' => t('Date'), 20 '#title' => t('Content type label'), 21 '#description' => t('The human-readable name for this content type. Only needed when creating a new content type.'), 22 ); 23 $form['type']['type_description'] = array( 24 '#type' => 'textarea', 25 '#default_value' => t('A date content type that is linked to a Views calendar.'), 26 '#title' => t('Content type description'), 27 '#description' => t('A description for the content type. Only needed when creating a new content type.'), 28 ); 29 $form['field'] = array( 30 '#type' => 'fieldset', 31 '#title' => t('Date field'), 32 ); 33 $form['field']['field_name'] = array( 34 '#type' => 'textfield', 35 '#default_value' => 'date', 36 '#field_prefix' => 'field_', 37 '#title' => t('Date field name'), 38 '#description' => t('Machine-readable name. Allowed values: (a-z, 0-9, _) Must not be an existing field name.'), 39 ); 40 $form['field']['label'] = array( 41 '#tree' => TRUE, 42 '#type' => 'textfield', 43 '#default_value' => t('Date'), 44 '#title' => t('Date field label'), 45 '#description' => t('The human-readable label for this field.'), 46 ); 47 $form['field']['widget_type'] = array( 48 '#type' => 'select', 49 '#options' => date_tools_wizard_widget_types(), 50 '#default_value' => 'date_select', 51 '#title' => t('Date widget type'), 52 ); 53 $form['field']['repeat'] = array( 54 '#type' => 'select', 55 '#default_value' => 0, 56 '#options' => array(0 => t('No'), 1 => t('Yes')), 57 '#title' => t('Show repeating date options'), 58 '#access' => module_exists('date_repeat'), 59 ); 60 $form['field']['advanced'] = array( 61 '#type' => 'fieldset', 62 '#collapsible' => TRUE, 63 '#collapsed' => TRUE, 64 '#title' => t('Advanced options'), 65 ); 66 $form['field']['advanced']['field_type'] = array( 67 '#type' => 'select', 68 '#options' => date_tools_wizard_field_types(), 69 '#default_value' => 'datetime', 70 '#title' => t('Date field type'), 71 '#description' => t("The recommend type is Datetime, except for historical dates or dates with only year or month granularity. Older or incomplete dates should use the Date type (an ISO date)."), 72 ); 73 $form['field']['advanced']['granularity'] = array( 74 '#type' => 'select', 75 '#options' => date_granularity_names(), 76 '#default_value' => array('month', 'day', 'year', 'hour', 'minute'), 77 '#title' => t('Granularity'), 78 '#multiple' => TRUE, 79 ); 80 $form['field']['advanced']['tz_handling'] = array( 81 '#type' => 'select', 82 '#options' => date_tools_wizard_tz_handling(), 83 '#default_value' => 'site', 84 '#title' => t('Date timezone handling'), 85 '#description' => t("Timezone handling should be set to 'none' for granularity without time elements.") 86 ); 87 $form['calendar'] = array( 88 '#type' => 'select', 89 '#default_value' => 1, 90 '#options' => array(0 => t('No'), 1 => t('Yes')), 91 '#title' => t('Create a calendar for this date field'), 92 '#access' => module_exists('calendar'), 93 ); 94 $form['blocks'] = array( 95 '#type' => 'select', 96 '#options' => array(0 => t('No'), 1 => t('Yes')), 97 '#default_value' => 0, 98 '#title' => t('Add calendar blocks to the current theme'), 99 '#access' => module_exists('calendar'), 100 ); 101 102 $form['submit'] = array( 103 '#type' => 'submit', 104 '#value' => t('Save'), 105 ); 106 return $form; 107 } 108 109 function date_tools_wizard_form_validate(&$form, &$form_state) { 110 $type_name = $form_state['values']['type_name']; 111 $field_name = 'field_'. $form_state['values']['field_name']; 112 if (db_result(db_query("SELECT type FROM {node_type} WHERE type='%s'", $type_name))) { 113 drupal_set_message(t('This content type name already exists, adding new field to existing content type.')); 114 } 115 if (!preg_match('!^[a-z0-9_]+$!', $type_name)) { 116 form_set_error('type_name', t('The machine-readable name must contain only lowercase letters, numbers, and underscores.')); 117 } 118 if (db_result(db_query("SELECT field_name FROM {content_node_field_instance} WHERE field_name='%s' AND type_name='%s'", $field_name, $type_name))) { 119 form_set_error('field_name', t('This field name already exists.')); 120 } 121 if (!date_has_time($form_state['values']['granularity']) && $form_state['values']['tz_handling'] != 'none') { 122 form_set_error('tz_handling', t('Timezone handling must be none for granularity without time.')); 123 } 124 } 125 126 function date_tools_wizard_form_submit(&$form, &$form_state) { 127 $view_name = date_tools_wizard_build($form_state['values']); 128 menu_rebuild(); 129 if (!empty($view_name)) { 130 drupal_set_message(t('Change the calendar as needed and save the view.'), 'error'); 131 $form['#redirect'] = 'admin/build/views/edit/'. $view_name; 132 } 133 else { 134 $form['#redirect'] = 'admin/content/node-type/'. str_replace('_', '-', $form_state['values']['type_name']) .'/fields'; 135 } 136 } 137 138 function date_tools_wizard_build($form_values) { 139 extract($form_values); 140 141 $field_name = 'field_'. $field_name; 142 143 if (!empty($repeat)) { 144 $widget_type .= '_repeat'; 145 } 146 147 // Create a node type if it doesn't already exist. 148 // This makes it possible to add additional date fields to 149 // an existing type. 150 $types = node_get_types('names'); 151 $type_settings = array(); 152 if (!array_key_exists($type_name, $types)) { 153 date_tools_wizard_create_content_type($name, $type_name, $type_description, $type_settings); 154 } 155 else { 156 $types = node_get_types(); 157 $type = $types[$type_name]; 158 if (!empty($type_settings)) { 159 foreach ($type_settings as $key => $setting) { 160 $type->$key = $setting; 161 } 162 node_type_save($type); 163 } 164 $name = $type->name; 165 } 166 167 $field_settings = array( 168 'field_name' => $field_name, 169 'label' => $label, 170 'type_name' => $type_name, 171 'type' => $field_type, 172 'widget_type' => $widget_type, 173 'granularity' => $granularity, 174 'tz_handling' => $tz_handling, 175 'repeat' => $repeat, 176 'multiple' => $repeat, 177 ); 178 179 $field_settings['widget']['label'] = $label; 180 $field_settings['widget']['type'] = $widget_type; 181 182 $date_field = date_tools_wizard_create_date_field($field_type, $widget_type, $tz_handling, $field_settings); 183 184 $view_name = ''; 185 if (!empty($calendar)) { 186 $view_name = date_tools_wizard_create_calendar($name, $type_name, $date_field); 187 if (!empty($blocks)) { 188 date_tools_wizard_create_blocks($type_name); 189 } 190 } 191 return $view_name; 192 193 } 194 195 function date_tools_wizard_include() { 196 module_load_include('inc', 'node', 'content_types'); 197 module_load_include('inc', 'node', 'node.pages'); 198 module_load_include('inc', 'content', 'includes/content.admin'); 199 module_load_include('inc', 'content', 'includes/content.crud'); 200 module_load_include('inc', 'date', 'date_admin'); 201 } 202 203 function date_tools_wizard_options($options = array()) { 204 $default_options = array(); 205 if (module_exists('date_popup')) { 206 $default_options[] = 'popups'; 207 } 208 if (module_exists('date_repeat')) { 209 $default_options[] = 'repeat'; 210 } 211 if (module_exists('calendar')) { 212 $default_options[] = 'linked'; 213 } 214 215 // Allow override of default options. 216 if (!empty($options)) { 217 return $options; 218 } 219 else { 220 return $default_options; 221 } 222 } 223 224 /** 225 * Return an array of the modules needed by this wizard. 226 */ 227 function date_tools_wizard_required_modules($options = array()) { 228 $options = date_tools_wizard_options($options); 229 $modules = array( 230 'date_timezone', 'date_api', 'content', 'date', 231 ); 232 if (module_exists('calendar')) { 233 $modules = array_merge($modules, array('calendar', 'views', 'views_ui')); 234 } 235 if (in_array('popups', $options)) { 236 $modules = array_merge($modules, array('date_popup', 'jcalendar')); 237 } 238 if (in_array('repeat', $options)) { 239 $modules = array_merge($modules, array('date_repeat')); 240 } 241 return $modules; 242 } 243 244 function date_tools_wizard_modules() { 245 return array( 246 'date' => t('Date'), 247 'date_api' => t('Date API'), 248 'date_timezone' => t('Date Timezone'), 249 'content' => t('Content'), 250 'calendar' => t('Calendar'), 251 'calendar_ical' => t('Calendar iCal'), 252 'date_repeat' => t('Date Repeat'), 253 'date_popup' => t('Date Popup'), 254 'jcalendar' => t('Calendar Popup'), 255 'text' => t('Text'), 256 'optionwidgets' => t('Optionwidgets'), 257 'nodereference' => t('Nodereference'), 258 'views' => t('Views'), 259 'views_ui' => t('Views UI'), 260 ); 261 } 262 263 function date_tools_wizard_enabled_modules($options = array()) { 264 $modules = date_tools_wizard_required_modules($options); 265 $enabled = array(); 266 $names = date_tools_wizard_modules(); 267 foreach ($modules as $option) { 268 if (module_exists($option)) { 269 $enabled[$option] = $names[$option] .' (<span class="admin-enabled">'. t('enabled') .'</span>)'; 270 } 271 } 272 return $enabled; 273 } 274 275 function date_tools_wizard_disabled_modules($options = array()) { 276 $modules = date_tools_wizard_required_modules($options); 277 $enabled = array(); 278 $names = date_tools_wizard_modules(); 279 foreach ($modules as $option) { 280 if (!module_exists($option)) { 281 $enabled[$option] = $names[$option] .' (<span class="admin-disabled">'. t('disabled') .'</span>)'; 282 } 283 } 284 return $enabled; 285 } 286 287 function date_tools_wizard_field_types() { 288 $field_types = array(); 289 foreach (date_field_info() as $name => $info) { 290 $field_types[$name] = $info['label']; //.' - '. $info['description']; 291 } 292 return $field_types; 293 } 294 295 function date_tools_wizard_widget_types() { 296 $widget_types = array(); 297 foreach (date_widget_info() as $name => $info) { 298 if (!strstr($name, '_repeat')) { 299 $widget_types[$name] = $info['label']; 300 } 301 } 302 return $widget_types; 303 } 304 305 function date_tools_wizard_tz_handling() { 306 include_once(drupal_get_path('module', 'date') .'/date_admin.inc'); 307 return date_timezone_handling_options(); 308 } 309 310 function date_tools_wizard_create_content_type($name, $type_name, $description, $type_settings = array()) { 311 312 date_tools_wizard_include(); 313 314 // Create the content type. 315 $values = array ( 316 'name' => $name, 317 'type' => $type_name, 318 'description' => $description, 319 'title_label' => 'Title', 320 'body_label' => 'Body', 321 'min_word_count' => '0', 322 'help' => '', 323 'node_options' => 324 array ( 325 'status' => 1, 326 'promote' => 1, 327 'sticky' => 0, 328 'revision' => 0, 329 ), 330 'language_content_type' => '0', 331 'old_type' => $type_name, 332 'orig_type' => '', 333 'module' => 'node', 334 'custom' => '1', 335 'modified' => '1', 336 'locked' => '0', 337 'url_str' => str_replace('_', '-', $type_name), 338 ); 339 340 // Allow overrides of these values. 341 foreach ($type_settings as $key => $value) { 342 $values[$key] = $value; 343 } 344 345 $type = (object) _node_type_set_defaults($values); 346 node_type_save($type); 347 348 if ($type == 'ical_feed') { 349 // Default type to not be promoted and have comments disabled. 350 variable_set('node_options_'. $type_name, array('status')); 351 variable_set('comment_'. $type_name, COMMENT_NODE_DISABLED); 352 353 // Don't display date and author information for this type by default. 354 $theme_settings = variable_get('theme_settings', array()); 355 $theme_settings['toggle_node_info_'. $type_name] = FALSE; 356 variable_set('theme_settings', $theme_settings); 357 } 358 359 // Update the menu router information. 360 menu_rebuild(); 361 content_clear_type_cache(); 362 363 } 364 365 366 function date_tools_wizard_create_date_field($field_type, $widget_type, $tz_handling, $overrides = array()) { 367 date_tools_wizard_include(); 368 369 $field_name = $overrides['field_name']; 370 $type_name = $overrides['type_name']; 371 372 // Create the date field. 373 374 // Set overrides that apply to all date fields. 375 $base_overrides = array( 376 377 'field_name' => $field_name, 378 'type_name' => $type_name, 379 380 // Can be set to 0 (none), 1 (unlimited or repeating), 381 // or 2-9 for a fixed number of values. 382 'multiple' => 0, 383 384 // Set a display format that will show complete date information, 385 // to help test that values are correct. 386 'default_format' => 'long', 387 388 // Set the date granularity. 389 'granularity' => array ( 390 'year' => 'year', 391 'month' => 'month', 392 'day' => 'day', 393 'hour' => 'hour', 394 'minute' => 'minute', 395 ), 396 397 // Shall this date include a 'To date'?, can be blank, 'optional', or 'required' 398 'todate' => 'optional', 399 400 ); 401 402 // Widget overrides: 403 $widget_overrides = array( 404 405 // Move the date right below the title. 406 'weight' => -4, 407 408 // Set default values to 'blank', 'now', or 'relative'. To dates can also use 'same'. 409 'default_value' => 'now', 410 'default_value_code' => '', // The code to use with 'relative', like '+1 day'. 411 'default_value2' => 'blank', 412 'default_value_code2' => '', // The code to use with 'relative', like '+1 day'. 413 414 // Set format string to use for input forms, it controls order and type of date parts. 415 // Input formats must have all date parts, including seconds. 416 'input_format_custom' => variable_get('date_format_short', 'm/d/Y - H:i') .':s', 417 418 // Increment for minutes and seconds, can be 1, 5, 10, 15, or 30. 419 'increment' => 15, 420 421 // Optional array of date parts that should be textfields in select widgets, 422 // can be any combination of year, month, day, hour, minute, second. 423 'text_parts' => array (), 424 425 // The number of years to go back and forward in drop-down year selectors. 426 'year_range' => '0:+1', 427 428 // The place for the date part labels, can be 'above', 'within', or 'none'. 429 'label_position' => 'above', 430 ); 431 432 $overrides = array_merge($base_overrides, $overrides); 433 $overrides['widget'] = array_merge($widget_overrides, $overrides['widget']); 434 435 // Get field default values for this combination of field and widget type, 436 // using a helper function in the Date module. 437 $field = date_field_default_values($field_type, $widget_type, $tz_handling, $overrides); 438 439 $field = content_field_instance_collapse($field); 440 $field = content_field_instance_create($field); 441 $field = content_field_instance_expand($field); 442 443 return $field; 444 } 445 446 function date_tools_wizard_create_calendar($name, $type_name, $date_field) { 447 448 $date_name = !empty($date_field) ? $date_field['field_name'] : ''; 449 $path = 'calendar-'. str_replace('field_', '', $date_name); 450 451 // Add a default calendar for this content type. 452 $calendar_options = variable_get('calendar_default_view_options', array()); 453 if (array_key_exists('calendar_'. $type_name, $calendar_options)) { 454 unset($calendar_options['calendar_'. $type_name]); 455 } 456 $option = array( 457 'name' => 'calendar_'. $type_name, 458 'description' => 'An event calendar for '. $date_field['widget']['label'], 459 'path' => $path, 460 'types' => array($type_name => $name), 461 'date_fields' => array($date_name), 462 'display_fields' => array('title' => array(), $date_name => array()), 463 ); 464 $calendar_options['calendar_'. $type_name] = $option; 465 variable_set('calendar_default_view_options', $calendar_options); 466 467 // Make sure menu items get rebuilt as necessary. 468 menu_rebuild(); 469 470 // Clear the views cache. 471 cache_clear_all('*', 'cache_views'); 472 473 // Clear the page cache. 474 cache_clear_all(); 475 476 // Remove this view from cache so we can edit it properly. 477 views_object_cache_clear('view', 'calendar_'. $type_name); 478 479 // Force Views to empty its cache and find the new view. 480 views_discover_default_views(); 481 482 return 'calendar_'. $type_name; 483 } 484 485 function date_tools_wizard_create_blocks($type_name) { 486 // Add calendar blocks to the default theme. 487 $current_theme = variable_get('theme_default', 'garland'); 488 489 // Legend block. 490 $block = new stdClass(); 491 $block->theme = $current_theme; 492 $block->status = 1; 493 $block->weight = -1; 494 $block->region = 'left'; 495 $block->title = ''; 496 $block->module = 'calendar'; 497 $block->delta = 0; 498 date_tools_wizard_add_block($block); 499 500 // Mini calendar block. 501 $block->module = 'views'; 502 $block->delta = 'calendar_'. $type_name .'-calendar_block_1'; 503 date_tools_wizard_add_block($block); 504 505 // Upcoming events block. 506 $block->module = 'views'; 507 $block->delta = 'calendar_'. $type_name .'-block_1'; 508 date_tools_wizard_add_block($block); 509 return; 510 } 511 512 /** 513 * Add a block. 514 */ 515 function date_tools_wizard_add_block($block) { 516 $bid = db_result(db_query("SELECT bid FROM {blocks} WHERE module='%s' AND delta='%s' AND theme='%s'", $block->module, $block->delta, $block->theme)); 517 if (empty($bid)) { 518 $block->bid = NULL; 519 drupal_write_record('blocks', $block); 520 } 521 else { 522 $block->bid = $bid; 523 drupal_write_record('blocks', $block, 'bid'); 524 } 525 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |