Ver código fonte

added per user showroom location field access

Bachir Soussi Chiadmi 7 anos atrás
pai
commit
5423f6d3bc

+ 95 - 64
sites/all/modules/gui/materiobasemod/materio_showroom.module

@@ -264,79 +264,110 @@ function materio_showroom_form_alter(&$form, &$form_state, $form_id) {
     // dsm($form, 'form');
     // dsm($form_state, 'form_state');
 
-    // define some constants
-    $field_type = 'field_materio_showroom_location';
-    $node = $form['#node'];
-    $nodetype = $form['type']['#value'];
+    _materio_showroom_alter_location_field_form($form, $form_state, $form_id);
+  }
+}
 
+function _materio_showroom_alter_location_field_form(&$form, &$form_state, $form_id){
+  // define some constants
+  $field_type = 'field_materio_showroom_location';
+  $node = $form['#node'];
+  $nodetype = $form['type']['#value'];
+
+  global $user;
+  $user = user_load($user->uid); // Make sure the user object is fully loaded
+  // dsm($user, 'user');
+
+  $fieldsmap = field_info_field_map();
+  $showroomfieldinstances = array();
+  $user_termref_field_instances = array();
+  foreach ($fieldsmap as $field_name => $field) {
+    // dsm($field,$field_name);
     // get all (probably one :p) showroom field instances from the current node
-    $fieldsmap = field_info_field_map();
-    $showroomfieldinstances = array();
-    foreach ($fieldsmap as $field_name => $field) {
-      // dsm($field,$field_name);
-      if ($field['type'] == $field_type
-          && isset($field['bundles']['node'])
-          && in_array($nodetype, $field['bundles']['node'])) {
-        $showroomfieldinstances[] = $field_name;
-      }
+    if ($field['type'] == $field_type
+        && isset($field['bundles']['node'])
+        && in_array($nodetype, $field['bundles']['node'])) {
+      $showroomfieldinstances[] = $field_name;
     }
-    // dsm($showroomfieldinstances, 'showroomfieldinstances');
-
-    if(!empty($showroomfieldinstances)){
-      // act on each field instance
-      foreach ($showroomfieldinstances as $field_name) {
-        // retrive various field infos
-        $field_info = field_info_field($field_name);
-        $field_instance = field_info_instance('node', $field_name, $nodetype);
-        // dsm($field_info, 'field_info');
-
-        // get all terms from chosen vocabulary in field instance widget settings
-        $vid = $field_instance['widget']['settings']['vocabulary'];
-        $tree = taxonomy_get_tree($vid);
-        foreach ($tree as $key => $term) {
-          $terms[$term->tid] = $term->name;
-        }
-        // dsm($terms, 'terms');
+    // get all term reference fields instance from users
+    if (isset($field['bundles']['user'])
+        && $field['type'] == 'taxonomy_term_reference') {
+      $field_info = field_info_field($field_name);
+      // dsm($field_info, "field_info");
+      $user_termref_field_instances[$field_info['settings']['allowed_values'][0]['vocabulary']] = $field_name;
+    }
+  }
+  // dsm($showroomfieldinstances, 'showroomfieldinstances');
+  // dsm($user_termref_field_instances, 'user_termref_field_instances');
+
+  // if there is no showroom field instances do nothing
+  if(!empty($showroomfieldinstances)){
+    // act on each field instance
+    foreach ($showroomfieldinstances as $field_name) {
+      // retrive various field infos
+      $field_info = field_info_field($field_name);
+      $field_instance = field_info_instance('node', $field_name, $nodetype);
+      // dsm($field_info, 'field_info');
+
+      // get all terms from chosen vocabulary in field instance widget settings
+      $vid = $field_instance['widget']['settings']['vocabulary'];
+      $voc = taxonomy_vocabulary_load($vid);
+      $tree = taxonomy_get_tree($vid);
+      foreach ($tree as $key => $term) {
+        $terms[$term->tid] = $term->name;
+      }
+      // dsm($terms, 'terms');
 
-        // get already recorded values
-        $old_items = field_get_items('node', $node, $field_name);
-        foreach ($old_items as $i => $value) {
-          $values[$value['showroom_tid']] = $value['location'];
-        }
-        // dsm($values, 'values');
-
-        // build new item list
-        foreach ($terms as $tid => $name) {
-          $items[] = array(
-            'showroom_tid' => $tid,
-            'location' => isset($values[$tid]) ? $values[$tid] : ''
-          );
-        }
-        // dsm($items, 'items');
-
-        // retrieve new field form with our custom items
-        $new_field_form = field_default_form('node', $node, $field_info, $field_instance, LANGUAGE_NONE, $items, $form, $form_state);
-        //dsm($new_field_form, 'default_form');
-
-        // change items location field title
-        $i = 0;
-        foreach ($terms as $tid => $name) {
-          $item = $new_field_form[$field_name][LANGUAGE_NONE][$i];
-          $new_field_form[$field_name][LANGUAGE_NONE][$i]['location']['#title'] = $terms[$item['showroom_tid']['#default_value']];
-          $i++;
+      // get user own term for current vocabulary (if any)
+      if(isset($user_termref_field_instances[$voc->machine_name])){
+        $user_field_name = $user_termref_field_instances[$voc->machine_name];
+        foreach (field_get_items('user', $user, $user_field_name) as $key => $value) {
+          $user_showrooms[] = $value['tid'];
         }
+        // dsm($user_showrooms, "user_showrooms");
+      }
+
+      // get already recorded values
+      $old_items = field_get_items('node', $node, $field_name);
+      foreach ($old_items as $i => $value) {
+        $values[$value['showroom_tid']] = $value['location'];
+      }
+      // dsm($values, 'values');
 
-        // remove the last one more item added by default
-        unset($new_field_form[$field_name][LANGUAGE_NONE][$i]);
+      // build new item list
+      foreach ($terms as $tid => $name) {
+        $items[] = array(
+          'showroom_tid' => $tid,
+          'location' => isset($values[$tid]) ? $values[$tid] : ''
+        );
+      }
+      // dsm($items, 'items');
+
+      // retrieve new field form with our custom items
+      $new_field_form = field_default_form('node', $node, $field_info, $field_instance, LANGUAGE_NONE, $items, $form, $form_state);
+      // dsm($new_field_form, 'default_form');
+
+      // change items location field title
+      // and check access comparing term id with user taged with
+      $i = 0;
+      foreach ($terms as $tid => $name) {
+        $item = $new_field_form[$field_name][LANGUAGE_NONE][$i];
+        $new_field_form[$field_name][LANGUAGE_NONE][$i]['location']['#title'] = $terms[$item['showroom_tid']['#default_value']];
+        $new_field_form[$field_name][LANGUAGE_NONE][$i]['location']['#disabled'] = !in_array($tid, $user_showrooms);
+        $i++;
+      }
 
-        // delete normal field form and replace it with our new custom field form
-        unset($form[$field_name]);
-        $form[$field_name] = $new_field_form[$field_name];
+      // remove the last one more item added by default
+      unset($new_field_form[$field_name][LANGUAGE_NONE][$i]);
 
-        // remove "add more" button
-        unset($form[$field_name]['und']['add_more']);
+      // delete normal field form and replace it with our new custom field form
+      unset($form[$field_name]);
+      $form[$field_name] = $new_field_form[$field_name];
 
-      }
+      // remove "add more" button
+      unset($form[$field_name]['und']['add_more']);
     }
   }
 }
+
+// TODO: migrate old location field to new one