<?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.01
Requires at least: 2.06
*/

// CAVEATS:
//   - it is not currently possible to hide Separator fields.
?>
<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
 require_once "/hsphere/local/home/c341633/qartguide.com/cmsAdmin/lib/viewer_functions.php";
  
  // load records
  list($subscriber_level_valuesRecords, $subscriber_level_valuesMetaData) = getRecords(array(
    'tableName'   => 'subscriber_level_values',
    'where'       => whereRecordNumberInUrl(1),
    'limit'       => '1',
  ));
  $subscriber_level_valuesRecord = @$subscriber_level_valuesRecords[0]; // get first record

?>
<?php 

// UPDATE THESE VALUES

$GLOBALS['BETA_USER_SKILL_FIELDS_SKILL_FIELD_NAME'] = 'allowedFolders';
 
// For each section, copy this block
$GLOBALS['BETA_USER_SKILL_FIELDS_CONFIG']['master_subscription_pages'] = array(
  
  // user's skill values and lists of fields to hide
  
 'entry_level'   => explode("\t", trim($subscriber_level_valuesRecord['entry_level_hide'], "\t")),
 'silver'   => explode("\t", trim($subscriber_level_valuesRecord['silver_level_hide'], "\t")),
 'gold'   => explode("\t", trim($subscriber_level_valuesRecord['gold_level_hide'], "\t")),
 'platinum'   => explode("\t", trim($subscriber_level_valuesRecord['platinum_level_hide'], "\t"))
);


$GLOBALS['BETA_USER_SKILL_FIELDS_CONFIG']['custom_branding'] = array(
  
  // user's skill values and lists of fields to hide
  
  'entry_level' => array('__separator001__','__separator002__','masthead', 'generic_map_url', 'generic_map_link_text','generic_vcard'),
  'silver' => array('__separator001__','__separator002__','masthead', 'generic_map_url', 'generic_map_link_text','generic_vcard')
  
  
);
//showme($GLOBALS['BETA_USER_SKILL_FIELDS_CONFIG']['master_subscription_pages']);
// DON'T UPDATE ANYTHING BELOW THIS LINE

// register hooks
addFilter('edit_buttonsRight', '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; }
  
  ?>
  <script>
    $(function(){
      <?php foreach($hiddenFields as $hiddenField): ?>
        $('*[name="<?php  echo $hiddenField;  ?>"]').closest('TR').hide();
        $('*[name^="<?php echo $hiddenField; ?>:"]').closest('TR').hide();
        $('#<?php echo $hiddenField;     ?>_iframe').closest('TR').hide();
    <?php endforeach; ?>
    });
  </script>
  <?php
  
  return $html;
}

?>
