| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\GridFieldAddOns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Core\Convert; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\View\Requirements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Forms\GridField\GridField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Forms\GridField\GridField_ColumnProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class GridFieldRecordHighlighter implements GridField_ColumnProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $alerts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     function __construct($alerts) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $this->alerts = $alerts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Add extra fields to the column list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param GridField $gridField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param array - List reference of all column names. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function augmentColumns($gridField, &$columns) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         array_unshift($columns, 'Alerts'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * List of handled columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param GridField $gridField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function getColumnsHandled($gridField) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return array('Alerts'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * Set titles for the column header | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param GridField $gridField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @param string $columnName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return array - Map of arbitrary metadata identifiers to their values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function getColumnMetadata($gridField, $columnName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return array('title' => ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Return a formfield for the extra field column or an edit button for the actions column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param  GridField $gridField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param  DataObject $record - Record displayed in this row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param  string $columnName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return string - HTML for the column. Return NULL to skip. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function getColumnContent($gridField, $record, $columnName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         Requirements::javascript('i-lateral/silverstripe-gridfield-addons:javascript/GridFieldRecordHighlighter.js'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         Requirements::css('i-lateral/silverstripe-gridfield-addons:css/GridFieldRecordHighlighter.css'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $alerts = $this->getAlerts($record); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $content = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         foreach ($alerts as $alert) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             if ($alert['status'] === 'alert') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $icon_class = "font-icon-attention"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } elseif ($alert['status'] == 'info') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $icon_class = "font-icon-info-circled"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 $icon_class = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $content[] = "<span class=\"ss-gridfield-alert {$icon_class}\" title=\"{$alert['message']}\"></span>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         return implode($content); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Generate HTML attributes for each individual cells as selectors for CSS and JS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param  GridField $gridField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param  DataObject $record displayed in this row | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param  string $columnName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     public function getColumnAttributes($gridField, $record, $columnName) | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         $attr = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         foreach ($this->getAlerts($record) as $alert) { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             if ($alert['status'] == 'alert') { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                 $attr = array('class' => 'ss-gridfield-highlight', 'data-highlight-status' => 'error'); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             } elseif (empty($attr)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |                 $attr = array('class' => 'ss-gridfield-highlight', 'data-highlight-status' => 'highlight'); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         return $attr; | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     function getAlerts($record) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $alerts = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         foreach ($this->alerts as $getter => $rule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $actualvalue = $record->hasField($getter) ? $record->$getter : $record->$getter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             foreach ($rule['patterns'] as $nominalvalue => $return) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 if (($rule['comparator'] == 'equal' &&          $actualvalue == $nominalvalue) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     ($rule['comparator'] == 'equalstrict' &&    $actualvalue === $nominalvalue) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     ($rule['comparator'] == 'unequal' &&        $actualvalue != $nominalvalue) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                     ($rule['comparator'] == 'unequalstrict' &&  $actualvalue !== $nominalvalue) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                     ($rule['comparator'] == 'greater' &&        $actualvalue > $nominalvalue) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                     ($rule['comparator'] == 'greaterorequal' && $actualvalue >= $nominalvalue) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                     ($rule['comparator'] == 'less' &&           $actualvalue < $nominalvalue) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     ($rule['comparator'] == 'lessorequal' &&    $actualvalue <= $nominalvalue) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                     ($rule['comparator'] == 'beginwith' &&      strtolower(substr($actualvalue, 0, strlen($nominalvalue))) == strtolower($nominalvalue)) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     ($rule['comparator'] == 'endwith' &&        strtolower(substr($actualvalue, -1 * strlen($nominalvalue))) == strtolower($nominalvalue)) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                     ($rule['comparator'] == 'contain' &&        stripos($actualvalue, $nominalvalue) !== false) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                     ($rule['comparator'] == 'regex' &&          preg_match($nominalvalue, $actualvalue)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                     $alerts[$getter] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                         'status' => $return['status'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                         'message' => sprintf($return['message'], Convert::raw2xml($nominalvalue), Convert::raw2xml($actualvalue)), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         return $alerts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 144 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 145 |  |  |  | 
            
                        
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.