| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Created by shalvah | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Date: 9/1/17 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Time: 12:10 PM | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace bedezign\yii2\audit\components\panels; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Trait RendersSummaryChartTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package bedezign\yii2\audit\components\panels | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Used by audit panels or controllers which want to render a summary chart in their view | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | trait RendersSummaryChartTrait | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * The name of the model for which the chart should be rendered | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @return string Fully namespaced class name | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected function getChartModel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         return static::className(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * Return audit data for the last seven days | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * to be rendered on the chart | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected function getChartData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         //initialise defaults (0 entries) for each day | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $defaults = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $startDate = strtotime('-6 days'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         foreach (range(-6, 0) as $day) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             $defaults[date('D: Y-m-d', strtotime($day . 'days'))] = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $panelModel = $this->getChartModel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $results = $panelModel::find() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ->select(["COUNT(DISTINCT id) as count", "created AS day"]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             ->where(['between', 'created', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 date('Y-m-d 00:00:00', $startDate), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 date('Y-m-d 23:59:59')]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->groupBy("day")->indexBy('day')->column(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         // replace defaults with data from db where available | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         foreach ($results as $date => $count) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $date = date('D: Y-m-d', strtotime($date)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             $defaults[$date] += $count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         return $defaults; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 58 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |  |