Commit 1301a99e authored by dragonwize's avatar dragonwize
Browse files

Meging changes from 6.x-1 branch to head

parent 22c7bfbf
; $Id$
name = Better Formats
description = "Enhances the core input format system by managing input format defaults and settings."
description = Enhances the core input format system by managing input format defaults and settings.
core = 6.x
......@@ -5,7 +5,7 @@
* @file
* Installs the better_formats module.
*
* Creates a database for use of multi-layered default formats and sets
* Creates a database for use of multi-layered default formats and sets
* default settings.
*/
......@@ -60,27 +60,27 @@ function better_formats_schema() {
function better_formats_install() {
// Create tables.
drupal_install_schema('better_formats');
// Increase module weight to prevent compatibility issues.
$sql = "UPDATE {system}
SET weight = 100
$sql = "UPDATE {system}
SET weight = 100
WHERE name = 'better_formats'";
db_query($sql);
// Insert format defaults.
$roles = user_roles();
$sql = "INSERT INTO {better_formats_defaults}
$sql = "INSERT INTO {better_formats_defaults}
VALUES (%d, '%s', %d, %d, %d)";
foreach ($roles as $rid => $role) {
db_query($sql, $rid, 'node', 0, 1, 0);
db_query($sql, $rid, 'comment', 0, 1, 0);
}
// Set default perms to be like core defaults.
$default_perms = ', show format selection, show format tips, show more format tips link, collapsible format selection, collapse format fieldset by default';
// Get current core perms.
$sql = "SELECT *
FROM {permission}
$sql = "SELECT *
FROM {permission}
WHERE rid IN (1,2)";
$result = db_query($sql);
$row_perms = array();
......@@ -89,12 +89,12 @@ function better_formats_install() {
}
// Add perms to core roles (anonymous user, authenticated user).
foreach ($role_perms as $perms) {
$sql = "UPDATE {permission}
SET perm = '%s'
$sql = "UPDATE {permission}
SET perm = '%s'
WHERE pid = %d";
db_query($sql, $perms->perm . $default_perms, $perms->pid);
}
// Clear the cached pages
cache_clear_all();
}
......@@ -105,9 +105,9 @@ function better_formats_install() {
function better_formats_uninstall() {
// Remove tables.
drupal_uninstall_schema('better_formats');
// Delete settings from varible table.
$sql = "DELETE FROM {variable}
$sql = "DELETE FROM {variable}
WHERE name LIKE 'better_formats%'";
db_query($sql);
}
......@@ -5,7 +5,7 @@
* @file
* Enhances Drupal's core input format settings.
*
* Allows setting of defaults per role and content type,
* Allows setting of defaults per role and content type,
* controls format display options, works with CCK fields.
*/
......@@ -17,13 +17,13 @@ function better_formats_help($path, $arg) {
case 'admin/help/better_formats':
$output = '<p>' . t('See the module README.txt file in the better_formats module directory for help.') . '</p>';
break;
case 'admin/settings/filters/defaults':
$output = '<p>' . t('Set the global default formats per role for NEW nodes and comments. These settings will be applied to all nodes and comments in the site unless overriden by specific content type defaults.') . '</p>';
$output .= '<p>' . t('Arrange the roles to provide weight that will determine what format is selected when a user has more than one role. Remember, that all logged in users are automatically given the authenticated user role in addition to their other assigned roles. For example, if you have an admin role place it at the top and generally you would want your anonymous user role at the bottom.') . '</p>';
break;
default:
default:
$output = '';
}
return $output;
......@@ -47,7 +47,7 @@ function better_formats_perm() {
*/
function better_formats_menu() {
$items = array();
$items['admin/settings/filters/settings'] = array(
'title' => 'Settings',
'description' => 'Manage input formats',
......@@ -68,7 +68,7 @@ function better_formats_menu() {
'weight' => 2,
'file' => 'better_formats_defaults.admin.inc',
);
return $items;
}
......@@ -92,20 +92,20 @@ function better_formats_theme() {
* Implementation of hook_form_alter().
*/
function better_formats_form_alter(&$form, $form_state, $form_id) {
// Alter new node and comment forms.
// Using $form['#id'] instead of $form_id because $form_id is in the form of
// 'TYPE_node_form' which varies with the content type while $form['#id']
// Alter new node and comment forms.
// Using $form['#id'] instead of $form_id because $form_id is in the form of
// 'TYPE_node_form' which varies with the content type while $form['#id']
// is always 'node-form'.
switch ($form['#id']) {
case 'comment-form':
better_formats_set_comment_format($form);
break;
case 'node-form':
better_formats_set_node_format($form);
break;
}
// Alter role add/delete and node type forms.
switch ($form_id) {
case 'node_type_form':
......@@ -113,13 +113,13 @@ function better_formats_form_alter(&$form, $form_state, $form_id) {
better_formats_node_type_form($form, $form_state);
}
break;
case 'user_admin_new_role':
if (!in_array('better_formats_new_role', $form['#submit'])) {
$form['#submit'][] = 'better_formats_new_role';
}
break;
case 'user_admin_role':
if (isset($form_state['post']['op']) && $form_state['post']['op'] == 'Delete role') {
$form['#submit'][] = 'better_formats_delete_role';
......@@ -130,22 +130,22 @@ function better_formats_form_alter(&$form, $form_state, $form_id) {
/**
* FAPI form to add to the content type edit form.
*
*
* @see better_formats_node_type_form_validate()
* @see better_formats_node_type_form_submit()
*/
function better_formats_node_type_form(&$form, $form_state) {
// Add JS to enhance form.
drupal_add_js(drupal_get_path('module', 'better_formats') . '/better_formats_node_type_form.js');
$node_type = $form['#node_type']->type;
// Build array of all formats for allowed checkboxes.
$formats = filter_formats();
foreach ($formats as $format) {
$format_boxes[$format->format] = $format->name;
}
$key = 'better_formats';
$form[$key] = array(
'#type' => 'fieldset',
......@@ -166,18 +166,18 @@ function better_formats_node_type_form(&$form, $form_state) {
'#description' => t('Limit the formats users have to choose from even if they have permission to use that format. This will NOT allow a user to use a format they do not have access rights to use. It will only hide additional formats they do have access rights to. If no boxes are checked, all formats that the user has permission to use will be allowed.'),
'#attributes' => array('class' => 'bf-allowed-formats'),
);
$dform = array(
'#tree' => TRUE,
'#theme' => 'better_formats_defaults_admin_form',
);
module_load_include('admin.inc', 'better_formats', 'better_formats_defaults');
$nform = better_formats_get_role_default_fields('node', $node_type);
$cform = better_formats_get_role_default_fields('comment', $node_type);
$form[$key]['better_formats_defaults'] = array_merge($dform, $nform, $cform);
// Attach our validate and submit handlers.
$form['#validate'][] = 'better_formats_node_type_form_validate';
$form['#submit'][] = 'better_formats_node_type_form_submit';
......@@ -185,7 +185,7 @@ function better_formats_node_type_form(&$form, $form_state) {
/**
* Handles validation of the addition to the content type edit form.
*
*
* @see better_formats_node_type_form()
* @see better_formats_node_type_form_submit()
*/
......@@ -196,20 +196,20 @@ function better_formats_node_type_form_validate($form, &$form_state) {
/**
* Handles submission of the addition to the content type edit form.
*
*
* @see better_formats_node_type_form()
* @see better_formats_node_type_form_validate()
*/
function better_formats_node_type_form_submit($form, &$form_state) {
$node_type = trim($form_state['values']['type']);
// Remove current db entries.
$sql = "DELETE FROM {better_formats_defaults}
WHERE type='comment/%s' OR type='node/%s'";
db_query($sql, $node_type, $node_type);
// Insert defualt values into DB.
$sql = "INSERT INTO {better_formats_defaults}
$sql = "INSERT INTO {better_formats_defaults}
VALUES (%d, '%s', %d, %d, %d)";
foreach ($form_state['values']['better_formats_defaults'] as $key => $values) {
if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0) {
......@@ -225,18 +225,19 @@ function better_formats_node_type_form_submit($form, &$form_state) {
/**
* Creates base format default entry for a newly created role.
*
*
* @see better_formats_form_alter()
*/
function better_formats_new_role($form, &$form_state) {
// Get the ID for the role just created.
$sql = "SELECT rid
FROM {role}
$sql = "SELECT rid
FROM {role}
ORDER BY rid DESC";
$rid = db_fetch_object(db_query_range($sql, 0, 1))->rid;
$row = db_fetch_object(db_query_range($sql, 0, 1));
$rid = $row->rid;
// Create stubs in per role table.
$sql = "INSERT INTO {better_formats_defaults}
$sql = "INSERT INTO {better_formats_defaults}
VALUES (%d, '%s', %d, %d, %d)";
db_query($sql, $rid, 'node', 0, 1, 25);
db_query($sql, $rid, 'comment', 0, 1, 25);
......@@ -244,12 +245,12 @@ function better_formats_new_role($form, &$form_state) {
/**
* Deletes role format default entries for roles being deleted.
*
*
* @see better_formats_form_alter()
*/
function better_formats_delete_role($form, &$form_state) {
// Delete role from format manager table.
$sql = "DELETE FROM {better_formats_defaults}
$sql = "DELETE FROM {better_formats_defaults}
WHERE rid = %d";
db_query($sql, $form['rid']['#value']);
}
......@@ -260,10 +261,10 @@ function better_formats_delete_role($form, &$form_state) {
function better_formats_node_type($op, $info) {
if ($op === 'delete') {
// Delete per node type settings on node type delete.
$sql = "DELETE FROM {better_formats_defaults}
$sql = "DELETE FROM {better_formats_defaults}
WHERE type IN ('node/%s', 'comment/%s')";
db_query($sql, $info->type, $info->type);
// Delete node type variables.
variable_del('better_formats_allowed_' . $info->type);
}
......@@ -271,7 +272,7 @@ function better_formats_node_type($op, $info) {
/**
* Implementation of hook_elements().
*
*
* Adds a process function to CCK's textarea FAPI element.
*/
function better_formats_elements() {
......@@ -284,39 +285,39 @@ function better_formats_elements() {
/**
* Processes a CCK textarea element.
*
*
* Resets the textareas filter area with bettter_formats default.
* This function is used to affect CCK textareas not core fields.
*
*
* @see text_textarea_process()
*/
function better_formats_textarea_process($element, $edit, $form_state, $form) {
$field = $form['#field_info'][$element['#field_name']];
if (!empty($field['text_processing'])) {
// Get core default for new or selected format for existing.
$filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format';
$format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : FILTER_FORMAT_DEFAULT;
$format = isset($element['#value'][$filter_key]) ? $element['#value'][$filter_key] : $element['#default_value'][$filter_key];
$parents = array_merge($element['#parents'] , array($filter_key));
$default = better_formats_get_default_format('node', $form['type']['#value']);
// Overwrite format default if new node.
if (!isset($form_state['values']['nid'])) {
if (!isset($form_state['values']['nid']) || !isset($format)) {
$format = $default;
}
$format = filter_resolve_format($format);
// Set default format for cck textarea.
$element['#value'][$filter_key] = $format;
// Set filter selection form.
$element[$filter_key] = better_formats_filter_form($format, $default, $form['type']['#value'], 1, $parents);
}
return $element;
}
/**
* Processes formats for core node body fields.
*
*
* @see better_formats_form_alter()
*/
function better_formats_set_node_format(&$form) {
......@@ -324,16 +325,16 @@ function better_formats_set_node_format(&$form) {
if (isset($form['body_field'])) {
// Get default for new entries.
$default = better_formats_get_default_format('node', $form['type']['#value']);
if (empty($form['nid']['#value'])) {
// Set format to default for new entries.
$format = $default;
}
}
else {
// Get existing format for core body field.
$format = better_formats_get_current_format($form['body_field']['format']);
}
// Overwrite the filter form with our own.
$form['body_field']['format'] = better_formats_filter_form($format, $default, $form['type']['#value']);
}
......@@ -341,20 +342,20 @@ function better_formats_set_node_format(&$form) {
/**
* Processes formats for core node comment form.
*
*
* @see better_formats_form_alter()
*/
function better_formats_set_comment_format(&$form) {
if (isset($form['comment_filter']['format'])) {
$node = node_load($form['nid']['#value']);
// Get BF default format.
$default = better_formats_get_default_format('comment', $node->type);
if (empty($form['cid']['#value'])) {
// Set format to default for new entries.
$format = $default;
}
}
else {
// Get existing format for comment.
$format = better_formats_get_current_format($form['comment_filter']['format']);
......@@ -366,12 +367,12 @@ function better_formats_set_comment_format(&$form) {
/**
* Returns the format for an existing node or comment.
*
*
* @param $form
* FAPI form array.
* @return
* Format ID.
*
*
* @see better_formats_set_node_format()
* @see better_formats_set_comment_format()
*/
......@@ -394,22 +395,22 @@ function better_formats_get_current_format($form) {
/**
* Returns the default format for an new node or comment.
*
*
* @param $mode
* 'node' or 'comment'. Describes the top level type of default.
* @return
* Format ID.
*
*
* @see better_formats_set_node_format()
* @see better_formats_set_comment_format()
* @see better_formats_textarea_process()
*/
function better_formats_get_default_format($mode, $node_type = '') {
static $format;
// Default our type to the mode (node or comment).
$type = $mode;
// Check if per node type is enabled and set type accordingly.
$per_node_type = variable_get('better_formats_per_node_type', FALSE);
if ($per_node_type && $node_type) {
......@@ -419,38 +420,38 @@ function better_formats_get_default_format($mode, $node_type = '') {
// Only pull from the DB if we have not already checked for this specific type.
if (!isset($format[$type])) {
global $user;
$types = $type;
$format = array();
$roles = implode(',', array_keys($user->roles));
// Prepare types for SQL.
if ($per_node_type && $node_type) {
$types .= "','" . $mode;
}
// Get user's lowest weight role default.
$sql = "SELECT format
FROM {better_formats_defaults}
WHERE rid IN (%s) AND type IN ('$types')
$sql = "SELECT format
FROM {better_formats_defaults}
WHERE rid IN (%s) AND type IN ('$types')
ORDER BY type_weight DESC, weight ASC";
$row = db_fetch_object(db_query_range($sql, $roles, 0, 1));
$format[$type] = filter_resolve_format($row->format);
}
return $format[$type];
}
/**
* Better Formats version of filter_form().
*
* Copied from filter.module with slight modification to handle options for
* hiding filter selection and/or tips.
* The $node_type param was added to the signature to enable condition by
*
* Copied from filter.module with slight modification to handle options for
* hiding filter selection and/or tips.
* The $node_type param was added to the signature to enable condition by
* content type.
*
* @see filter_form()
*
* @see filter_form()
*/
function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_format, $node_type = '', $weight = 1, $parents = array('format')) {
$value = filter_resolve_format($value);
......@@ -460,7 +461,7 @@ function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_for
$show_tips_link = user_access('show more format tips link');
$per_node_type = variable_get('better_formats_per_node_type', FALSE);
$allowed_formats = variable_get('better_formats_allowed_' . $node_type, FALSE);
// Check if there are node type restrictions on allowed formats.
// If there are no retrictions set, we use the site globals as default.
if ($per_node_type && $allowed_formats) {
......@@ -470,7 +471,7 @@ function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_for
}
}
}
// Ensure that our default value is allowed or change default to one that is.
if (isset($formats[$value])) {
// Use existing or BF default value if available.
......@@ -488,19 +489,19 @@ function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_for
// Use core site default as a fallback if all else fails.
$default = filter_resolve_format(FILTER_FORMAT_DEFAULT);
}
if (count($formats) > 1 && $show_selection) {
$collapsed = user_access('collapse format fieldset by default');
$collapsible = user_access('collapsible format selection');
$fieldset_title = variable_get('better_formats_fieldset_title', '');
if (module_exists('i18n_strings') && $fieldset_title) {
$fieldset_title = tt($fieldset_title);
}
else {
$fieldset_title = $fieldset_title ? $fieldset_title : t('Input format');
}
$form = array(
'#type' => 'fieldset',
'#title' => $fieldset_title,
......@@ -509,7 +510,7 @@ function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_for
'#weight' => $weight,
'#element_validate' => array('filter_form_validate'),
);
// Multiple formats available: display radio buttons with tips.
foreach ($formats as $format) {
// Generate the parents as the autogenerator does, so we will have a
......@@ -523,7 +524,7 @@ function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_for
'#parents' => $parents,
'#id' => form_clean_id('edit-' . implode('-', $parents_for_id)),
);
if ($show_tips) {
$form[$format->format]['#description'] = theme('filter_tips', _filter_tips($format->format, FALSE));
}
......@@ -533,7 +534,7 @@ function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_for
$form[$format->format]['#description'] = '';
}
}
if ($show_tips_link) {
$extra = theme('better_formats_filter_tips_more_info');
$form[] = array('#value' => $extra);
......@@ -543,23 +544,23 @@ function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_for
// see http://drupal.org/node/344169
$form[] = array('#value' => '');
}
}
}
else {
// Only one format available or hiding the form: use a hidden form item.
$format = $formats[$default];
$form[$format->format] = array(
'#type' => 'value',
'#value' => $format->format,
'#type' => 'value',
'#value' => $format->format,
'#parents' => $parents,
);
if ($show_tips) {
$tips = _filter_tips($format->format, FALSE);
$form['format']['guidelines'] = array(
'#title' => t('Formatting guidelines'),
'#value' => theme('filter_tips', $tips, FALSE),
);
}
}
else {
// Ensure expected filter_form() structure.
// see http://drupal.org/node/344169
......@@ -568,27 +569,27 @@ function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_for
'#value' => '',
);
}
// Only show long tips link if there are guidelines to the format.
if ($show_tips_link) {
$extra = theme('better_formats_filter_tips_more_info');
$form[] = array('#value' => $extra);
}
}
else {
// Ensure expected filter_form() structure.
// see http://drupal.org/node/344169
$form[] = array('#value' => '');
}
}
return $form;
}
/**
* Theme function for fitler tips more info.
*
*
* This is copied from theme_fitler_tips_more_info() with small modifications.
*
*
* @return
* Filter tips more info HTML.
*/
......
......@@ -5,14 +5,14 @@
* @file
* Contains FAPI and theme functions for the format defaults form.
*/
/**
* Builds the form for the filters admin.
*
*
* @return
* FAPI array
*
*
* @see better_formats_defaults_admin_form_validate()
* @see better_formats_defaults_admin_form_submit()
*/
......@@ -20,22 +20,22 @@ function better_formats_defaults_admin_form() {
$form = array(
'#tree' => TRUE,
);
$nform = better_formats_get_role_default_fields('node');
$cform = better_formats_get_role_default_fields('comment');
$form = array_merge($form, $nform, $cform);