Search This Blog

Showing posts with label Drupal. Show all posts
Showing posts with label Drupal. Show all posts

May 19, 2011

Contact Form Module for Drupal - Last

Next is the contact-block.tpl.php file. This is where you display the contact form.

<?php

<div id="contact-container">
 <h1>Leave Us a Message</h1>
 <div id="form-container">
   <?php print $contact_form; ?>
   <div id="contact-message"></div>
 </div>
</div>

<?php print $contact_form; ?> - as you can see, we call the $contact_form variable to display the form that we created in the module file.

The last two files are the css and the js file which add style and clear fields functionality in your contact form.

contact.css

#contact-container{
margin:20px auto;
width:463px;
background:#f8f0e7;
}
#contact-container h1{
font-size:18px;
padding:12px 25px;
}
#form-container{
padding:0px 25px 20px;
}
#contact-message{
margin-top:5px;
color:#B76600;
}
#contact-form .form-item label{
float:left;
margin-top:6px;
font-weight:normal;
width:73px;
}
#edit-message-wrapper label{
margin-right: 6px;
}
.grippie{
width:317px;
}
#contact-form .form-radios{
width: 450px; 
margin-top: 0px; 
margin-bottom: 0px;
}
#contact-form  #edit-submit{
padding-top:3px;
}
#contact-form .resizable-textarea{
width:317px;
margin-left:79px;
}


contact.js

Drupal.behaviors.contact = function(context) {
if ($("#contact-message div div").hasClass("status")){
$("#edit-name,#edit-email,#edit-subject, textarea").val(""); 
}
}

Thanks for reading. Here is the download link of contact form module.

Contact Form Module for Drupal - Part 2

Next on the list is the contact.module. In this one you probably know the basic hooks. So let me break down it by hooks

contact.module


hook help - Provide online user help.
<?php
// $Id: nvr.module,v 1.297.2.4 2009/02/25 12:21:53 traxbarn Exp $

function contact_help($path, $arg){
switch ($path) {
case 'admin/help#contact':
return t('Place your instruction here.');
}
}

hook init - Perform setup tasks. See also, hook_boot.
function contact_init(){
drupal_add_css(drupal_get_path('module','contact') .'/contact.css', 'module');
drupal_add_js(drupal_get_path('module','contact') .'/contact.js', 'module');
}

hook perm - Define user permissions.
function contact_perm(){
return array('view contact');
}

hook access - Control access to a node.
function contact_access($op, $node) {
if (user_access('view contact')) {
return TRUE;
}
}

hook theme - Register a module (or theme's) theme implementations.
function contact_theme(){
return array(
'contact_block' => array(
'arguments' => array('form' => NULL),
'template' => 'contact-block',
),
);
}

hook_block - allows you to specify any custom configuration settings, and how to display the block.
function contact_block($op = 'list', $delta = 0) {
if ($op == 'list') {
$blocks[0]['info'] = t('nvr contact');
$blocks[0]['cache'] = BLOCK_NO_CACHE;

return $blocks;
}
else if($op == 'view' && $delta == 0 && user_access('view contact')){

$block['content'] = theme('contact_block', NULL);

return $block;
}
}

hook menu - Define menu items and page callbacks.
function contact_menu(){
$items['contact'] = array(
'page callback' => 'contact_form_validate',
'access arguments' => array('view contact'),
'type' => MENU_CALLBACK,
); 

return $items;
}

hook_form - Display a node editing form.

function contact_form(&$form_state){
$form['name'] = array(
'#title' => 'Name',
'#type' => 'textfield',
'#attributes' => array('style' => 'height:22px;margin-left:6px;width:194px;'),
'#maxlength' => 50,
'#required' => TRUE,
);

$form['email'] = array(
'#title' => 'Email',
'#type' => 'textfield',
'#attributes' => array('style' => 'height:22px;margin:5px 0px 0px 6px;width:194px;'),
'#required' => TRUE,
);

$form['subject'] = array(
'#title' => 'Subject',
'#type' => 'textfield',
'#attributes' => array('style' => 'height:22px;margin:5px 0px 0px 6px;width:315px;'),
'#required' => TRUE,
);

$form['message'] = array(
'#title' => 'Message',
'#type' => 'textarea',
'#attributes' => array('style' => 'margin:5px 0px 0px 0px; width:315px; cursor: text;'),
'#maxlength' => 200,
'#required' => TRUE,
);
$form['join_list']['join'] = array(
'#type' => 'radios',
'#title' => t('Join our Mailing List'),
'#size' => 20,
'#default_value' => variable_get('join', 1),
'#options' => array(t('Yes'), t('No')),
'#required' => TRUE
); 

$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit Email'),
'#attributes' => array('style' => 'width:120px;padding-bottom:3px;'),
'#suffix' => '</div><div style="clear: both;"></div>',
'#prefix' => '<div class="form-item" style="margin-left:78px;margin-top:15px;">',
'#ahah' => array(
'event' => 'click',
'path' => 'contact',
'wrapper' => 'contact-message',
'method' => 'replace',
'effect' => 'fade',
'progress' => array(
'type' => 'throbber',
),  
),  
);

return $form;
}

hook_validate - Perform node validation before a node is created or updated.

function contact_form_validate(){

$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];

$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];

$form_id = array_shift($form_state);

$form['#redirect'] = FALSE;
$form['#post'] = $_POST;
$form['#programmed'] = FALSE;

$form_state['name']   = $_POST['name'];
$form_state['email']   = $_POST['email'];
$form_state['subject'] = $_POST['subject'];
$form_state['message'] = $_POST['message'];
$form_state['join']   = $_POST['join'];


if(empty($form_state['name'])){
form_set_error('name', t('Name field is Required.'));
}


$email = isValidEmail($form_state['email']);

if(empty($form_state['subject'])){
form_set_error('subject', t('Subject field is Required.'));
}
if(empty($form_state['message'])){
form_set_error('message', t('Message field is Required.'));
}


//rechecking for the validation

if ($email[1] == TRUE && !empty($form_state['name'])&& !empty($form_state['subject'])&& !empty($form_state['message'])){

$params['name']   = $form_state['name'];
$params['email']  = $form_state['email'];
$params['subject'] = $form_state['subject'];
$params['message'] = $form_state['message'];
$params['join'] = $form_state['join'];
$params['ae_mail'] = 'info@naturesvillageresort.net';

drupal_mail('contact_send_acknowledgment1', $key = NULL, $params['ae_mail'], $language, $params, $params['email'], $send = TRUE);
drupal_mail('contact_send_acknowledgment' , $key = NULL, $email[2], $language, $params, $params['ae_mail'], $send = TRUE);

// Insert the name and email if yes option is choosen 
if ($params['join']==0){
db_query("INSERT INTO {contact} (id, name, email) VALUES (%d, '%s', '%s')", $id, $params['name'] ,$params['email']);
drupal_set_message(t('Message sent. You have successfully joined our mailing list.'));
}else{
drupal_set_message(t('Message Sent.'));  
}

drupal_process_form($form_id, $form, $form_state);
$output = theme('status_messages');
drupal_json(array('status' => FALSE, 'data' => $output));

} 
else{
drupal_process_form($form_id, $form, $form_state);
$output = theme('status_messages');
drupal_json(array('status' => FALSE, 'data' => $output));
} 
}
function template_preprocess_contact_block(&$variables) {
$variables['contact_form'] = drupal_get_form('contact_form');
}

function isValidEmail($email){
if(empty($email)){
$emails[] = form_set_error('mail', t('Email is Required.'));
$emails[] = FALSE;
return $emails;
}
else if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
$emails[] =  form_set_error('mail', t('The Email Address you specified is not Valid.'));
$emails[] = FALSE;
return $emails;
}
else{
$emails[] = '';
$emails[] = TRUE;
$emails[] = $email;
return $emails;
}
}


hook_mail - Prepare a message based on parameters; called from drupal_mail().
function contact_send_acknowledgment1_mail($key, &$message, $params){
$message['subject'] = t("Webdosh.net:")."   ".$params['subject'];
$message['body']  =  t("\n\r\n\r Message From The Webdosh.net \n\r");
$message['body'] .=  t("\n\r\n\r------------------------------------------\n\r\n\r");
$message['body'] .=  $params['message']. t("\n\r\n\r");
$message['body'] .=  t("\n\r(63) (34) 495-0808 / 4953368 to 69 / 712-1272");  
$message['body'] .=  t("\n\r 6115 Talisay City");
$message['body'] .=  t("\n\r Negros Occidental, Philippines");
$message['body'] .=  t("\n\r------------------------------------------");
$message['body'] .=  t("\n\r\n\r(c) 2010 Webdosh.net. All Rights Reserved. Webdosh.net");
}
function contact_send_acknowledgment_mail($key, &$message, $params){
$message['subject'] = t("Webdosh.net- Auto Responding Email");
$message['body']  =   t("\n\r\n\r Your Email has been successfully sent to ". $params['ae_mail'] .".\n\r");
$message['body'] .=  t("\n\r\n\r We will check our availability and send you an email response within 24 hours.");
$message['body'] .=  t("\n\r\n\r------------------------------------------");
$message['body'] .=  t("\n\r(63) (34) 495-0808 / 4953368 to 69 / 712-1272");
$message['body'] .=  t("\n\r 6115 Talisay City");
$message['body'] .=  t("\n\r Address 2, Philippines");
$message['body'] .=  t("\n\r------------------------------------------");
$message['body'] .=  t("\n\r\n\r(c) 2010 Webdosh.net. All Rights Reserved. Webdosh.net");
}


Continue to Part 3

Contact Form Module for Drupal - Part 1

There are several contact form module that built for Drupal. However, I would like to introduce to you on how to create your own Contact Form module for Drupal. In this moment, I assume that you at least read and understand how Drupal Module works. You can learn the basic steps in creating module in their Module’s Developer Guide.

In building Drupal Module you need to go under the following ladder of learning. You can refer to the following links in moving forward.

It is quite need time to master all of the module stuff but all you need to do is to dedicate and a lot of coding practice.
Let me show one basic example of Drupal Module. This is a contact form module.
In creating contact form module, you will need more or less the following file.
  • contact.info - info file
  • contact.install – the installation file
  • contact.module – module file
  • contact -block.tpl.php – the tpl file
  • contact.css – a css file
  • contact.js – js file

Let’s go one by one.

Contact.info

The contact.info file contain the following

name (Required) – the display name of your module.
description (Required) – A short description of your module
core (Required) – it refers to the drupal version where module compatible with
dependencies (Optional) – dependcies module which required by your module to be create before some one can successfully install it.
package (Optional) – it is simply group you modules with the same package name in drupal backend. Good example is the name of your company. In our example, our .info file looks like this.

; $Id: Contact.info,v 1.0 2009/07/01 05:50:57 t
name = NVR Contact
description = NVR Contact.
package = NVR Contact
core = 6.x

; Information added by drupal.org packaging script on 2009-07-1
version = "6.x"
project = "nvr"
datestamp = "1240859105"

The second file is the contact.install. As the name speaks itself, this file is the one which create the database schema for our module.

There are three actions that we do to our contact.install file. The first is, the installation of contact table in your drupal database.
function contact_install() {
  db_query("UPDATE {system} SET weight = 0 WHERE name = 'contact'");
  drupal_install_schema('contact'); 
}
contact_install is drupal hooks that install table in your database. To understand what is hooks and know other hooks, visit this page.

Ok, the next hook that we need to do is the hook schema. This hook assign fields to our newly created table.
function contact_schema() { 
  $schema['contact'] = array( 
    'fields' => array( 
      'id'      => array('type' => 'serial', 'not null' => TRUE), 
      'name'    => array('type' => 'varchar', 'length' => 50, 'not null' => TRUE),
      'email'   => array('type' => 'varchar','length' => 50, 'not null' => TRUE),  
    ), 
    'primary key' => array('id'), 
  );
  return $schema; 
}

And the last, if there were hook install there should be a hook uninstall.
/**
 * Implementation of hook_uninstall().
 */
function nvr_uninstall() {
  // Remove tables.
  drupal_uninstall_schema('contact');
  variable_del('contact');
  
  db_query("DELETE FROM {system} WHERE name = 'contact'");
  db_query("DELETE FROM {menu_links} WHERE module = 'contact'");
}

The 3 combined hooks complete the contact.install file.

Continue here...

Mar 2, 2011

Creating Content in Drupal Admin using CKEditor

Please follow these steps in creating content in Drupal Admin.

1. In Admin Backend Panel – Click Create content and choose content type to use. Content types are explained in image below.


2. Creating a content using Ex."Page Content Type".


2.1 Enter desired page title.
2.2 Insert content in body. The body use CKEditor for user inputs.
2.3 Select “Full HTML” in Input Format.
2.4 Choose Comment Setting Option.
2.5 Enter URL path of the pages you want to create. Ex. for about page. Enter “about”.
2.6 Click Save Button







CKEditor Basic Function

1. Rectangular Shape represents the basic formatting function of editor. (See image – From left to right)


a. Bold
b. Italic
c. Underline
d. Strikethrough
e.f.g.h Left center, right and justify alignment
i.j Bullet and numbering
k. Insert Anchor
l. Insert Image
l. Font Format
m. Font-family
n. Font-size
o. Font-style
p. Table
q. flash


2. Circe Shape highlighted are tables, images and flash

Inserting Image to your Content

a. Click the image icon in CKEditor.


b. You can directly paste image URL in the box or “Browse Server”. Clicking Browser server will pop up a screen below.

c. Images that already uploaded in FTP / SERVER are list at the right side of the screen.

d. To upload new image click the upload link in the left side of the screen and do the upload process.

e. If upload complete the image will be visible in the image list.

f. Double click the desired image to insert it in your content.

g. The pop screen will close and image URL will be inserted in URL Text-field. Please see image below. Click OK to insert it in your content.

NOTE: Process in inserting an image is also applied in inserting FLASH object.

How to Create and Assign User Roles in Drupal

Creating User Roles

1. Login is as the administrator and go to the administrator panel.

2. You need to create a new user by Administer -> User Management -> Users and click Add user



3. Create new user as shown.



The user currently has limited privileges. Since we want this user to have admin privileges, we have to create an administrator role and assign this user to that role.

5. Go Administer -> User Management -> Roles and create an administrator role as shown.



6. Edit the permissions of the administrator role.



7.Give this role full access by check marking everything.



8. Edit the newly created user.



9.Assign this newly created user the administrator role.



10. Now when the new admin logs in, he will have all the menus and controls that an administrator will have.
Note:
• Adding new roles for specific users with limited permissions will add security to what they can access on the backend.
• Check only the role (permission) that applies to the added user.

Assigning User Roles

Go to Administer -> User Management -> Users

1. Choose user (s) by checking checkbox beside user name.
2. Select type of roles in update options (dropdown button), then click the update button to update the roles of selected user(s).


Aug 19, 2010

Why Drupal?

            Drupal is a free software package that allows an individual, a community of users, or an enterprise to easily publish, manage and organize a wide variety of content on a website. Hundreds of thousands of people and organizations are using Drupal to power an endless variety of web sites, including


Features             
Built in Application
               
                Drupal has various built in application like forums, contacts, and polls etc. These applications provide valuable functions.

Management
Managing your site with Drupal is ease, convenient and available through web-based administration- you can administer your site from a web browser anywhere without any further installation or need of a third party software. Drupal has the capability to manipulate content in simple and organize way which fit to City of Larksur site. It has an analytical, statistical and tracking utility built in that most of site needed.
               
                Performance
One of the major benefits in the Drupal side is the caching functionality which reduces the queries that will help the busy site stay up and running fast for the users benefits. It also allows balancing in multiple servers which keep the site running quickly.

                Ease of use
Server page language and friendly URLs are the only built-ins here. 
Other benefits

    • Reduce costs of site maintenance
    • Increase security, Greater consistency
    • Reduce information duplication
    • Improve site navigation
    • Quick turnaround time for new pages and changes