This repository was archived by the owner on Jul 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebspeech_tts.module
More file actions
264 lines (242 loc) · 7.68 KB
/
webspeech_tts.module
File metadata and controls
264 lines (242 loc) · 7.68 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* @file
* Read nodes in a variety of vocies and, separately, languages/accents.
*/
/**
* Implements hook_help().
*/
function webspeech_tts_help($path, $arg) {
switch ($path) {
case 'admin/help#webspeech_tts':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Webspeech TTS module allows users with the <em>Administer site configuration</em> permission to quickly and easily change voices and languages(accents) associated with content in a node. Like the API itself, it is experimental and not intended for enterprise usage. Clear both the Drupal AND browser cache frequently when testing. </p>');
$output .= '<h3>' . t('How to use') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Enabling on a node') . '</dt>';
$output .= '<dd>' . t("You may enable WebspeechTTS on a node in edit mode in the 'WebspeechTTS' tab. In order to place the Play-Pause-Cancel buttons adjancent to the node, locate the 'WebspeechTTS' block.") . '</dd>';
$output .= '</dl>';
return $output;
}
}
drupal_static_reset('sites/all/modules/custom/webspeech_tts/webspeech_tts.js?v=1');
/**
* Implements hook_menu().
*/
function webspeech_tts_menu() {
$items = array();
$items['node/%/webspeech_tts'] = array(
'title' => 'Webspeech TTS for Chrome',
'description' => 'Option to Read Node Content Out Loud',
'page callback' => 'drupal_get_form',
'page arguments' => array('webspeech_tts_form'),
);
$items['admin/config/content/webspeech_tts'] = array(
'title' => 'Webspeech TTS for Chrome',
'description' => 'Administer Webspeech TTS',
'page callback' => 'drupal_get_form',
'page arguments' => array('webspeech_tts_admin_settings_form'),
'access arguments' => array('administer webspeech_tts settings'),
'file' => 'webspeech_tts.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_form().
*/
function webspeech_tts_form($form, &$form_state) {
$tts_lang_only = variable_get('webspeech_tts_lang_only');
$tts_voice = variable_get('webspeech_tts_voice');
$tts_vol = variable_get('webspeech_tts_volume', 'volume');
$tts_rate = variable_get('webspeech_tts_rate', 'rate');
$tts_pitch = variable_get('webspeech_tts_pitch', 'pitch');
drupal_add_js(array('webspeech_tts' => array('tts_lang_only_js' => $tts_lang_only)), 'setting');
drupal_add_js(array('webspeech_tts' => array('tts_voice_js' => $tts_voice)), 'setting');
drupal_add_js(array('webspeech_tts' => array('tts_volume_js' => $tts_vol)), 'setting');
drupal_add_js(array('webspeech_tts' => array('tts_rate_js' => $tts_rate)), 'setting');
drupal_add_js(array('webspeech_tts' => array('tts_pitch_js' => $tts_pitch)), 'setting');
$path_args = explode('/', current_path());
$node = node_load((int) $path_args[1]);
$node_content_to_js = $node->body[LANGUAGE_NONE][0]['value'];
drupal_static_reset('sites/all/modules/custom/webspeech_tts/webspeech_tts.js?v=1');
drupal_add_js(array('webspeech_tts' => array('proof_of_concept' => $node_content_to_js)), 'setting');
drupal_add_js('sites/all/modules/custom/webspeech_tts/webspeech_tts.js?v=1');
print '<pre>';
print '</pre>';
$form['read_text'] = array(
'#type' => 'submit',
'#value' => t('Read Text Aloud'),
'#action' => url('#'),
'#id' => 'read_text',
'#attributes' => array('onclick' => 'return (false);'),
);
$form['pause_button'] = array(
'#type' => 'button',
'#value' => t('Pause'),
'#action' => url('#'),
'#id' => 'pause_button',
'#attributes' => array('onclick' => 'return (false);'),
);
$form['cancel_button'] = array(
'#type' => 'button',
'#value' => t('Cancel'),
'#action' => url('#'),
'#id' => 'cancel_button',
'#attributes' => array('onclick' => 'return (false);'),
);
return $form;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function webspeech_tts_form_node_form_alter(&$form, $form_state) {
$node = $form['#node'];
$types = variable_get('webspeech_tts_types', array());
if (!empty($types[$node->type]) && user_access('administer webspeech_tts settings')) {
$form['webspeech_tts'] = array(
'#title' => t('webspeech_tts'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#group' => 'additional_settings',
);
$form['webspeech_tts']['webspeech_tts_enabled'] = array(
'#title' => t('Enable WebspeechTTS'),
'#type' => 'checkbox',
'#default_value' => isset($node->webspeech_tts_enabled) ?
$node->webspeech_tts_enabled : FALSE,
);
}
}
/**
* Implements hook_VERB_node_enabled().
*/
function webspeech_tts_get_node_enabled($nid) {
if (is_numeric($nid)) {
$result = db_query("SELECT nid FROM {webspeech_tts_enabled}
WHERE nid = :nid", array('nid' => $nid))->fetchField();
if ($result) {
return TRUE;
}
}
return FALSE;
}
/**
* Implements hook_VERB_node_enabled().
*/
function webspeech_tts_set_node_enabled($nid) {
if (is_numeric($nid) & !webspeech_tts_get_node_enabled($nid)) {
db_insert('webspeech_tts_enabled')
->fields(array('nid' => $nid))
->execute();
}
}
/**
* Implements hook_VERB_node_enabled().
*/
function webspeech_tts_delete_node_enabled($nid) {
if (is_numeric($nid)) {
db_delete('webspeech_tts_enabled')
->condition('nid', $nid)
->execute();
}
}
/**
* Implements hook_node_load().
*/
function webspeech_tts_node_load($nodes, $types) {
foreach ($nodes as $nid => $node) {
$node->webspeech_tts_enabled = webspeech_tts_get_node_enabled($node->nid);
}
}
/**
* Implements hook_node_insert().
*/
function webspeech_tts_node_insert($node) {
if ($node->webspeech_tts_enabled) {
webspeech_tts_set_node_enabled($node->nid);
}
}
/**
* Implements hook_node_update().
*/
function webspeech_tts_node_update($node) {
webspeech_tts_delete_node_enabled($node->nid);
if ($node->webspeech_tts_enabled) {
webspeech_tts_set_node_enabled($node->nid);
}
}
/**
* Implements hook_node_delete().
*/
function webspeech_tts_node_delete($node) {
webspeech_tts_delete_node_enabled($node->nid);
}
/**
* Implements hook_access_tab().
*/
function webspeech_tts_access_tab($permission, $nid) {
return webspeech_tts_get_node_enabled($nid) && user_access($permission);
}
/* Conditional to toggle voices and languages which are incompatible. */
$the_switch = variable_get('webspeech_tts_voice_or_lang_only');
if ($the_switch == 'voice') {
variable_set('webspeech_tts_lang_only', 'en-US');
}
else {
if ($the_switch == 'lang_only') {
variable_set('webspeech_tts_voice', '0');
}
}
/**
* Implements hook_block_info().
*/
function webspeech_tts_block_info() {
$blocks = array();
$blocks['webspeech_tts'] = array(
'info' => t('Webspeech TTS'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function webspeech_tts_block_view($delta = '') {
switch ($delta) {
case 'webspeech_tts':
if (arg(0) == 'node' && is_numeric(arg(1)) && webspeech_tts_get_node_enabled(arg(1))) {
$nid = arg(1);
$form = drupal_get_form('webspeech_tts_form');
$block = array(
'content' => theme('webspeech_tts_block', array('form' => drupal_render($form))),
);
return $block;
}
break;
}
}
/**
* Implements hook_theme().
*/
function webspeech_tts_theme() {
$theme = array();
$theme['webspeech_tts_block'] = array(
'variables' => array(
'form' => '',
),
'template' => 'webspeech_tts-block',
);
return $theme;
}
/**
* Implements hook_permission().
*/
function webspeech_tts_permission() {
return array(
'administer Webspeech TTS' => array(
'title' => t('Administer Webspeech TTS'),
'description' => t('Perform administration tasks for Webspeech TTS.'),
),
);
}