| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Nayjest\Grids; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | class Filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     /** @var FilterConfig */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** @var FieldConfig */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @param FilterConfig $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @param FieldConfig $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @param Grid $grid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         FilterConfig $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         FieldConfig $column, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         Grid $grid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->config = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->column = $column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->grid = $grid; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Returns input name for the filter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     public function getInputName() | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         $key = $this->grid->getInputProcessor()->getKey(); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $name = $this->config->getId(); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         return "{$key}[filters][{$name}]"; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * Returns filter configuration. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return FilterConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function getConfig() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return $this->config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Returns filters value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 59 |  | View Code Duplication |     public function getValue() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $from_input = $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ->grid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ->getInputProcessor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ->getFilterValue($this->config->getId()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if ($from_input === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return $this->config->getDefaultValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return $from_input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Renders filtering control. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function render() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $data = $this->grid->getViewData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $data['column'] = $this->column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $data['filter'] = $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $data['label'] = $this->config->getLabel(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return View::make( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->getTemplate(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         )->render(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Returns name of template for filtering control. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     protected function getTemplate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $filter_tpl = $this->config->getTemplate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $grid_tpl = $this->grid->getConfig()->getTemplate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         return str_replace('*.', "$grid_tpl.filters.", $filter_tpl); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * Applies filtering to data source. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function apply() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $value = $this->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if (null === $value || '' === $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if ($func = $this->config->getFilteringFunc()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $func($value, $this->grid->getConfig()->getDataProvider()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $operator = $this->config->getOperator(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         if ($operator === FilterConfig::OPERATOR_LIKE || $operator === FilterConfig::OPERATOR_LIKE_R) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             // Search for non-escaped wildcards | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             $found = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             for ($i = 0; $i < mb_strlen($value); $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 if (in_array(mb_substr($value, $i, 1), ['%', '_']) && $i > 0 && mb_substr($value, $i - 1, 1) != '\\') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     $found = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             // If none are found, insert wildcards to improve user experience | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             if (!$found) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 if ($operator === FilterConfig::OPERATOR_LIKE) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     $value = "%$value%"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 } else if ($operator === FilterConfig::OPERATOR_LIKE_R) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     $value .= '%'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $this->grid->getConfig()->getDataProvider()->filter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             $this->config->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             $this->config->getOperator(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 139 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 140 |  |  |  | 
            
                        
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: