Browse Source

Fixed Tripal Panel. Allowed users to add/remove/order panels and re-oragnize fields into a new panel.

Chun-Huai Cheng 9 năm trước cách đây
mục cha
commit
7c8a184f0a
1 tập tin đã thay đổi với 59 bổ sung39 xóa
  1. 59 39
      tripal_fields_layout/tripal_fields_layout.module

+ 59 - 39
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,9 +302,7 @@ 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.
-
+  dpm(array($row['human_name']['#markup'] => $row,$panel => 'panel'));
   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(
@@ -436,23 +453,23 @@ function theme_tripal_fields_layout_form_draggable_panel_table ($variables) {
     // 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');
+    $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;
+    $element[$id]['remove']['#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.
     $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']),
+        // Add remove column
+        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'
@@ -464,20 +481,23 @@ function theme_tripal_fields_layout_form_draggable_panel_table ($variables) {
 
   // 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'));
+  $header = array(t('Label'), t('Weight'), t('Action'));
 
   // 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';
+  $table_id = 'tripal_panel-arrange_panel_table';
 
   // We can render our tabledrag table for output.
-  $output = theme('table', array(
-    'header' => $header,
-    'rows' => $rows,
-    'attributes' => array('id' => $table_id),
-  ));
+  $output = '';
+  if (count($rows) > 0) {
+    $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);
@@ -490,7 +510,7 @@ function theme_tripal_fields_layout_form_draggable_panel_table ($variables) {
   // - 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');
+  drupal_add_tabledrag($table_id, 'order', 'sibling', 'tripal_panel-item-weight');
 
   return $output;
 }