outlook_calendar-8.x-4.1/outlook_calendar.module
outlook_calendar.module
<?php
/**
* @file
* Displays the outlook calendar events.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Database\Database;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\node\Entity\Node;
module_load_include('inc', 'outlook_calendar', 'outlook_calendar.auth');
/**
* Implements hook_help().
*
* @inheritdoc
*/
function outlook_calendar_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.outlook_calendar':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Outlook Calendar module is used to fetch Outlook Calendar events
and display them in a block. This block can be configured and displayed like a normal drupal
block.') . '</p>';
$output .= '<h3>' . t('Configuration') . '</h3>';
$output .= '<dt>' . t('Adding Outlook Account') . '</dt>';
$output .= '<dd>' . t('Outlook Account can be configured on the <a href=":accounts">Outlook Accounts</a>
page.', [':accounts' => Url::fromRoute('outlook_calendar.account')->toString()]
) . '</dd>';
$output .= '<dt>' . t('View Outlook Events') . '</dt>';
$output .= '<dd>' . t('Outlook Events of the configured accounts can be viewed on <a href=":events">
Outlook Calendar</a> page.', [':events' => Url::fromRoute('outlook_calendar.form')->toString()]
) . '</dd>';
return $output;
}
}
/**
* Returns the dates within date range().
*/
function outlook_calendar_date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d') {
$dates = [];
$current = strtotime($first);
$last = strtotime($last);
while ($current <= $last) {
$dates[] = date($output_format, $current);
$current = strtotime($step, $current);
}
return $dates;
}
/**
* Returns the date range().
*/
function outlook_calendar_week_range($date) {
$ts = strtotime($date);
$start = (date('w', $ts) == 0) ? $ts : strtotime('now', $ts);
return [
date('c', $start),
date('c', strtotime('next month', $start)),
];
}
/**
* Returns the outlook calendar().
*/
function outlook_calendar_display($range = '+7day',$keyparams = '') {
list($start_date, $end_date) = outlook_calendar_week_range(date('Y-m-d H:i:s'));
$dates = outlook_calendar_date_range($start_date, $end_date);
$outlook_calendar_accounts = outlook_calendar_accounts();
$scheduler = [];
$i = 0;
foreach ($outlook_calendar_accounts as $account) {
$events = outlook_calendar_list($account);
foreach ($events as $event) {
if (!empty($event)) {
$changekey = $event->ItemId->ChangeKey;
$eventid = $event->ItemId->Id;
$start = $event->Start;
$end = $event->End;
$subject = $event->Subject;
$location = $event->Location;
$organizer = $event
->Organizer
->Mailbox->Name;
if (in_array(date('Y-m-d', strtotime($start)), $dates)) {
date_default_timezone_set('Asia/Kolkata');
$scheduler[$i]['title'] = $subject;
$scheduler[$i]['organizer'] = $organizer;
$scheduler[$i]['location'] = $location;
$scheduler[$i]['start_ist'] = date('Y-m-d H:i:s', strtotime($start));
$scheduler[$i]['end_ist'] = date('Y-m-d H:i:s', strtotime($end));
if ($keyparams) {
$scheduler[$i]['changekey'] = $changekey;
$scheduler[$i]['eventid'] = $eventid;
$scheduler[$i]['start_time'] = $start;
$scheduler[$i]['end_time'] = $end;
}
$i++;
}
}
}
}
return $scheduler;
}
/**
* Returns the outlook accounts().
*/
function outlook_calendar_accounts() {
$conn = Database::getConnection();
$query = $conn->select('outlook_calendar', 'oe');
$query->fields('oe', ['uid', 'mail', 'password']);
$results = $query->execute()
->fetchAll();
return $results;
}
/**
* Returns the range().
*/
function outlook_calendar_account_list() {
// Select outlook id from table.
$conn = Database::getConnection();
$query = $conn->select('outlook_calendar', 'oe');
$query->fields('oe', ['id', 'mail']);
$results = $query->execute()
->fetchAll();
$rows = [];
foreach ($results as $data) {
$delete = Url::fromRoute('outlook_calendar.delete',['cid'=> $data->id]);
$edit = Url::fromRoute('outlook_calendar.edit',$query = ['num' => $data->id]);
// Print the data from table.
$rows[] = [
'id' => $data->id,
'mail' => $data->mail,
'opt' => Link::fromTextAndUrl(t('Edit'), $edit),
'opt1' => Link::fromTextAndUrl(t('Delete'), $delete) ,
];
}
return $rows;
}
/**
* Implements hook_form_alter().
*/
function outlook_calendar_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form_id == 'node_outlook_events_edit_form') {
// Set id and change key field as read only in order to avoid editing.
$form['field_event_id']['widget']['0']['value']['#attributes'] = ['disabled' => 'disabled'];
$form['field_change_key']['widget']['0']['value']['#attributes'] = ['disabled' => 'disabled'];
}
}
/**
* Creates the events as nodes on hook cron().
*/
function outlook_calendar_cron() {
// Create node object with event details.
$results = outlook_calendar_display(1,1);
foreach ($results as $key => $value) {
$conn = Database::getConnection();
$query = $conn->select('node__field_event_id', 'eid');
$query->fields('eid', ['entity_id']);
$query->condition('field_event_id_value',$value['eventid']);
$nid = $query->execute()
->fetchField();
//If the event id doesn't exist then create the event as a node.
if (empty($nid)) {
$node = Node::create([
'type' => 'outlook_events',
'title' => $value['title'],
'field_event_id' => $value['eventid'],
'field_change_key' => $value['changekey'],
'field_start_time' => rtrim($value['start_time'],'Z'),
'field_end_time' => rtrim($value['end_time'],'Z'),
'field_event_organizer' => $value['organizer'],
]);
$node->uid = 1;
$node->save();
}
else {
$node = Node::load($nid);
$key = $node->changekey;
//If the event id does exists and existing change key value doesn't match then the event is updated.
if ($key != $value['changekey']) {
$node->title = $value['title'];
$node->changekey = $value['changekey'];
$node->start_time = rtrim($value['start_time'],'Z');
$node->end_time = rtrim($value['end_time'],'Z');
$node->organizer = $value['organizer'];
}
$node->save();
}
}
}
