Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GB-Drupal #1

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

Binary file added css/background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions css/guestbook.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* Messages style. */
.guestbook-messages {
width: 100%;
height: auto;
float: left;
margin-top: 10px;
margin-bottom: 10px;
overflow: auto;
text-align: left;
background: #e1f1ff;
padding: 5px;
-moz-box-sizing: border-box;
box-sizing: border-box;
}

/* Button style. */
.guestbook-button {
background: -webkit-gradient(linear, left top, left bottom, color-stop(0.05, #ededed), color-stop(1, #bab1ba));
background: -moz-linear-gradient(top, #ededed 5%, #bab1ba 100%);
background: -webkit-linear-gradient(top, #ededed 5%, #bab1ba 100%);
background: -o-linear-gradient(top, #ededed 5%, #bab1ba 100%);
background: -ms-linear-gradient(top, #ededed 5%, #bab1ba 100%);
background: linear-gradient(to bottom, #ededed 5%, #bab1ba 100%);
filter:progid: DXImageTransform.Microsoft.gradient(startColorstr='#ededed', endColorstr='#bab1ba',GradientType=0);
background-color: #ededed;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
border: 1px solid #d6bcd6;
display: inline-block;
cursor: pointer;
color: #3a8a9e;
font-family: arial;
font-size: 12px;
padding:5px 15px;
text-decoration: none;
text-shadow: 0px 1px 0px #e1e2ed;
margin-left: 5px;
}
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
.guestbook-button:hover {
background: -webkit-gradient(linear, left top, left bottom, color-stop(0.05, #bab1ba), color-stop(1, #ededed));
background: -moz-linear-gradient(top, #bab1ba 5%, #ededed 100%);
background: -webkit-linear-gradient(top, #bab1ba 5%, #ededed 100%);
background: -o-linear-gradient(top, #bab1ba 5%, #ededed 100%);
background: -ms-linear-gradient(top, #bab1ba 5%, #ededed 100%);
background: linear-gradient(to bottom, #bab1ba 5%, #ededed 100%);
filter:progid: DXImageTransform.Microsoft.gradient(startColorstr = '#bab1ba', endColorstr = '#ededed',GradientType = 0);
background-color: #bab1ba;
}
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
.guestbook-button:active {
position: relative;
top: 1px;
}
5 changes: 5 additions & 0 deletions guestbook.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name = Guest Book
description = Guestbook.
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
core = 7.x
version = 7.x-1.0
files[] = css/guestbook.css
PavelSPN marked this conversation as resolved.
Show resolved Hide resolved
47 changes: 47 additions & 0 deletions guestbook.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
function guestbook_schema() {
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
$schema['guestbook_drupal'] = array(
// Specification for table "node".
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
'description' => 'The base table for guestbook.',
'fields' => array(
'id' => array(
PavelSPN marked this conversation as resolved.
Show resolved Hide resolved
'description' => 'The primary identifier for a message.',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
),
'uid' => array(
'description' => 'Identifier for a user.',
'type' => 'int',
'unsigned' => TRUE,
PavelSPN marked this conversation as resolved.
Show resolved Hide resolved
'not null' => TRUE,
),
'sid' => array(
'description' => 'Identifier for a session id.',
'type' => 'varchar',
'length' => 100,
'not null' => TRUE,
'default' => '',
),
'date' => array(
'description' => 'Message date.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'message' => array(
'description' => 'The message by user.',
'type' => 'varchar',
'length' => 100,
'not null' => TRUE,
'default' => '',
),
),
'unique keys' => array(
'id' => array(
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
'id',
),
),
);
return $schema;
}
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
177 changes: 177 additions & 0 deletions guestbook.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
module_load_include('inc', 'guestbook', 'include/guestbook_db');
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
module_load_include('inc', 'guestbook', 'include/guestbook_core');
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
module_load_include('inc', 'guestbook', 'include/guestbook.theme');
module_load_include('inc', 'guestbook', 'include/guestbook_weather');
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved

/**
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
* @file
* Build page, form for guestbook and weather block.
*/

/**
* Implements hook_menu().
*/
function guestbook_menu() {
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
$items = array();
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
// Main page.
$items['guestbook-page'] = array(
'title' => 'Guestbook',
'page callback' => 'drupal_get_form',
'page arguments' => array('guestbook_page_form'),
'access callback' => TRUE,
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
);
// Edit message.
$items['guestbook-page/%mid/edit'] = array(
'title' => 'Guestbook',
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
'page callback' => 'drupal_get_form',
'page arguments' => array('guestbook_page_form', 1),
'access callback' => TRUE,
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
'type' => MENU_CALLBACK,

egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
);
// Delete message.
$items['guestbook-page/%mid/delete'] = array(
'title' => 'Delete message',
'page callback' => 'guestbook_message_delete',
'page arguments' => array(1),
'access callback' => TRUE,
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
'type' => MENU_CALLBACK,
);
return $items;
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Implements hook_form.
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
*
* @param $value
* Implements hook_load
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
* 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;
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved

// Get html list of messages.
$text = guestbook_html();
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved

$form = [];
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved

if (isset($value['id'])) {
// Get tex for editing.
$text_edit = $value['message'];
$form['infoById'] = array('#type' => 'value', '#value' => $value);
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
}

$form['#attached']['css'][] = drupal_get_path('module', 'guestbook') . '/css/guestbook.css';

$form['message_window'] = [
// Show messages if it exists.
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
'#markup' => $text,
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
];

// 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')),
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
'#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')),

egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
];
}
}
return $form;
}

/**
* Validate guestbook_page_form.
*/
function guestbook_page_form_validate($form, &$form_state) {
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
if (drupal_strlen($form_state['values']['message']) > 1000 || empty($form_state['values']['message'])) {
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
form_set_error('message', t('Field "@fieldname" must be less than @c chars and cant be empty .',
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
array('@fieldname' => t('message'), '@c' => 1000)));
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
}
}

/**
* Submit for guestbook_page_form.
*/
function guestbook_page_form_submit($form, &$form_state) {
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
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) {
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
$query = db_select('guestbook_drupal', 't')
PavelSPN marked this conversation as resolved.
Show resolved Hide resolved
->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',
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
'status' => TRUE,
'weight' => -10,
);
return $blocks;
}

/*
* Implements hook_block_view().
*/
function guestbook_block_view($delta = '') {
$block = [];
switch($delta){

egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
case 'weather':
$block['subject'] = t('Vitebsk Weather');
// Get prepared html data.
$block['content'] = guestbook_weather();
break;
}
return $block;
}
46 changes: 46 additions & 0 deletions include/guestbook.theme.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
/**
* @file
* The implementation of the theme functions.
*/

/**
* Generates messages html markup.
*
* @return
* Html markup.
*/
function guestbook_html() {
global $user;

// Variable $mes is fully generated html markup.
$mes = NULL;
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved

// Get messages data array from database.
$variables = guestbook_read_db('guestbook_drupal', 'id', 'DESC', '10');
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved

foreach ($variables as $value) {
// Get message creation date.
$date = format_date($value['3'], $type = 'medium', $format = '', $timezone = NULL, $langcode = NULL);
// Get user name.
$name = guestbook_name_by_id($value['1']);
// Generate html markup for date and name.
$data = '<div><em>'.$date.' '.$name.'</em></div>';
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
// Get message and generate html markup for it.
$message = '<div><b>'.check_plain($value['4']).'</b></div>';
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved

// If user is logged and current session id = stored id in database
// then generate html markup for edit button and delete button.
if ($user->uid) {
if ($value['2'] == $user->sid) {
$params = drupal_get_query_parameters();
$message .= l(t('edit'), 'guestbook-page/' . $value['0'] . '/edit', array('attributes' => array('class' => array('guestbook-button')), 'query' => $params))
egorbogatyrev marked this conversation as resolved.
Show resolved Hide resolved
. l(t('delete'), 'guestbook-page/' . $value['0'] . '/delete', array('attributes' => array('class' => array('guestbook-button')), 'query' => $params));
}
}
$mes .= '<div class = "guestbook-messages">' . $data . $message . '</div>';
}

$mes .= theme('pager');
return '<div class = "guestbook-messages-all">' . $mes . '</div>';
}
16 changes: 16 additions & 0 deletions include/guestbook_core.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* @file
* Implementation of user functions.
*/

/**
* Get current time.
*
* @return
* Return time stamp.
*/
function guestbook_time() {
$date = new DateTime();
return $date->getTimestamp();
}
Loading