View source
<?php
function pmpapi_pull_pull_doc_by_guid() {
$form = array();
$form['guid'] = array(
'#type' => 'textfield',
'#title' => t('PMP GUID'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Get document'),
);
return $form;
}
function pmpapi_pull_pull_doc_by_guid_validate($form, &$form_state) {
$guid = trim($form_state['values']['guid']);
$pattern = '/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i';
if (!preg_match($pattern, $guid)) {
form_set_error('guid', t('Not a valid GUID.'));
}
}
function pmpapi_pull_pull_doc_by_guid_submit($form, &$form_state) {
$guid = trim($form_state['values']['guid']);
pmpapi_pull_pull_doc($guid);
}
function pmpapi_pull_filter_form($form, &$form_state, $search_term = '') {
$limit = !empty($_GET['limit']) ? $_GET['limit'] : 10;
$params = array(
'limit' => $limit,
);
$per_page = 10;
if ($search_term) {
$params['text'] = $search_term;
}
if (!empty($_GET['tag'])) {
$tag = $_GET['tag'];
$params['tag'] = $tag;
}
if (!empty($_GET['profile'])) {
$profile = $_GET['profile'];
$params['profile'] = $profile;
}
$profiles = array(
'All',
) + pmpapi_get_profile_list();
if (!empty($_GET['creator'])) {
$creator = $_GET['creator'];
$params['creator'] = $creator;
}
$creators = array(
'All',
) + array_flip(pmpapi_get_creators());
if (!empty($_GET['property'])) {
$program = $_GET['property'];
$params['collection'] = $program;
}
$programs = array(
0 => t('All'),
) + pmpapi_get_properties();
if (!empty($_GET['page'])) {
$params['offset'] = $_GET['page'] * $per_page;
}
$form['text'] = array(
'#type' => 'fieldset',
'#title' => t('Text search'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$text_t_args = array(
'@syntax' => url('http://www.lucenetutorial.com/lucene-query-syntax.html'),
);
$form['text']['search_term'] = array(
'#title' => 'Search term',
'#type' => 'textfield',
'#default_value' => $search_term,
'#description' => t("Limit query to search snippet (using the <a href='@syntax'>Lucene query syntax</a>)", $text_t_args),
);
$form['text']['tag'] = array(
'#title' => 'Tag(s)',
'#type' => 'textfield',
'#default_value' => !empty($tag) ? $tag : '',
'#description' => t('Limit query to the listed tags (space-separated).'),
);
$form['filter'] = array(
'#type' => 'fieldset',
'#title' => t('Filter by'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['filter']['profile'] = array(
'#title' => 'Profile',
'#type' => 'select',
'#default_value' => !empty($profile) ? $profile : 0,
'#options' => $profiles,
);
$form['filter']['creator'] = array(
'#title' => 'Creator',
'#type' => 'select',
'#default_value' => !empty($creator) ? $creator : 0,
'#options' => $creators,
);
$form['filter']['property'] = array(
'#title' => 'Program',
'#type' => 'select',
'#default_value' => !empty($program) ? $program : 0,
'#options' => $programs,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Search'),
);
$pmp = pmpapi_query($params, FALSE);
if (!$pmp->errors) {
$docs = pmpapi_pull_parse_doc_metadata($pmp->query->results->docs);
$form['doc_list'] = array(
'#markup' => pmpapi_pull_create_doc_list_markup($docs),
);
$form['pager'] = array(
'#markup' => pmpapi_pull_create_pager($pmp->query->links->navigation['self']->totalitems),
);
}
return $form;
}
function pmpapi_pull_filter_form_submit($form, &$form_state) {
if ($form_state['values']['op'] == 'Search') {
$path = 'admin/content/pmp/search';
$options = array(
'query' => array(),
);
if (!empty($form_state['values']['search_term'])) {
$path = $path . '/' . $form_state['values']['search_term'];
}
if ($form_state['values']['tag']) {
$options['query']['tag'] = $form_state['values']['tag'];
}
if ($form_state['values']['profile']) {
$options['query']['profile'] = $form_state['values']['profile'];
}
if ($form_state['values']['creator']) {
$options['query']['creator'] = $form_state['values']['creator'];
}
if ($form_state['values']['property']) {
$options['query']['property'] = $form_state['values']['property'];
}
$form_state['redirect'] = array(
$path,
$options,
);
}
}
function pmpapi_pull_parse_doc_metadata($docs) {
$metadata = array();
$pulling = variable_get('pmpapi_pull_pull_active');
foreach ($docs as $doc) {
$title = $doc->attributes->title;
$pieces = explode('/', $doc->links->profile[0]->href);
$profile = array_pop($pieces);
$guid = $doc->attributes->guid;
$api_date = $doc->attributes->published;
$timestamp = strtotime($api_date);
$pub_date = format_date($timestamp, 'custom', 'D, M jS Y \\a\\t g:i a');
$been_pulled = pmpapi_pull_doc_has_been_pulled($guid);
$mapped_profile = pmpapi_pull_find_mapped_entity($profile);
if (!$pulling || !$mapped_profile || pmpapi_get_eid_from_guid($guid) && !$been_pulled) {
$preview = check_plain($title);
}
elseif ($been_pulled) {
$preview = check_plain($title) . ' ' . l('[' . t('Re-pull') . ']', 'admin/content/pmp/preview/' . $guid);
}
else {
$preview = l($title, 'admin/content/pmp/preview/' . $guid);
}
$metadata[] = array(
$preview,
$profile,
$guid,
$pub_date,
);
}
return $metadata;
}
function pmpapi_pull_create_doc_list_markup($docs) {
$vars['header'] = array(
'preview_link' => array(
'data' => t('Headline (click to preview)'),
),
'profile' => array(
'data' => t('Profile'),
),
'guid' => array(
'data' => t('GUID'),
),
'pub_date' => array(
'data' => t('Publish Date'),
'sort' => 'desc',
),
);
$data = array();
$vars['rows'] = $docs;
return theme('table', $vars);
}
function pmpapi_pull_create_pager($total) {
$page = isset($_GET['page']) ? abs((int) $_GET['page']) : 0;
global $pager_page_array, $pager_total;
$pager_total[0] = ceil($total / 10);
$pager_page_array[0] = $page;
return theme('pager', array(
'quantity' => 4,
));
}
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];
$profile_href = $pmp->query->results->docs[0]->links->profile[0]->href;
$pieces = explode('/', $profile_href);
$profile = array_pop($pieces);
$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');
}
}
function pmpapi_pull_publish_from_preview() {
return drupal_get_form('pmpapi_pull_publish_from_preview_form');
}
function pmpapi_pull_publish_from_preview_form($form, &$form_state) {
$guid = arg(4);
pmpapi_pull_pull_doc($guid);
$form_state['redirect'] = 'admin/content/pmp/search';
drupal_redirect_form($form_state);
}
function pmpapi_pull_create_mock_file($uri) {
$pieces = drupal_parse_url($uri);
$file = array();
$file['fid'] = 0;
$file['uid'] = variable_get('pmpapi_pull_pull_user', 1);
$file['filename'] = end(explode('/', $uri));
$file['uri'] = $uri;
$file['filemime'] = file_get_mimetype($pieces['path']);
$file['filesize'] = 1000;
$file['status'] = 1;
$file['timestamp'] = REQUEST_TIME;
return $file;
}