forked from josereyero/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.admin.inc
96 lines (86 loc) · 2.14 KB
/
telegram.admin.inc
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
<?php
/**
* @file
* Telegram administration.
*/
/**
* Test form.
*/
function telegram_test_form($form, &$form_state) {
// Display output and logs.
$form = _telegram_test_form($form, $form_state);
$form['dialogs'] = array('#type' => 'submit', '#value' => t('Dialog list'));
$form['contacts'] = array('#type' => 'submit', '#value' => t('Contact list'));
return $form;
}
/**
* Test form submit.
*/
function telegram_test_form_submit($form, &$form_state) {
if ($client = _telegram_test_client()) {
switch ($form_state['values']['op']) {
case t('Dialog list'):
$data = $client->getDialogList();
break;
case t('Contact list'):
$data = $client->getContactList();
break;
}
$output = array(
'#type' => 'fieldset',
'#title' => $form_state['values']['op'],
);
$output['output']['content'] = array(
'#type' => 'markup',
'#markup' => '<pre>' . check_plain(print_r($data, TRUE)) . '</pre>',
);
$form_state['output'] = $output;
$client->stop();
}
$form_state['rebuild'] = TRUE;
}
/**
* Base form for testing and logs.
*/
function _telegram_test_form($form, &$form_state) {
if (isset($form_state['output'])) {
$form['output'] = $form_state['output'];
}
$form += _telegram_test_logs();
return $form;
}
/**
* Get telegram client with some messages.
*/
function _telegram_test_client() {
static $client;
if (!isset($client)) {
$client = telegram_client();
if ($client->start()) {
drupal_set_message(t('Telegram client started successfully'));
}
else {
drupal_set_message(t('Cannot start Telegram client'), 'error');
$client = FALSE;
}
}
return $client;
}
/**
* Build element with telegram logs
*/
function _telegram_test_logs() {
$element = array();
if ($logs = telegram_logger()->formatLogs()) {
$element['logs'] = array(
'#type' => 'fieldset',
'#title' => t('Log messages'),
'#collapsible' => TRUE, '#collapsed' => TRUE,
);
$element['logs']['content'] = array(
'#type' => 'markup',
'#markup' => '<pre>'. check_plain($logs) . '</pre>',
);
}
return $element;
}