Commit 24a34382 authored by dragonwize's avatar dragonwize
Browse files

Moved some default related functions from better_formats.module to...

Moved some default related functions from better_formats.module to better_formats_defaults.admin.inc
parent d04e5103
...@@ -171,6 +171,8 @@ function better_formats_node_type_form(&$form, $form_state) { ...@@ -171,6 +171,8 @@ function better_formats_node_type_form(&$form, $form_state) {
'#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');
$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);
...@@ -600,122 +602,3 @@ function theme_better_formats_filter_tips_more_info() { ...@@ -600,122 +602,3 @@ function theme_better_formats_filter_tips_more_info() {
} }
return '<p>' . l($text, 'filter/tips') . '</p>'; return '<p>' . l($text, 'filter/tips') . '</p>';
} }
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
\ No newline at end of file
...@@ -84,6 +84,124 @@ function better_formats_defaults_admin_form_submit($form, &$form_state) { ...@@ -84,6 +84,124 @@ function better_formats_defaults_admin_form_submit($form, &$form_state) {
drupal_set_message(t('Defaults have been saved.')); 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. * Process variables for better-defaults-admin-form.tpl.php.
* *
......
Supports Markdown
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