| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Rougin\Combustor\Generator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Rougin\Combustor\Common\Tools; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * View Generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Generates CodeIgniter-based views. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package Combustor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author  Rougin Gutib <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | class ViewGenerator extends BaseGenerator implements GeneratorInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * Prepares the data before generation. | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * @param  array $data | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 9 |  |     public function prepareData(array &$data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $bootstrap = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 9 |  |             'button' => 'btn btn-default', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 6 |  |             'buttonPrimary' => 'btn btn-primary', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 6 |  |             'formControl' => 'form-control', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 6 |  |             'formGroup' => 'form-group col-lg-12 col-md-12 col-sm-12 col-xs-12', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 6 |  |             'label' => 'control-label', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 6 |  |             'table' => 'table table table-striped table-hover', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             'textRight' => 'text-right' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 6 |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 9 |  |         if ($data['isBootstrap']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 9 |  |             $data['bootstrap'] = $bootstrap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 9 |  |         $data['camel'] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 9 |  |         $data['underscore'] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 9 |  |         $data['foreignKeys'] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 9 |  |         $data['primaryKeys'] = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 9 |  |         $data['plural'] = plural($data['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 9 |  |         $data['singular'] = singular($data['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 9 |  |         $primaryKey = 'get_' . $this->describe->getPrimaryKey($data['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 9 |  |         $data['primaryKey'] = $primaryKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 9 |  |         if ($this->data['isCamel']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 6 |  |             $data['primaryKey'] = camelize($data['primaryKey']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 4 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 9 |  |         $data['columns'] = $this->describe->getTable( | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 | 9 |  |             $data['name'] | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 6 |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 9 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * Generates set of code based on data. | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 9 |  |     public function generate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 9 |  |         $this->prepareData($this->data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 9 |  |         foreach ($this->data['columns'] as $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 9 |  |             $field = strtolower($column->getField()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 9 |  |             $this->data['camel'][$field] = $this->transformField( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 9 |  |                 $field, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 3 |  |                 'camelize' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 6 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 9 |  |             $this->data['underscore'][$field] = $this->transformField( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 9 |  |                 $field, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |                 'underscore' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 6 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 9 |  |             if ($column->isForeignKey()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 9 |  |                 $referencedTable = Tools::stripTableSchema( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 9 |  |                     $column->getReferencedTable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 6 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 9 |  |                 $this->data['foreignKeys'][$field] = plural($referencedTable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 9 |  |                 $singular = $field . '_singular'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 9 |  |                 $this->data['foreignKeys'][$singular] = singular( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  |                     $referencedTable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 6 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 9 |  |                 $this->data = $this->getPrimaryKey( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 9 |  |                     $this->data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 9 |  |                     $field, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 3 |  |                     $referencedTable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 6 |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 6 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 6 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 | 9 |  |         return $this->data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  |