Commit 3c7c9c96 authored by dragonwize's avatar dragonwize
Browse files

More clean-up.

parent 1256395a
; $Id$ ; $Id$
name = Better Formats 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 core = 6.x
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* @file * @file
* Installs the better_formats module. * 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. * default settings.
*/ */
...@@ -60,27 +60,27 @@ function better_formats_schema() { ...@@ -60,27 +60,27 @@ function better_formats_schema() {
function better_formats_install() { function better_formats_install() {
// Create tables. // Create tables.
drupal_install_schema('better_formats'); drupal_install_schema('better_formats');
// Increase module weight to prevent compatibility issues. // Increase module weight to prevent compatibility issues.
$sql = "UPDATE {system} $sql = "UPDATE {system}
SET weight = 100 SET weight = 100
WHERE name = 'better_formats'"; WHERE name = 'better_formats'";
db_query($sql); db_query($sql);
// Insert format defaults. // Insert format defaults.
$roles = user_roles(); $roles = user_roles();
$sql = "INSERT INTO {better_formats_defaults} $sql = "INSERT INTO {better_formats_defaults}
VALUES (%d, '%s', %d, %d, %d)"; VALUES (%d, '%s', %d, %d, %d)";
foreach ($roles as $rid => $role) { foreach ($roles as $rid => $role) {
db_query($sql, $rid, 'node', 0, 1, 0); db_query($sql, $rid, 'node', 0, 1, 0);
db_query($sql, $rid, 'comment', 0, 1, 0); db_query($sql, $rid, 'comment', 0, 1, 0);
} }
// Set default perms to be like core defaults. // 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, show format tips, show more format tips link, collapsible format selection, collapse format fieldset by default';
// Get current core perms. // Get current core perms.
$sql = "SELECT * $sql = "SELECT *
FROM {permission} FROM {permission}
WHERE rid IN (1,2)"; WHERE rid IN (1,2)";
$result = db_query($sql); $result = db_query($sql);
$row_perms = array(); $row_perms = array();
...@@ -89,12 +89,12 @@ function better_formats_install() { ...@@ -89,12 +89,12 @@ function better_formats_install() {
} }
// Add perms to core roles (anonymous user, authenticated user). // Add perms to core roles (anonymous user, authenticated user).
foreach ($role_perms as $perms) { foreach ($role_perms as $perms) {
$sql = "UPDATE {permission} $sql = "UPDATE {permission}
SET perm = '%s' SET perm = '%s'
WHERE pid = %d"; WHERE pid = %d";
db_query($sql, $perms->perm . $default_perms, $perms->pid); db_query($sql, $perms->perm . $default_perms, $perms->pid);
} }
// Clear the cached pages // Clear the cached pages
cache_clear_all(); cache_clear_all();
} }
...@@ -105,9 +105,9 @@ function better_formats_install() { ...@@ -105,9 +105,9 @@ function better_formats_install() {
function better_formats_uninstall() { function better_formats_uninstall() {
// Remove tables. // Remove tables.
drupal_uninstall_schema('better_formats'); drupal_uninstall_schema('better_formats');
// Delete settings from varible table. // Delete settings from varible table.
$sql = "DELETE FROM {variable} $sql = "DELETE FROM {variable}
WHERE name LIKE 'better_formats%'"; WHERE name LIKE 'better_formats%'";
db_query($sql); db_query($sql);
} }
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* @file * @file
* Enhances Drupal's core input format settings. * 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. * controls format display options, works with CCK fields.
*/ */
...@@ -17,13 +17,13 @@ function better_formats_help($path, $arg) { ...@@ -17,13 +17,13 @@ function better_formats_help($path, $arg) {
case 'admin/help/better_formats': case 'admin/help/better_formats':
$output = '<p>' . t('See the module README.txt file in the better_formats module directory for help.') . '</p>'; $output = '<p>' . t('See the module README.txt file in the better_formats module directory for help.') . '</p>';
break; break;
case 'admin/settings/filters/defaults': 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('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>'; $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; break;
default: default:
$output = ''; $output = '';
} }
return $output; return $output;
...@@ -47,7 +47,7 @@ function better_formats_perm() { ...@@ -47,7 +47,7 @@ function better_formats_perm() {
*/ */
function better_formats_menu() { function better_formats_menu() {
$items = array(); $items = array();
$items['admin/settings/filters/settings'] = array( $items['admin/settings/filters/settings'] = array(
'title' => 'Settings', 'title' => 'Settings',
'description' => 'Manage input formats', 'description' => 'Manage input formats',
...@@ -68,7 +68,7 @@ function better_formats_menu() { ...@@ -68,7 +68,7 @@ function better_formats_menu() {
'weight' => 2, 'weight' => 2,
'file' => 'better_formats_defaults.admin.inc', 'file' => 'better_formats_defaults.admin.inc',
); );
return $items; return $items;
} }
...@@ -92,20 +92,20 @@ function better_formats_theme() { ...@@ -92,20 +92,20 @@ function better_formats_theme() {
* Implementation of hook_form_alter(). * Implementation of hook_form_alter().
*/ */
function better_formats_form_alter(&$form, $form_state, $form_id) { function better_formats_form_alter(&$form, $form_state, $form_id) {
// Alter new node and comment forms. // Alter new node and comment forms.
// Using $form['#id'] instead of $form_id because $form_id is in the form of // 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'] // 'TYPE_node_form' which varies with the content type while $form['#id']
// is always 'node-form'. // is always 'node-form'.
switch ($form['#id']) { switch ($form['#id']) {
case 'comment-form': case 'comment-form':
better_formats_set_comment_format($form); better_formats_set_comment_format($form);
break; break;
case 'node-form': case 'node-form':
better_formats_set_node_format($form); better_formats_set_node_format($form);
break; break;
} }
// Alter role add/delete and node type forms. // Alter role add/delete and node type forms.
switch ($form_id) { switch ($form_id) {
case 'node_type_form': case 'node_type_form':
...@@ -113,13 +113,13 @@ function better_formats_form_alter(&$form, $form_state, $form_id) { ...@@ -113,13 +113,13 @@ function better_formats_form_alter(&$form, $form_state, $form_id) {
better_formats_node_type_form($form, $form_state); better_formats_node_type_form($form, $form_state);
} }
break; break;
case 'user_admin_new_role': case 'user_admin_new_role':
if (!in_array('better_formats_new_role', $form['#submit'])) { if (!in_array('better_formats_new_role', $form['#submit'])) {
$form['#submit'][] = 'better_formats_new_role'; $form['#submit'][] = 'better_formats_new_role';
} }
break; break;
case 'user_admin_role': case 'user_admin_role':
if (isset($form_state['post']['op']) && $form_state['post']['op'] == 'Delete role') { if (isset($form_state['post']['op']) && $form_state['post']['op'] == 'Delete role') {
$form['#submit'][] = 'better_formats_delete_role'; $form['#submit'][] = 'better_formats_delete_role';
...@@ -130,22 +130,22 @@ function better_formats_form_alter(&$form, $form_state, $form_id) { ...@@ -130,22 +130,22 @@ function better_formats_form_alter(&$form, $form_state, $form_id) {
/** /**
* FAPI form to add to the content type edit form. * FAPI form to add to the content type edit form.
* *
* @see better_formats_node_type_form_validate() * @see better_formats_node_type_form_validate()
* @see better_formats_node_type_form_submit() * @see better_formats_node_type_form_submit()
*/ */
function better_formats_node_type_form(&$form, $form_state) { function better_formats_node_type_form(&$form, $form_state) {
// Add JS to enhance form. // Add JS to enhance form.
drupal_add_js(drupal_get_path('module', 'better_formats') . '/better_formats_node_type_form.js'); drupal_add_js(drupal_get_path('module', 'better_formats') . '/better_formats_node_type_form.js');
$node_type = $form['#node_type']->type; $node_type = $form['#node_type']->type;
// Build array of all formats for allowed checkboxes. // Build array of all formats for allowed checkboxes.
$formats = filter_formats(); $formats = filter_formats();
foreach ($formats as $format) { foreach ($formats as $format) {
$format_boxes[$format->format] = $format->name; $format_boxes[$format->format] = $format->name;
} }
$key = 'better_formats'; $key = 'better_formats';
$form[$key] = array( $form[$key] = array(
'#type' => 'fieldset', '#type' => 'fieldset',
...@@ -166,18 +166,18 @@ function better_formats_node_type_form(&$form, $form_state) { ...@@ -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.'), '#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'), '#attributes' => array('class' => 'bf-allowed-formats'),
); );
$dform = array( $dform = array(
'#tree' => TRUE, '#tree' => TRUE,
'#theme' => 'better_formats_defaults_admin_form', '#theme' => 'better_formats_defaults_admin_form',
); );
module_load_include('admin.inc', 'better_formats', 'better_formats_defaults'); module_load_include('admin.inc', 'better_formats', 'better_formats_defaults');
$nform = better_formats_get_role_default_fields('node', $node_type); $nform = better_formats_get_role_default_fields('node', $node_type);
$cform = better_formats_get_role_default_fields('comment', $node_type); $cform = better_formats_get_role_default_fields('comment', $node_type);
$form[$key]['better_formats_defaults'] = array_merge($dform, $nform, $cform); $form[$key]['better_formats_defaults'] = array_merge($dform, $nform, $cform);
// Attach our validate and submit handlers. // Attach our validate and submit handlers.
$form['#validate'][] = 'better_formats_node_type_form_validate'; $form['#validate'][] = 'better_formats_node_type_form_validate';
$form['#submit'][] = 'better_formats_node_type_form_submit'; $form['#submit'][] = 'better_formats_node_type_form_submit';
...@@ -185,7 +185,7 @@ function better_formats_node_type_form(&$form, $form_state) { ...@@ -185,7 +185,7 @@ function better_formats_node_type_form(&$form, $form_state) {
/** /**
* Handles validation of the addition to the content type edit form. * Handles validation of the addition to the content type edit form.
* *
* @see better_formats_node_type_form() * @see better_formats_node_type_form()
* @see better_formats_node_type_form_submit() * @see better_formats_node_type_form_submit()
*/ */
...@@ -196,20 +196,20 @@ function better_formats_node_type_form_validate($form, &$form_state) { ...@@ -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. * Handles submission of the addition to the content type edit form.
* *
* @see better_formats_node_type_form() * @see better_formats_node_type_form()
* @see better_formats_node_type_form_validate() * @see better_formats_node_type_form_validate()
*/ */
function better_formats_node_type_form_submit($form, &$form_state) { function better_formats_node_type_form_submit($form, &$form_state) {
$node_type = trim($form_state['values']['type']); $node_type = trim($form_state['values']['type']);
// Remove current db entries. // Remove current db entries.
$sql = "DELETE FROM {better_formats_defaults} $sql = "DELETE FROM {better_formats_defaults}
WHERE type='comment/%s' OR type='node/%s'"; WHERE type='comment/%s' OR type='node/%s'";
db_query($sql, $node_type, $node_type); db_query($sql, $node_type, $node_type);
// Insert defualt values into DB. // Insert defualt values into DB.
$sql = "INSERT INTO {better_formats_defaults} $sql = "INSERT INTO {better_formats_defaults}
VALUES (%d, '%s', %d, %d, %d)"; VALUES (%d, '%s', %d, %d, %d)";
foreach ($form_state['values']['better_formats_defaults'] as $key => $values) { foreach ($form_state['values']['better_formats_defaults'] as $key => $values) {
if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0) { if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0) {
...@@ -225,19 +225,19 @@ function better_formats_node_type_form_submit($form, &$form_state) { ...@@ -225,19 +225,19 @@ function better_formats_node_type_form_submit($form, &$form_state) {
/** /**
* Creates base format default entry for a newly created role. * Creates base format default entry for a newly created role.
* *
* @see better_formats_form_alter() * @see better_formats_form_alter()
*/ */
function better_formats_new_role($form, &$form_state) { function better_formats_new_role($form, &$form_state) {
// Get the ID for the role just created. // Get the ID for the role just created.
$sql = "SELECT rid $sql = "SELECT rid
FROM {role} FROM {role}
ORDER BY rid DESC"; ORDER BY rid DESC";
$row = db_fetch_object(db_query_range($sql, 0, 1)); $row = db_fetch_object(db_query_range($sql, 0, 1));
$rid = $row->rid; $rid = $row->rid;
// Create stubs in per role table. // Create stubs in per role table.
$sql = "INSERT INTO {better_formats_defaults} $sql = "INSERT INTO {better_formats_defaults}
VALUES (%d, '%s', %d, %d, %d)"; VALUES (%d, '%s', %d, %d, %d)";
db_query($sql, $rid, 'node', 0, 1, 25); db_query($sql, $rid, 'node', 0, 1, 25);
db_query($sql, $rid, 'comment', 0, 1, 25); db_query($sql, $rid, 'comment', 0, 1, 25);
...@@ -245,12 +245,12 @@ function better_formats_new_role($form, &$form_state) { ...@@ -245,12 +245,12 @@ function better_formats_new_role($form, &$form_state) {
/** /**
* Deletes role format default entries for roles being deleted. * Deletes role format default entries for roles being deleted.
* *
* @see better_formats_form_alter() * @see better_formats_form_alter()
*/ */
function better_formats_delete_role($form, &$form_state) { function better_formats_delete_role($form, &$form_state) {
// Delete role from format manager table. // Delete role from format manager table.
$sql = "DELETE FROM {better_formats_defaults} $sql = "DELETE FROM {better_formats_defaults}
WHERE rid = %d"; WHERE rid = %d";
db_query($sql, $form['rid']['#value']); db_query($sql, $form['rid']['#value']);
} }
...@@ -261,10 +261,10 @@ function better_formats_delete_role($form, &$form_state) { ...@@ -261,10 +261,10 @@ function better_formats_delete_role($form, &$form_state) {
function better_formats_node_type($op, $info) { function better_formats_node_type($op, $info) {
if ($op === 'delete') { if ($op === 'delete') {
// Delete per node type settings on node type 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')"; WHERE type IN ('node/%s', 'comment/%s')";
db_query($sql, $info->type, $info->type); db_query($sql, $info->type, $info->type);
// Delete node type variables. // Delete node type variables.
variable_del('better_formats_allowed_' . $info->type); variable_del('better_formats_allowed_' . $info->type);
} }
...@@ -272,7 +272,7 @@ function better_formats_node_type($op, $info) { ...@@ -272,7 +272,7 @@ function better_formats_node_type($op, $info) {
/** /**
* Implementation of hook_elements(). * Implementation of hook_elements().
* *
* Adds a process function to CCK's textarea FAPI element. * Adds a process function to CCK's textarea FAPI element.
*/ */
function better_formats_elements() { function better_formats_elements() {
...@@ -285,39 +285,39 @@ function better_formats_elements() { ...@@ -285,39 +285,39 @@ function better_formats_elements() {
/** /**
* Processes a CCK textarea element. * Processes a CCK textarea element.
* *
* Resets the textareas filter area with bettter_formats default. * Resets the textareas filter area with bettter_formats default.
* This function is used to affect CCK textareas not core fields. * This function is used to affect CCK textareas not core fields.
* *
* @see text_textarea_process() * @see text_textarea_process()
*/ */
function better_formats_textarea_process($element, $edit, $form_state, $form) { function better_formats_textarea_process($element, $edit, $form_state, $form) {
$field = $form['#field_info'][$element['#field_name']]; $field = $form['#field_info'][$element['#field_name']];
if (!empty($field['text_processing'])) { if (!empty($field['text_processing'])) {
// Get core default for new or selected format for existing. // Get core default for new or selected format for existing.
$filter_key = (count($element['#columns']) == 2) ? $element['#columns'][1] : 'format'; $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] : FILTER_FORMAT_DEFAULT;
$parents = array_merge($element['#parents'] , array($filter_key)); $parents = array_merge($element['#parents'] , array($filter_key));
$default = better_formats_get_default_format('node', $form['type']['#value']); $default = better_formats_get_default_format('node', $form['type']['#value']);
// Overwrite format default if new node. // Overwrite format default if new node.
if (!isset($form_state['values']['nid'])) { if (!isset($form_state['values']['nid'])) {
$format = $default; $format = $default;
} }
// Set default format for cck textarea. // Set default format for cck textarea.
$element['#value'][$filter_key] = $format; $element['#value'][$filter_key] = $format;
// Set filter selection form. // 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, $form['type']['#value'], 1, $parents);
} }
return $element; return $element;
} }
/** /**
* Processes formats for core node body fields. * Processes formats for core node body fields.
* *
* @see better_formats_form_alter() * @see better_formats_form_alter()
*/ */
function better_formats_set_node_format(&$form) { function better_formats_set_node_format(&$form) {
...@@ -325,16 +325,16 @@ function better_formats_set_node_format(&$form) { ...@@ -325,16 +325,16 @@ function better_formats_set_node_format(&$form) {
if (isset($form['body_field'])) { if (isset($form['body_field'])) {
// Get default for new entries. // Get default for new entries.
$default = better_formats_get_default_format('node', $form['type']['#value']); $default = better_formats_get_default_format('node', $form['type']['#value']);
if (empty($form['nid']['#value'])) { if (empty($form['nid']['#value'])) {
// Set format to default for new entries. // Set format to default for new entries.
$format = $default; $format = $default;
} }
else { else {
// Get existing format for core body field. // Get existing format for core body field.
$format = better_formats_get_current_format($form['body_field']['format']); $format = better_formats_get_current_format($form['body_field']['format']);
} }
// Overwrite the filter form with our own. // 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, $form['type']['#value']);
} }
...@@ -342,20 +342,20 @@ function better_formats_set_node_format(&$form) { ...@@ -342,20 +342,20 @@ function better_formats_set_node_format(&$form) {
/** /**
* Processes formats for core node comment form. * Processes formats for core node comment form.
* *
* @see better_formats_form_alter() * @see better_formats_form_alter()
*/ */
function better_formats_set_comment_format(&$form) { function better_formats_set_comment_format(&$form) {
if (isset($form['comment_filter']['format'])) { if (isset($form['comment_filter']['format'])) {
$node = node_load($form['nid']['#value']); $node = node_load($form['nid']['#value']);
// Get BF default format. // Get BF default format.
$default = better_formats_get_default_format('comment', $node->type); $default = better_formats_get_default_format('comment', $node->type);
if (empty($form['cid']['#value'])) { if (empty($form['cid']['#value'])) {
// Set format to default for new entries. // Set format to default for new entries.
$format = $default; $format = $default;
} }
else { else {
// Get existing format for comment. // Get existing format for comment.
$format = better_formats_get_current_format($form['comment_filter']['format']); $format = better_formats_get_current_format($form['comment_filter']['format']);
...@@ -367,12 +367,12 @@ function better_formats_set_comment_format(&$form) { ...@@ -367,12 +367,12 @@ function better_formats_set_comment_format(&$form) {
/** /**
* Returns the format for an existing node or comment. * Returns the format for an existing node or comment.
* *
* @param $form * @param $form
* FAPI form array. * FAPI form array.
* @return * @return
* Format ID. * Format ID.
* *
* @see better_formats_set_node_format() * @see better_formats_set_node_format()
* @see better_formats_set_comment_format() * @see better_formats_set_comment_format()
*/ */
...@@ -395,22 +395,22 @@ function better_formats_get_current_format($form) { ...@@ -395,22 +395,22 @@ function better_formats_get_current_format($form) {
/** /**
* Returns the default format for an new node or comment. * Returns the default format for an new node or comment.
* *
* @param $mode * @param $mode
* 'node' or 'comment'. Describes the top level type of default. * 'node' or 'comment'. Describes the top level type of default.
* @return * @return
* Format ID. * Format ID.
* *
* @see better_formats_set_node_format() * @see better_formats_set_node_format()
* @see better_formats_set_comment_format() * @see better_formats_set_comment_format()
* @see better_formats_textarea_process() * @see better_formats_textarea_process()
*/ */
function better_formats_get_default_format($mode, $node_type = '') {