? .cache ? .project ? .settings Index: user_force_term.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/user_force_term/user_force_term.install,v retrieving revision 1.3 diff -u -p -r1.3 user_force_term.install --- user_force_term.install 9 Mar 2008 21:19:43 -0000 1.3 +++ user_force_term.install 12 Mar 2008 20:53:29 -0000 @@ -1,9 +1,6 @@ +function user_force_term_unistall() { + switch ($GLOBALS['db_type']) { + case 'mysql': + case 'mysqli': + db_query("DROP TABLE {user_force_term}"); + break; + } + variable_del('user_force_term_vocabulary_action'); + variable_del('user_force_term_priority_method'); + variable_del('user_force_term_settings'); +} \ No newline at end of file Index: user_force_term.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/user_force_term/user_force_term.module,v retrieving revision 1.6 diff -u -p -r1.6 user_force_term.module --- user_force_term.module 9 Mar 2008 23:03:49 -0000 1.6 +++ user_force_term.module 12 Mar 2008 20:53:30 -0000 @@ -1,10 +1,5 @@ t('')); + $options = array(0 => ''); foreach ($tree as $term) { $options[$term->tid] = $term->name; } - $account = $form['_account']['#value']; + $terms = array_values(user_force_get_terms_by_level('user', $account->uid)); + + $form['user_uid'] = array( + '#type' => 'hidden', + '#value' => $account->uid, + ); $form['account']['tid'] = array( '#type' => 'select', '#title' => t('Forced term association'), - '#default_value' => $account->tid, + '#default_value' => empty($terms)?0:$terms[0], '#options' => $options, '#description' => t('Force all content submitted by this user to be associated with the selected term.'), '#weight' => 10, ); + $form['#submit']['user_force_term_user_submit'] = array(); } } + + if ($form_id == 'node_type_form' && $access && $settings['type']['vocabulary']) { + $tree = taxonomy_get_tree($settings['type']['vocabulary']); + $types_terms = user_force_term_get_types_terms(); + + $options = array(0 => ''); + foreach ($tree as $term) { + $options[$term->tid] = $term->name; + } + $form['account']['tid'] = array( + '#type' => 'select', + '#title' => t('Term association'), + '#default_value' => $types_terms[$form['#node_type']->type], + '#options' => $options, + '#description' => t('Force all content submitted of this type to be associated with the selected term.'), + '#weight' => 10, + ); + + $form['#submit']['user_force_term_type_submit'] = array(); + } + + + // change form element on node edit form from dropdown to hidden field if (preg_match('/_node_form$/', $form_id)) { - $vocabulary = taxonomy_get_vocabulary($vid); - // only act if the node type matches the node types on the vocanulary - if (in_array($form['#node']->type, $vocabulary->nodes)) { - global $user; - - // only change the element if it exists + global $user; + + $terms = user_force_get_terms($form['#node']->type, $user->uid); + $is_new = !isset($form["changed"]['#default_value']); + $action = $is_new?1:variable_get('user_force_term_vocabulary_action', 3); + + // only change the element if it exists + $vocabularies = user_force_term_get_vocabularies_list($settings); + foreach ($vocabularies as $vid) { if (isset($form['taxonomy'][$vid])) { - if ($access) { + if ($user->uid == 1) { // the admin user can see the element, but we append an explanation to the description. - $form['taxonomy'][$vid]['#description'] .= t(' Depending on your settings for the user force term module, this dropdown may not be visible to all users.', array('!url' => url('admin/user/user/user_force_term'))); + $form['taxonomy'][$vid]['#description'] .= t(' Depending on your settings for the user force term module, this dropdown may not be visible to all users.', array('%url' => url('admin/settings/user_force_term'))); } else { - // set term id - if (empty($form['nid']['#value'])) { - $account = user_load(array('uid' => $user->uid)); - } - else { - $account = user_load(array('uid' => $form['uid']['#value'])); - } - $tid = $account->tid; + _user_force_term_set_terms_field(&$form['taxonomy'], $vid, $action, $terms[$vid]); + } + } + } + } +} - // change the element to a hidden field - $form['taxonomy'][$vid] = array( +function _user_force_term_set_terms_field(&$taxonomy_fields, $vid, $action, $terms) { + $is_multiple = $taxonomy_fields[$vid]['#multiple'] == 1; + $taxonomy_fields[$vid]['#attributes']['style'] = 'display:none'; + $taxonomy_fields[$vid]['#description'] .= '
' . t('This field has been disabled by the Administrator.'); + if (!isset($terms)) { + return; + } + if ($action == 1) { // action is replace... + // change the element to a hidden field + if ($is_multiple) { + $taxonomy_fields[$vid]['#type'] = 'hidden'; + $taxonomy_fields[$vid]['#value'] = ''; + foreach ($terms as $term) { + $taxonomy_fields[] = array( + '#type' => 'hidden', + '#default_value' => $term + ); + } + } + else { + $taxonomy_fields[$vid]['#type'] = 'hidden'; + $taxonomy_fields[$vid]['#value'] = isset($terms)?$terms[0]:''; + } + } + elseif ($action == 3) { // action is none, do nothing... + } + elseif ($action == 2) { // action is append... + if ($is_multiple) { + $taxonomy_fields[$vid]['#type'] = 'hidden'; + $taxonomy_fields[$vid]['#value'] = ''; + foreach ($terms as $term) { + if (!in_array($term, $taxonomy_fields[$vid]['#default_value'])) { + $taxonomy_fields[] = array( '#type' => 'hidden', - '#value' => $tid, - ); + '#default_value' => $term + ); } } } - } + elseif (empty($taxonomy_fields[$vid]['#default_value'])) { + $taxonomy_fields[$vid]['#type'] = 'hidden'; + $taxonomy_fields[$vid]['#value'] = isset($terms)?$terms[0]:''; + } + // ELSE adding, but the term already exists do nothing + } + } /** * Implementation of hook_help(). */ -function user_force_term_help($section='') { +function user_force_term_help($section = '') { switch ($section) { case 'admin/modules#description': return t('Force content submitted by users to be associated with a taxonomy term.'); case 'admin/user/user/user_force_term': return t('By selecting a vocabulary from the list below, you can force all content created by specific users to be associated with a term from that vocabulary. When you have selected a vocabulary, you can edit a user to select the forced term you wish to associate with all his or her content.'); + case 'admin/user/roles': + return t('To assign a taxonomy term to force for a specific role you must go to the + role force administration', + array('@role_term' => url('admin/user/user/user_force_roles'))); } } @@ -104,8 +170,16 @@ function user_force_term_menu($may_cache 'access' => user_access('administer site configuration'), 'type' => MENU_LOCAL_TASK, ); - } - + $items[] = array( + 'path' => 'admin/user/user/user_force_roles', + 'title' => t('Roles force taxonomy term'), + 'description' => t('Assign taxonomy terms to be force for specific roles.'), + 'callback' => 'drupal_get_form', + 'callback arguments' => array('user_force_term_admin_roles'), + 'access' => user_access('administer site configuration'), + 'type' => MENU_LOCAL_TASK, + ); + } return $items; } @@ -116,34 +190,27 @@ function user_force_term_perm() { return array('administer forced term associations'); } -/** - * Implementation of hook_user(). - */ -function user_force_term_user($op, &$edit, &$account, $category = NULL) { - $uid = $account->uid; - switch ($op) { - case 'load': - $result = db_query('SELECT tid FROM {user_force_term} WHERE uid = %d', $uid); - $account->tid = db_result($result); - break; - case 'insert': - case 'update': - if (isset($edit['tid'])) { - $tid = $edit['tid']; - db_query('DELETE FROM {user_force_term} WHERE uid = %d', $uid); - if ($tid > 0) { - db_query('INSERT INTO {user_force_term} (uid, tid) VALUES (%d, %d)', $uid, $tid); - } - } - break; - case 'delete': - db_query('DELETE FROM {user_force_term} WHERE uid = %d', $uid); - break; - } -} - /* --- FORMS ---------------------------------------------------------------- */ +function user_force_term_type_submit($formid, $values, $extra = null) { + if ($formid == 'node_type_form') { + + $result = db_query(" + DELETE FROM {user_force_term} WHERE + level_type = 'type' AND ename = '%s' + ", $values['type']); + + if ($values['op'] == $values['submit']) { + $result = db_query(" + REPLACE {user_force_term} + (level_type, ename, eid, tid) + VALUES + ('type', '%s', 0, %d) + ", $values['type'], $values['tid']); + } + } +} + /** * admin settings form */ @@ -154,19 +221,367 @@ function user_force_term_admin_settings( drupal_set_message(t('You need to create at least one taxonomy to use this feature.'), 'error'); } else { + $settings = user_force_term_get_settings(); $options = array(0 => t('')); foreach ($vocabularies as $vocabulary) { $options[$vocabulary->vid] = $vocabulary->name; } - $form['user_force_term_vocabulary'] = array( + $action = array(1 => 'Replace', 2 => 'Append', 3 => 'None'); + + $form['user_force_term_general'] = array( + '#type' => 'fieldset', + '#title' => t('Priority Method'), + '#weight' => -8, + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['user_force_term_general']['priority_method'] = array( + '#type' => 'radios', + '#title' => t('Level Terms Method'), + '#default_value' => variable_get('user_force_term_priority_method', 1), + '#options' => array(t('Priority Override'), t('Append')), + '#description' => t('Define how the terms must be chocen when default terms are defined in the different levels.'). + '
' . + t('For the Append action to work properly, the taxonomy term should be set to Multiple Select'), + ); + $form['user_force_term_general']['user_force_term_vocabulary_action'] = array( + '#type' => 'select', + '#title' => t('Update Action'), + '#default_value' => variable_get('user_force_term_vocabulary_action', 3), + '#options' => $action, + '#description' => t('Please choose the action that should take place when users update a node.') . + '
' . + t('For the Append action to work properly, the taxonomy term should be set to Multiple Select'), + ); + + $form['user_force_term_type'] = array( + '#type' => 'fieldset', + '#title' => t('Type level force'), + '#weight' => -6, + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['user_force_term_type']['type_taxonomy'] = array( '#type' => 'select', - '#title' => t('Force taxonomy term'), - '#default_value' => variable_get('user_force_term_vocabulary', 0), + '#title' => t('Taxonomy'), + '#default_value' => $settings['type']['vocabulary'], '#options' => $options, - '#description' => t('Force all content submitted by users to be attached to a term from this vocabulary.'), + '#description' => t('The taxonomy that will be force at a type level.'), ); - +/* $form['user_force_term_type']['type_visibility'] = array( + '#type' => 'checkbox', + '#title' => t('Visibility'), + '#default_value' => $settings['type']['visibility'], + '#description' => t('Is the taxonomy field going to be visible at a type level.'), + ); + $form['user_force_term_type']['type_enforce'] = array( + '#type' => 'checkbox', + '#title' => t('Enforce'), + '#default_value' => $settings['type']['enforce'], + '#description' => t('Is the taxonomy going to be enforced at a type level.'), + );*/ + + $form['user_force_term_role'] = array( + '#type' => 'fieldset', + '#title' => t('Role level force'), + '#weight' => -4, + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['user_force_term_role']['role_taxonomy'] = array( + '#type' => 'select', + '#title' => t('Taxonomy'), + '#default_value' => $settings['role']['vocabulary'], + '#options' => $options, + '#description' => t('The taxonomy that will be force at a role level.'), + ); +/* $form['user_force_term_role']['role_visibility'] = array( + '#type' => 'checkbox', + '#title' => t('Visibility'), + '#default_value' => $settings['role']['visibility'], + '#description' => t('Is the taxonomy field going to be visible at a role level.'), + ); + $form['user_force_term_role']['role_enforce'] = array( + '#type' => 'checkbox', + '#title' => t('Enforce'), + '#default_value' => $settings['role']['enforce'], + '#description' => t('Is the taxonomy going to be enforced at a role level.'), + );*/ + + $form['user_force_term_user'] = array( + '#type' => 'fieldset', + '#title' => t('User level force'), + '#weight' => -2, + '#collapsible' => TRUE, + '#collapsed' => FALSE, + ); + $form['user_force_term_user']['user_taxonomy'] = array( + '#type' => 'select', + '#title' => t('Taxonomy'), + '#default_value' => $settings['user']['vocabulary'], + '#options' => $options, + '#description' => t('The taxonomy that will be force at a user level.'), + ); +/* $form['user_force_term_user']['user_visibility'] = array( + '#type' => 'checkbox', + '#title' => t('Visibility'), + '#default_value' => $settings['user']['visibility'], + '#description' => t('Is the taxonomy field going to be visible at a user level.'), + ); + $form['user_force_term_user']['user_enforce'] = array( + '#type' => 'checkbox', + '#title' => t('Enforce'), + '#default_value' => $settings['user']['enforce'], + '#description' => t('Is the taxonomy going to be enforced at a user level.'), + );*/ + + $form['#submit'] = array( + 'user_force_term_admin_settings_submit' => array(), + ); + return system_settings_form($form); } } + +function user_force_term_admin_settings_submit($formid, $values, $extra = null) { + if ($formid == 'user_force_term_admin_settings') { + if ($values['op'] == $values['submit']) { + variable_set('user_force_term_priority_method', $values['priority_method']); + $settings = user_force_term_get_settings(); + $settings['type']['vocabulary'] = $values['type_taxonomy']; + $settings['type']['visibility'] = $values['type_visibility']; + $settings['type']['enforce'] = $values['type_enforce']; + $settings['role']['vocabulary'] = $values['role_taxonomy']; + $settings['role']['visibility'] = $values['role_visibility']; + $settings['role']['enforce'] = $values['role_enforce']; + $settings['user']['vocabulary'] = $values['user_taxonomy']; + $settings['user']['visibility'] = $values['user_visibility']; + $settings['user']['enforce'] = $values['user_enforce']; + $serialized_var = serialize($settings); + variable_set('user_force_term_settings', $serialized_var); + drupal_set_message(t('Settings saved')); + } + else { + variable_set('user_force_term_priority_method', 1); + $settings = user_force_term_get_settings(true); + $serialized_var = serialize($settings); + variable_set('user_force_term_settings', $serialized_var); + drupal_set_message(t('Settings back to defaults')); + } + } +} + +function user_force_term_admin_roles() { + $vid = variable_get('user_force_term_vocabulary', 0); + $settings = user_force_term_get_settings(); + + if ($settings['role']['vocabulary']) { + $tree = taxonomy_get_tree($settings['role']['vocabulary']); + $roles = user_roles(); + $roles_terms = user_force_term_get_roles_terms(); + + $options = array(0 => ''); + foreach ($tree as $term) { + $options[$term->tid] = $term->name; + } + + $form['roles'] = array( + '#type' => 'fieldset', + '#title' => t('Roles'), + '#attributes' => array('class' => 'force_role_fieldset'), + '#description' => t('Force all content submitted by this role to be associated with the selected term.'), + ); + + $count = 0; + foreach ($roles as $role_id => $role_name) { + $form['roles']['role_' . $role_id] = array( + '#type' => 'select', + '#title' => $role_name, + '#default_value' => $roles_terms[$role_id], + '#options' => $options, + '#attributes' => array('class' => 'force_role_item'), + '#weight' => $count++, + ); + } + + $form['#submit'] = array( + 'user_force_term_admin_roles_submit' => array(), + ); + } + return system_settings_form($form); +} + +function user_force_term_admin_roles_submit($formid, $values, $extra = null) { + if ($formid == 'user_force_term_admin_roles') { + $result = db_query("DELETE FROM {user_force_term} WHERE level_type = 'role'"); + if ($values['op'] == $values['submit']) { + foreach ($values as $name => $value) { + if (substr($name, 0, 5) == 'role_') { + $role_id = substr($name, 5); + $result = db_query(" + REPLACE {user_force_term} + (level_type, ename, eid, tid) + VALUES + ('role', '', %d, %d) + ", $role_id, $value); + } + } + drupal_set_message(t('Settings saved')); + } + else { + drupal_set_message(t('Settings back to defaults')); + } + } +} + +function user_force_term_user_submit($formid, $values, $extra = null) { + if ($formid == 'user_edit' || $formid == 'user_register') { + $result = db_query(" + DELETE FROM {user_force_term} WHERE level_type = 'user' AND eid = %d" + , $values['user_uid']); + + $result = db_query(" + INSERT INTO {user_force_term} + (level_type, eid, tid) + VALUES + ('user', %d, %d) + ", $values['user_uid'], $values['tid']); + } +} + +/* --- FUNCTIONS ------------------------------------------------------------ */ + +function user_force_term_get_settings($force_defaults = false) { + $settings = array( + 'type' => array( + 'vocabulary' => 0, + 'visibility' => 0, + 'enforce' => 0, + 'weight' => 1, + ), + 'role' => array( + 'vocabulary' => 0, + 'visibility' => 0, + 'enforce' => 0, + 'weight' => 2, + ), + 'user' => array( + 'vocabulary' => 0, + 'visibility' => 0, + 'enforce' => 0, + 'weight' => 3, + ), + ); + if ($force_defaults) { + return $settings; + } + $serialized_var = serialize($settings); + return unserialize(variable_get('user_force_term_settings', $serialized_var)); +} + +function user_force_term_get_weight_list($settings) { + $weights = array(); + foreach ($settings as $key => $value) { + $weights[$value['weight']] = $key; + } + return $weights; +} + +function user_force_term_get_vocabularies_list($settings) { + $list = array(); + foreach ($settings as $level => $values) { + if (!in_array($values['vocabulary'], $list)) { + $list[] = $values['vocabulary']; + } + } + return $list; +} + +function user_force_term_get_roles_terms() { + $roles = array(); + $result = db_query("SELECT eid, tid FROM {user_force_term} WHERE level_type = 'role'"); + while ($row = db_fetch_array($result)) { + $roles[$row['eid']] = $row['tid']; + } + return $roles; +} + +function user_force_term_get_types_terms() { + $types = array(); + $result = db_query("SELECT ename, tid FROM {user_force_term} WHERE level_type = 'type'"); + while ($row = db_fetch_array($result)) { + $types[$row['ename']] = $row['tid']; + } + return $types; +} + +function user_force_get_terms($type, $uid) { + $settings = user_force_term_get_settings(); + $weights = user_force_term_get_weight_list($settings); + $terms = array(); + + foreach ($weights as $level) { + switch ($level) { + case 'type': + $level_terms = user_force_get_terms_by_level($level, $type); + break; + case 'role': + $level_terms = user_force_get_terms_by_level($level, $uid); + break; + case 'user': + $level_terms = user_force_get_terms_by_level($level, $uid); + break; + } + if (variable_get('user_force_term_priority_method', 1) == 1) { + $terms = array_merge($terms, $level_terms); + } + else { + $terms = $level_terms; + } + } + return $terms; +} + +function user_force_get_terms_by_level($level, $eid) { + $settings = user_force_term_get_settings(); + $terms = array(); + + switch ($level) { + case 'type': + $result = db_query( + "SELECT t.vid, ft.level_type, ft.ename, ft.eid, ft.tid + FROM {user_force_term} ft INNER JOIN {term_data} t ON ft.tid = t.tid + WHERE ft.level_type = 'type' AND ft.ename = '%s'" + , $eid); + break; + case 'role': + $result = db_query( + "SELECT t.vid, ft.level_type, ft.ename, ft.eid, ft.tid + FROM {user_force_term} ft + INNER JOIN {users_roles} role ON ft.eid = role.rid + INNER JOIN {term_data} t ON ft.tid = t.tid + WHERE ft.level_type = 'role' AND role.uid = %d + UNION + SELECT t.vid, ft.level_type, ft.ename, ft.eid, ft.tid + FROM {user_force_term} ft INNER JOIN {term_data} t ON ft.tid = t.tid + WHERE ft.level_type = 'role' + AND ft.eid = %d" + , $eid, ($eid == 0 ? DRUPAL_ANONYMOUS_RID : DRUPAL_AUTHENTICATED_RID)); + break; + case 'user': + $result = db_query( + "SELECT t.vid, ft.level_type, ft.ename, ft.eid, ft.tid + FROM {user_force_term} ft + INNER JOIN {term_data} t ON ft.tid = t.tid + WHERE ft.level_type = 'user' AND ft.eid = %d" + , $eid); + break; + } + if ($result) { + while ($row = db_fetch_array($result)) { + $terms[$row['vid']][] = $row['tid']; + } + } + return $terms; +}