| 1 |
<?php
|
| 2 |
// $Id: rolespecific_node.module,v 1.10 2008/11/19 22:51:09 lapino Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* The rolespecific node module that enables "linking" between a role and a content type.
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Implementation of hook_help().
|
| 10 |
*/
|
| 11 |
function rolespecific_node_help($path, $arg) {
|
| 12 |
$output = '';
|
| 13 |
switch ($path) {
|
| 14 |
case "admin/help#rolespecific_node":
|
| 15 |
$output = '<p>'. t("Publishes or unpublishes a node of a specified content type when a user's role is changed.") .'</p>';
|
| 16 |
break;
|
| 17 |
}
|
| 18 |
return $output;
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Implementation of hook_admin().
|
| 23 |
*/
|
| 24 |
function rolespecific_node_admin() {
|
| 25 |
$form['rolespecific_node_role'] = array(
|
| 26 |
'#type' => 'select',
|
| 27 |
'#title' => t('Role'),
|
| 28 |
'#default_value' => variable_get('rolespecific_node_role', NULL),
|
| 29 |
'#rows' => 1,
|
| 30 |
'#multiple' => FALSE,
|
| 31 |
'#description' => t('When a user gets assigned this role, a node of the specified type will be created (or published if it exists already). When the user loses this role, the node gets unpublished.'),
|
| 32 |
'#options' => user_roles(TRUE),
|
| 33 |
'#required' => TRUE,
|
| 34 |
);
|
| 35 |
$node_types = node_get_types('names', NULL, TRUE);
|
| 36 |
$form['rolespecific_node_nodetype'] = array(
|
| 37 |
'#type' => 'select',
|
| 38 |
'#title' => t('Node type'),
|
| 39 |
'#default_value' => variable_get('rolespecific_node_nodetype', NULL),
|
| 40 |
'#rows' => 1,
|
| 41 |
'#multiple' => FALSE,
|
| 42 |
'#description' => t('The node type that will be (un)published or created, based on the circumstances.'),
|
| 43 |
'#options' => $node_types,
|
| 44 |
'#required' => TRUE,
|
| 45 |
);
|
| 46 |
$form['rolespecific_node_titleprefix'] = array(
|
| 47 |
'#type' => 'textfield',
|
| 48 |
'#title' => t('Title prefix'),
|
| 49 |
'#default_value' => variable_get('rolespecific_node_titleprefix', ''),
|
| 50 |
'#description' => t('Text to include before the automatically generated title configured below.'),
|
| 51 |
'#required' => FALSE,
|
| 52 |
);
|
| 53 |
$titlefield_options = array(
|
| 54 |
'name' => t('User name'),
|
| 55 |
'mail' => t('User email'),
|
| 56 |
);
|
| 57 |
if(module_exists("profile")){
|
| 58 |
$result = db_query('SELECT name, title FROM {profile_fields}');
|
| 59 |
while ($record = db_fetch_object($result)) {
|
| 60 |
$titlefield_options[$record->name] = $record->title;
|
| 61 |
}
|
| 62 |
}
|
| 63 |
$form['rolespecific_node_titlefield'] = array(
|
| 64 |
'#type' => 'select',
|
| 65 |
'#title' => t('Title field'),
|
| 66 |
'#default_value' => variable_get('rolespecific_node_titlefield', 'name'),
|
| 67 |
'#rows' => 1,
|
| 68 |
'#multiple' => FALSE,
|
| 69 |
'#description' => t('This field will be used to generate the title of the automatically created node. Options include profile fields when available.'),
|
| 70 |
'#options' => $titlefield_options,
|
| 71 |
'#required' => TRUE,
|
| 72 |
);
|
| 73 |
$form['rolespecific_node_message'] = array(
|
| 74 |
'#type' => 'textfield',
|
| 75 |
'#title' => t('Message'),
|
| 76 |
'#default_value' => variable_get('rolespecific_node_message', ''),
|
| 77 |
'#description' => t('Message to show after changes have been made.'),
|
| 78 |
'#required' => FALSE,
|
| 79 |
);
|
| 80 |
return system_settings_form($form);
|
| 81 |
}
|
| 82 |
|
| 83 |
/**
|
| 84 |
* Implementation of hook_menu().
|
| 85 |
*/
|
| 86 |
function rolespecific_node_menu() {
|
| 87 |
$items = array();
|
| 88 |
|
| 89 |
$items['admin/settings/rolespecific_node'] = array(
|
| 90 |
'title' => 'Rolespecific Node',
|
| 91 |
'description' => 'This module allows you to create or (un)publish specific nodes on a user role change.',
|
| 92 |
'page callback' => 'drupal_get_form',
|
| 93 |
'page arguments' => array('rolespecific_node_admin'),
|
| 94 |
'access arguments' => array('access administration pages'),
|
| 95 |
'type' => MENU_NORMAL_ITEM,
|
| 96 |
);
|
| 97 |
|
| 98 |
return $items;
|
| 99 |
}
|
| 100 |
|
| 101 |
/**
|
| 102 |
* Implementation of hook_user().
|
| 103 |
*
|
| 104 |
* Checks if a user has a certain role (to be correct: will get the role after the insert or update)
|
| 105 |
* and creates or (un)publishes an associated content type.
|
| 106 |
*/
|
| 107 |
function rolespecific_node_user($op, &$edit, &$account, $category = NULL) {
|
| 108 |
if ($op == 'update' || $op == 'insert') {
|
| 109 |
// Check if changes have been made to the roles
|
| 110 |
if (array_key_exists('roles', $edit) ) {
|
| 111 |
// Settings
|
| 112 |
$role = variable_get('rolespecific_node_role', NULL);
|
| 113 |
if (!isset($role)) {
|
| 114 |
drupal_set_message(t('You have to configure the user role for the module Rolespecific Node.'), 'error', FALSE);
|
| 115 |
return;
|
| 116 |
}
|
| 117 |
$type = variable_get('rolespecific_node_nodetype', NULL);
|
| 118 |
if (!isset($type)) {
|
| 119 |
drupal_set_message(t('You have to configure the node type for the module Rolespecific Node.'), 'error', FALSE);
|
| 120 |
return;
|
| 121 |
}
|
| 122 |
$title_prefix = variable_get('rolespecific_node_titleprefix', '');
|
| 123 |
$pref_title = variable_get('rolespecific_node_titlefield', 'name');
|
| 124 |
$message = variable_get('rolespecific_node_message', '');
|
| 125 |
|
| 126 |
// Load the existing node if applicable
|
| 127 |
$result = db_query('SELECT n.nid, n.status FROM {node} n WHERE n.uid = %d and n.type = "%s"', $account->uid, $type);
|
| 128 |
$assoc_node = db_fetch_object($result);
|
| 129 |
|
| 130 |
// Check if the user will get the specified role
|
| 131 |
$is_role = ($role==2 && $account->uid) || (array_key_exists( $role, $edit['roles'] ) && ($edit['roles'][$role] == TRUE));
|
| 132 |
if ($is_role == TRUE) { // The user is of the specified role, if he doesn't have a profile, we create one for him
|
| 133 |
if ($assoc_node == FALSE) {
|
| 134 |
$node = new stdClass();
|
| 135 |
$node_options = variable_get('node_options_'. $type, array());
|
| 136 |
$node->type = $type;
|
| 137 |
$node->uid = $account->uid;
|
| 138 |
$node->status = 1;
|
| 139 |
$node->title = $title_prefix . $account->$pref_title;
|
| 140 |
$node->body = '';
|
| 141 |
$node->teaser = '';
|
| 142 |
$node->name = $account->name;
|
| 143 |
$node->promote = (in_array('promote', $node_options)?1:0);
|
| 144 |
$node->moderate = (in_array('moderate', $node_options)?1:0);
|
| 145 |
$node->sticky = (in_array('sticky', $node_options)?1:0);
|
| 146 |
node_save($node);
|
| 147 |
}
|
| 148 |
elseif ($assoc_node->status==0) { // user has role and profile, but the profile is unpublished, we change this
|
| 149 |
$sql = db_query('UPDATE {node} n SET n.status = 1 WHERE n.nid = %d', $assoc_node->nid);
|
| 150 |
}
|
| 151 |
if ($message!='') {
|
| 152 |
drupal_set_message($message, 'status', FALSE);
|
| 153 |
}
|
| 154 |
}
|
| 155 |
else { // User doesn't have this role, let's check if he has a profile that is published and if so, unpublish it
|
| 156 |
if ($assoc_node!=NULL && $assoc_node->status==1) {
|
| 157 |
$sql = db_query('UPDATE {node} n SET n.status = 0 WHERE n.nid = %d', $assoc_node->nid);
|
| 158 |
}
|
| 159 |
}
|
| 160 |
}
|
| 161 |
}
|
| 162 |
}
|