/[drupal]/contributions/modules/better_formats/better_formats_defaults.admin.inc
ViewVC logotype

Contents of /contributions/modules/better_formats/better_formats_defaults.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Tue Mar 17 08:17:38 2009 UTC (8 months, 1 week ago) by dragonwize
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +3 -5 lines
File MIME type: text/x-php
Fixed #404484: Missing t()'s
1 <?php
2 // $Id: better_formats_defaults.admin.inc,v 1.3.2.5 2009/03/17 08:16:52 dragonwize Exp $
3
4 /**
5 * @file
6 * Contains FAPI and theme functions for the format defaults form.
7 */
8
9
10 /**
11 * Builds the form for the filters admin.
12 *
13 * @return
14 * FAPI array
15 *
16 * @see better_formats_defaults_admin_form_validate()
17 * @see better_formats_defaults_admin_form_submit()
18 */
19 function better_formats_defaults_admin_form() {
20 $form = array(
21 '#tree' => TRUE,
22 );
23
24 $nform = better_formats_get_role_default_fields('node');
25 $cform = better_formats_get_role_default_fields('comment');
26 $form = array_merge($form, $nform, $cform);
27
28 $form['submit'] = array(
29 '#type' => 'submit',
30 '#value' => t('Save defaults'),
31 );
32
33 return $form;
34 }
35
36 /**
37 * Validates better_formats_admin_filter_form.
38 *
39 * @see better_formats_defaults_admin_form()
40 * @see better_formats_defaults_admin_form_submit()
41 */
42 function better_formats_defaults_admin_form_validate($form, &$form_state) {
43 $formats = filter_formats();
44 foreach ($formats as $fid => $format) {
45 $roles[$fid] = explode(',', $format->roles);
46 }
47 // Get roles that have administer filters permission.
48 $admin_roles = better_formats_get_filter_admin_roles();
49
50 foreach ($form_state['values'] as $key => $values) {
51 if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0) {
52 list($type, $rid) = explode('-', $key);
53 if (in_array($rid, $admin_roles)) {
54 // Role has the 'administer filters' permission so it can use all formats.
55 continue;
56 }
57 $fid = $values['format'];
58 if ($fid != 0 && !in_array($rid, $roles[$fid])) {
59 form_set_error($key, t('Role does not have access to selected format.'));
60 }
61 }
62 }
63 }
64
65 /**
66 * Updates database from better_formats_admin_filter_form.
67 *
68 * @see better_formats_defaults_admin_form()
69 * @see better_formats_defaults_admin_form_validate()
70 */
71 function better_formats_defaults_admin_form_submit($form, &$form_state) {
72 // Update DB.
73 $sql = "UPDATE {better_formats_defaults}
74 SET format=%d, weight=%d
75 WHERE rid=%d AND type='%s'";
76
77 foreach ($form_state['values'] as $key => $values) {
78 if (strpos($key, 'node-') === 0 || strpos($key, 'comment-') === 0) {
79 list($type, $rid) = explode('-', $key);
80 db_query($sql, $values['format'], $values['weight'], $rid, $type);
81 }
82 }
83
84 drupal_set_message(t('Defaults have been saved.'));
85 }
86
87 /**
88 * Builds FAPI form elements for the default format selection.
89 *
90 * @param $mode
91 * 'node' or 'comment'. Top most level type for requested default.
92 * @param $node_type
93 * Type of node this request is for.
94 * @return
95 * FAPI array for the default select field.
96 */
97 function better_formats_get_role_default_fields($mode, $node_type = '') {
98 $form = array();
99 $format_options = better_formats_get_formats_per_role();
100 $type = $types = $mode;
101 $per_node_type = variable_get('better_formats_per_node_type', FALSE);
102
103 if ($per_node_type && $node_type) {
104 $type = $mode . '/' . $node_type;
105 $types = $type . "','" . $mode;
106 }
107
108 // get data from db
109 $sql = "SELECT bf.*, role.name
110 FROM {better_formats_defaults} AS bf
111 INNER JOIN {role} AS role
112 ON bf.rid = role.rid
113 WHERE bf.type IN ('$types')
114 ORDER BY bf.type_weight DESC, bf.weight, role.rid";
115 $result = db_query($sql);
116
117 $roles_set = array();
118
119 while ($role = db_fetch_object($result)) {
120 if (in_array($role->rid, $roles_set)) {
121 continue;
122 }
123
124 $roles_set[] = $role->rid;
125 $key = $mode . '-' . $role->rid;
126
127 $form[$key]['role'] = array(
128 '#value' => $role->name,
129 );
130 $form[$key]['format'] = array(
131 '#type' => 'select',
132 '#options' => $format_options[$role->rid],
133 '#default_value' => $role->format,
134 '#attributes' => array('class' => 'bf-default-formats'),
135 );
136 $form[$key]['weight'] = array(
137 '#type' => 'weight',
138 '#delta' => 25,
139 '#default_value' => $role->weight,
140 );
141 }
142
143 return $form;
144 }
145
146 /**
147 * Retrieve the formats available to users by role.
148 *
149 * Gets all formats then creates an array keyed by role IDs
150 * that lists the formats available to that role. This is determined
151 * by Drupal core's format permissions set at
152 * admin/settings/filters/[filter_id].
153 *
154 * @return
155 * Multi-dim array with role IDs for keys and list of allowed formats.
156 *
157 * @see better_formats_get_role_default_fields()
158 */
159 function better_formats_get_formats_per_role() {
160 $formats = filter_formats();
161 $roles = user_roles();
162
163 // Get roles that have administer filters permission.
164 $admin_roles = better_formats_get_filter_admin_roles();
165
166 $site_default_format = filter_resolve_format(FILTER_FORMAT_DEFAULT);
167
168 foreach ($formats as $format) {
169 $roles_allowed = $format->roles ? explode(',', trim($format->roles, ',')) : array();
170 foreach ($roles as $rid => $role) {
171 $format_options[$rid][0] = t('Site default');
172 if ($format->format == $site_default_format || in_array($rid, $admin_roles) || in_array($rid, $roles_allowed)) {
173 $format_options[$rid][$format->format] = $format->name;
174 }
175 }
176 }
177
178 return $format_options;
179 }
180
181 /**
182 * Get a list of roles that have the admin filter perm.
183 *
184 * @return
185 * An numeric indexed key of role IDs.
186 */
187 function better_formats_get_filter_admin_roles() {
188 static $roles;
189 if (!$roles) {
190 $sql = "SELECT rid
191 FROM {permission}
192 WHERE perm LIKE '%administer filters%'
193 ORDER BY rid";
194 $result = db_query($sql);
195 $roles = array();
196 while ($row = db_fetch_object($result)) {
197 $roles[] = $row->rid;
198 }
199 }
200 return $roles;
201 }
202
203 /**
204 * Process variables for better-defaults-admin-form.tpl.php.
205 *
206 * @param $variables
207 * The $variables array contains the following arguments:
208 * - $form
209 */
210 function template_preprocess_better_formats_defaults_admin_form(&$variables) {
211 foreach (element_children($variables['form']) as $key) {
212 $form_row = &$variables['form'][$key];
213
214 $type = strpos($key, 'node-') === 0 ? 'node' : 'comment';
215
216 if (isset($form_row['role'])) {
217 // Set special classes needed for table drag and drop.
218 $form_row['weight']['#attributes']['class'] = 'better-formats-role-' . $type . '-weight';
219
220 $row = new stdClass();
221 $row->role = drupal_render($form_row['role']);
222 $row->format_select = drupal_render($form_row['format']);
223 $row->weight_select = drupal_render($form_row['weight']);
224
225 $variables[$type . '_default_rows'][$key] = $row;
226 }
227 }
228
229 $variables['form_submit'] = drupal_render($variables['form']);
230 }

  ViewVC Help
Powered by ViewVC 1.1.2