-
Notifications
You must be signed in to change notification settings - Fork 0
/
voipviews.module
160 lines (143 loc) · 5.05 KB
/
voipviews.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
<?php
/**
* @file
* Views dev.
*/
/**
* Implementation of hook_views_api().
*/
function voipviews_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'voipviews') . '/views',
);
}
/**
* Implementation of hook_theme().
*/
function voipviews_theme($existing, $type, $theme, $path) {
return array(
'voipviews_view_preview' => array(
'arguments' => array(
'voipscriptstore' => '',
),
'file' => 'theme/voipviews.theme.inc',
),
);
}
/**
* Impelementation of hook_menu().
*/
function voipviews_menu() {
// we can check for new, changed, extension on form submission, but not programatically
// similarly on deleting there is nothing in $view->delete (just form submission if from ui)
// a menu rebuild is however always triggered, so updating our extensions here
voipviews_refresh_extensions();
}
/**
* Add, update and remove voip views extensions.
*/
function voipviews_refresh_extensions() {
if (! module_exists('voipextension')) {
return;
}
$extensions = voipextension_load_module_extensions('voipviews');
// cycle through all voip views displays
$views = views_get_applicable_views('uses voipextension');
foreach ($views as $data) {
list($view, $display_id) = $data;
$view->set_display($display_id);
// if the voipextension checkbox is enabled
if ($view->display_handler->get_option('voipextension_enable')) {
// check to see if it exists already
if (isset($extensions[$view->name . '|' . $display_id])) {
$extension = $extensions[$view->name . '|' . $display_id];
$extension['title'] = $view->display_handler->get_option('voipextension_title');
$extension['description'] = $view->display_handler->get_option('voipextension_description');
$extension['script_callback'] = 'voipviews_voipextension_script_callback';
voipextension_update($extension);
unset($extensions[$view->name . '|' . $display_id]);
}
else {
// otherwise create new extension
$extension = array(
'title' => $view->display_handler->get_option('voipextension_title'),
'description' => $view->display_handler->get_option('voipextension_description'),
'module' => 'voipviews',
'module_id' => $view->name . '|' . $display_id,
'script_callback' => 'voipviews_voipextension_script_callback',
);
voipextension_create($extension);
}
}
}
// remove any extensions that are remaining they are deleted or disabled.
foreach($extensions as $module_id => $extension) {
voipextension_delete($extension['eid']);
}
// Save memory: Destroy those views.
foreach ($views as $data) {
list($view, $display_id) = $data;
$view->destroy();
}
}
/**
* Voipextension script callback.
*/
function voipviews_voipextension_script_callback(&$extension) {
list($view_name, $display_id) = explode('|', $extension['module_id']);
$extension['script_arguments']['view_name'] = $view_name;
$extension['script_arguments']['display_id'] = $display_id;
return 'voipviews_read_view';
}
/**
* Implementation of hook_voipscript_get_script_names().
*/
function voipviews_voipscript_get_script_names() {
return array(
'voipviews_read_view',
);
}
/**
* Implementation of hook_voipscript_load_script().
*/
function voipviews_voipscript_load_script($script_name, $params = NULL) {
switch ($script_name) {
case 'voipviews_read_view':
$args = (! empty($params['args'])) ? json_decode($params['args']) : array();
return voipviews_read_view_script($params['view_name'], $params['display_id'], $args);
}
}
/**
* Return a script from a view display.
*/
function voipviews_read_view_script($view_name, $display_id, $args = array()) {
$view = views_get_view($view_name);
if (!$view || !$view->access($display_id)) {
$script = new VoipScript('voipviews_script_view_read');
watchdog('voipviews', 'Unable to load or access view @view_name display @display_id', array('@view_name' => $view_name, '@display_id' => $display_id), WATCHDOG_ERROR);
$script->addSay('There has been an error accessing the information you require. Sorry.');
$script->addReturn();
return $script;
}
if (! $view->set_display($display_id)) {
$script = new VoipScript('voipviews_script_view_read');
watchdog('voipviews', 'Unable to set display @display_id on view @view_name', array('@view_name' => $view_name, '@display_id' => $display_id), WATCHDOG_ERROR);
$script->addSay('There has been an error accessing the information you require. Sorry.');
$script->addReturn();
return $script;
}
if (! empty($options['filters'])) {
$view->set_exposed_input($options['filters']);
}
$view->pre_execute($args);
$stored_script = $view->display_handler->preview();
$view->post_execute();
// @todo put this in the store class.
$script = new VoipScript($stored_script->getName(), $stored_script->getVariables());
foreach ($stored_script as $line) {
list($command, $params) = $line;
call_user_func_array(array($script, $command), $params);
}
return $script;
}