/[drupal]/contributions/modules/taxorole/taxorole.module
ViewVC logotype

Contents of /contributions/modules/taxorole/taxorole.module

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


Revision 1.2 - (show annotations) (download) (as text)
Wed Jan 16 21:45:32 2008 UTC (22 months, 1 week ago) by codexgr
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +2 -1 lines
File MIME type: text/x-php
Fixed issue #200505
1 <?php
2 /* Copyright CODEX, Konstantinos Margaritis markos@codex.gr
3 * Licensed under the GPL
4 * $Id: taxorole.module,v 1.1 2007/11/14 17:25:17 codexgr Exp $
5 */
6
7 /**
8 * Display help and module information
9 * @param section which section of the site we're displaying help
10 * @return help text for section
11 */
12 function taxorole_help($section='') {
13
14 $output = '';
15
16 switch ($section) {
17 case "admin/help#taxorole":
18 $output = '<p>'. t("Uses taxonomy terms as user roles"). '</p>';
19 break;
20 }
21
22 return $output;
23 } // function taxorole_help
24
25 /**
26 * Valid permissions for this module
27 * @return array An array of valid permissions for the onthisdate module
28 */
29 function taxorole_perm() {
30 return array('administer taxorole');
31 } // function taxorole_perm()
32
33 /**
34 * The menu entry for taxorole module admin pages.
35 */
36 function taxorole_menu() {
37
38 $items = array();
39
40 $items[] = array(
41 'path' => 'admin/settings/taxorole',
42 'title' => t('Taxorole settings'),
43 'description' => t('Administer taxonomies to be used as roles'),
44 'callback' => 'drupal_get_form',
45 'callback arguments' => 'taxorole_admin',
46 'access' => user_access('access administration pages'),
47 'type' => MENU_NORMAL_ITEM,
48 );
49
50 return $items;
51 }
52
53 /**
54 * Administer taxorole. Select the vocabularies that will act as role pools,
55 * the prefix, delimeter, and some other options, that will show if some other
56 * modules are installed. Specifically, if mass_contact and/or tac_lite are installed,
57 * taxorole will create a respective category for mass_contact and it will setup the
58 * necessary permissions in tac_lite, so that content tagged with a term in the
59 * selected vocabulary, will only be visible to the user with the role of the same name.
60 */
61 function taxorole_admin() {
62 $vocabularies = taxonomy_get_vocabularies();
63
64 foreach ($vocabularies as $vid => $vocab) {
65 $options[$vid] = $vocab->name;
66 }
67 $form['taxorole_categories'] =
68 array('#type' => 'select',
69 '#title' => 'Vocabularies',
70 '#default_value' => variable_get('taxorole_categories', null),
71 '#options' => $options,
72 '#description' => t('Select the taxonomies, whose terms will be used as roles.'),
73 '#multiple' => true,
74 );
75 $form['taxorole_prefix_role_name'] =
76 array('#type' => 'checkbox',
77 '#title' => 'Prefix role with vocabulary name',
78 '#default_value' => variable_get('taxorole_prefix_role_name', 1),
79 '#description' => t('The user role that will be created will be prefixed with the vocabulary name.'),
80 );
81 $form['taxorole_prefix_delimeter'] =
82 array('#type' => 'textfield',
83 '#title' => 'Prefix delimeter',
84 '#size' => 2,
85 '#maxlength' => 1,
86 '#default_value' => variable_get('taxorole_prefix_delimeter', "_"),
87 '#description' => t('The delimeter to be used with the prefix.'),
88 );
89 if (module_exists('mass_contact')) {
90 $form['taxorole_add_mass_contact_category'] =
91 array('#type' => 'checkbox',
92 '#title' => 'Add the new role(s) to Mass Contact module categories',
93 '#default_value' => variable_get('taxorole_add_mass_contact_category', 1),
94 '#description' => t('If mass contact module is installed and the setting is enabled, a new category will be added with each new role.'),
95 );
96 }
97 if (module_exists('tac_lite')) {
98 $form['taxorole_configure_tac_lite'] =
99 array('#type' => 'checkbox',
100 '#title' => 'Configure access for the new roles in tac_lite. ',
101 '#default_value' => variable_get('taxorole_configure_tac_lite', 1),
102 '#description' => t('Any node tagged with term in the selected vocabulary(ies) will be visible only by the respective role(s).'),
103 );
104 }
105 $form['taxorole_synchronize_on_taxonomy'] =
106 array('#type' => 'checkbox',
107 '#title' => 'Automatic update on taxonomy update',
108 '#default_value' => variable_get('taxorole_synchronize_on_taxonomy', 1),
109 '#description' => t('Whenever a new term is added in the selected vocabulary, a new role will be created automatically, and -if selected- the respected category in Mass Contact will be created and the proper access in TAC lite.'),
110 );
111
112 $ret = system_settings_form($form);
113 // Special handling is required when this form is submitted.
114 $ret['#submit']['taxorole_admin_settings_submit'] = array('#type' => 'submit', '#value' => t('Save'));
115 return $ret;
116 }
117
118 /**
119 * This form submit callback ensures that the form values are saved, and also
120 * calls adds the new role(s) to Drupal's roles.
121 */
122 function taxorole_admin_settings_submit($form_id, $form_values) {
123 // First, save settings the default way.
124 system_settings_form_submit($form_id, $form_values);
125
126 $newvids = array();
127 $newgrants = array();
128
129 foreach ($form_values['taxorole_categories'] as $vid) {
130 $newvids[$vid] = $vid;
131
132 $voc = taxonomy_get_vocabulary($vid);
133 $terms = taxonomy_get_tree($vid);
134 foreach ($terms as $term) {
135 if ($form_values['taxorole_prefix_role_name'] == 1) {
136 $newrole = $voc->name.$form_values['taxorole_prefix_delimeter'];
137 }
138 $newrole .= $term->name;
139
140 $newrid = taxorole_role_add($newrole);
141 $newgrants[$newrid][$vid] = array($term->tid => $term->tid);
142
143 drupal_set_message(t('Created role ').$newrole.'.');
144 if (module_exists('mass_contact') && $form_values['taxorole_add_mass_contact_category'] == 1) {
145 taxorole_mass_contact_add_category($newrole, $newrid);
146 drupal_set_message(t('Added category ').$newrole.t(' in Mass Contact categories.'));
147 unset($newrole);
148 }
149 }
150 }
151
152 if (module_exists('mass_contact') && $form_values['taxorole_configure_tac_lite'] == 1) {
153 taxorole_role_tac_set($newvids, $newgrants);
154 drupal_set_message(t('Configured term access control for the new roles in tac_lite module.'));
155 }
156 }
157
158 /**
159 * Helper function to insert the new category in mass_contact's categories.
160 */
161 function taxorole_mass_contact_add_category($role, $rid) {
162 if (!db_result(db_query("SELECT COUNT(*) FROM {mass_contact} WHERE category = '%s'", $role))) {
163 db_query("INSERT INTO {mass_contact} (category, recipients, reply, weight, selected) VALUES ('%s', '%s', '%s', %d, %d)", $role, $rid, null, 0, 0);
164 }
165 }
166
167 /**
168 * Helper function to add/insert the role into the Drupal database.
169 */
170 function taxorole_role_add($rolename) {
171 if (!db_result(db_query("SELECT COUNT(*) FROM {role} WHERE name = '%s'", $rolename))) {
172 db_query("INSERT INTO {role} (name) VALUES ('%s')", $rolename);
173 }
174 return db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", $rolename));
175 }
176
177 /**
178 * Helper function that sets up the access in tac_lite for the new role(s).
179 */
180 function taxorole_role_tac_set($vids, $grants) {
181 $tac_vids = variable_get('tac_lite_categories', null);
182 foreach ($vids as $vid) {
183 $tac_vids[$vid] = $vid;
184 }
185 variable_set('tac_lite_categories', $tac_vids);
186 $tac_grants = variable_get('tac_lite_default_grants', array());
187 foreach (array_keys($grants) as $rid) {
188 $tac_grants[$rid] = $grants[$rid];
189 }
190 variable_set('tac_lite_default_grants', $tac_grants);
191 }
192
193 /**
194 * Implementation for hook_taxonomy().
195 * Whenever a new term is added to a vocabulary, we check if this is one of the selected vocabularies
196 * and if so, we do the same procedure as in taxorole_admin_settings_submit():
197 * 1. add the new role
198 * 2. add the mass_contact category
199 * 3. setup the access control for the new role in tac_lite.
200 */
201 function taxorole_taxonomy($op, $type, $array = NULL) {
202 if (variable_get('taxorole_synchronize_on_taxonomy', 0)) {
203 if ($op == 'insert' || $op == 'update') {
204 switch ($type) {
205 case 'term':
206 $vid = $array['vid'];
207 $voc = taxonomy_get_vocabulary($vid);
208 $newvids = array( $vid => $vid );
209
210 $tid = $array['tid'];
211 $name = $array['name'];
212
213 $newgrants = array();
214
215 if (variable_get('taxorole_prefix_role_name', 1) == 1) {
216 $newrole = $voc->name.variable_get('taxorole_prefix_delimeter','_');
217 }
218 $newrole .= $name;
219 $newrid = taxorole_role_add($newrole);
220 $newgrants[$newrid][$vid] = array($tid => $tid);
221
222 drupal_set_message(t('Created role ').$newrole.'.');
223 if (module_exists('mass_contact') && variable_get('taxorole_add_mass_contact_category', 0)) {
224 taxorole_mass_contact_add_category($newrole, $newrid);
225 drupal_set_message(t('Added category ').$newrole.t(' in Mass Contact categories.'));
226 }
227
228 if (module_exists('mass_contact') && variable_get('taxorole_configure_tac_lite', 0)) {
229 taxorole_role_tac_set($newvids, $newgrants);
230 drupal_set_message(t('Configured term access control for the new roles in tac_lite module.'));
231 }
232
233 break;
234 case 'vocabulary':
235 break;
236 }
237 }
238 }
239 }

  ViewVC Help
Powered by ViewVC 1.1.2