| 1 |
<?php
|
| 2 |
|
| 3 |
/**
|
| 4 |
* Implementation of hook_init
|
| 5 |
*/
|
| 6 |
function input_format_permissions_init() {
|
| 7 |
// If this is the page where permissions are set, add some javascript
|
| 8 |
if (drupal_match_path($_GET['q'], 'admin/user/permissions')) {
|
| 9 |
$formats = input_format_permissions_filter_load();
|
| 10 |
// This array will define a Drupal.settings variable we will need in JS
|
| 11 |
$default_format = array(
|
| 12 |
'input_format_permissions_default_filter' =>
|
| 13 |
$formats[variable_get('filter_default_format', 0)],
|
| 14 |
);
|
| 15 |
|
| 16 |
$path = drupal_get_path('module', 'input_format_permissions');
|
| 17 |
// Add some javascript
|
| 18 |
drupal_add_js($default_format, 'setting');
|
| 19 |
drupal_add_js($path . '/input_format_permissions.js');
|
| 20 |
}
|
| 21 |
}
|
| 22 |
|
| 23 |
/**
|
| 24 |
* Return all filters in the system
|
| 25 |
*
|
| 26 |
* @return array
|
| 27 |
*/
|
| 28 |
function input_format_permissions_filter_load() {
|
| 29 |
$filters = array();
|
| 30 |
$result = db_query('SELECT format, name FROM {filter_formats}');
|
| 31 |
while ($filter = db_fetch_array($result)) {
|
| 32 |
$filters[$filter['format']] =
|
| 33 |
input_format_permissions_formatname_to_permission($filter['name']);
|
| 34 |
}
|
| 35 |
return $filters;
|
| 36 |
}
|
| 37 |
|
| 38 |
/**
|
| 39 |
* Implementation of hook_perm
|
| 40 |
*/
|
| 41 |
function input_format_permissions_perm() { return input_format_permissions_filter_load(); }
|
| 42 |
|
| 43 |
/**
|
| 44 |
* Implementation of hook_form_alter
|
| 45 |
*/
|
| 46 |
function input_format_permissions_form_alter(&$form, $form_state, $form_id) {
|
| 47 |
// Add our own special submit function to the permissions form
|
| 48 |
if ($form_id == 'user_admin_perm') {
|
| 49 |
$form['#submit'][] = 'input_format_permissions_perm_submit';
|
| 50 |
// Remove the ability to change roles from the configure filter form
|
| 51 |
} else if ($form_id == 'filter_admin_format_form') {
|
| 52 |
$form['roles']['#access'] = FALSE;
|
| 53 |
}
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Based on the submitted permissions form, setup filter permissions
|
| 58 |
* @param array $form_state The submitted form_state
|
| 59 |
* @return void
|
| 60 |
*/
|
| 61 |
function input_format_permissions_perm_submit($form, &$form_state) {
|
| 62 |
$filters = input_format_permissions_filter_load();
|
| 63 |
$sql = 'UPDATE {filter_formats} SET roles = "%s" WHERE format = "%d"';
|
| 64 |
// Determine which roles are associated with each filter
|
| 65 |
foreach ($filters as $fid => $filter) {
|
| 66 |
$roles = array();
|
| 67 |
foreach ($form_state['values'] as $id => $submitted_permissions) {
|
| 68 |
if (!is_numeric($id)) { continue; }
|
| 69 |
if (isset($submitted_permissions[$filter])) { $roles[] = $id; }
|
| 70 |
}
|
| 71 |
// Update the roles for an input format
|
| 72 |
db_query($sql, ',' . implode(',', $roles) . ',', $fid);
|
| 73 |
cache_clear_all($fid . ':', 'cache_filter', TRUE);
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
| 77 |
/**
|
| 78 |
* Create a permission name based on the filter name
|
| 79 |
*/
|
| 80 |
function input_format_permissions_formatname_to_permission($name) {
|
| 81 |
return 'use the ' . check_plain(strtolower($name)) . ' input format';
|
| 82 |
}
|