You are here

function pmpapi_pull_preview in Public Media Platform API Integration 7

Page callback: Previews a pulled doc without creating a node.

Callback for admin/content/pmp/preview/%

Return value

array A Drupal form array

See also

pmpapi_pull_menu()

1 string reference to 'pmpapi_pull_preview'
pmpapi_pull_menu in pmpapi_pull/pmpapi_pull.module
Implements hook_menu().

File

pmpapi_pull/pmpapi_pull.pages.inc, line 300

Code

function pmpapi_pull_preview($form, &$form_state, $guid = '') {
  if (variable_get('pmpapi_pull_pull_active')) {
    $pmp = pmpapi_fetch($guid);
    if ($pmp && !$pmp->errors) {
      $pmp_doc = $pmp->query->results->docs[0];

      // hackalicious
      $profile_href = $pmp->query->results->docs[0]->links->profile[0]->href;
      $pieces = explode('/', $profile_href);
      $profile = array_pop($pieces);

      // parse out the bits we want for preview
      // define (fake) $entity
      $entity_values = array();
      $mapped_entity = pmpapi_pull_find_mapped_entity($profile);
      $entity_type = $mapped_entity['entity_type'];
      $bundle_name = $mapped_entity['bundle_name'];
      $uname = $entity_type . '__' . $bundle_name;
      $map = variable_get('pmpapi_pull_mapping_' . $uname . '_' . $profile);
      if ($entity_type == 'file') {
        $file_uri = pmpapi_pull_get_enclosure_url($pmp_doc->links->enclosure[0]);
        $entity_values += pmpapi_pull_create_mock_file($file_uri);
      }
      $entity_info = entity_get_info($entity_type);
      $key = $entity_info['entity keys']['id'];
      $entity_values[$key] = 0;
      if (!empty($entity_info['entity keys']['bundle'])) {
        $key = $entity_info['entity keys']['bundle'];
        $entity_values[$key] = $bundle_name;
      }
      $label = '';
      if (!empty($entity_info['entity keys']['label'])) {
        $label = $entity_info['entity keys']['label'];
        if ($pmp_key = array_search($label, $map)) {
          $entity_values[$label] = $pmp_doc->attributes->{$pmp_key};
        }
        else {
          $entity_values[$label] = $pmp_doc->attributes->title;
        }
      }
      $entity_values['created'] = REQUEST_TIME;
      $uid = variable_get('pmpapi_pull_pull_user', 1);
      $account = user_load($uid);
      $entity_values['uid'] = $uid;
      $entity_values['name'] = format_username($account);
      $default_format = filter_default_format($account);
      $special_fields = array(
        'image',
        'audio',
        'tags',
      );
      foreach ($map as $pmp_field => $local_field) {
        $local_ok = $local_field && $local_field !== $label;
        $pmp_ok = !in_array($pmp_field, $special_fields) && !empty($pmp_doc->attributes->{$pmp_field});
        if ($local_ok && $pmp_ok) {
          $entity_values[$local_field] = array(
            LANGUAGE_NONE => array(
              array(
                'value' => $pmp_doc->attributes->{$pmp_field},
                'format' => $default_format,
              ),
            ),
          );
        }
      }
      $entity = entity_create($entity_type, $entity_values);
      $entity_view = drupal_render(entity_view($entity_type, array(
        $entity,
      )));
      $markup = '<div class="preview">' . $entity_view . '</div>';
      $form['doc'] = array(
        '#markup' => $markup,
      );
      $form['publish'] = array(
        '#type' => 'submit',
        '#submit' => array(
          'pmpapi_pull_publish_from_preview',
        ),
        '#value' => t('Pull'),
      );
    }
    return $form;
  }
  else {
    $message = t('If you like to preview and/or pull this PMP doc, please activate the pull process by mapping PMP doc(s) to an existing entity: !link', array(
      '!link' => l('Pull Settings', 'admin/config/services/pmp/pull'),
    ));
    drupal_set_message($message, 'warning');
  }
}