| 1 |
<?php
|
| 2 |
// $Id: preferred_format.module,v 1.7 2008/03/26 17:50:35 gdevlugt Exp $
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* This module allows your users to set their preferred default input format
|
| 6 |
* for placing comments and each content type.
|
| 7 |
*
|
| 8 |
* As the site administrator you can choose which roles are allowed to
|
| 9 |
* choose their preferred default input on the permissions page.
|
| 10 |
*
|
| 11 |
* Maintained by: Geoffrey de Vlugt <gdevlugt@gmail.com>
|
| 12 |
*/
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Implementation of hook_help().
|
| 16 |
*/
|
| 17 |
function preferred_format_help($path, $arg) {
|
| 18 |
$output = '';
|
| 19 |
|
| 20 |
switch ($path) {
|
| 21 |
case "admin/by-module/#description" :
|
| 22 |
case "admin/help#preferred_format" :
|
| 23 |
$output = '<p>'. t('This module allows your users to set their preferred default input format for placing comments and each content type.') .'</p>';
|
| 24 |
case "admin/help#preferred_format" :
|
| 25 |
$output .= '<p>'. t('As the site administrator you can choose which roles are allowed to choose their preferred default input on the permissions page.') .'</p>';
|
| 26 |
break;
|
| 27 |
}
|
| 28 |
|
| 29 |
return $output;
|
| 30 |
}
|
| 31 |
|
| 32 |
/**
|
| 33 |
* Implementation of hook_perm().
|
| 34 |
*/
|
| 35 |
function preferred_format_perm() {
|
| 36 |
return array('can set preferred format');
|
| 37 |
}
|
| 38 |
|
| 39 |
/**
|
| 40 |
* Implementation of hook_user().
|
| 41 |
*/
|
| 42 |
function preferred_format_user($op, &$edit, &$account) {
|
| 43 |
global $user;
|
| 44 |
switch ($op) {
|
| 45 |
case 'form' :
|
| 46 |
// Add the user can choose it's own preferred input format,
|
| 47 |
// append the user edit form with the necessary form field.
|
| 48 |
if (user_access('can set preferred format')) {
|
| 49 |
// Get a list of all input formats available to the user.
|
| 50 |
$formats = _preferred_format_get_available_input_formats($account);
|
| 51 |
|
| 52 |
// Get a list of all node types which the user is allowed to create.
|
| 53 |
$types = _preferred_format_get_available_node_types($account);
|
| 54 |
|
| 55 |
$form['preferred_format'] = array(
|
| 56 |
'#tree' => TRUE,
|
| 57 |
'#type' => 'fieldset',
|
| 58 |
'#title' => t('Preferred input formats'),
|
| 59 |
'#description' => t('Choose the preferred input format you like to use for each content type.'),
|
| 60 |
'#collapsible' => TRUE,
|
| 61 |
'#weight' => 9,
|
| 62 |
);
|
| 63 |
|
| 64 |
$form['preferred_format']['comments'] = array(
|
| 65 |
'#type' => 'select',
|
| 66 |
'#title' => t('Preferred input format for comments'),
|
| 67 |
'#options' => $formats,
|
| 68 |
'#default_value' => _preferred_format_load($account->uid, ''),
|
| 69 |
);
|
| 70 |
|
| 71 |
if (count($types)) {
|
| 72 |
foreach ($types as $key => $name) {
|
| 73 |
$form['preferred_format']['node_types'][$key] = array(
|
| 74 |
'#type' => 'select',
|
| 75 |
'#title' => t('Preferred input format for !nodetype', array('!nodetype' => $name)),
|
| 76 |
'#options' => $formats,
|
| 77 |
'#default_value' => _preferred_format_load($account->uid, $key),
|
| 78 |
);
|
| 79 |
}
|
| 80 |
}
|
| 81 |
|
| 82 |
return $form;
|
| 83 |
}
|
| 84 |
break;
|
| 85 |
case 'update' :
|
| 86 |
// Store preferred format settings for comments.
|
| 87 |
if (isset($edit['preferred_format']['comments'])) {
|
| 88 |
// Store preferred input format settings of user for comments.
|
| 89 |
db_query("DELETE FROM {preferred_format} WHERE uid = %d AND node_type = ''", $account->uid);
|
| 90 |
db_query("INSERT INTO {preferred_format} (uid, format_id, node_type) VALUES (%d, %d, '')", $account->uid, $edit['preferred_format']['comments']);
|
| 91 |
}
|
| 92 |
|
| 93 |
// If available, store preferred input format settings for each node type.
|
| 94 |
if (count($edit['preferred_format']['node_types'])) {
|
| 95 |
foreach ($edit['preferred_format']['node_types'] as $node_type => $format_id) {
|
| 96 |
db_query("DELETE FROM {preferred_format} WHERE uid = %d AND node_type = '%s'", $account->uid, $node_type);
|
| 97 |
db_query("INSERT INTO {preferred_format} (uid, format_id, node_type) VALUES (%d, %d, '%s')", $account->uid, $format_id, $node_type);
|
| 98 |
}
|
| 99 |
}
|
| 100 |
break;
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
/**
|
| 105 |
* Implementation of hook_form_alter().
|
| 106 |
*/
|
| 107 |
function preferred_format_form_alter(&$form, &$form_state, $form_id) {
|
| 108 |
global $user;
|
| 109 |
|
| 110 |
// Check whether the user is allowed to have a preferred input format.
|
| 111 |
if (user_access('can set preferred format')) {
|
| 112 |
// If on an node add form or a comment add form.
|
| 113 |
if ($form['#id'] == 'node-form' && $form['nid']['#value'] == '' && is_array($form['body_field']['format'])) {
|
| 114 |
$type = $form['type']['#value'];
|
| 115 |
$form_field = 'body_field';
|
| 116 |
}
|
| 117 |
elseif ($form['#id'] == 'comment-form' && $form['cid']['#value'] == '' && is_array($form['comment_filter']['format'])) {
|
| 118 |
$type = '';
|
| 119 |
$form_field = 'comment_filter';
|
| 120 |
}
|
| 121 |
|
| 122 |
if (isset($form_field)) {
|
| 123 |
$preferred_format = _preferred_format_load($user->uid, $type);
|
| 124 |
|
| 125 |
// Unselect all input formats and set the preferred input format.
|
| 126 |
foreach ($form[$form_field]['format'] as $key => $value) {
|
| 127 |
if (intval($key)) {
|
| 128 |
unset($form[$form_field]['format'][$key]['#default_value']);
|
| 129 |
|
| 130 |
if ($key == $preferred_format) {
|
| 131 |
$form[$form_field]['format'][$key]['#default_value'] = $preferred_format;
|
| 132 |
}
|
| 133 |
}
|
| 134 |
}
|
| 135 |
|
| 136 |
$form['has_preferred_input_format'] = array(
|
| 137 |
'#type' => 'value',
|
| 138 |
'#value' => 1,
|
| 139 |
);
|
| 140 |
|
| 141 |
}
|
| 142 |
}
|
| 143 |
}
|
| 144 |
|
| 145 |
/**
|
| 146 |
* Implementation of hook_nodeapi().
|
| 147 |
*/
|
| 148 |
function preferred_format_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
|
| 149 |
if ($op == 'insert' && !isset($node->has_preferred_input_format)) {
|
| 150 |
$input_format = _preferred_format_load($node->uid, $node->type);
|
| 151 |
db_query('UPDATE {node_revisions} SET format = %d WHERE nid = %d', $input_format, $node->nid);
|
| 152 |
}
|
| 153 |
}
|
| 154 |
|
| 155 |
/**
|
| 156 |
* Returns the preferred input format.
|
| 157 |
*
|
| 158 |
* @param $uid
|
| 159 |
* User ID
|
| 160 |
* @param $type
|
| 161 |
* String containing the node type or empty string (default) for comments.
|
| 162 |
* @return
|
| 163 |
* ID of the input format.
|
| 164 |
*/
|
| 165 |
function _preferred_format_load($uid, $type = '') {
|
| 166 |
$format = db_result(db_query("SELECT format_id FROM {preferred_format} WHERE uid = %d AND node_type = '%s'", $uid, $type));
|
| 167 |
|
| 168 |
if ($format == NULL) {
|
| 169 |
return variable_get('filter_default_format', 1);
|
| 170 |
}
|
| 171 |
else {
|
| 172 |
return $format;
|
| 173 |
}
|
| 174 |
}
|
| 175 |
|
| 176 |
/**
|
| 177 |
* Returns a list of all available node types for the given user.
|
| 178 |
*
|
| 179 |
* @param $account
|
| 180 |
* User object (optional)
|
| 181 |
* @return
|
| 182 |
* Associative array with as keys the node type internal names and
|
| 183 |
* as values the node type descriptive names.
|
| 184 |
*/
|
| 185 |
function _preferred_format_get_available_node_types($account = NULL) {
|
| 186 |
$types = node_get_types('names');
|
| 187 |
|
| 188 |
if (count($types)) {
|
| 189 |
if (user_access('administer nodes', $account)) {
|
| 190 |
// All node types are available for this user.
|
| 191 |
return $types;
|
| 192 |
}
|
| 193 |
else {
|
| 194 |
$types_list = array();
|
| 195 |
foreach ($types as $key => $type_name) {
|
| 196 |
// Check if the current user can access the node/add/<content_type>
|
| 197 |
// page to determine whether the user can set a preferred format for
|
| 198 |
// this type.
|
| 199 |
$router_item = menu_get_item('node/add/'. $key);
|
| 200 |
if ($router_item['access'] == 1) {
|
| 201 |
$types_list[$key] = $type_name;
|
| 202 |
}
|
| 203 |
}
|
| 204 |
return $types_list;
|
| 205 |
}
|
| 206 |
}
|
| 207 |
}
|
| 208 |
|
| 209 |
/**
|
| 210 |
* Returns a list of available input formats.
|
| 211 |
*
|
| 212 |
* @param $account
|
| 213 |
* User object (optional)
|
| 214 |
* @return
|
| 215 |
* Associative array with as keys the input format ID's and
|
| 216 |
* as values the input format descriptive names.
|
| 217 |
*/
|
| 218 |
function _preferred_format_get_available_input_formats($account = NULL) {
|
| 219 |
global $user;
|
| 220 |
|
| 221 |
// Ugly hack needed to get the list of filters for a given user.
|
| 222 |
// This because filter_formats() only returns the formats.
|
| 223 |
// Temporary store current user and set user to be the user
|
| 224 |
// identified by account.
|
| 225 |
if ($account != NULL) {
|
| 226 |
$user_temp = $user;
|
| 227 |
$user = $account;
|
| 228 |
}
|
| 229 |
|
| 230 |
$formats = filter_formats();
|
| 231 |
|
| 232 |
// Ugly hack needed to get the list of filters for a given user.
|
| 233 |
// This because filter_formats() only returns the formats.
|
| 234 |
// Switching back to the original current user.
|
| 235 |
if ($account != NULL) {
|
| 236 |
$user = $user_temp;
|
| 237 |
}
|
| 238 |
|
| 239 |
$format_list = array();
|
| 240 |
if (count($formats)) {
|
| 241 |
foreach ($formats as $key => $format) {
|
| 242 |
$format_list[$key] = $format->name;
|
| 243 |
}
|
| 244 |
}
|
| 245 |
|
| 246 |
return $format_list;
|
| 247 |
}
|