| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Tinyissue package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) Mohamed Alsharaf <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Tinyissue\Form\Former\Fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Former\Form\Fields\Checkbox; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * CheckboxButton is a Former field class for converting checkboxes to buttons with custom colors for each. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @author Mohamed Alsharaf <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class CheckboxButton extends Checkbox | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * Render radio buttons. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function render() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         return '<div class="btn-toolbar checkbox-btn" data-toggle="buttons">' . parent::render() . '</div>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * Render checkable radio button. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param array|string $item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param int          $fallbackValue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 41 |  |  |     protected function createCheckable($item, $fallbackValue = 1) | 
            
                                                        
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 43 |  |  |         // Make sure the parent class will create inline radios | 
            
                                                        
            
                                    
            
            
                | 44 |  |  |         $this->inline = true; | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |         $this->grouped(); | 
            
                                                        
            
                                    
            
            
                | 46 |  |  | //        $this= | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         // Extract the color for this button/radio & unset it to prevent creating attribute | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |         $color = $item['attributes']['color']; | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |         unset($item['attributes']['color']); | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         // Color for selected button/radio | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         $selectedColor = ';color:' . $color . ';'; | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         // Data attribute for the button color | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         $item['attributes']['data-color'] = $color; | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         // Generate the radio button | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         $item = parent::createCheckable($item, $fallbackValue); | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         // If property checked found, then make the button selected with styles | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         if (strpos($item, 'checked') !== false) { | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             $selectedColor = ';background:' . $color . ';color:white;'; | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         // Add bootstrap classes for styling the buttons | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         $item = str_replace('inline', 'btn btn-default', $item); | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |         // Add styles to the wrapper label tag | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |         $style = 'border-color: ' . $color . $selectedColor; | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |         $item  = str_replace('<label', '<label style="' . $style . '"', $item); | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |         return $item; | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |  |