<?php
/*
Plugin Name: Show/Hide Fields For Users
Description: Allows customization of which fields appear depending on the current user's 'skill_level' field.
Author: Chris
Version: 0.02
Requires at least: 3.00
*/

// CAVEATS:
//   - it is not currently possible to hide Separator fields.

// UPDATE THESE VALUES

$GLOBALS['BETA_USER_SKILL_FIELDS_SKILL_FIELD_NAME'] = 'skill_level';

// For each section, copy this block
$GLOBALS['BETA_USER_SKILL_FIELDS_CONFIG']['my_section'] = array(
  
  // user's skill values and lists of fields to hide
  'Beginner' => array('expert_field_1', 'expert_field_2'),
  'Expert'   => array()
  
);

// DON'T UPDATE ANYTHING BELOW THIS LINE

// register hooks
addFilter('adminUI_args', 'beta_user_skill_fields_edit_filter', null, 3);

// add javascript to edit pages
function beta_user_skill_fields_edit_filter($html, $tablename, $record) {
  global $CURRENT_USER;
  $hiddenFields = @$GLOBALS['BETA_USER_SKILL_FIELDS_CONFIG'][$tablename][@$CURRENT_USER[$GLOBALS['BETA_USER_SKILL_FIELDS_SKILL_FIELD_NAME']]];
  if (!$hiddenFields) { return $html; }
  
  $html['PRE_FORM_HTML'] .= ob_capture(function() use($hiddenFields){ ?>
    <script>
      $(function(){
        <?php foreach($hiddenFields as $hiddenField): ?>
          $('[name="<?php  echo $hiddenField;  ?>"]')   .closest('.form-group').hide();
          $('[name^="<?php echo $hiddenField; ?>:"]')   .closest('.form-group').hide(); // hide date fields, ie: date:mon, date:day, and date:year
          $('[name^="<?php echo $hiddenField; ?>[]"]')  .closest('.form-group').hide(); // hide multi-checkboxes field
          $('#<?php echo $hiddenField;     ?>_iframe')  .closest('.form-group').hide(); // hide upload field
      <?php endforeach; ?>
      });
    </script>
  <?php });
  
  return $html;
}


// EOF
