Need assistance...wanting to modify Chris' greyHiddenRecords to redSoldRecords

6 posts by 4 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: July 18, 2017   (RSS)

By Codee - January 30, 2017

Hello IT,

I've been using Chris' greyHiddenRecords plugin for some time. I wanted to add a similar function in order to turn any sold records on the list page to the color red. So, grey for hidden records, and red for sold records.  I've modified Chris' greyHiddenRecords plugin to the following (below) and the function works EXCEPT when the greyHiddenRecords plugin is also activated.  When the greyHiddenRecords plugin is activated (at the same time the redSoldRecords plugin is activated) then only the hidden records turn color (grey) and the sold records stay normal.

modified code for red sold records:

$GLOBALS['redRecordFieldNamesToOptions'] = [];

// Copy this block to add more fields to red-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['redRecordFieldNamesToOptions']['sold'] = [             // field name
                                              'value'      => '1',  // value to compare the actual field value to. ie: if ($record['sold'] == $GLOBALS['redRecordFieldNamesToOptions']['sold']['value']){}
                                              'comparator' => '==', // comparison operators - accepted values: "==", "===", "!=", "<>", "!==", "<", ">", "<=", ">="
                                              'tableName'  => 'gallery',   // optional: table name where the target field exists. If not set, all the tables where the target field exists will be redded-out if the condition is met
                                            ];

/* Example: red-out if 'date' is in the past
$GLOBALS['redRecordFieldNamesToOptions']['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',      'red_sold_records_listRow_trStyle', null, 3);

function red_sold_records_listRow_trStyle($trStyle, $tableName, $record) {
 
  foreach($GLOBALS['redRecordFieldNamesToOptions'] 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 = red_sold_records_isMatched($record[$fieldName], $conditions['value'], $conditions['comparator']);
    if ($isMatched){
      return "color: #900";
    }
  }
 
}

function red_sold_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;
}

?>
-------------------------------------------------------------------

Chris' original code

$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;
}

?>

----------------------------------------------------------------

What can I change in order to have both plugins working at the same time?

Thanks!

By ross - January 31, 2017

Hi equinox

Thanks for posting.

This will be a conversation we can have through consulting.

You can email me via consulting@interactivetools.com to continue it there.

In the meantime, let's leave the thread going though and see if anyone in the community has any suggestions.

Thanks.

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By wizzle - July 15, 2017 - edited: July 15, 2017

Any luck equinox?

I found myself trying to do the same thing.  What I realized is that both the greyHiddenRecords and and redSoldRecords plugins where altering the same Core Plugin Hook called listRow_trStyle

What it seems like to me is that with the updated plugin, you can now add additonal conditions such as 

$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
                                          ];

but there is not a separate text formatting for those conditions.  Basically any record meeting any of the conditions will change the listRow_trStyle to the same thing.

Looking at the file   /cmsb/lib/menus/default/list_functions.php you will see this at line 139

 $trStyle = applyFilters('listRow_trStyle', '', $tableName, $record);

and then this just a few lines down

print "<tr class='$trClass' style='$trStyle'>\n";
displayListColumns($listFields, $record, $options);
print "</tr>\n";

So it would appear that there is a single listRow_trSyle hook that is Global and both of your plugins were modifying it.  I suppose whichever plugin is processed 2nd wins.

Greg Williams

By Dave - July 18, 2017

Hi Guys, 

I'd recommend just making a custom plugin for what you want to do.  This is one of those cases where the pre-made plugin doesn't really make it much simpler than the custom code.  Here's some custom plugin code for setting the text color:

addFilter('listRow_trStyle', 'v203_highlightAdminUsers', null, 3);

// admin record lists: update style of <tr> rows
function v203_highlightAdminUsers($trStyle, $tableName, $record) {

  // accounts: add style to <tr> rows if record has 'isAdmin' checked
  if ($tableName == 'accounts' && $record['isAdmin']) {
    $trStyle = "color: red;";
  }

  return $trStyle;
}

You just want an if, elseif chain so that you can control which one goes first.

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By Codee - July 18, 2017

Hi Dave,

Thank you for providing that info and also for the explanation and insight so I could learn better.

Cheers!