Commit 22c7bfbf authored by dragonwize's avatar dragonwize
Browse files

Merging changes from 6.x--1-0 branch back to HEAD.

parent 90c7cb82
<?php
// $Id$
/**
* @file
* Default theme implementation to configure format manager filter admin page
* Default theme implementation to configure format manager filter admin page.
*
* Available variables:
* - $form_submit: Form submit button.
......
; $Id$
name = Better Formats
description = "Enhances the core input format system by managing input format defaults and settings."
core = 6.x
;dependencies[] =
;package =
\ No newline at end of file
......@@ -58,16 +58,16 @@ function better_formats_schema() {
* Implementation of hook_install().
*/
function better_formats_install() {
// create tables
// Create tables.
drupal_install_schema('better_formats');
// increase module weight to prevent compatibility issues
// Increase module weight to prevent compatibility issues.
$sql = "UPDATE {system}
SET weight = 100
WHERE name = 'better_formats'";
db_query($sql);
// insert defaults
// Insert format defaults.
$roles = user_roles();
$sql = "INSERT INTO {better_formats_defaults}
VALUES (%d, '%s', %d, %d, %d)";
......@@ -76,9 +76,9 @@ function better_formats_install() {
db_query($sql, $rid, 'comment', 0, 1, 0);
}
// 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
// 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}
WHERE rid IN (1,2)";
......@@ -87,21 +87,26 @@ function better_formats_install() {
while ($row = db_fetch_object($result)) {
$role_perms[] = $row;
}
// add perms to core roles (anonymous user, authenticated user)
// 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 cached pages
cache_clear_all();
}
/**
* Implementation of hook_uninstall().
*/
function better_formats_uninstall() {
// Remove tables.
drupal_uninstall_schema('better_formats');
// delete settings variables from varible table
// Delete settings from varible table.
$sql = "DELETE FROM {variable}
WHERE name LIKE 'better_formats%'";
db_query($sql);
......
This diff is collapsed.
......@@ -10,7 +10,7 @@
/**
* Builds the form for the filters admin.
*
* @return array
* @return
* FAPI array
*
* @see better_formats_defaults_admin_form_validate()
......@@ -20,7 +20,7 @@ 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);
......@@ -44,12 +44,18 @@ function better_formats_defaults_admin_form_validate($form, &$form_state) {
foreach ($formats as $fid => $format) {
$roles[$fid] = explode(',', $format->roles);
}
// Get roles that have administer filters permission.
$admin_roles = better_formats_get_filter_admin_roles();
foreach ($form_state['values'] as $key => $values) {
if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 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])) {
if ($fid != 0 && !in_array($rid, $roles[$fid])) {
form_set_error($key, t('Role does not have access to selected format.'));
}
}
......@@ -63,7 +69,7 @@ function better_formats_defaults_admin_form_validate($form, &$form_state) {
* @see better_formats_defaults_admin_form_validate()
*/
function better_formats_defaults_admin_form_submit($form, &$form_state) {
// update DB
// Update DB.
$sql = "UPDATE {better_formats_defaults}
SET format=%d, weight=%d
WHERE rid=%d AND type='%s'";
......@@ -78,16 +84,134 @@ function better_formats_defaults_admin_form_submit($form, &$form_state) {
drupal_set_message(t('Defaults have been saved.'));
}
/**
* Builds FAPI form elements for the default format selection.
*
* @param $mode
* 'node' or 'comment'. Top most level type for requested default.
* @param $node_type
* Type of node this request is for.
* @return
* FAPI array for the default select field.
*/
function better_formats_get_role_default_fields($mode, $node_type = '') {
$form = array();
$format_options = better_formats_get_formats_per_role();
$type = $types = $mode;
$per_node_type = variable_get('better_formats_per_node_type', FALSE);
if ($per_node_type && $node_type) {
$type = $mode . '/' . $node_type;
$types = $type . "','" . $mode;
}
// get data from db
$sql = "SELECT bf.*, role.name
FROM {better_formats_defaults} AS bf
INNER JOIN {role} AS role
ON bf.rid = role.rid
WHERE bf.type IN ('$types')
ORDER BY bf.type_weight DESC, bf.weight, role.rid";
$result = db_query($sql);
$roles_set = array();
while ($role = db_fetch_object($result)) {
if (in_array($role->rid, $roles_set)) {
continue;
}
$roles_set[] = $role->rid;
$key = $mode . '-' . $role->rid;
$form[$key]['role'] = array(
'#value' => $role->name,
);
$form[$key]['format'] = array(
'#type' => 'select',
'#options' => $format_options[$role->rid],
'#default_value' => $role->format,
'#attributes' => array('class' => 'bf-default-formats'),
);
$form[$key]['weight'] = array(
'#type' => 'weight',
'#delta' => 25,
'#default_value' => $role->weight,
);
}
return $form;
}
/**
* Retrieve the formats available to users by role.
*
* Gets all formats then creates an array keyed by role IDs
* that lists the formats available to that role. This is determined
* by Drupal core's format permissions set at
* admin/settings/filters/[filter_id].
*
* @param $default_title
* Allows configuration of the label of the default seletion.
* @return
* Multi-dim array with role IDs for keys and list of allowed formats.
*
* @see better_formats_get_role_default_fields()
*/
function better_formats_get_formats_per_role($default_title = 'Site') {
$formats = filter_formats();
$roles = user_roles();
// Get roles that have administer filters permission.
$admin_roles = better_formats_get_filter_admin_roles();
$site_default_format = filter_resolve_format(FILTER_FORMAT_DEFAULT);
foreach ($formats as $format) {
$roles_allowed = $format->roles ? explode(',', trim($format->roles, ',')) : array();
foreach ($roles as $rid => $role) {
$format_options[$rid][0] = $default_title . ' default';
if ($format->format == $site_default_format || in_array($rid, $admin_roles) || in_array($rid, $roles_allowed)) {
$format_options[$rid][$format->format] = $format->name;
}
}
}
return $format_options;
}
/**
* Get a list of roles that have the admin filter perm.
*
* @return
* An numeric indexed key of role IDs.
*/
function better_formats_get_filter_admin_roles() {
static $roles;
if (!$roles) {
$sql = "SELECT rid
FROM {permission}
WHERE perm LIKE '%administer filters%'
ORDER BY rid";
$result = db_query($sql);
$roles = array();
while ($row = db_fetch_object($result)) {
$roles[] = $row->rid;
}
}
return $roles;
}
/**
* Process variables for better-defaults-admin-form.tpl.php.
*
* @param array $variables
* @param $variables
* 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];
$form_row = &$variables['form'][$key];
$type = strpos($key, 'node-') === 0 ? 'node' : 'comment';
......
......@@ -8,15 +8,18 @@
* 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
// 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
// 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
// Add hide/show events for allowed formats.
var format_boxes = $('input.bf-allowed-formats');
format_boxes.click(function() {
better_formats_toggle_formats($(this));
......@@ -28,8 +31,16 @@ function better_formats_init() {
}
}
/**
* 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
// 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();
}
......@@ -44,21 +55,20 @@ function better_formats_toggle_formats(el, init) {
}
});
// do not modify count on intial run
// 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
// Keep num_checked from going below zero.
Drupal.settings.better_formats.num_checked -= 1;
}
}
// show all globally allowed formats if no boxes are checked
// 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
// Show global formats available to roles because no format allowed boxes are checked.
$('select.bf-default-formats option').show();
}
}
......
......@@ -14,18 +14,17 @@
function better_formats_settings_admin_form() {
$form = array();
$key = 'better_formats_settings_display';
$form[$key] = array(
$form['display'] = array(
'#type' => 'fieldset',
'#title' => t('Display'),
);
$form[$key]['better_formats_fieldset_title'] = array(
$form['display']['better_formats_fieldset_title'] = array(
'#type' => 'textfield',
'#default_value' => variable_get('better_formats_fieldset_title', ''),
'#title' => t('Selection title'),
'#description' => t('Change the format selection title. Defaults to "Input format"'),
);
$form[$key]['better_formats_long_tips_link_text'] = array(
$form['display']['better_formats_long_tips_link_text'] = array(
'#type' => 'textfield',
'#default_value' => variable_get('better_formats_long_tips_link_text', ''),
'#title' => t('More format tips link text'),
......@@ -33,31 +32,16 @@ function better_formats_settings_admin_form() {
);
$key = 'better_formats_settings_control';
$form[$key] = array(
$form['control'] = array(
'#type' => 'fieldset',
'#title' => t('Control'),
);
$form[$key]['better_formats_per_node_type'] = array(
$form['control']['better_formats_per_node_type'] = array(
'#type' => 'checkbox',
'#title' => t('Control formats per node type'),
'#description' => t('Control formats allowed and default formats per node type. Global settings will be used until a content type admin page is saved with different values.'),
'#description' => t('Control formats allowed and default formats per node type. Global settings will be used until a content type admin page is saved.'),
'#default_value' => variable_get('better_formats_per_node_type', 0),
);
$key = 'better_formats_settings_advanced';
$form[$key] = array(
'#type' => 'fieldset',
'#title' => t('Advanced'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form[$key]['better_formats_debug'] = array(
'#type' => 'checkbox',
'#title' => t('Debug mode'),
'#description' => t('Turn on debug mode. This will output information to the message area that should be filed with issue reports when asked. Do not leave this turned on.'),
'#default_value' => variable_get('better_formats_debug', 0),
);
return system_settings_form($form);
}
\ No newline at end of file
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment