| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Backpack\CRUD\app\Models\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  | trait HasIdentifiableAttribute | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |      * Get the name of the attribute that best defines the entry, from the user perspective. | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |      * Rephrased: In most cases a user will NOT identify an Article because its ID is "4", but | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      * because its name is "10 Ways to Burn Fat". This method returns the column in the database | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      * that represents what is better to show to the user as an identifier rather than the ID. | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |      * Ex: name, title, label, description etc. | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @return string The name of the column that best defines this entry from the user perspective. | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function identifiableAttribute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         if (property_exists($this, 'identifiableAttribute')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             return $this->identifiableAttribute; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 25 |  |  |         return static::guessIdentifiableColumnName(); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * Get the most likely column in the db table that could be used as an identifiable attribute. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @return string The name of the column in the database that is most likely to be a good identifying attribute. | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private static function guessIdentifiableColumnName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $instance = new static(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $connection = $instance->getConnectionWithExtraTypeMappings(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $table = $instance->getTableWithPrefix(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $columnNames = app('DatabaseSchema')->listTableColumnsNames($connection->getName(), $table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $indexes = app('DatabaseSchema')->listTableIndexes($connection->getName(), $table); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         // these column names are sensible defaults for lots of use cases | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $sensibleDefaultNames = ['name', 'title', 'description', 'label']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         // if any of the sensibleDefaultNames column exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         // that's probably a good choice | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         foreach ($sensibleDefaultNames as $defaultName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             if (in_array($defaultName, $columnNames)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 return $defaultName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         // if none of the sensible defaults exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         // we get the first column from database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         // that is NOT indexed (usually primary, foreign keys) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         foreach ($columnNames as $columnName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             if (! in_array($columnName, $indexes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 //check for convention "field<_id>" in case developer didn't add foreign key constraints. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 if (strpos($columnName, '_id') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 return $columnName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         // in case everything fails we just return the first column in database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $firstColumnInTable = Arr::first($columnNames); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         if (! empty($firstColumnInTable)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             return $firstColumnInTable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // if there are no columns in the table, we need to throw an exception as there is nothing we can use to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         // correlate with the entry. Developer need to tell Backpack what attribute to use. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |         throw new \Exception("There are no columns in the table «{$table}». Please add a column to the table or define a 'public function identifiableAttribute()' in the model."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  |