• Avail guidance to develop Drupal modules & Drupal themes
  • Share and get review of your code
  • Get Access to free CodeBooks and ThemeBooks
  • Drupal 5, 6 and 7 covered

Userpoints Meter

0 Points /1000 Points

Node information

in Project codes

This CodeLet shows a link on nodes to request more information for a node. By clicking the link, the user will be redirected to the site wide contact page and the subject field of the contact form is filled with the node title. Also, the link to the node will be added just before the mail will be sent. Thus preventing the user from modifying the link.

<?php
//$Id$


/**
* @file
*
* Add a link to node requesting more information related to a node/product
*
* @author Drupal Deveveloper <http://twitter.com/DrupalD>
*/


/**
* Implementation of hook_menu
*
* @return unknown_type
* @author Bhaivn H. Joshi <bhavinjosi@joshics.in>
*/
function node_feedback_menu() {
 
$item = array();
 
$item['admin/settings/more-info'] = array(
   
'title' => 'Node feedback',
   
'title callback' => 't',
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('node_feedback_admin'),
   
'access arguments' => array('administer site configuration'),
  );

  return
$item;
}


/**
* Implementation of hook_nodeapi
*
* @param $node
* @param $op
* @param $a3
* @param $a4
* @return unknown_type
* @author Drupal Deveveloper <http://twitter.com/DrupalD>
*/
function node_feedback_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch(
$op) {
    case
'insert':
    case
'update':
      if (
$node->request_info && $node->request_category) {
       
set_feedback($node->nid, 1, $node->request_category);
      }
      else {
       
set_feedback($node->nid, 0);
      }
      break;
  }
}



/**
* Implementation of hook_form_FORM_ID_alter
*
* @param $form
* @param $form_state
* @author Drupal Deveveloper <http://twitter.com/DrupalD>
*/
function node_feedback_form_alter(&$form, $form_state, $form_id) {
  global
$user;
  if (
$form_id == 'contact_mail_page' || $form_id == 'contact_mail_user') {
   
$sn__nid = $_GET['node'];
   
$ss__title = get_node_title($sn__nid);
   
$ss__subject = variable_get('subject_text', '@link_text: @node_title');
   
$b__force_feedback = variable_get('more_force_feedback', 0);
   
$sn__cid = $sn__cid = $b__force_feedback ? variable_get('more_force_request_category', 0) : get_feedback($sn__nid, 0);
   
$am__tokens = array(
     
'@link_text' => variable_get('more_link_text', t("Send feedback")),
     
'@node_title' => $ss__title,
     
'@node_id' => $sn__nid,
     
'@username' => $user->name,
     
'@category' => _get_contact_category($sn__cid),
    );
   
    if (
$sn__nid) {
     
$form['subject']['#default_value'] = t($ss__subject, $am__tokens);
      if (
$sn__cid) {
       
$form['cid']['#default_value'] = $sn__cid
      }
       

     
$form['link'] = array(
       
'#type' => 'hidden',
       
'#value' => $sn__nid,
      );
    }
  }
 
  if (
strstr($form_id, 'node_form')) {
    if (
variable_get('more_force_feedback', 0) == 0) {
     
$form['contact_info'] = array(
       
'#type' => 'fieldset',
       
'#title' => t("Node feedabck"),
       
'#collapsible' => FALSE,
       
'#collapsed' => TRUE,
       
'#weight' => -1,
      );
 
     
$form['contact_info']['request_info'] = array(
       
'#type' => 'checkbox',
       
'#title' => t("Display '@node_feedback' link",
          array(
'@node_feedback' => variable_get('more_link_text', t("Send feedback")))),
       
'#description' => t("If checked, the link '@node_feedback' will apear on node",
          array(
'@node_feedback' => variable_get('more_link_text', t("Send feedback")))),
       
'#default_value' => get_feedback(arg(1), 1),
       
'#weight' => -10,
      );
     
     
$form['contact_info']['request_category'] = array(
       
'#type' => 'select',
       
'#title' => t("Category"),
       
'#description' => t("Select the category which will be set as selected on the contact form when sending a message."),
       
'#options' => _get_contact_category(),
       
'#default_value' => get_feedback(arg(1), 0),
       
'#weight' => -9,
      );
     
      if (
user_access("administer site configuration")) {
       
$form['contact_info']['info'] = array(
         
'#type' => 'item',
         
'#value' => t("To change the link text and other options, please visit !node_feedback_settings page.",
            array(
'!node_feedback_settings' => l(t("settings"), 'admin/settings/more-info'))),
        );
      }
    }
  }
}



/**
* get_node_title
*
* Return title of the node
*
* @param $sn__nid
* @return node title
* @author Drupal Deveveloper <http://twitter.com/DrupalD>
*/
function get_node_title($sn__nid) {
  return
db_result(db_query("SELECT title from {node} WHERE nid = %d", $sn__nid));
}



/**
* Implementation of hook_mail_alter
*
* @param $message
* @author Drupal Deveveloper <http://twitter.com/DrupalD>
*/
function node_feedback_mail_alter(&$message) {
  if (
$message['id'] == 'contact_page_mail') {
   
$sn__nid = $_POST['link'];
   
$message['body'][1] = "<a href='". url("node/". $sn__nid, array('absolute' => TRUE))
      .
"'>". drupal_get_path_alias("node/". $sn__nid) ."</a>\r\n". $message['body'][1];
   
$message['headers']['Content-Type'] = 'text/html;charset=UTF-8; format=flowed; delsp=yes';
  }
}



/**
* Implementation of hook_link
*
* @param $type
* @param $object
* @param $teaser
* @return $links
* @author Drupal Deveveloper <http://twitter.com/DrupalD>
*/
function node_feedback_link($type, $object, $teaser = FALSE) {
 
$ss__teaser_view = variable_get('more_tease_view', 1);
 
$ss__full_view = variable_get('more_full_view', 1);
 
$b__teaser = TRUE;
  switch (
$ss__teaser_view) {
    case
0:
     
$b__teaser = $teaser ? FALSE : TRUE;
      break;
    case
1:
     
$b__teaser = $teaser ? TRUE : FALSE;
      break;
  }
 
  switch (
$ss__teaser_view) {
    case
0:
     
$b__full = FALSE;
      break;
    case
1:
     
$b__full = TRUE;
      break;
  }

  if (
$type == 'node' && ($b__teaser || $b__full)) {
   
$am__more_nodes = variable_get('more_node_type', "");
   
$b__force_feedback = variable_get('more_force_feedback', 0);
   
$sn__cid = $b__force_feedback ? variable_get('more_force_request_category', 0) : get_feedback($object->nid, 0);
    if (
get_feedback($object->nid, 1) || $b__force_feedback) {
     
$sn__form = variable_get("display_on", 0);
      if (!
$sn__form) {
       
$links['node_feedback'] = array(
         
'href' => 'contact',
         
'query' => "node=". $object->nid ."&cid=". $sn__cid,
         
'title' => variable_get('more_link_text', t("Send feedback")),
        );
      }
      else {
//        if (!empty($am__more_nodes) && in_array($object->type, array_keys($am__more_nodes))) {
           
$links['node_feedback'] = array(
             
'href' => 'user/'. $object->uid ."/contact",
             
'query' => "node=". $object->nid ."&cid=". $sn__cid,
             
'title' => variable_get('more_link_text', t("Send feedback")),
            );
//       }
     
}
    }
  }
  return
$links;
}



/**
* node_feedback_admin
*
* @return unknown_type
* @author Drupal Deveveloper <http://twitter.com/DrupalD>
*/
function node_feedback_admin() {
 
$form = array();
 
$form['more_tease_view'] = array(
   
'#type' => 'checkbox',
   
'#title' => t("Display '@node_feedback' link on node teasers",
      array(
'@node_feedback' => variable_get('more_link_text', t("Send feedback")))),
   
'#default_value' => variable_get('more_tease_view', 1),
  );

 
$form['more_full_view'] = array(
   
'#type' => 'checkbox',
   
'#title' => t("Display '@node_feedback' link on full node view",
      array(
'@node_feedback' => variable_get('more_link_text', t("Send feedback")))),
   
'#default_value' => variable_get('more_full_view', 1),
  );

 
$form['more_force_feedback'] = array(
   
'#type' => 'checkbox',
   
'#title' => t("Force '@node_feedback' link on nodes",
      array(
'@node_feedback' => variable_get('more_link_text', t("Send feedback")))),
   
'#default_value' => variable_get('more_force_feedback', 0),
   
'#description' => t("Node authors will not be able to change settings on individual nodes."),
  );

 
$ss__disabled = variable_get('more_force_feedback', 0) ? "" : "disabled";
 
 
$form ['more_force_request_category'] = array(
   
'#type' => 'select',
   
'#title' => t("Category"),
   
'#description' => t("Select a category when forcing '@node_feedback' link on nodes.",
      array(
'@node_feedback' => variable_get('more_link_text', t("Send feedback")))),
   
'#disabled' => $ss__disabled,
   
'#options' => _get_contact_category(),
   
'#default_value' => variable_get('more_force_request_category', 0),
  );
 
 
$ss__disabled = variable_get('more_force_feedback', 0) ? "disabled" : "";
 
$am__more_nodes = variable_get('more_node_type', "");
 
$form['more_node_type'] = array(
   
'#type' => 'select',
   
'#title' => t("Node types"),
   
'#options' => _get_node_types(),
   
'#default_value' => $am__more_nodes,
   
'#multiple' => TRUE,
   
'#disabled' => $ss__disabled,
   
'#size' => 5,
   
'#description' => t("Select the node types on which you want to display the '@node_feedback' link. Please note that if the link is forced, it will be displayed on all nodes.",
      array(
'@node_feedback' => variable_get('more_link_text', t("Send feedback")))),
  ); 
 
 
 
$form['more_link_text'] = array(
   
'#type' => 'textfield',
   
'#title' => t("Link text"),
   
'#description' => t("Set the text of the link that will be diplayed on teaser and full node."),
   
'#default_value' => variable_get('more_link_text', t("Send feedback")),
  );
 
 
$form['display_on'] = array(
   
'#type' => 'radios',
   
'#title' => t("Use form"),
   
'#description' => t("Select the form to be used"),
   
'#required' => TRUE,
   
'#options' => array(
     
0 => t("Site wide contact form"),
     
1 => t("User's personal contact form"),
    ),
   
'#default_value' => variable_get("display_on", 0),
  );
 
 
$form['subject_text'] = array(
   
'#type' => 'textfield',
   
'#title' => t("Contact form subject text"),
   
'#description' => t("Set the default pattern of contact form subject text"),
   
'#default_value' => variable_get('subject_text', '@link_text: @node_title'),
  );
 
 
$form['token_field'] = array(
   
'#type' => 'fieldset',
   
'#title' => t("Available fields"),
  );
 
 
$am__token = array(
   
t("@link_text: Link text"),
   
t("@node_title: Node title"),
   
t("@node_id: Node id"),
   
t("@username: Username"),
   
t("@category: Category"),
  );
 
 
$form['token_field']['node_feedback_token'] = array(
   
'#type' => 'item',
   
'#value' => theme_item_list($am__token),
  );

  return
system_settings_form($form);
}


/**
* set_feedback
*
* Set the node settings for feedback link
*
* @param unknown_type $sn__nid
* @param unknown_type $sn__value
* @author Drupal Deveveloper <http://twitter.com/DrupalD>
*/
function set_feedback($sn__nid, $sn__value, $sn__cid = NULL) {
 
db_query("UPDATE {node_feedback} SET value = %d, cid = %d WHERE nid = %d",
   
$sn__value, $sn__cid, $sn__nid);
  if (!
db_affected_rows()) {
    @
db_query("INSERT INTO {node_feedback} VALUES (%d, %d, %d)",
     
$sn__nid, $sn__value, $sn__cid);
  }
}



/**
* get_feedback
*
* @param unknown_type $sn__nid
* @param unknown_type $sn__default
* @author Drupal Deveveloper <http://twitter.com/DrupalD>
*
*/
function get_feedback($sn__nid, $sn__default = 1) {
   if (
$sn__nid && $sn__default) {
     return
db_result(db_query("SELECT value FROM {node_feedback} WHERE nid = %d", $sn__nid));
   }
  
   if (
$sn__nid && !$sn__default) {
     return
db_result(db_query("SELECT cid FROM {node_feedback} WHERE nid = %d", $sn__nid));
   }
}


/**
*
* @param unknown_type $sn__cid
* @author Drupal Deveveloper <http://twitter.com/DrupalD>
*/
function _get_contact_category($sn__cid = NULL) {
  if (!
$sn__cid) {
   
$am__category[0] = "-- ". t("Select category") ." --";
   
$r__result = db_query("SELECT cid, category FROM {contact} ORDER BY weight ASC");
    while (
$om__result = db_fetch_object($r__result)) {
     
$am__category[$om__result->cid] = $om__result->category;
    }
   
    return
$am__category;
  }
  else {
    return
db_result(db_query("SELECT category FROM {contact} WHERE cid = %d", $sn__cid));
  }
 
}

/**
* _get_node_types
*
*  @author Drupal Deveveloper <http://twitter.com/DrupalD>
*/
function _get_node_types() {
 
$am__node_type = node_get_types();
  foreach (
$am__node_type as $key => $value) {
   
$am__type[$key] = $value->name;
  }
  return
$am__type;
}
?>

5
Your rating: None Average: 5 (3 votes)
Syndicate content

Recent comments