forked from anil614sagar/smartdocs_extend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmartdocs_extend.module
More file actions
executable file
·126 lines (117 loc) · 4.63 KB
/
smartdocs_extend.module
File metadata and controls
executable file
·126 lines (117 loc) · 4.63 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
<?php
function smartdocs_extend_menu() {
$items['admin/content/smartdocs/models/%smartdocs_model/download-original/%smartdocs_revision'] = array(
'title' => 'Download Original',
'page callback' => 'smartdocs_model_download_original',
'page arguments' => array(4, 6),
'access arguments' => array('administer smartdocs content'),
'file' => 'smartdocs_extend.admin.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
function smartdocs_extend_form_smartdocs_revision_detail_alter(&$form, &$form_state, $form_id) {
$form['verbose'] = array(
'#prefix' => "<div>",
'#markup' => "<br>" . l('Download Original Spec', 'admin/content/smartdocs/models/'. arg(4) . '/download-original/' . arg(6), array('attributes' => array('class' => array('button', 'spec-download-link')))),
'#suffix' => "<br><br></div>",
'#weight' => 900
);
}
function smartdocs_extend_smartdocs_model_import($raw_source, $mime_type, $document_format, Apigee\SmartDocs\Model $model, $source) {
// Convert YAML to JSON.
if ($mime_type == "application/yaml") {
require_once libraries_get_path('spyc') . '/Spyc.php';
try {
$swagger_obj = spyc_load($raw_source);
}
catch (Exception $e) {
// Spyc may throw "Too many keys" exception in edge cases where it
// fails to parse valid well-formed YAML. This occurs when an object
// or array is written in "short syntax" and spans more than one line.
// See https://github.com/mustangostang/spyc/issues/30
// This same bug also afflicts Symfony\Yaml, though PECL yaml reads
// the data correctly (using libyaml).
watchdog_exception('smartdocs', $e);
drupal_set_message(t('Warning: If your model contains parameter enums, they may not have imported correctly.'), 'warning');
}
if ($swagger_obj) {
$raw_source = json_encode($swagger_obj);
$mime_type = "application/json";
}
}
// If JSON - Store locally
if ($mime_type == "application/json") {
$uuid = (string) $model->getUuid();
$revision = ( int ) $model->getLatestRevisionNumber();
$raw_source = (string) $raw_source;
db_merge('smartdocs_raw')
->key(array('uuid' => $uuid))
->fields(array(
'raw_spec' => $raw_source,
'spec_format' => $mime_type
))
->execute();
db_merge('smartdocs_raw_revisions')
->key(array('uuid' => $uuid, 'revision' => $revision))
->fields(array(
'raw_spec' => $raw_source,
'spec_format' => $mime_type
))
->execute();
}
}
function smartdocs_extend_node_view($node, $view_mode, $langcode) {
if ($node->type == 'smart_method' && $view_mode == 'full') {
$model_name = db_select('smartdata', 's')
->condition('nid', $node->nid)
->fields('s', array('model'))
->execute()
->fetchField();
$model = smartdocs_model_load($model_name);
$mid = $model['id'];
// Load Spec
$raw_spec = db_select('smartdocs_raw', 'sr')
->condition('uuid', $mid)
->fields('sr')
->execute()
->fetchAll();
if (count($raw_spec) && $raw_spec[0]->spec_format == 'application/json') {
$path_items = field_get_items('node', $node, 'field_smart_method_resource_path', $langcode = NULL);
$path = $path_items[0]['value'];
$verb_items = field_get_items('node', $node, 'field_smart_method_verb', $langcode = NULL);
$verb = strtolower($verb_items[0]['taxonomy_term']->name);
// Retrieve the spec and associated definitions from raw spec.
$open_api = drupal_json_decode($raw_spec[0]->raw_spec);
$api_spec = $open_api['paths'][$path][$verb];
$definitions = $open_api['definitions'];
// Inject this into page as javascript....
$open_api_raw_spec = array();
$open_api_raw_spec['path_info'] = $api_spec;
$open_api_raw_spec['spec_definitions'] = $definitions;
$open_api_raw_spec_string = drupal_json_encode($open_api_raw_spec);
$jscript_embed = "var api_raw_spec = '" . $open_api_raw_spec_string . "';";
drupal_add_js($jscript_embed, array(
'type' => 'inline',
'scope' => 'header',
'weight' => -100,
));
}
}
}
function smartdocs_extend_js_alter(&$javascript) {
$version = variable_get('smartdocs_local_asset_version', '6');
$path = drupal_get_path('module', 'smartdocs');
// Add model js for smartdocs.
$javascript[$path . '/local/js/v' . $version . '/model.js'] = array(
'data' => drupal_get_path('module', 'smartdocs_extend') . '/js/model.js',
'scope' => 'header',
'group' => JS_DEFAULT,
'weight' => 9999,
'every_page' => TRUE,
'type' => 'file',
'preprocess' => TRUE,
'defer' => TRUE,
'cache' => TRUE,
);
}