I have a custom module that is displaying events fed by a JSON feed. The problem I am having is when the JSON feed is updated (for example a title name is updated) it creates a new event instead of updating the original event.
I am new to php and what I have tried is not working. Any help would be appreciated.
$category_matches = taxonomy_get_term_by_name('Dicate', 'events');
$category = array_pop($category_matches);
if (!$category) {
watchdog('rad', 'no dicate found', 'error');
return;
}
$feed_url = 'https://las/rad-forms/api/v1/getfeed';
$options = array('headers' => array());
$options['headers']['Authorization'] = 'Basic ' . base64_encode('rad:6WLUMwFgAjdOQPazZ7K0yxBRxklVAEWZaaaAcQuzQh4V7v7ZWP2cHl/BtCdhlj1ngacrhA==');
$response = drupal_http_request($feed_url, $options);
$response_data = $response->data;
$json_data = json_decode($response_data);
$feed_data = $json_data->dicate;
if (!count($feed_data)) {
watchdog('rad_feed', 'bad feed request - ' . print_r($response, TRUE));
}
foreach ($feed_data as $event) {
// make sure it doesn't already exist
$event_nids = FALSE;
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'event')
->propertyCondition('title', $event->{'draftTitle'}, '=')
->fieldCondition('field_event_dates', 'value', strtotime($event->startTime) , '=')
->range(0, 1) //($start, $length)
->addMetaData('account', user_load(1)); // Run the query as user 1.
$result = $query->execute();
if (isset($result['node'])) { // existing event found, just count it
$counter_existing++;
//ADDED TO UPDATE
$event->{'draftTitle'} = $node->title;
$node->field_event_category[$node->language][0]['tid'] = $category->tid;
$node->field_event_dates[$node->language][0]['value'] = strtotime($event->startTime);
node_save($node);
}
else { // event doesn't exist; create new one
$counter_new++;
$node = new stdClass();
$node->type = 'event';
$node->language = LANGUAGE_NONE;
node_object_prepare($node);
$node->status = 1;
$node->promote = 0;
$node->sticky = 0;
$node->comment = 0;
$node->uid = 1;
$node->title = $event->{'draftTitle'};
$node->field_event_category[$node->language][0]['tid'] = $category->tid;
$node->field_event_dates[$node->language][0]['value'] = strtotime($event->startTime);
node_save($node);
}
}
variable_set('rad_feed_last_run', $time);
watchdog('rad_feed', 'ran feed update: ' . $counter_new . ' new events; found ' . $counter_existing . ' existing events');
}
JSON Example:
"dictate":[{"form_id" : "1234", "draftTitle" : "Test123", "startTime" : "Wed, Jan 19, 2017"}]