You are here

function office_hours_feeds_set_target in Office Hours 7

Implements hook_feeds_set_target().

1 string reference to 'office_hours_feeds_set_target'
office_hours_feeds_processor_targets_alter in ./office_hours.feeds.inc
Implements hook_feeds_processor_targets_alter().

File

./office_hours.feeds.inc, line 74
Implementation of Feeds mapping API for office_hours.

Code

function office_hours_feeds_set_target($source, $entity, $target, $feed_element) {

  // Remember current Entity ID, Field name and day number.
  // Re-use them when the hours are set in another column.
  static $sv_vid = NULL;
  static $sv_field_name = NULL;
  static $sv_day = 0;
  if (is_array($feed_element)) {
    $feed_element = $feed_element[0];
  }

  // list($field_name, $sub_field, $start_end_field) = explode(':', $target, 3);
  $field = explode(':', $target, 3);
  $field_name = isset($field[0]) ? $field[0] : '';
  $sub_field = isset($field[1]) ? $field[1] : '';
  $start_end_field = isset($field[2]) ? $field[2] : '';
  switch ($sub_field) {
    case 'day':

      // If we enter a new entity, or another field within the same entity,
      // the field must be cleared. Else, we append the current field with the data.
      // Each OfficeHours-feeds line MUST have a day-column BEFORE any hours-columns;
      if ($entity->vid != $sv_vid || $field_name != $sv_field_name) {
        $sv_vid = $entity->vid;
        $sv_field_name = $field_name;
        $entity->{$field_name}[$entity->language] = array();
      }

      // Convert day to day number.
      // @todo: better way to process texts, perhaps only first 3 characters.
      // @todo: test for collaboration with Feeds Tamper.
      // @todo: support other first day of week.
      if (is_numeric($feed_element)) {

        // Day is passed as day number: sunday: 1->0; monday: 2->1; tuesday: 3->2; etc.
        $sv_day = $feed_element - 1;
      }
      else {

        // Day is passed as text.
        switch (strtolower(substr($feed_element, 0, 3))) {
          case 'sun':

          // en
          case 'zon':

          // nl
          case 'søn':

            // da
            $sv_day = '0';
            break;
          case 'mon':

          // en
          case 'maa':

          // nl
          case 'man':

            // da
            $sv_day = '1';
            break;
          case 'tue':
          case 'din':
          case 'tir':
            $sv_day = '2';
            break;
          case 'wed':
          case 'woe':
          case 'ons':
            $sv_day = '3';
            break;
          case 'thu':
          case 'don':
          case 'tor':
            $sv_day = '4';
            break;
          case 'fri':
          case 'vri':
          case 'fre':
            $sv_day = '5';
            break;
          case 'sat':
          case 'zat':
          case 'lør':
            $sv_day = '6';
            break;
        }
      }
      break;
    case 'morehours':

    // Since we have unlimited bocks per day, there is no difference between
    // 'morehours' and 'hours'.
    // break;  // do not break, proceed with 'hours'
    case 'hours':

      // Open a new hours_block element.
      // Remove time delimiters & spaces: ' 10:00 - 18:00  ' --> '1000-1800'
      // The following is not sufficient to strip all unvisible characters,
      // therefor intval() is used later on.
      $feed_element = str_replace(' ', '', $feed_element);
      $feed_element = str_replace(':', '', $feed_element);
      $feed_element = str_replace('.', '', $feed_element);

      // Add the hours_block to the field.
      switch ($start_end_field) {
        case 'start':
          $hours_block['day'] = $sv_day;
          $hours_block['starthours'] = intval($feed_element);
          break;
        case 'end':
          $hours_block['day'] = $sv_day;
          $hours_block['endhours'] = intval($feed_element);
          break;
        default:

          // $feed_element contains both Start and End.
          list($hours_start, $hours_end) = explode('-', $feed_element, 2);
          $hours_block['day'] = $sv_day;
          $hours_block['starthours'] = intval($hours_start);
          $hours_block['endhours'] = intval($hours_end);
          break;
      }
      $entity->{$field_name}[$entity->language][] = $hours_block;
      break;
    default:
      watchdog('feeds office_hours', 'field %field element %element is not correct', array(
        '%field' => '$sub_field',
        '%element' => $feed_element,
      ));
      break;
  }
  return $entity;
}