<?php
/*
Plugin Name: Show/Hide Dependant Fields
Description: Allows customization of which fields appear when certain list options are selected.
Author: Chris
Version: 0.01
Requires at least: 2.06
*/

// CAVEATS:
//   - it is not currently possible to hide Separator fields.
//   - upload fields do not work well: they will end up being enormously tall.

// UPDATE THESE VALUES

// For each section and list field, copy this block 
// Fields will start hidden if they appear in any "fields to show" list  
$GLOBALS['SHOWHIDE_DEPENDANT_FIELDS_CONFIG']['test']['select'] = array( 
// list option => fields to show 
  ''           => array(), // show these fields when the topmost '<select>' option is selected 
  'simple'      => array('simple1,simple2'), 
  'complex'       => array('complex1,complex2'), 
);

// DON'T UPDATE ANYTHING BELOW THIS LINE

// register hooks
addFilter('edit_buttonsRight', 'showHideDependantFields_edit_filter', null, 3);

// prep hide rules
foreach ($GLOBALS['SHOWHIDE_DEPENDANT_FIELDS_CONFIG'] as $tableName => $tableData) {
  foreach ($tableData as $fieldName => $fieldData) {
    $GLOBALS['SHOWHIDE_DEPENDANT_FIELDS_HIDE'][$tableName] = array();
    foreach ($fieldData as $listValue => $fieldsToShow) {
      foreach ($fieldsToShow as $fieldToShow) {
        $GLOBALS['SHOWHIDE_DEPENDANT_FIELDS_HIDE'][$tableName][$fieldToShow] = true;
      }
    }
  }
}

// add javascript to edit pages
function showHideDependantFields_edit_filter($html, $tablename, $record) {
  $showRules = @$GLOBALS['SHOWHIDE_DEPENDANT_FIELDS_CONFIG'][$tablename];
  if (!$showRules) { return $html; }
  $hideRules = array_keys(@$GLOBALS['SHOWHIDE_DEPENDANT_FIELDS_HIDE'][$tablename]);
  ?>
  <script>
    var showHideDependantFields_showRules = <?php echo json_encode($showRules, true); ?>;
    var showHideDependantFields_hideRules = <?php echo json_encode($hideRules, true); ?>;
    $(function(){
      for (var fieldName in showHideDependantFields_showRules) {
        $('SELECT[name="' + fieldName + '"]').change(showHideDependantFields_update);
      }
      showHideDependantFields_update();
    });
    function showHideDependantFields_update() {
      // hide all
      for (i in showHideDependantFields_hideRules) {
        $('*[name="'  + showHideDependantFields_hideRules[i] +  '"]').closest('TR').hide();
        $('*[name^="' + showHideDependantFields_hideRules[i] + ':"]').closest('TR').hide();
        $('#' + showHideDependantFields_hideRules[i] +     '_iframe').closest('TR').hide();
      }
      // for each list field, show requested fields
      for (var listFieldName in showHideDependantFields_showRules) {
        var value = $('SELECT[name="' + listFieldName + '"] OPTION:selected').val();
        var fieldsToShow = showHideDependantFields_showRules[listFieldName][value];
        if (fieldsToShow) {
          for (i in fieldsToShow) {
            $('*[name="'  + fieldsToShow[i] +  '"]').closest('TR').show();
            $('*[name^="' + fieldsToShow[i] + ':"]').closest('TR').show();
            $('#' + fieldsToShow[i] +     '_iframe').closest('TR').show();
          }
        }
      }
    }
  </script>
  <?php

  return $html;
}

?>
