Browse Source

Merge branch 'entities' of github.com:tripal/tripal into entities

Stephen Ficklin 9 years ago
parent
commit
78be5380a2

+ 14 - 0
tripal_fields_layout/theme/templates/tripal_fields_layout_generic.tpl.php

@@ -0,0 +1,14 @@
+<?php
+
+$panels = $variables['element']['#panels'];
+$fields = $variables['element']['#fields'];
+
+// Group fields into panels
+foreach ($fields AS $panel_id => $field) { ?>
+  <div style="border:solid 1px #CCC;margin:15px 0px;padding:15px;"><?php
+    print '<h1>' . $panels[$panel_id] . '</h1>';
+    print render($field);
+  ?>
+  </div><?php
+}
+

+ 148 - 76
tripal_fields_layout/tripal_fields_layout.module

@@ -76,13 +76,6 @@ function tripal_fields_layout_form_field_ui_display_overview_form_alter(&$form,
     '#description' => t('Please provide a human readable label for this
         panel. This is the name that will appear to site visitors.')
   );
-  $form['te_add_panels']['message'] = array(
-    '#type' => 'textarea',
-    '#title' => 'Empty Message',
-    '#rows' => 2,
-    '#description' => t('When the panel has no fields the following
-        message will be shown in the form above.')
-  );
   $form['te_add_panels']['add_button'] = array(
     '#type' => 'submit',
     '#value' => 'Add Panel',
@@ -92,7 +85,7 @@ function tripal_fields_layout_form_field_ui_display_overview_form_alter(&$form,
   // Layout Panels
   $form['te_layout_panels'] = array(
     '#type' => 'fieldset',
-    '#title' => 'Order Panels',
+    '#title' => 'Arrange Panels',
     '#collapsible' => TRUE,
     '#collapsed' => TRUE,
     '#group' => 'overview_vert_tabs'
@@ -103,12 +96,15 @@ function tripal_fields_layout_form_field_ui_display_overview_form_alter(&$form,
   );
   $form['te_layout_panels']['panel_items']['#tree'] = TRUE;
   // Get available panels
-  $result = db_query('SELECT panel_id, name, label, weight FROM {tripal_panels} ORDER BY weight ASC');
+  $result = db_select('tripal_panels', 'tp')
+            ->fields('tp', array('panel_id', 'name', 'label', 'weight'))
+            ->condition('name', 'te_base', '<>')
+            ->condition('name', 'te_disabled', '<>')
+            ->orderby('weight', 'asc')
+            ->execute();
+  $has_panel = FALSE;
   foreach ($result as $item) {
     $form['te_layout_panels']['panel_items'][$item->panel_id] = array(
-      'name' => array(
-        '#markup' => check_plain($item->name),
-      ),
       'label' => array(
         '#markup' => check_plain($item->label),
       ),
@@ -119,18 +115,29 @@ function tripal_fields_layout_form_field_ui_display_overview_form_alter(&$form,
         '#delta' => 50,
         '#title_display' => 'invisible',
       ),
+      'remove' => array(
+        '#type' => 'button',
+        '#value' => 'Remove',
+        '#name' => "arrange-panel-remove-$item->panel_id",
+      ) 
     );
+    $has_panel = TRUE;
+  }
+  if ($has_panel) {
+    $form['te_layout_panels']['panel_items']['#theme_wrappers'] = array('tripal_fields_layout_form_draggable_panel_table');
+    // Now we add our submit button, for submitting the form results.
+    //
+    // The 'actions' wrapper used here isn't strictly necessary for tabledrag,
+    // but is included as a Form API recommended practice.
+    $form['te_layout_panels']['add_button'] = array(
+      '#type' => 'submit',
+      '#value' => 'Save Panel Order',
+      '#name' => 'order-panel-submit'
+    );
+  }
+  else {
+    $form['te_layout_panels']['instructions']['#markup'] = t('You need to add some panel first.');
   }
-  $form['te_layout_panels']['panel_items']['#theme_wrappers'] = array('tripal_fields_layout_form_draggable_panel_table');
-  // Now we add our submit button, for submitting the form results.
-  //
-  // The 'actions' wrapper used here isn't strictly necessary for tabledrag,
-  // but is included as a Form API recommended practice.
-  $form['te_layout_panels']['add_button'] = array(
-    '#type' => 'submit',
-    '#value' => 'Save Panel Order',
-    '#name' => 'order-panel-submit'
-  );
   
   // Configure Panels
   $form['te_configure_panels'] = array(
@@ -223,6 +230,7 @@ function _tripal_fields_layout_check_default_field_panels($bundle) {
   $te_base = db_select('tripal_panels', 'tp')
     ->fields('tp')
     ->condition('name', 'te_base')
+    ->condition('bundle_id', $bundle->id)
     ->execute()
     ->fetchObject();
   if (!$te_base) {
@@ -243,6 +251,7 @@ function _tripal_fields_layout_check_default_field_panels($bundle) {
   $te_base = db_select('tripal_panels', 'tp')
     ->fields('tp')
     ->condition('name', 'te_disabled')
+    ->condition('bundle_id', $bundle->id)
     ->execute()
     ->fetchObject();
   if (!$te_base) {
@@ -293,8 +302,6 @@ function tripal_fields_layout_field_ui_row_region($row) {
   if (!$panel) {
     $panel = $default_panel;
   }
-  
-  // See if there is a record in the tripal_panel_fields for this field.
 
   return $panel;
 
@@ -332,6 +339,17 @@ function tripal_fields_layout_field_ui_validate($form, &$form_state) {
   else if ($form_state ['clicked_button'] ['#name'] == 'op') {
     
   }
+  else if (preg_match('/^arrange-panel-remove-/', $form_state ['clicked_button'] ['#name'])) {
+    $table = $form['te_layout_panels']['panel_items'];
+    $button = $form_state ['triggering_element'] ['#name'];
+    $panel_id = str_replace ('arrange-panel-remove-', '', $button);
+    db_delete('tripal_panels')
+    ->condition ('panel_id', $panel_id)
+    ->execute();
+    db_delete('tripal_panel_fields')
+    ->condition ('panel_id', $panel_id)
+    ->execute();
+  }
   
 }
 /**
@@ -346,9 +364,8 @@ function tripal_fields_layout_field_ui_submit($form, &$form_state) {
     $bundle_id = $form_state['build_info']['args'][1]->id;
     $name      = $form_state['values']['panel_name'];
     $label     = $form_state['values']['panel_label'];
-    $message   = $form_state['values']['message'];
     $settings = array(
-      'message' => $message,
+      'message' => 'Place field in this panel.',
     );
     db_insert('tripal_panels')
     ->fields(array(
@@ -429,68 +446,42 @@ function theme_tripal_fields_layout_form_draggable_panel_table ($variables) {
 
   foreach (element_children($element) as $id) {
 
-    // Before we add our 'weight' column to the row, we need to give the
-    // element a custom class so that it can be identified in the
-    // drupal_add_tabledrag call.
-    //
-    // This could also have been done during the form declaration by adding
-    // '#attributes' => array('class' => 'example-item-weight'),
-    // directy to the 'weight' element in tabledrag_example_simple_form().
-    $element[$id]['weight']['#attributes']['class'] = array('panel-item-weight');
+    // Add a custom class that can be identified by 'drupal_add_tabledrag'
+    $element[$id]['weight']['#attributes']['class'] = array('tripal_panel-item-weight');
 
-    $element[$id]['name']['#printed'] = FALSE;
     $element[$id]['label']['#printed'] = FALSE;
     $element[$id]['weight']['#printed'] = FALSE;
-    // We are now ready to add each element of our $form data to the $rows
-    // array, so that they end up as individual table cells when rendered
-    // in the final table.  We run each element through the drupal_render()
-    // function to generate the final html markup for that element.
+    $element[$id]['remove']['#printed'] = FALSE;
+
     $rows[] = array(
       'data' => array(
-        // Add our 'name' column.
-        drupal_render($element[$id]['name']),
-        // Add our 'description' column.
         drupal_render($element[$id]['label']),
-        // Add our 'weight' column.
         drupal_render($element[$id]['weight']),
+        drupal_render($element[$id]['remove'])
       ),
-      // To support the tabledrag behaviour, we need to assign each row of the
-      // table a class attribute of 'draggable'. This will add the 'draggable'
-      // class to the <tr> element for that row when the final table is
-      // rendered.
+      // Add draggable to each row to support the tabledrag behaviour
       'class' => array('draggable'),
     );
   }
+  
+  // Create table header
+  $header = array(t('Label'), t('Weight'), t('Action'));
+
+  // Create a unique id for drupal_add_tabledrag() to find the table object
+  $table_id = 'tripal_panel-arrange_panel_table';
+
+  // Create output
+  $output = '';
+  if (count($rows) > 0) {
+    $output = theme('table', array(
+      'header' => $header,
+      'rows' => $rows,
+      'attributes' => array('id' => $table_id),
+    ));
+  }
 
-  // We now define the table header values.  Ensure that the 'header' count
-  // matches the final column count for your table.
-  $header = array(t('Name'), t('Label'), t('Weight'));
-
-  // We also need to pass the drupal_add_tabledrag() function an id which will
-  // be used to identify the <table> element containing our tabledrag form.
-  // Because an element's 'id' should be unique on a page, make sure the value
-  // you select is NOT the same as the form ID used in your form declaration.
-  $table_id = 'panel-items-table';
-
-  // We can render our tabledrag table for output.
-  $output = theme('table', array(
-    'header' => $header,
-    'rows' => $rows,
-    'attributes' => array('id' => $table_id),
-  ));
-
-  // And then render any remaining form elements (such as our submit button).
-  //$output .= drupal_render_children($element);
-
-  // We now call the drupal_add_tabledrag() function in order to add the
-  // tabledrag.js goodness onto our page.
-  //
-  // For a basic sortable table, we need to pass it:
-  // - the $table_id of our <table> element,
-  // - the $action to be performed on our form items ('order'),
-  // - a string describing where $action should be applied ('siblings'),
-  // - and the class of the element containing our 'weight' element.
-  drupal_add_tabledrag($table_id, 'order', 'sibling', 'panel-item-weight');
+  // Call to the drupal_add_tabledrag
+  drupal_add_tabledrag($table_id, 'order', 'sibling', 'tripal_panel-item-weight');
 
   return $output;
 }
@@ -503,5 +494,86 @@ function tripal_fields_layout_theme($existing, $type, $theme, $path) {
     'tripal_fields_layout_form_draggable_panel_table' => array(
       'render element' => 'element',
     ),
+    'tripal_fields_layout_generic' => array(
+      'render element' => 'element',
+      'template' => 'tripal_fields_layout_generic',
+      'path' => "$path/theme/templates",
+    ),
   );
+}
+
+/**
+ * Implements hook_node_view
+ * 
+ * @param unknown $node
+ * @param unknown $view_mode
+ * @param unknown $langcode
+ */
+function tripal_fields_layout_entity_view($entity, $type, $view_mode, $langcode) {
+  //dpm(array($entity, $type, $view_mode, $langcode));
+  switch ($type) {
+    case 'BioData':
+      // Use the generic template to render the fields
+      if ($view_mode == 'full') {
+
+        $bundle = db_select('tripal_bundle', 'tb')
+          ->fields('tb', array('id', 'bundle', 'label'))
+          ->condition('bundle', $entity->bundle)
+          ->execute()
+          ->fetchObject();
+        
+        _tripal_fields_layout_check_default_field_panels ($bundle);
+        
+        $results = db_select ('tripal_panels', 'tp')
+          ->fields('tp', array('panel_id','name', 'label'))
+          ->condition('bundle_id', $bundle->id)
+          ->execute();
+        
+        $panels = array();
+        $fields = array();
+        $disabled_panel_id = 0;
+        foreach ($results AS $row) {
+          if ($row->name == 'te_disabled') {
+            $disabled_panel_id = $row->panel_id;
+          } else {
+            $panels[$row->panel_id] = $row->label;
+            $fields[$row->panel_id] = array();
+          }
+        }
+
+        // Organize fields into panels
+        $fields = array();
+        foreach (element_children($entity->content) as $field_name) {
+          $field_instance = field_info_instance ($type, $field_name, $entity->bundle);
+          // Get default panel_id
+          $default_panel_id = db_select('tripal_panels', 'tp')
+            ->fields('tp', array('panel_id'))
+            ->condition('bundle_id', $bundle->id)
+            ->condition('name', 'te_base')
+            ->execute()
+            ->fetchField();
+          // Get panel_id for the field
+          $panel_id = db_select('tripal_panel_fields', 'tpf')
+            ->fields('tpf', array('panel_id'))
+            ->condition('field_id', $field_instance['id'])
+            ->execute()
+            ->fetchField();
+          $panel_id = $panel_id ? $panel_id : $default_panel_id;
+          // Do not show disabled fields
+          if ($panel_id != $disabled_panel_id) {
+            $fields[$panel_id][$field_name] = $entity->content[$field_name];
+          }
+          // Unset the field
+          unset ($entity->content[$field_name]);
+        }
+        $entity->content['tripal_fields_layout_generic'] = array(
+          '#theme' => 'tripal_fields_layout_generic',
+          '#panels' => $panels,
+          '#fields' => $fields,
+          '#weight' => -100,
+        );
+      }
+      
+      break;
+  }
 }