Have been struggling for a few hours on a basic LogicHook to highlight overdue dates in List View.
I followed the instructions here and that worked very nicely.
Then I hoped it would be a simple case of;
if($bean->due_date < $now) {
//highlight red
}
However, $bean->due_date is in the format d/m/Y - as per my user settings - and I know that this will change for another user so I can’t rely on this being consistent.
I think the closest I got was using:
$due_date_time = new DateTime();
$due_date_time = $timedate->fromUser($bean->due_date, $current_user);
But then I get the error:
[ERROR] fromUser: Conversion of 20/09/2021 from user format d/m/Y H:i failed
Any ideas on how I can proceed?
Maybe I can go and get the date from the database instead of relying on $bean->due_date, but this is where my skills ran out.
Many thanks in advance.
Will share final solution here for others to use once I’ve managed it.
<?php
//prevents directly accessing this file from a web browser
if (!defined('sugarEntry') ||!sugarEntry) die('Not A Valid Entry Point');
class HighlightOverdueLogicHook{
public function highlightOverdue(SugarBean $bean, $event, $arguments){
//If due date in past then highlight it red, otherwise highlight blue
$due_date = $GLOBALS['timedate']->fromString($bean->due_date);
//LoggerManager::getLogger()->debug($due_date);
global $timedate;
$yesterday = $timedate->getNow();
$yesterday->sub(new DateInterval('PT24H'));
//LoggerManager::getLogger()->debug($yesterday);
if($due_date < $yesterday) {
$bean->due_date = "<div style='border: solid 3px red'>".$bean->due_date."</div>";
} else {
$bean->due_date = "<div style='border: solid 3px blue'>".$bean->due_date."</div>";
}
}
}
Anybody wishing to implement this code will need to read this first:
The code above replaces the second example block, with the first example block modified appropriately for the class, function and module names.