Skip to content

Instantly share code, notes, and snippets.

@MKorostoff
Created May 7, 2015 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MKorostoff/fb9f8c36a3ee9dff360c to your computer and use it in GitHub Desktop.
Save MKorostoff/fb9f8c36a3ee9dff360c to your computer and use it in GitHub Desktop.
<?php
function azure_demo_menu() {
$items = array();
$items['azure-demo'] = array(
'title' => 'Azure Demo',
'description' => 'Azure demo.',
'page callback' => 'drupal_get_form',
'page arguments' => array('azure_demo_form'),
'access callback' => TRUE
);
return $items;
}
function azure_demo_form($form, &$form_state) {
$form['get'] = array(
'#type' => 'submit',
'#value' => t('Get all values'),
);
$form['post'] = array(
'#type' => 'submit',
'#value' => t('Send all values'),
);
return $form;
}
function azure_demo_form_submit($form, &$form_state) {
$http_method = strtoupper($form_state['clicked_button']['#parents'][0]);
$options = array(
"headers" => array(
"api-key" => '*****************************',
'Content-Type' => 'application/json'
),
"method" => $http_method,
);
$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->execute();
$nodes = node_load_multiple(array_keys($result['node']));
end($nodes);
$last_node = key($nodes);
reset($nodes);
if ($http_method === 'POST') {
$resource = 'indexes/drupal/docs/index';
$options['data'] = '{"value": [';
foreach ($nodes as $nid => $node) {
$n = array(
"@search.action" => "upload",
"id" => azure_demo_node_id($node),
"title" => azure_demo_node_title($node),
"body" => azure_demo_node_body($node),
"image" => azure_demo_image($node),
"type" => azure_demo_get_type($node),
"tags" => azure_demo_get_tags($node),
);
$options['data'] .= json_encode($n);
if ($nid != $last_node) {
$options['data'] .= ',';
}
}
$options['data'] .= ']}';
}
elseif ($http_method === 'GET') {
$resource = 'indexes/drupal/docs';
}
$result = drupal_http_request('https://nbcsportsdemo.search.windows.net/' . $resource . '?api-version=2015-02-28&search=*', $options);
dpm(json_decode($result->data)->value);
}
function azure_demo_node_id($node) {
return md5($node->nid);
}
function azure_demo_node_title($node) {
return $node->title;
}
function azure_demo_node_body($node) {
$body = strip_tags($node->body['und'][0]['safe_value']);
$body = trim($body);
$body = str_replace("\n", '', $body);
return $body;
}
function azure_demo_image($node) {
return file_create_url($node->field_image['und'][0]['uri']);
}
function azure_demo_get_type($node) {
return $node->type;
}
function azure_demo_get_tags($node) {
$terms = array();
foreach ($node->field_tags['und'] as $key => $term) {
$terms[] = db_query("SELECT name FROM taxonomy_term_data WHERE tid=:tid", array(":tid" => $term['tid']))->fetchField();
}
return $terms;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment