-
Notifications
You must be signed in to change notification settings - Fork 0
/
guestbook.module
177 lines (157 loc) · 4.43 KB
/
guestbook.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
<?php
module_load_include('inc', 'guestbook', 'include/guestbook_db');
module_load_include('inc', 'guestbook', 'include/guestbook_core');
module_load_include('inc', 'guestbook', 'include/guestbook.theme');
module_load_include('inc', 'guestbook', 'include/guestbook_weather');
/**
* @file
* Build page, form for guestbook and weather block.
*/
/**
* Implements hook_menu().
*/
function guestbook_menu() {
$items = array();
// Main page.
$items['guestbook-page'] = array(
'title' => 'Guestbook',
'page callback' => 'drupal_get_form',
'page arguments' => array('guestbook_page_form'),
'access callback' => TRUE,
);
// Edit message.
$items['guestbook-page/%mid/edit'] = array(
'title' => 'Guestbook',
'page callback' => 'drupal_get_form',
'page arguments' => array('guestbook_page_form', 1),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
// Delete message.
$items['guestbook-page/%mid/delete'] = array(
'title' => 'Delete message',
'page callback' => 'guestbook_message_delete',
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_form.
*
* @param $value
* Implements hook_load
* contains array('id', 'uid', 'sid', 'date', 'message').
* @return
* Return form.
*/
function guestbook_page_form($form, $form_state, $value= NULL) {
global $user;
// Text for editing.
$text_edit = NULL;
// Get html list of messages.
$text = guestbook_html();
$form = [];
if (isset($value['id'])) {
// Get tex for editing.
$text_edit = $value['message'];
$form['infoById'] = array('#type' => 'value', '#value' => $value);
}
$form['#attached']['css'][] = drupal_get_path('module', 'guestbook') . '/css/guestbook.css';
$form['message_window'] = [
// Show messages if it exists.
'#markup' => $text,
];
// Show area for input text if user is logged.
if ($user->uid) {
$form['message'] = [
'#type' => 'textarea',
'#title' => t('message'),
'#size' => 1000,
'#default_value' => $text_edit,
];
$form['submit'] = [
'#type' => 'submit',
'#attributes' => array('class' => array('guestbook-button')),
'#value' => isset($value['id']) ? t('Edit') : t('Save'),
];
// Show or hide the cancel button.
if (isset($value['id'])) {
$form['cancel_button'] = [
'#type' => 'link',
'#title' => t('Cancel'),
'#href' => 'guestbook-page',
'#options' => array('query' => drupal_get_query_parameters()),
'#attributes' => array('class' => array('guestbook-button')),
];
}
}
return $form;
}
/**
* Validate guestbook_page_form.
*/
function guestbook_page_form_validate($form, &$form_state) {
if (drupal_strlen($form_state['values']['message']) > 1000 || empty($form_state['values']['message'])) {
form_set_error('message', t('Field "@fieldname" must be less than @c chars and cant be empty .',
array('@fieldname' => t('message'), '@c' => 1000)));
}
}
/**
* Submit for guestbook_page_form.
*/
function guestbook_page_form_submit($form, &$form_state) {
guestbook_save_edit_db($form_state);
}
/**
* Loads a message info.
*
* @param $mid
* Integer specifying the message id to load.
* @return
* A fully-loaded array of message info ('id', 'uid', 'sid', 'date', 'message').
* id - message id; uid - user id; sid - session id; date - message creation date; message - message text.
*/
function mid_load($mid) {
$query = db_select('guestbook_drupal', 't')
->fields('t', array('id', 'uid', 'sid', 'date', 'message'))
->condition('t.id', $mid)
->execute();
$value = $query->fetchAssoc();
return $value;
}
/*
* Implements hook_cron().
*/
function guestbook_cron() {
guestbook_get_xml_weather();
}
/*
* Implements hook_block_info().
*/
function guestbook_block_info() {
// Build weather block.
$blocks['weather'] = array(
'info' => t('The Weather'),
'cache' => DRUPAL_CACHE_GLOBAL,
'region' => 'sidebar_first',
'status' => TRUE,
'weight' => -10,
);
return $blocks;
}
/*
* Implements hook_block_view().
*/
function guestbook_block_view($delta = '') {
$block = [];
switch($delta){
case 'weather':
$block['subject'] = t('Vitebsk Weather');
// Get prepared html data.
$block['content'] = guestbook_weather();
break;
}
return $block;
}