-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrealtime.module
More file actions
143 lines (122 loc) · 4.47 KB
/
realtime.module
File metadata and controls
143 lines (122 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* @file
*/
/**
* Implements hook_menu().
*/
function realtime_menu() {
$items = array();
$items['admin/config/realtime'] = array(
'title' => 'Google Realtime authentication settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('realtime_admin_auth_form'),
'file' => 'realtime.pages.inc',
'access arguments' => array('administer realtime'),
);
return $items;
}
/**
* Implements hook_node_prepare().
*/
function realtime_node_prepare($node) {
if(!empty($node->nid)) {
$realtimeOptions = array(
//Client ID from the APIs Console.
'clientId' => variable_get('realtime_client_id'),
//The ID of the button to click to authorize. Must be a DOM element ID.
'authButtonElementId' => 'authorizeButton',
//Function to be called when a Realtime model is first created.
'initializeModel' => 'initializeModel',
//Autocreate files right after auth automatically.
'autoCreate' => true,
//Autocreate files right after auth automatically.
'defaultTitle' => 'NodeID:' . $node->nid,
//Function to be called every time a Realtime file is loaded.
'onFileLoaded' => 'onFileLoaded',
'body' => $node->body['und'][0]['value'],
);
drupal_add_js('http://apis.google.com/js/api.js', 'external');
drupal_add_js(array('realtime' => $realtimeOptions), 'setting');
$querystring = drupal_get_query_parameters();
if(!empty($querystring['fileId'])) {
$firstcome = db_query('SELECT uid FROM {realtime_node_gdoc} WHERE nid = :nid',array(':nid' => $node->nid))->fetchField();
if(empty($firstcome)){
global $user;
db_merge('realtime_node_gdoc')
->key(array('docid' => $querystring['fileId']))
->fields(array(
'docid' => $querystring['fileId'],
'nid' => $node->nid,
'uid' => $user->uid,
))
->execute();
}
}
}
}
/**
* Implements hook_form_alter().
*/
function realtime_form_node_form_alter(&$form, &$form_state) {
array_unshift($form['#validate'], 'collision_buster_bust');
$form['#validate'][] = 'collision_buster_reset';
/* dpr($form); */
$form['mymarkup'] = array(
'#type' => 'markup',
'#markup' => '<button id="authorizeButton" disabled>Signin to Google+ to collaborate</button>',
'#weight' => -99,
);
if(!empty($form['nid']['#value'])) {
drupal_add_js(drupal_get_path('module', 'realtime') .'/js/realtime.js', array('type' => 'file', 'scope' => 'footer', 'weight' => 5));
drupal_add_js(drupal_get_path('module', 'realtime') .'/js/realtime-client-utils.js', array('type' => 'file', 'scope' => 'footer', 'weight' => 5));
drupal_add_js('startRealtime()', array('type' => 'inline', 'scope' => 'footer', 'weight' => 15));
}
}
/**
* Implement hook_node_update() to unlock a node after it's saved.
*
* This hook is invoked after hook_node_save() is invoked and after
* the changes to the node are actually saved to the database, so it's
* the right place to unlock a modified node.
*
* @param $node
* The node which was saved.
*/
function realtime_node_update($node) {
db_query('DELETE FROM {realtime_node_gdoc} WHERE nid = :nid',array(':nid' => $node->nid));
}
function hook_node_delete($node) {
db_query('DELETE FROM {realtime_node_gdoc} WHERE nid = :nid',array(':nid' => $node->nid));
}
/**
* Change the $node->changed value to now.
*
* This fools the collision check performed in node_validate().
*/
function collision_buster_bust($form, &$form_state) {
if (isset($form_state['values']['nid'])) {
$form_state['values']['collision_buster_changed'] = $form_state['values']['changed'];
$form_state['values']['changed'] = time();
}
}
/**
* Reset $node->changed to its original value.
*/
function collision_buster_reset($form, &$form_state) {
if (isset($form_state['values']['collision_buster_changed'])) {
$form_state['values']['changed'] = $form_state['values']['collision_buster_changed'];
unset($form_state['values']['collision_buster_changed']);
}
}
function realtime_url_outbound_alter(&$path, &$options, $original_path) {
if (preg_match('|^node/([0-9]*)/edit(/.*)?|', $path, $matches)) {
$node = node_load($matches[1]);
if ($node->nid == $matches[1]) {
$fileID = db_query('SELECT docid FROM {realtime_node_gdoc} WHERE nid = :nid',array(':nid' => $matches[1]))->fetchField();
if($fileID) {
$options['query']['fileId'] = $fileID;
}
}
}
}