Commit 0d7d56c8 authored by dragonwize's avatar dragonwize
Browse files

Updating HEAD with 1.2 version of BF.

parent f8788c43
......@@ -9,9 +9,10 @@ Features
* Expand the selection fieldset by default.
* Disable the expand/collapse of the selection fieldset.
* Set selection fieldset title.
* Set default formats for nodes and comments separately.
* Set default formats for nodes, comments, and blocks separately.
* Works with CCK textareas.
* Panels comment support
* Panels comment support.
* I18n module support.
* and more.
-------------------------------------------------------------------
......@@ -36,6 +37,27 @@ Simple 4-step usage:
-------------------------------------------------------------------
Important:
When setting default formats ensure that you arranged the roles correctly
placing roles in their order of precedence. This is used to determine what
default a user will get when they have more than 1 role.
NOTE:
All logged in users are automatically assigned the authenticated user role
so this role must be below all other roles that you want to set a default for or
they will get the authenticated user role default instead.
Example:
Let's say you have the 2 roles that come with Drupal and have added an
'admin' role. You would most likely want to arrange the roles in this order:
admin
authenticated user
anonymous user
-------------------------------------------------------------------
Extended usage and notes:
* The default format will only be set on NEW nodes and comments. The format
......@@ -63,3 +85,11 @@ Extended usage and notes:
3. Global default format
4. First allowed format
5. Drupal core site default format
* User 1 is treated the same as all other users when it comes to a default
format. If user 1 has not been assigned any roles then it will be assigned
the authenticated user role's default format. If you want user 1 to have the
default of another role assign that role to user 1.
* Ensure you read the important notes in the previous section marked important.
It explains how you must order your roles to effectively get your defaults.
......@@ -3,7 +3,7 @@
/**
* @file
* Default theme implementation to configure format manager filter admin page.
* Default theme implementation to configure Better Formats defaults admin page.
*
* Available variables:
* - $form_submit: Form submit button.
......@@ -20,15 +20,19 @@
// Add table javascript
drupal_add_tabledrag('node-format-defaults', 'order', 'sibling', 'better-formats-role-node-weight');
drupal_add_tabledrag('comment-format-defaults', 'order', 'sibling', 'better-formats-role-comment-weight');
drupal_add_tabledrag('block-format-defaults', 'order', 'sibling', 'better-formats-role-block-weight');
?>
<div class="description"><?php print t('Only formats that a role has permission to use are shown.'); ?></div>
<div class="description">
<?php print '<p><strong>' . t('Defaults only affect NEW content NOT existing content.') . '</strong></p>'; ?>
<?php print '<p><strong>' . t('Place roles in order of precedence by dragging more important roles to the top.') . '</strong></p>'; ?>
</div>
<fieldset>
<legend><strong><?php print t('Node Defaults'); ?></strong></legend>
<legend><strong><?php print t('Node defaults'); ?></strong></legend>
<table id="node-format-defaults">
<thead>
<tr>
<th><?php print t('Role'); ?></th>
<th><?php print t('Default Format'); ?></th>
<th><?php print t('Default format'); ?></th>
<th><?php print t('Weight'); ?></th>
</tr>
</thead>
......@@ -47,12 +51,12 @@
</fieldset>
<fieldset>
<legend><strong><?php print t('Comment Defaults'); ?></strong></legend>
<legend><strong><?php print t('Comment defaults'); ?></strong></legend>
<table id="comment-format-defaults">
<thead>
<tr>
<th><?php print t('Role'); ?></th>
<th><?php print t('Default Format'); ?></th>
<th><?php print t('Default format'); ?></th>
<th><?php print t('Weight'); ?></th>
</tr>
</thead>
......@@ -70,4 +74,37 @@
</table>
</fieldset>
<?php if ($_GET['q'] === 'admin/settings/filters/defaults'): ?>
<fieldset>
<legend><strong><?php print t('Block defaults'); ?></strong></legend>
<?php if (isset($block_default_rows)): ?>
<table id="block-format-defaults">
<thead>
<tr>
<th><?php print t('Role'); ?></th>
<th><?php print t('Default format'); ?></th>
<th><?php print t('Weight'); ?></th>
</tr>
</thead>
<tbody>
<?php $row = 0; ?>
<?php foreach ($block_default_rows as $rid => $data): ?>
<tr class="draggable <?php print $row % 2 ? 'odd' : 'even'; ?>">
<td class=""><?php print $data->role; ?></td>
<td><?php print $data->format_select; ?></td>
<td><?php print $data->weight_select; ?></td>
</tr>
<?php $row++; ?>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</fieldset>
<?php endif; ?>
<div class="description">
<?php print '<p>' . t('* Only formats that a role has permission to use are shown in the default format drop downs.') . '</p>'; ?>
</div>
<?php print $form_submit; ?>
......@@ -74,29 +74,33 @@ function better_formats_install() {
foreach ($roles as $rid => $role) {
db_query($sql, $rid, 'node', 0, 1, 0);
db_query($sql, $rid, 'comment', 0, 1, 0);
db_query($sql, $rid, 'block', 0, 1, 25);
}
// 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';
$default_perms = ', show format selection for nodes, show format selection for comments, show format selection for blocks, show format tips, show more format tips link, collapsible format selection, collapse format fieldset by default';
// Get current core perms.
$sql = "SELECT *
FROM {permission}
WHERE rid IN (1,2)";
$result = db_query($sql);
$row_perms = array();
$role_perms = array();
while ($row = db_fetch_object($result)) {
$role_perms[] = $row;
$role_perms[$row->rid] = $row;
}
// Add perms to core roles (anonymous user, authenticated user).
foreach ($role_perms as $perms) {
$sql = "UPDATE {permission}
SET perm = '%s'
WHERE pid = %d";
db_query($sql, $perms->perm . $default_perms, $perms->pid);
// If a role has no permissions set it will not have a row in the database.
// Assume that roles do not need perms set for BF as well if none are set.
if (!empty($role_perms)) {
// Add perms to core roles (anonymous user, authenticated user).
foreach ($role_perms as $perms) {
$sql = "UPDATE {permission}
SET perm = '%s'
WHERE pid = %d";
db_query($sql, $perms->perm . $default_perms, $perms->pid);
}
// Clear the cache.
cache_clear_all();
}
// Clear the cached pages
cache_clear_all();
}
/**
......@@ -111,3 +115,46 @@ function better_formats_uninstall() {
WHERE name LIKE 'better_formats%'";
db_query($sql);
}
/**
* Update from 1.0 to 1.1.
*/
function better_formats_update_6110() {
$ret = array();
// Insert block format defaults.
$roles = user_roles();
$sql = "INSERT INTO {better_formats_defaults}
VALUES (%d, '%s', %d, %d, %d)";
foreach ($roles as $rid => $role) {
$result = db_query($sql, $rid, 'block', 0, 1, 25);
$ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql));
}
// Split show format selection permission.
// Get permissions by role.
$sql = "SELECT *
FROM {permission}";
$result = db_query($sql);
$row_perms = array();
while ($row = db_fetch_object($result)) {
$role_perms[$row->rid] = $row;
}
if (!empty($role_perms)) {
// Add perms to core roles (anonymous user, authenticated user).
foreach ($role_perms as $perms) {
// Only enable spit permissions if previous was enabled.
$replace = ', show format selection for nodes, show format selection for comments, show format selection for blocks,';
$perms->perm = str_replace(', show format selection,', $replace, $perms->perm);
$sql = "UPDATE {permission}
SET perm = '%s'
WHERE pid = %d";
$result = db_query($sql, $perms->perm, $perms->pid);
$ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql));
}
// Clear the cache.
cache_clear_all();
}
return $ret;
}
......@@ -18,11 +18,6 @@ function better_formats_help($path, $arg) {
$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:
$output = '';
}
......@@ -34,7 +29,9 @@ function better_formats_help($path, $arg) {
*/
function better_formats_perm() {
return array(
'show format selection',
'show format selection for nodes',
'show format selection for comments',
'show format selection for blocks',
'show format tips',
'show more format tips link',
'collapse format fieldset by default',
......@@ -105,6 +102,11 @@ function better_formats_form_alter(&$form, $form_state, $form_id) {
case 'node-form':
better_formats_set_node_format($form);
break;
case 'block-add-block-form':
case 'block-admin-configure':
better_formats_set_block_format($form);
break;
}
// Alter role add/delete and node type forms.
......@@ -242,6 +244,7 @@ function better_formats_new_role($form, &$form_state) {
VALUES (%d, '%s', %d, %d, %d)";
db_query($sql, $rid, 'node', 0, 1, 25);
db_query($sql, $rid, 'comment', 0, 1, 25);
db_query($sql, $rid, 'block', 0, 1, 25);
}
/**
......@@ -274,25 +277,29 @@ function better_formats_node_type($op, $info) {
/**
* Implementation of hook_elements().
*
* Adds a process function to CCK's textarea FAPI element.
* Adds a process function to CCK's text module FAPI elements.
*/
function better_formats_elements() {
return array(
'text_textfield' => array(
'#process' => array('better_formats_text_process'),
),
'text_textarea' => array(
'#process' => array('better_formats_textarea_process'),
'#process' => array('better_formats_text_process'),
),
);
}
/**
* Processes a CCK textarea element.
* Processes a CCK text elements.
*
* Resets the textareas filter area with bettter_formats default.
* This function is used to affect CCK textareas not core fields.
* Resets the filter area with bettter_formats default.
* This function is used to affect CCK text module fields not core fields.
*
* @see text_textfield_process()
* @see text_textarea_process()
*/
function better_formats_textarea_process($element, $edit, $form_state, $form) {
function better_formats_text_process($element, $edit, $form_state, $form) {
$field = $form['#field_info'][$element['#field_name']];
if (!empty($field['text_processing'])) {
......@@ -302,15 +309,18 @@ function better_formats_textarea_process($element, $edit, $form_state, $form) {
$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']) || !isset($format)) {
// Overwrite format default if new node unless the field has a default text
// or is being previewed.
dsm($element);
dsm($format);
if ((!isset($form_state['values']['nid']) && !isset($element['#default_value']['value'])) || empty($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);
$element[$filter_key] = better_formats_filter_form($format, $default, 'node', $form['type']['#value'], 1, $parents);
}
return $element;
......@@ -327,7 +337,7 @@ function better_formats_set_node_format(&$form) {
// Get default for new entries.
$default = better_formats_get_default_format('node', $form['type']['#value']);
if (empty($form['nid']['#value'])) {
if (empty($form['nid']['#value']) && !isset($form['#parameters'][1]['node_preview'])) {
// Set format to default for new entries.
$format = $default;
}
......@@ -337,7 +347,7 @@ function better_formats_set_node_format(&$form) {
}
// Overwrite the filter form with our own.
$form['body_field']['format'] = better_formats_filter_form($format, $default, $form['type']['#value']);
$form['body_field']['format'] = better_formats_filter_form($format, $default, 'node', $form['type']['#value']);
}
}
......@@ -362,7 +372,31 @@ function better_formats_set_comment_format(&$form) {
$format = better_formats_get_current_format($form['comment_filter']['format']);
}
// Overwrite the filter form with our own.
$form['comment_filter']['format'] = better_formats_filter_form($format, $default, $node->type);
$form['comment_filter']['format'] = better_formats_filter_form($format, $default, 'comment', $node->type);
}
}
/**
* Processes formats for core block form.
*
* @see better_formats_form_alter()
*/
function better_formats_set_block_format(&$form) {
if (isset($form['block_settings']['body_field']['format'])) {
// Get BF default format.
$default = better_formats_get_default_format('block');
if (empty($form['delta']['#value'])) {
// Set format to default for new entries.
$format = $default;
}
else {
// Get existing format for block.
$format = better_formats_get_current_format($form['block_settings']['body_field']['format']);
}
// Overwrite the filter form with our own.
$form['block_settings']['body_field']['format'] = better_formats_filter_form($format, $default, 'block');
}
}
......@@ -398,7 +432,7 @@ 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.
* 'node', 'comment', or 'block'. Describes the top level type of default.
* @return
* Format ID.
*
......@@ -427,7 +461,7 @@ function better_formats_get_default_format($mode, $node_type = '') {
$roles = implode(',', array_keys($user->roles));
// Prepare types for SQL.
if ($per_node_type && $node_type) {
if ($mode !== 'block' && $per_node_type && $node_type) {
$types .= "','" . $mode;
}
......@@ -454,10 +488,10 @@ function better_formats_get_default_format($mode, $node_type = '') {
*
* @see filter_form()
*/
function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_format, $node_type = '', $weight = 1, $parents = array('format')) {
function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_format, $mode = 'node', $node_type = '', $weight = 1, $parents = array('format')) {
$value = filter_resolve_format($value);
$formats = filter_formats();
$show_selection = user_access('show format selection');
$show_selection = user_access('show format selection for ' . $mode . 's');
$show_tips = user_access('show format tips');
$show_tips_link = user_access('show more format tips link');
$per_node_type = variable_get('better_formats_per_node_type', FALSE);
......@@ -481,7 +515,8 @@ function better_formats_filter_form($value = FILTER_FORMAT_DEFAULT, $default_for
else if (isset($formats[$default_format])) {
// Use currently set BF default as a fallback.
$default = $default_format;
} else if (!empty($formats)) {
}
else if (!empty($formats)) {
// Current and default format are not allowed, so use first allowed format.
reset($formats);
$default = key($formats);
......
......@@ -23,7 +23,8 @@ function better_formats_defaults_admin_form() {
$nform = better_formats_get_role_default_fields('node');
$cform = better_formats_get_role_default_fields('comment');
$form = array_merge($form, $nform, $cform);
$bform = better_formats_get_role_default_fields('block');
$form = array_merge($form, $nform, $cform, $bform);
$form['submit'] = array(
'#type' => 'submit',
......@@ -45,17 +46,18 @@ function better_formats_defaults_admin_form_validate($form, &$form_state) {
$roles[$fid] = explode(',', $format->roles);
}
// Get roles that have administer filters permission.
$admin_roles = better_formats_get_filter_admin_roles();
$admin_roles = better_formats_get_roles_by_perm('administer filters');
foreach ($form_state['values'] as $key => $values) {
if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0) {
if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0 || strpos($key, 'block-') === 0) {
list($type, $rid) = explode('-', $key);
if (in_array($rid, $admin_roles)) {
// Role has the 'administer filters' permission so it can use all formats.
continue;
}
$fid = $values['format'];
if ($fid != 0 && !in_array($rid, $roles[$fid])) {
$site_default = filter_resolve_format(FILTER_FORMAT_DEFAULT);
if ($fid != 0 && !in_array($rid, $roles[$fid]) && $fid !== $site_default) {
form_set_error($key, t('Role does not have access to selected format.'));
}
}
......@@ -75,7 +77,7 @@ function better_formats_defaults_admin_form_submit($form, &$form_state) {
WHERE rid=%d AND type='%s'";
foreach ($form_state['values'] as $key => $values) {
if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0) {
if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0 || strpos($key, 'block-') === 0) {
list($type, $rid) = explode('-', $key);
db_query($sql, $values['format'], $values['weight'], $rid, $type);
}
......@@ -88,7 +90,7 @@ function better_formats_defaults_admin_form_submit($form, &$form_state) {
* Builds FAPI form elements for the default format selection.
*
* @param $mode
* 'node' or 'comment'. Top most level type for requested default.
* 'node', 'comment', or 'block'. Top most level type for requested default.
* @param $node_type
* Type of node this request is for.
* @return
......@@ -161,7 +163,7 @@ function better_formats_get_formats_per_role() {
$roles = user_roles();
// Get roles that have administer filters permission.
$admin_roles = better_formats_get_filter_admin_roles();
$admin_roles = better_formats_get_roles_by_perm('administer filters');
$site_default_format = filter_resolve_format(FILTER_FORMAT_DEFAULT);
......@@ -179,39 +181,44 @@ function better_formats_get_formats_per_role() {
}
/**
* Get a list of roles that have the admin filter perm.
* Get a list of roles that have a permission.
*
* @param $perm
* Permission string to get roles for.
* @param $reset
* Boolean to clear static cache.
* @return
* An numeric indexed key of role IDs.
* An array of role IDs that have the requested permission.
*/
function better_formats_get_filter_admin_roles() {
function better_formats_get_roles_by_perm($perm, $reset = FALSE) {
static $roles;
if (!$roles) {
if ($reset || !isset($roles[$perm])) {
$sql = "SELECT rid
FROM {permission}
WHERE perm LIKE '%administer filters%'
WHERE perm LIKE '%$perm%'
ORDER BY rid";
$result = db_query($sql);
$roles = array();
$roles[$perm] = array();
while ($row = db_fetch_object($result)) {
$roles[] = $row->rid;
$roles[$perm][] = $row->rid;
}
}
return $roles;
return $roles[$perm];
}
/**
* Process variables for better-defaults-admin-form.tpl.php.
*
* @param $variables
* @param $vars
* The $variables array contains the following arguments:
* - $form
*/
function template_preprocess_better_formats_defaults_admin_form(&$variables) {
foreach (element_children($variables['form']) as $key) {
$form_row = &$variables['form'][$key];
function template_preprocess_better_formats_defaults_admin_form(&$vars) {
foreach (element_children($vars['form']) as $key) {
$form_row = &$vars['form'][$key];
$type = strpos($key, 'node-') === 0 ? 'node' : 'comment';
//$type = strpos($key, 'node-') === 0 ? 'node' : 'comment';
$type = substr($key, 0, strpos($key, '-'));
if (isset($form_row['role'])) {
// Set special classes needed for table drag and drop.
......@@ -222,9 +229,9 @@ function template_preprocess_better_formats_defaults_admin_form(&$variables) {
$row->format_select = drupal_render($form_row['format']);
$row->weight_select = drupal_render($form_row['weight']);
$variables[$type . '_default_rows'][$key] = $row;
$vars[$type . '_default_rows'][$key] = $row;
}
}
$variables['form_submit'] = drupal_render($variables['form']);
$vars['form_submit'] = drupal_render($vars['form']);
}
// $Id$
/**
* @file
* Enhances the default format selection on content type edit forms.
*
* Fixes bug that shows weight field when drag and drop is enabled
* because the field is hidden by default.
* Also hides formats that are not available per the Allowed checkboxes.
*/
/**
* Initialize Better Formats setttings and defaults.
*/
function better_formats_init() {
// Set default settings check for use of global allowed formats.
Drupal.settings.better_formats = {"num_checked" : $('input.bf-allowed-formats:checked').length};
// Collapsing the input format setting after the weight columns have been hidden.
$('.input-format-settings > legend > a').click();
// Add hide/show events for allowed formats.
var format_boxes = $('input.bf-allowed-formats');
format_boxes.click(function() {
better_formats_toggle_formats($(this));
});
if (Drupal.settings.better_formats.num_checked > 0) {
format_boxes.each(function() {
better_formats_toggle_formats($(this), true);
});
}
}
/**
* Toggle format display in dropdowns in sync with allowed checkboxes.
*
* @param el
* DOM element of event.
* @param init
* Boolean value to determine first toggle.
*/
function better_formats_toggle_formats(el, init) {
// Hide all formats except site default when the first box is checked.
if (Drupal.settings.better_formats.num_checked === 0) {
$('select.bf-default-formats option[value != "0"][value != "' + el.val() + '"]').removeAttr('selected').hide();
}
$('select.bf-default-formats option[value = "' + el.val() + '"]').each(function() {
var option = $(this);
if (el.attr('checked')) {
option.show();
}
else {
option.removeAttr('selected').hide();
}
});
// Do not modify count on intial run.
if ( ! init) {
if (el.attr('checked')) {
Drupal.settings.better_formats.num_checked += 1;
}
else if (Drupal.settings.better_formats.num_checked > 0) {
// Keep num_checked from going below zero.
Drupal.settings.better_formats.num_checked -= 1;
}
}
// Show all globally allowed formats if no boxes are checked.
if (Drupal.settings.better_formats.num_checked === 0) {
// Show global formats available to roles because no format allowed boxes are checked.
$('select.bf-default-formats option').show();
}
}
$(document).ready(better_formats_init);
\ No newline at end of file