-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathshorthand.module
287 lines (249 loc) · 8.09 KB
/
shorthand.module
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/**
* @file
* Connects with Shorthand and allows publishing stories to Drupal site.
*/
require_once 'includes/api.php';
/**
* Implements hook_help().
*/
function shorthand_help($path, $arg) {
$output = '';
switch ($path) {
case "admin/help#shorthand":
$output .= '<p>' . t("A module that allows the publishing of Shorthand stories directly to Drupal.") . '</p>';
break;
}
return $output;
}
/**
* Implements hook_menu().
*/
function shorthand_menu() {
$items = array();
$items['admin/config/services/shorthand'] = array(
'title' => 'Shorthand Connect settings',
'description' => 'Configure Shorthand integration for story publishing',
'page callback' => 'drupal_get_form',
'page arguments' => array('shorthand_admin_settings'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
'file' => 'shorthand.admin.inc',
);
return $items;
}
/**
* Implements hook_menu_alter().
*/
function shorthand_menu_alter(&$items) {
$items['node/add/shorthand-story']['access callback'] = 'shorthand_account_check';
}
/**
* Prevent addition of Shorthand nodes without account details.
*/
function shorthand_account_check() {
$shorthand_user_id = variable_get('shorthand_user_id', '');
$shorthand_token = variable_get('shorthand_token', '');
if (empty($shorthand_user_id) || empty($shorthand_token)) {
$message = t('No Shorthand account details found. Please go to the <a href="@page">Shorthand Connect settings page</a>.', array('@page' => url('admin/config/services/shorthand')));
drupal_get_messages();
drupal_set_message($message, 'warning');
return FALSE;
}
return TRUE;
}
/**
* Implements hook_field_widget_info().
*/
function shorthand_field_widget_info() {
return array(
'shorthand_story_select' => array(
'label' => 'Shorthand stories selection',
'field types' => array('text'),
'settings' => array(
'size' => '60',
),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
),
);
}
/**
* Implements hook_field_widget_form().
*/
function shorthand_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$value = isset($items[$delta]['value']) ? $items[$delta]['value'] : '';
$widget = $element;
$widget['#delta'] = $delta;
switch ($instance['widget']['type']) {
case 'shorthand_story_select':
$data['serverURL'] = variable_get('shorthand_server_url', 'https://app.shorthand.com');
$data['stories'] = sh_get_stories();
drupal_add_js('var shStoryData=' . json_encode($data) . ';', 'inline');
$widget += array(
'#type' => 'textfield',
'#default_value' => $value,
'#size' => 10,
'#maxlength' => 10,
'#attached' => array(
'css' => array(drupal_get_path('module', 'shorthand') . '/field_widget_form.css'),
'js' => array(drupal_get_path('module', 'shorthand') . '/field_widget_form.js'),
),
);
break;
}
$element['value'] = $widget;
return $element;
}
/**
* Implements hook_node_insert().
*/
function shorthand_node_insert($node) {
update_node_data($node);
}
/**
* Implements hook_node_update().
*/
function shorthand_node_update($node) {
update_node_data($node);
}
/**
* Create or update shorthand story node.
*
* @param object $node
* The newly-created or existing node.
*/
function update_node_data($node) {
if (isset($node->shorthand_story_id)) {
$node->original = isset($node->original) ? $node->original : NULL;
$story = sh_copy_story($node->nid, $node->shorthand_story_id[LANGUAGE_NONE][0]['value']);
// Handle texts in other languages.
$opts = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
$context = stream_context_create($opts);
$head = _shorthand_fix_content_paths($story['url'], file_get_contents($story['path'] . '/component_head.html', FALSE, $context));
// Allow modules to implement hook_shorthand_story_head_alter.
drupal_alter('shorthand_story_head', $head, $node->nid);
$node->shorthand_story_head[LANGUAGE_NONE][0]['value'] = $head;
$node->shorthand_story_head[LANGUAGE_NONE][0]['format'] = 'full_html';
$body = _shorthand_fix_content_paths($story['url'], file_get_contents($story['path'] . '/component_article.html', FALSE, $context));
// Allow modules to implement hook_shorthand_story_body_alter.
drupal_alter('shorthand_story_body', $body, $node->nid);
$node->shorthand_story_body[LANGUAGE_NONE][0]['value'] = $body;
$node->shorthand_story_body[LANGUAGE_NONE][0]['format'] = 'full_html';
field_attach_update('node', $node);
}
}
/**
* Implements hook_node_delete().
*/
function shorthand_node_delete($node) {
$story_path = drupal_realpath('public://') . 'shorthand/' . $node->nid . '/' . $node->shorthand_story_id[LANGUAGE_NONE][0]['value'];
if (file_exists($story_path)) {
file_unmanaged_delete_recursive($story_path);
}
}
/**
* Helper function to fix paths in the shorthand story.
*
* @param string $assets_path
* The correct path in Drupal.
* @param string $content
* The Shorthand story.
*
* @return string
* The story.
*/
function _shorthand_fix_content_paths($assets_path, $content) {
$content = str_replace('./static/', $assets_path . '/static/', $content);
$content = str_replace('./media/', $assets_path . '/media/', $content);
return $content;
}
/**
* Implements hook_preprocess_HOOK().
*/
function shorthand_preprocess_page(&$variables) {
if (isset($variables['node']->type)) {
$variables['theme_hook_suggestions'][] = 'page__' . $variables['node']->type;
}
}
/**
* Implements hook_preprocess_html().
*/
function shorthand_preprocess_html(&$vars) {
$node = menu_get_object();
if ($node && $node->type == 'shorthand_story' && $node->nid) {
drupal_add_css(drupal_get_path('module', 'shorthand') . '/shorthand.css', array(
'group' => CSS_THEME,
'weight' => 1000,
));
if (!empty($node->shorthand_story_head[LANGUAGE_NONE][0]['value'])) {
$vars['shorthand_story_head'] = $node->shorthand_story_head[LANGUAGE_NONE][0]['value'];
}
$vars['theme_hook_suggestions'][] = 'html__' . $node->type;
}
}
/**
* Implements hook_preprocess_node().
*/
function shorthand_preprocess_node(&$vars) {
if ($vars['type'] == 'shorthand_story') {
$vars['story_id'] = $vars['shorthand_story_id'][0]['value'];
$vars['story_body'] = $vars['shorthand_story_body'][0]['value'];
$vars['story_extra_html'] = !empty($vars['shorthand_story_extra_html']) ? $vars['shorthand_story_extra_html'][0]['value'] : "";
}
}
/**
* Unzip the Shorthand archive.
*
* @param string $file
* The zip file path.
* @param string $directory
* The path to the directory to unzip the archive.
*
* @return \ArchiverZip|\ZipArchive
* Either ArchiverZip from Drupal or PHP ZipArchive.
*
* @throws \Exception
*/
function shorthand_archive_extract($file, $directory) {
if (class_exists('ZipArchive', FALSE)) {
$archiver = new ZipArchive();
if (!$archiver->open($file)) {
throw new Exception(t('Cannot extract %file, not a valid zip archive.', array('%file' => $file)));
}
if (file_exists($directory)) {
file_unmanaged_delete_recursive($directory);
}
$archiver->extractTo($directory);
$archiver->close();
return $archiver;
}
// If ZipArchive is not available try Drupal.
return _shorthand_unzip_file($file, $directory);
}
/**
* Unzip the archive with Drupal implementation of extracting archives.
*
* @param string $file
* The zip file path.
* @param string $directory
* The path to the directory to unzip the archive.
*
* @return \ArchiverZip
* ArchiverZip from Drupal
*
* @throws \Exception
*/
function _shorthand_unzip_file($file, $directory) {
module_load_include('inc', 'system', 'system.archiver');
$archiver = new ArchiverZip($file);
if (!$archiver) {
throw new Exception(t('Cannot extract %file, not a valid zip archive.', array('%file' => $file)));
}
if (file_exists($directory)) {
file_unmanaged_delete_recursive($directory);
}
$archiver->extract($directory);
return $archiver;
}