<?php
/*
Plugin Name: Grey Hidden Records
Author: chris@interactivetools.com
Description: Changes the colour of records on the list page to grey if their "hidden" field is checked. Includes option to add specific fields with custom conditions for greying out records. Examples, if "date" is older than today, if "task" is complete.
Version: 1.01
Requires at least: 3.00
*/

$GLOBALS['greyRecordFieldNamesToOptions'] = [];

// Copy this block to add more fields to grey-out
// .. order by priority field to check - if the first field's condition is true, it will not check for the other fields anymore
$GLOBALS['greyRecordFieldNamesToOptions']['hidden'] = [             // field name
                                              'value'      => '1',  // value to compare the actual field value to. ie: if ($record['hidden'] == $GLOBALS['greyRecordFieldNamesToOptions']['hidden']['value']){}
                                              'comparator' => '==', // comparison operators - accepted values: "==", "===", "!=", "<>", "!==", "<", ">", "<=", ">="
                                              'tableName'  => '',   // optional: table name where the target field exists. If not set, all the tables where the target field exists will be greyed-out if the condition is met
                                            ];

/* Example: grey-out if 'date' is in the past
$GLOBALS['greyRecordFieldNamesToOptions']['date'] = [
                                            'value'      => date("Y-m-d H:i:s"), // date today
                                            'comparator' => '<', // compare if this is a date in the past
                                            'tableName'  => 'sample_table', // grey-out the record only on this table/section list page
                                          ];
*/


// DO NOT UPDATE ANYTHING BELOW
// register callbacks
addFilter('listRow_trStyle',      'grey_hidden_records_listRow_trStyle', null, 3);

function grey_hidden_records_listRow_trStyle($trStyle, $tableName, $record) {
  
  foreach($GLOBALS['greyRecordFieldNamesToOptions'] as $fieldName => $conditions){
    
    if (is_null(@$record[$fieldName])) { continue; } // skip if the field doesn't exists on this record
    if (@$conditions['tableName'] && @$conditions['tableName'] != $tableName) { continue; } // skip if the target table/section is set but doesn't match the record's tableName
    
    //
    $isMatched = grey_hidden_records_isMatched($record[$fieldName], $conditions['value'], $conditions['comparator']);
    if ($isMatched){
      return "color: #CCC";
    }
  }
  
}

function grey_hidden_records_isMatched($fieldValue, $valueToMatch, $comparator){
  $isMatched = false;
  switch($comparator) {
    case "==":  $isMatched = $fieldValue ==  $valueToMatch; break;
    case "===": $isMatched = $fieldValue === $valueToMatch; break;
    case "!=":  $isMatched = $fieldValue !=  $valueToMatch; break;
    case "<>":  $isMatched = $fieldValue <>  $valueToMatch; break;
    case "!==": $isMatched = $fieldValue !== $valueToMatch; break;
    case "<":   $isMatched = $fieldValue <   $valueToMatch; break;
    case ">":   $isMatched = $fieldValue >   $valueToMatch; break;
    case "<=":  $isMatched = $fieldValue <=  $valueToMatch; break;
    case ">=":  $isMatched = $fieldValue >=  $valueToMatch; break;
  }
  return $isMatched;
}

?>
