Projet

Général

Profil

Révision 599a39cd

Ajouté par Assos Assos il y a environ 3 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/date/date_repeat_field/date_repeat_field.module
2 2

  
3 3
/**
4 4
 * @file
5
 * Creates the option of Repeating date fields and manages Date fields that use the Date Repeat API.
5
 * Creates the option of Repeating Date fields and manages Date Repeat fields.
6 6
 *
7 7
 * The Repeating functionality is pretty tightly intermingled with other code,
8 8
 * so the process of pulling it out into this module will happen gradually.
......
32 32
        'item' => NULL,
33 33
        'entity_type' => NULL,
34 34
        'entity' => NULL,
35
        'dates' => NULL
35
        'dates' => NULL,
36 36
      ),
37 37
      'function' => 'theme_date_repeat_display',
38 38
    ),
......
44 44
/**
45 45
 * Theme the human-readable description for a Date Repeat rule.
46 46
 *
47
 * TODO -
48
 * add in ways to store the description in the date so it isn't regenerated
49
 * over and over and find a way to allow description to be shown or hidden.
47
 * @todo Add in ways to store the description in the date so it isn't
48
 * regenerated over and over and find a way to allow description to be shown or
49
 * hidden.
50 50
 */
51 51
function theme_date_repeat_display($vars) {
52 52
  $field = $vars['field'];
......
62 62

  
63 63
/**
64 64
 * Implements hook_menu().
65
 *
66
 * Add menu tabs to display pages with details about repeating date values.
67 65
 */
68 66
function date_repeat_field_menu() {
67
  // Add menu tabs to display pages with details about repeating date values.
69 68
  $items = array();
70 69

  
71 70
  $values = date_repeat_field_bundles();
......
126 125
 *
127 126
 * @return bool
128 127
 *   Return TRUE if there is at least one date field attached to this entity,
129
 *   and the current user has the permission 'view date repeats'; FALSE otherwise.
128
 *   and the current user has the permission 'view date repeats'.
130 129
 */
131 130
function date_repeat_field_show($entity_type = 'node', $entity = NULL) {
132 131
  if (!user_access('view date repeats')) {
......
181 180
}
182 181

  
183 182
/**
184
 * Return an array of all entity types and bundles that have repeating date fields.
183
 * All entity types and bundles that have repeating date fields.
185 184
 */
186 185
function date_repeat_field_bundles() {
187 186
  $values = array();
......
224 223
 * Implements hook_date_field_insert_alter().
225 224
 */
226 225
function date_repeat_field_date_field_insert_alter(&$items, $context) {
227

  
228 226
  $entity = $context['entity'];
229 227
  $field = $context['field'];
230 228
  $instance = $context['instance'];
......
248 246
 * Implements hook_date_field_update_alter().
249 247
 */
250 248
function date_repeat_field_date_field_update_alter(&$items, $context) {
251

  
252 249
  // If an RRULE with a frequency of NONE made it this far, unset it.
253 250
  if (!empty($items[0]['rrule']) && strpos($items[0]['rrule'], 'FREQ=NONE')) {
254 251
    $items[0]['rrule'] = NULL;
255 252
  }
256 253

  
257
  // If you have a repeating date field on a user and don't check the box to repeat it,
258
  // we end up with $items[0]['rrule'] = array('additions' => '', 'exceptions' => ''));
259
  // This will clean it up by getting rid of those bogus values.
260
  // @TODO Figure out where that's coming from. It doesn't happen on nodes.
254
  // If you have a repeating date field on a user and don't check the box to
255
  // repeat it, we end up with $items[0]['rrule'] = array('additions' => '',
256
  // 'exceptions' => '')); This will clean it up by getting rid of those bogus
257
  // values.
258
  // @todo Figure out where that's coming from. It doesn't happen on nodes.
261 259
  if (!empty($items[0]['rrule']) && is_array($items[0]['rrule'])) {
262 260
    $items[0]['rrule'] = NULL;
263 261
  }
......
267 265
 * Implements hook_field_widget_form_alter().
268 266
 */
269 267
function date_repeat_field_field_widget_form_alter(&$element, &$form_state, $context) {
270

  
271 268
  $field = $context['field'];
272 269
  $instance = $context['instance'];
273 270
  $items = $context['items'];
......
298 295
/**
299 296
 * Validation for date repeat form element.
300 297
 *
301
 * Create multiple values from the RRULE results.
302
 * Lots more work needed here.
298
 * Create multiple values from the RRULE results. Lots more work needed here.
303 299
 */
304 300
function date_repeat_field_widget_validate($element, &$form_state) {
305 301
  $field = field_widget_field($element, $form_state);
......
314 310
  // If the widget has been hidden by #access, the RRULE will still be in its
315 311
  // original string form here. Nothing to process.
316 312
  if (date_hidden_element($element)) {
317

  
318
    // If this was a hidden repeating date, we lost all the repeating values in the widget processing.
319
    // Add them back here if that happened since we are skipping the re-creation of those values.
313
    // If this was a hidden repeating date, we lost all the repeating values in
314
    // the widget processing. Add them back here if that happened since we are
315
    // skipping the re-creation of those values.
320 316
    if (!empty($form_state['storage']['date_items'][$field_name])) {
321 317
      array_pop($element['#parents']);
322 318
      form_set_value($element, $form_state['storage']['date_items'][$field_name][$langcode], $form_state);
......
327 323
  module_load_include('inc', 'date_repeat', 'date_repeat_form');
328 324
  $instance = field_widget_instance($element, $form_state);
329 325

  
330
  // Here 'values' returns an array of input values, which includes the original RRULE, as a string.
331
  // and 'input' returns an array of the form elements created by the repeating date widget, with
332
  // RRULE values as an array of the selected elements and their chosen values.
326
  // Here 'values' returns an array of input values, which includes the
327
  // original RRULE, as a string. and 'input' returns an array of the form
328
  // elements created by the repeating date widget, with RRULE values as an
329
  // array of the selected elements and their chosen values.
333 330
  $item = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
334 331
  $input = drupal_array_get_nested_value($form_state['input'], $element['#parents'], $input_exists);
335 332

  
336 333
  $rrule_values = date_repeat_merge($input['rrule'], $element['rrule']);
337 334

  
338
  // If no repeat information was set, treat this as a normal, non-repeating value.
335
  // If no repeat information was set, treat this as a normal, non-repeating
336
  // value.
339 337
  if ($rrule_values['FREQ'] == 'NONE' || empty($input['show_repeat_settings'])) {
340 338
    $item['rrule'] = NULL;
341 339
    form_set_value($element, $item, $form_state);
......
373 371
    form_set_error($error_field_count, t("Missing value in 'Range of repeat'. (COUNT).", array(), array('context' => 'Date repeat')));
374 372
  }
375 373

  
374
  $error_field_count = $error_field_base . '][rrule][weekly][INTERVAL';
375
  if (isset($rrule_values['INTERVAL']) && isset($rrule_values['FREQ']) && !empty($item['rrule']) && $rrule_values['FREQ'] == 'WEEKLY' && $rrule_values['INTERVAL'] == '') {
376
    form_set_error($error_field_count, t('Missing value in Repeats Every # of weeks. (INTERVAL).', array(), array('context' => 'Date repeat')));
377
  }
378

  
379
  $error_field_count = $error_field_base . '][rrule][daily][INTERVAL_child';
380
  if (isset($rrule_values['INTERVAL']) && isset($rrule_values['FREQ']) && !empty($item['rrule']) && $rrule_values['FREQ'] == 'DAILY' && $rrule_values['INTERVAL'] == '') {
381
    form_set_error($error_field_count, t('Missing value in Repeats Every # of days. (INTERVAL).', array(), array('context' => 'Date repeat')));
382
  }
383

  
376 384
  if (form_get_errors()) {
377 385
    return;
378 386
  }
......
380 388
  // If the rule, the start date, or the end date have changed, re-calculate
381 389
  // the repeating dates, wipe out the previous values, and populate the
382 390
  // field with the new values.
383

  
384 391
  $rrule = $item['rrule'];
385 392
  if (!empty($rrule)) {
386

  
387 393
    // Avoid undefined index problems on dates that don't have all parts.
388 394
    $possible_items = array('value', 'value2', 'timezone', 'offset', 'offset2');
389 395
    foreach ($possible_items as $key) {
......
392 398
      }
393 399
    }
394 400

  
395
    // We only collect a date for UNTIL, but we need it to be inclusive,
396
    // so force it to a full datetime element at the last possible second of the day.
401
    // We only collect a date for UNTIL, but we need it to be inclusive, so
402
    // force it to a full datetime element at the last possible second of the
403
    // day.
397 404
    if (!empty($rrule_values['UNTIL'])) {
398 405
      $gran = array('year', 'month', 'day', 'hour', 'minute', 'second');
399 406
      $rrule_values['UNTIL']['datetime'] .= ' 23:59:59';
......
403 410
    $value = date_repeat_build_dates($rrule, $rrule_values, $field, $item);
404 411
    // Unset the delta value of the parents.
405 412
    array_pop($element['#parents']);
406
    // Set the new delta values for this item to the array of values returned by the repeat rule.
413
    // Set the new delta values for this item to the array of values returned
414
    // by the repeat rule.
407 415
    form_set_value($element, $value, $form_state);
408 416
  }
409 417
}
410 418

  
411 419
/**
412 420
 * Implements the form after_build().
413
 *
414
 * Remove the 'Add more' elements from a repeating date form.
415 421
 */
416 422
function date_repeat_after_build(&$element, &$form_state) {
423
  // Remove the 'Add more' elements from a repeating date form.
417 424
  foreach ($form_state['storage']['repeat_fields'] as $field_name => $parents) {
418 425
    // Remove unnecessary items in the form added by the Add more handling.
419 426
    $value = drupal_array_get_nested_value($element, $parents);
......
429 436
/**
430 437
 * Helper function to build repeating dates from a $node_field.
431 438
 *
432
 * Pass in either the RRULE or the $form_values array for the RRULE,
433
 * whichever is missing will be created when needed.
439
 * Pass in either the RRULE or the $form_values array for the RRULE, whichever
440
 * is missing will be created when needed.
434 441
 */
435
// @codingStandardsIgnoreStart
436 442
function date_repeat_build_dates($rrule = NULL, $rrule_values = NULL, $field, $item) {
437
// @codingStandardsIgnoreEnd
438 443
  include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'date_api') . '/date_api_ical.inc';
439 444
  $field_name = $field['field_name'];
440 445

  
......
445 450
    $rrule_values = date_ical_parse_rrule(NULL, $rrule);
446 451
  }
447 452

  
448
  // By the time we get here, the start and end dates have been
449
  // adjusted back to UTC, but we want localtime dates to do
450
  // things like '+1 Tuesday', so adjust back to localtime.
453
  // By the time we get here, the start and end dates have been adjusted back to
454
  // UTC, but we want localtime dates to do things like '+1 Tuesday', so adjust
455
  // back to localtime.
451 456
  $timezone = date_get_timezone($field['settings']['tz_handling'], $item['timezone']);
452 457
  $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
453 458
  $start = new DateObject($item['value'], $timezone_db, date_type_format($field['type']));
......
497 502
  $dates = date_repeat_calc($rrule, $start_datetime, $end_datetime, $exceptions, $timezone, $additions);
498 503
  $value = array();
499 504
  foreach ($dates as $delta => $date) {
500
    // date_repeat_calc always returns DATE_DATETIME dates, which is
501
    // not necessarily $field['type'] dates.
502
    // Convert returned dates back to db timezone before storing.
505
    // date_repeat_calc always returns DATE_DATETIME dates, which is not
506
    // necessarily $field['type'] dates. Convert returned dates back to db
507
    // timezone before storing.
503 508
    $date_start = new DateObject($date, $timezone, DATE_FORMAT_DATETIME);
504 509
    $date_start->limitGranularity($field['settings']['granularity']);
505 510
    date_timezone_set($date_start, timezone_open($timezone_db));
506
    $date_end = clone($date_start);
511
    $date_end = clone $date_start;
507 512
    date_modify($date_end, '+' . $duration . ' seconds');
508 513
    $value[$delta] = array(
509 514
      'value' => date_format($date_start, date_type_format($field['type'])),
......
520 525

  
521 526
/**
522 527
 * Implements hook_date_combo_process_alter().
523
 *
524
 * This hook lets us make changes to the date_combo element.
525 528
 */
526 529
function date_repeat_field_date_combo_process_alter(&$element, &$form_state, $context) {
527

  
530
  // This hook lets us make changes to the date_combo element.
528 531
  $field = $context['field'];
529 532
  $instance = $context['instance'];
530 533
  $field_name = $element['#field_name'];
531 534
  $delta = $element['#delta'];
532 535

  
533
  // Add a date repeat form element, if needed.
534
  // We delayed until this point so we don't bother adding it to hidden fields.
536
  // Add a date repeat form element, if needed. We delayed until this point so
537
  // we don't bother adding it to hidden fields.
535 538
  if (date_is_repeat_field($field, $instance)) {
536

  
537 539
    $item = $element['#value'];
538 540
    $element['rrule'] = array(
539 541
      '#type' => 'date_repeat_rrule',
......
551 553
      '#weight' => $instance['widget']['weight'] + .4,
552 554
    );
553 555
  }
554

  
555 556
}
556 557

  
557 558
/**
558 559
 * Implements hook_date_combo_pre_validate_alter().
559
 *
560
 * This hook lets us alter the element or the form_state before the rest
561
 * of the date_combo validation gets fired.
562 560
 */
563 561
function date_repeat_field_date_combo_pre_validate_alter(&$element, &$form_state, $context) {
562
  // This hook lets us alter the element or the form_state before the rest of
563
  // the date_combo validation gets fired.
564 564
  // Just a placeholder for now.
565 565
}
566 566

  
567 567
/**
568 568
 * Implements hook_field_info_alter().
569
 *
570
 * This Field API hook lets us add a new setting to the fields.
571 569
 */
572 570
function date_repeat_field_field_info_alter(&$info) {
573

  
571
  // This Field API hook lets us add a new setting to the fields.
574 572
  $info['date']['settings'] += array(
575 573
    'repeat' => 0,
576 574
  );
......
584 582

  
585 583
/**
586 584
 * Implements hook_field_formatter_info_alter().
587
 *
588
 * This hook lets us add settings to the formatters.
589 585
 */
590 586
function date_repeat_field_field_formatter_info_alter(&$info) {
591

  
587
  // This hook lets us add settings to the formatters.
592 588
  if (isset($info['date_default'])) {
593 589
    $info['date_default']['settings'] += array(
594 590
      'show_repeat_rule' => 'show',
......
598 594

  
599 595
/**
600 596
 * Implements hook_field_widget_info_alter().
601
 *
602
 * This Field API hook lets us add a new setting to the widgets.
603 597
 */
604 598
function date_repeat_field_field_widget_info_alter(&$info) {
605

  
599
  // This Field API hook lets us add a new setting to the widgets.
606 600
  $info['date_text']['settings'] += array(
607 601
    'repeat_collapsed' => 0,
608 602
  );
......
618 612

  
619 613
/**
620 614
 * Implements hook_date_field_settings_form_alter().
621
 *
622
 * This hook lets us alter the field settings form.
623 615
 */
624 616
function date_repeat_field_date_field_settings_form_alter(&$form, $context) {
625

  
617
  // This hook lets us alter the field settings form.
626 618
  $field = $context['field'];
627 619
  $instance = $context['instance'];
628 620
  $has_data = $context['has_data'];
......
649 641
    return;
650 642
  }
651 643

  
652
  // If using repeating dates, override the Field module's handling of the multiple values option.
644
  // If using repeating dates, override the Field module's handling of the
645
  // multiple values option.
653 646
  if (date_is_repeat_field($field, $instance)) {
654 647
    $form['field']['cardinality']['#disabled'] = TRUE;
655 648
    $form['field']['cardinality']['#value'] = FIELD_CARDINALITY_UNLIMITED;
......
659 652
}
660 653

  
661 654
/**
662
 * Ensure the cardinality gets updated if the option to make a date repeating is checked.
655
 * Ensure the cardinality is updated if the date can repeat.
663 656
 */
664 657
function date_repeat_field_set_cardinality($element, &$form_state) {
665 658
  if (!empty($form_state['values']['field']['settings']['repeat'])) {
......
669 662

  
670 663
/**
671 664
 * Implements hook_date_field_instance_settings_form_alter().
672
 *
673
 * This hook lets us alter the field instance settings form.
674 665
 */
675 666
function date_repeat_field_date_field_instance_settings_form_alter(&$form, $context) {
676
  // Just a placeholder for now.
667
  // This hook lets us alter the field instance settings form. Just a
668
  // placeholder for now.
677 669
}
678 670

  
679 671
/**
680 672
 * Implements hook_date_field_widget_settings_form_alter().
681
 *
682
 * This hook lets us alter the field widget settings form.
683 673
 */
684 674
function date_repeat_field_date_field_widget_settings_form_alter(&$form, $context) {
685

  
675
  // This hook lets us alter the field widget settings form.
686 676
  $field = $context['field'];
687 677
  $instance = $context['instance'];
688 678

  
......
692 682
      '#default_value' => 1,
693 683
      '#options' => array(
694 684
        0 => t('Expanded', array(), array('context' => 'Date repeat')),
695
        1 => t('Collapsed', array(), array('context' => 'Date repeat'))
685
        1 => t('Collapsed', array(), array('context' => 'Date repeat')),
696 686
      ),
697 687
      '#title' => t('Repeat display', array(), array('context' => 'Date repeat')),
698 688
      '#description' => t("Should the repeat options form start out expanded or collapsed? Set to 'Collapsed' to make those options less obtrusive.", array(), array('context' => 'Date repeat')),
......
703 693

  
704 694
/**
705 695
 * Implements hook_date_field_foramatter_settings_form_alter().
706
 *
707
 * This hook lets us alter the field formatter settings form.
708 696
 */
709 697
function date_repeat_field_date_field_formatter_settings_form_alter(&$form, &$form_state, $context) {
710

  
698
  // This hook lets us alter the field formatter settings form.
711 699
  $field = $context['field'];
712 700
  $instance = $context['instance'];
713 701
  $view_mode = $context['view_mode'];
......
730 718

  
731 719
/**
732 720
 * Implements hook_date_field_foramatter_settings_summary_alter().
733
 *
734
 * This hook lets us alter the field formatter settings summary.
735 721
 */
736 722
function date_repeat_field_date_field_formatter_settings_summary_alter(&$summary, $context) {
737

  
723
  // This hook lets us alter the field formatter settings summary.
738 724
  $field = $context['field'];
739 725
  $instance = $context['instance'];
740 726
  $view_mode = $context['view_mode'];

Formats disponibles : Unified diff