better_formats_defaults.admin.inc 6.85 KB
Newer Older
dragonwize's avatar
dragonwize committed
1
<?php
dragonwize's avatar
dragonwize committed
2
// $Id$
dragonwize's avatar
dragonwize committed
3

dragonwize's avatar
dragonwize committed
4
5
6
7
/**
 * @file
 * Contains FAPI and theme functions for the format defaults form.
 */
dragonwize's avatar
dragonwize committed
8
9


dragonwize's avatar
dragonwize committed
10
/**
dragonwize's avatar
dragonwize committed
11
 * Builds the form for the filters admin.
dragonwize's avatar
dragonwize committed
12
 *
13
 * @return
dragonwize's avatar
dragonwize committed
14
 *  FAPI array
dragonwize's avatar
dragonwize committed
15
 *
dragonwize's avatar
dragonwize committed
16
17
 * @see better_formats_defaults_admin_form_validate()
 * @see better_formats_defaults_admin_form_submit()
dragonwize's avatar
dragonwize committed
18
19
20
21
22
 */
function better_formats_defaults_admin_form() {
  $form = array(
    '#tree' => TRUE,
  );
dragonwize's avatar
dragonwize committed
23

dragonwize's avatar
dragonwize committed
24
25
  $nform = better_formats_get_role_default_fields('node');
  $cform = better_formats_get_role_default_fields('comment');
26
27
  $bform = better_formats_get_role_default_fields('block');
  $form  = array_merge($form, $nform, $cform, $bform);
dragonwize's avatar
dragonwize committed
28

dragonwize's avatar
dragonwize committed
29
30
31
32
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save defaults'),
  );
dragonwize's avatar
dragonwize committed
33

dragonwize's avatar
dragonwize committed
34
35
36
37
  return $form;
}

/**
dragonwize's avatar
dragonwize committed
38
 * Validates better_formats_admin_filter_form.
dragonwize's avatar
dragonwize committed
39
 *
dragonwize's avatar
dragonwize committed
40
41
 * @see better_formats_defaults_admin_form()
 * @see better_formats_defaults_admin_form_submit()
dragonwize's avatar
dragonwize committed
42
43
44
45
46
47
 */
function better_formats_defaults_admin_form_validate($form, &$form_state) {
  $formats = filter_formats();
  foreach ($formats as $fid => $format) {
    $roles[$fid] = explode(',', $format->roles);
  }
48
  // Get roles that have administer filters permission.
49
  $admin_roles = better_formats_get_roles_by_perm('administer filters');
dragonwize's avatar
dragonwize committed
50

dragonwize's avatar
dragonwize committed
51
  foreach ($form_state['values'] as $key => $values) {
52
    if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0 || strpos($key, 'block-') === 0) {
dragonwize's avatar
dragonwize committed
53
      list($type, $rid) = explode('-', $key);
54
      if (in_array($rid, $admin_roles)) {
55
        // Role has the 'administer filters' permission so it can use all formats.
56
57
        continue;
      }
dragonwize's avatar
dragonwize committed
58
      $fid = $values['format'];
59
      if ($fid != 0 && !in_array($rid, $roles[$fid])) {
dragonwize's avatar
dragonwize committed
60
61
62
63
64
65
66
        form_set_error($key, t('Role does not have access to selected format.'));
      }
    }
  }
}

/**
dragonwize's avatar
dragonwize committed
67
 * Updates database from better_formats_admin_filter_form.
dragonwize's avatar
dragonwize committed
68
 *
dragonwize's avatar
dragonwize committed
69
70
 * @see better_formats_defaults_admin_form()
 * @see better_formats_defaults_admin_form_validate()
dragonwize's avatar
dragonwize committed
71
72
 */
function better_formats_defaults_admin_form_submit($form, &$form_state) {
73
  // Update DB.
dragonwize's avatar
dragonwize committed
74
75
  $sql = "UPDATE {better_formats_defaults}
          SET format=%d, weight=%d
dragonwize's avatar
dragonwize committed
76
          WHERE rid=%d AND type='%s'";
dragonwize's avatar
dragonwize committed
77

dragonwize's avatar
dragonwize committed
78
  foreach ($form_state['values'] as $key => $values) {
79
    if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0 || strpos($key, 'block-') === 0) {
dragonwize's avatar
dragonwize committed
80
81
82
83
      list($type, $rid) = explode('-', $key);
      db_query($sql, $values['format'], $values['weight'], $rid, $type);
    }
  }
dragonwize's avatar
dragonwize committed
84

dragonwize's avatar
dragonwize committed
85
86
87
  drupal_set_message(t('Defaults have been saved.'));
}

88
89
/**
 * Builds FAPI form elements for the default format selection.
dragonwize's avatar
dragonwize committed
90
 *
91
 * @param $mode
92
 *  'node', 'comment', or 'block'. Top most level type for requested default.
93
94
95
96
97
98
99
100
101
102
 * @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);
103
  $block_admins  = better_formats_get_roles_by_perm('administer blocks');
dragonwize's avatar
dragonwize committed
104

105
106
107
108
  if ($per_node_type && $node_type) {
    $type = $mode . '/' . $node_type;
    $types = $type . "','" . $mode;
  }
dragonwize's avatar
dragonwize committed
109

110
  // get data from db
dragonwize's avatar
dragonwize committed
111
112
  $sql = "SELECT bf.*, role.name
          FROM {better_formats_defaults} AS bf
113
          INNER JOIN {role} AS role
dragonwize's avatar
dragonwize committed
114
115
          ON bf.rid = role.rid
          WHERE bf.type IN ('$types')
116
117
          ORDER BY bf.type_weight DESC, bf.weight, role.rid";
  $result = db_query($sql);
dragonwize's avatar
dragonwize committed
118

119
  $roles_set = array();
dragonwize's avatar
dragonwize committed
120

121
  while ($role = db_fetch_object($result)) {
122
    if (in_array($role->rid, $roles_set) || ($mode === 'block' && !in_array($role->rid, $block_admins))) {
123
124
      continue;
    }
dragonwize's avatar
dragonwize committed
125

126
127
    $roles_set[] = $role->rid;
    $key = $mode . '-' . $role->rid;
dragonwize's avatar
dragonwize committed
128

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    $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,
    );
  }
dragonwize's avatar
dragonwize committed
144

145
146
147
148
149
  return $form;
}

/**
 * Retrieve the formats available to users by role.
dragonwize's avatar
dragonwize committed
150
 *
151
 * Gets all formats then creates an array keyed by role IDs
dragonwize's avatar
dragonwize committed
152
153
 * that lists the formats available to that role. This is determined
 * by Drupal core's format permissions set at
154
 * admin/settings/filters/[filter_id].
dragonwize's avatar
dragonwize committed
155
156
 *
 * @return
157
 *  Multi-dim array with role IDs for keys and list of allowed formats.
dragonwize's avatar
dragonwize committed
158
 *
159
160
 * @see better_formats_get_role_default_fields()
 */
dragonwize's avatar
dragonwize committed
161
function better_formats_get_formats_per_role() {
162
163
  $formats = filter_formats();
  $roles   = user_roles();
dragonwize's avatar
dragonwize committed
164

165
  // Get roles that have administer filters permission.
166
  $admin_roles = better_formats_get_roles_by_perm('administer filters');
dragonwize's avatar
dragonwize committed
167

168
  $site_default_format = filter_resolve_format(FILTER_FORMAT_DEFAULT);
dragonwize's avatar
dragonwize committed
169

170
171
172
  foreach ($formats as $format) {
    $roles_allowed = $format->roles ? explode(',', trim($format->roles, ',')) : array();
    foreach ($roles as $rid => $role) {
dragonwize's avatar
dragonwize committed
173
      $format_options[$rid][0] = t('Site default');
174
175
176
177
178
      if ($format->format == $site_default_format || in_array($rid, $admin_roles) || in_array($rid, $roles_allowed)) {
        $format_options[$rid][$format->format] = $format->name;
      }
    }
  }
dragonwize's avatar
dragonwize committed
179

180
181
182
183
  return $format_options;
}

/**
184
 * Get a list of roles that have a permission.
dragonwize's avatar
dragonwize committed
185
 *
186
187
188
189
 * @param $perm
 *  Permission string to get roles for.
 * @param $reset
 *  Boolean to clear static cache.
190
 * @return
191
 *  An array of role IDs that have the requested permission.
192
 */
193
function better_formats_get_roles_by_perm($perm, $reset = FALSE) {
194
  static $roles;
195
  if ($reset || !$roles[$perm]) {
dragonwize's avatar
dragonwize committed
196
197
    $sql = "SELECT rid
            FROM {permission}
198
            WHERE perm LIKE '%$perm%'
199
200
            ORDER BY rid";
    $result = db_query($sql);
201
    $roles[$perm] = array();
202
    while ($row = db_fetch_object($result)) {
203
      $roles[$perm][] = $row->rid;
204
205
    }
  }
206
  return $roles[$perm];
207
208
}

dragonwize's avatar
dragonwize committed
209
/**
dragonwize's avatar
dragonwize committed
210
 * Process variables for better-defaults-admin-form.tpl.php.
dragonwize's avatar
dragonwize committed
211
 *
212
 * @param $vars
dragonwize's avatar
dragonwize committed
213
214
 *  The $variables array contains the following arguments:
 *    - $form
dragonwize's avatar
dragonwize committed
215
 */
216
217
218
function template_preprocess_better_formats_defaults_admin_form(&$vars) {
  foreach (element_children($vars['form']) as $key) {
    $form_row = &$vars['form'][$key];
dragonwize's avatar
dragonwize committed
219

220
221
    //$type = strpos($key, 'node-') === 0 ? 'node' : 'comment';
    $type = substr($key, 0, strpos($key, '-'));
dragonwize's avatar
dragonwize committed
222

dragonwize's avatar
dragonwize committed
223
224
225
226
227
228
229
230
    if (isset($form_row['role'])) {
      // Set special classes needed for table drag and drop.
      $form_row['weight']['#attributes']['class'] = 'better-formats-role-' . $type . '-weight';

      $row = new stdClass();
      $row->role = drupal_render($form_row['role']);
      $row->format_select = drupal_render($form_row['format']);
      $row->weight_select = drupal_render($form_row['weight']);
dragonwize's avatar
dragonwize committed
231

232
      $vars[$type . '_default_rows'][$key] = $row;
dragonwize's avatar
dragonwize committed
233
234
    }
  }
dragonwize's avatar
dragonwize committed
235

236
  $vars['form_submit'] = drupal_render($vars['form']);
dragonwize's avatar
dragonwize committed
237
}