This repository was archived by the owner on Oct 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxowl.module
More file actions
322 lines (285 loc) · 11.2 KB
/
xowl.module
File metadata and controls
322 lines (285 loc) · 11.2 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php
/**
* @file
* Module to interact with Xowl for semantic tagging
* @author Ximdex <dev@ximdex.com>
*/
/**
* <p>Implements hook_help().</p>
*
* <p>Displays help and module information</p>
*
* @param string path
* path of the site we're using to display help
* @param array arg
* Array that holds the current path as returned from arg() function
*/
function xowl_help($path, $arg)
{
switch ($path) {
case "admin/help#xowl":
return '<p>' . t("A module to provide semantic tagging to Drupal content through Xowl") . '</p>';
break;
default:
break;
}
}
/**
* <p>Implements hook_menu().</p>
*
*
*/
function xowl_menu()
{
$items = array();
$items['admin/settings/xowl'] = array(
'title' => t('Xowl Configuration'),
'description' => t('Configuration for Xowl module'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM
);
// Admin interface Xowl server
$items['admin/settings/xowl/server'] = array(
'title' => t('Xowl server configuration'),
'description' => t('Xowl server-related configuration'),
'page callback' => 'drupal_get_form',
'page arguments' => array('xowl_server_configuration_form'),
'access arguments' => array('access administration pages'), // Only users with that permission
'type' => MENU_NORMAL_ITEM
);
//Admin interface Content type selection form
$items['admin/settings/xowl/allowed-content-type'] = array(
'title' => t('Xowl Content Type selection'),
'description' => t('Allows selecting Content Types that will be enabled to use Xowl to enrich content'),
'page callback' => 'drupal_get_form',
'page arguments' => array('xowl_allowed_content_type_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
//Endpoint receiving Xowl enhance requests
$items['xowl/enhance'] = array(
'title' => t('Xowl Ajax enhance'),
'description' => t('Analyze and enhance the content using Xowl'),
'page callback' => 'xowl_enhance_content',
'file' => '',
'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
function xowl_server_configuration_form_validate($form, &$form_state) {
$xowl_usertoken = variable_get('xowl_usertoken');
$token_input = $form_state['values']['token_input'] . '';
variable_set('xowl_usertoken', $token_input);
}
/**
* <p>Configuration form for Xowl module</p>
*/
function xowl_server_configuration_form($form, &$form_state)
{
$form = array();
$form['overview'] = array(
'#markup' => t('This interface allows the user to integrate the Xowl module with Drupal. If you need a token, visit: <a target="_blank" href="@xowlPortal">xowl register</a>', array('@xowlPortal' => 'http://x8.ximdex.net/register/signup')),
'#prefix' => '<p>',
'#suffix' => '</p>',
);
$form['xowl_endpoint'] = array(
'#title' => t('Xowl API Endpoint'),
'#description' => t('Set the endpoint of the Xowl API. Example: @exampleUrl', array('@exampleUrl' => 'http://xowl.ximdex.net/api/v1')),
'#type' => 'textfield',
'#required' => true,
'#default_value' => variable_get('xowl_endpoint', 'http://xowl.ximdex.net/api/v1'),
);
$token = variable_get('xowl_usertoken', '');
drupal_add_css('.inline-elems>div{display: inline;}', array('group' => CSS_THEME, 'type' => 'inline'));
$form['inlined'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => array('inline-elems')
),
'token_input' => array(
'#type' => 'textfield',
'#default_value' => $token,
'#title' => t("API token"),
),
'pair' => array(
'#prefix' => '<div>',
'#suffix' => '</div>',
'ajax_button' => array(
'#type' => 'button',
'#value' => t("Check token"),
'#ajax' => array(
'callback' => 'ajax_getapi_callback'
),
),
'token_valid' => array(
'#prefix' => '<span id="token_valid_span">',
'#suffix' => '</span>',
)
),
);
return system_settings_form($form);
}
function ajax_getapi_callback($form, $form_state) {
$endpoint = variable_get('xowl_endpoint');
if (!$endpoint) {
watchdog('MISSING ENDPOINT', "Cannot get api without proper endpoint", WATCHDOG_ERROR);
return false;
}
$getapikeyUrl = $endpoint . "/check?token=" . $form_state['values']['token_input'] ;
// request apikey to xowl service
$ch = curl_init($getapikeyUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(curl_errno($ch)){
watchdog('BAD RESPONSE', "Server response was wrong ($ch): " . curl_error($ch), WATCHDOG_ERROR);
return false;
}
$json = curl_exec($ch);
curl_close($ch);
// pass response to form
$data = json_decode($json, true);
$commands = array();
if (isset($data['status']) && $data['status'] == "ok") {
variable_set('xowl_usertoken',$form_state['values']['token_input'] );
$commands[] = ajax_command_html('#token_valid_span', '<span style="color:green">' . t('valid token') . '</span>');
}
else {
$commands[] = ajax_command_html('#token_valid_span', '<span style="color:red">' . t('invalid token') . '</span>');
}
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Implements hook_form.
* form to save content types and fields
*
*/
function xowl_allowed_content_type_form($node, &$form_state) {
$form = array();
$form['overview'] = array(
'#prefix' => '<p>',
'#markup' => t('This interface allows the user to integrate the Xowl module with Drupal. If you need a token, visit: <a target="_blank" href="@xowlPortal">xowl register</a>', array('@xowlPortal' => 'http://x8.ximdex.net/register/signup')),
'#suffix' => '</p>',
);
// Get the content_types names:
$contentTypes = node_type_get_names();
// Get the default values:
$currentValues = variable_get('xowl_enabled_content_types', array());
//Set all the content types which are having file fields
$form['xowl_enabled_content_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Select the content types that will have Xowl plugin'),
'#options' => $contentTypes,
'#default_value' => $currentValues,
);
$current_forms_values = check_plain(variable_get('xowl_allowed_forms', ''));
$form['xowl_allowed_forms'] = array(
'#type' => 'textarea',
'#title' => t('Set other allowed forms where it will use the xowl plugin'),
'#description' => t('Set a set of forms separated by commas'),
'#default_value' => $current_forms_values,
);
return system_settings_form($form);
}
/**
* <p>Implements hook_form_alter()</p>
* <p>Modify the edit form to include some JS variables needed to decide whether the enhancer button must be enabled or not</p>
*/
function xowl_form_alter(&$form, &$form_state, $form_id) {
$validContentTypes = array_filter(variable_get('xowl_enabled_content_types', array()));
// Get the allowed forms:
$valid_forms = check_plain(variable_get('xowl_allowed_forms', ''));
$valid_forms_values = explode(',', $valid_forms);
// Check if is valid content type:
$check_content_type = isset($form['#node']) && in_array($form['#node']->type, $validContentTypes);
// Check if is a allowed form_id:
$check_is_allowed_form = !empty($valid_forms_values) && in_array($form_id, array_map('trim', $valid_forms_values));
if ($check_content_type || $check_is_allowed_form) {
drupal_add_js(array('xowl' => array('enable_xowl' => 1,"xowl_path" => drupal_get_path('module', 'xowl'), "basedir" => base_path() . drupal_get_path('module', 'xowl'), "dbase_path" => url(NULL, array('absolute' => TRUE)) ), ),'setting');
drupal_add_css(drupal_get_path('module', 'xowl') . '/resources/css/xowl.css', 'file');
// Adding a hidden field to store the selected suggestions
$form['suggestions'] = array(
'#title' => t('Suggestions'),
'#description' => t('Selected suggestions'),
'#type' => 'hidden',
'#attributes' => array('id' => "suggestions_field"),
'#weight' => 0
);
$form['#validate'][] = 'xowl_validate';
$form['#submit'][] = 'xowl_submit';
}
}
/**
* <p>Validate function for Xowl forms which will call the 'xowl_form_validate' hook
* to allow other modules to attach their handlers</p>
* @param mixed $form
* @param mixed $form_state
*/
function xowl_validate($form, &$form_state) {
$form_state['values']['suggestions'] = isset($form_state['values']['suggestions']) && !empty($form_state['values']['suggestions']) ? json_decode($form_state['values']['suggestions'], true) : array();
module_invoke_all('xowl_form_validate', $form, $form_state);
}
/**
* <p>Submit function for Xowl forms which will call the 'xowl_form_submit' hook
* to allow other modules to attach their handlers</p>
* @param Sub $form
* @param type $form_state
*/
function xowl_submit($form, &$form_state) {
module_invoke_all('xowl_forms_submit', $form, $form_state);
}
function filter_post_data($content) {
$changeFrom = array('/<a class="xowl-suggestion"(.*)(data-(.*))?>/iUs');
$content = preg_replace($changeFrom, '<a \1>', $content);
return $content;
}
/**
* <p>Implements hook_node_insert</p>
* <p>Creates the XML representation of the node to be sent to Ximdex</p>
*/
function processNode($node) {
module_load_include('util', 'xowl', 'includes/xowl');
$body = isset($node->body['und'][0]['value']) ? $node->body['und'][0]['value'] : '';
$body = filter_post_data($body);
$node->body['und'][0]['value'] = $body;
// xowl_createXMLRepresentation($body);
}
function xowl_node_presave($node)
{
processNode($node);
}
/**
* <p>Implements hook_ckeditor_plugin().</p>
*
*/
function xowl_ckeditor_plugin()
{
return array(
'xowl_enhance_plugin' => array(
'name' => 'xowl_enhance_plugin',
'path' => drupal_get_path('module', 'xowl') . '/ckeditor/plugins/xowl_enhance_plugin/',
'desc' => t('Xowl plugin to allow enhancing content in CKEditor'),
'buttons' => array(
'xowl_enhance_plugin_button' => array(
'icon' => 'icons/xowl_enhance_plugin_button.png',
'label' => 'Xowl enhance',
)
),
'load' => TRUE,
),
);
}
/**
* <p>Callback function for 'xowl/enhance' endpoint</p>
* <p>This function calls Ximdex Xowl endpoint to analyze and enhance the content</p>
*/
function xowl_enhance_content()
{
module_load_include('php', 'xowl', 'includes/XowlStanbolService');
$xowlEndpoint = variable_get('xowl_endpoint');
$content = filter_input(INPUT_POST, 'content');
$service = new XowlStanbolService($xowlEndpoint);
echo $service->suggest($content);
}