[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/date/date_tools/ -> date_tools.wizard.inc (source)

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


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7