| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Backpack\CRUD\app\Library\CrudPanel\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Backpack\CRUD\app\Library\CrudPanel\CrudColumn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | trait Columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     use ColumnsProtectedMethods; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     // ------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     // COLUMNS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     // ------------ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * Get the CRUD columns for the current operation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @return array CRUD columns. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function columns() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         return $this->getOperationSetting('columns') ?? []; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Add a bunch of column names and their details to the CRUD object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param  array|string  $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function setColumns($columns) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         // clear any columns already set | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->removeAllColumns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         // if array, add a column for each of the items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         if (is_array($columns) && count($columns)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             foreach ($columns as $key => $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 // if label and other details have been defined in the array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 if (is_array($column)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     $this->addColumn($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                     $this->addColumn([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                         'name'  => $column, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                         'label' => mb_ucfirst($column), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                         'type'  => 'text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if (is_string($columns)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $this->addColumn([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 'name'  => $columns, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |                 'label' => mb_ucfirst($columns), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 'type'  => 'text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Add a column at the end of to the CRUD object's "columns" array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param  array|string  $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function addColumn($column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $column = $this->makeSureColumnHasNeededAttributes($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->addColumnToOperationSettings($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * Add multiple columns at the end of the CRUD object's "columns" array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param  array  $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function addColumns($columns) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         if (count($columns)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             foreach ($columns as $key => $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 $this->addColumn($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Move the most recently added column after the given target column. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param  string|array  $targetColumn  The target column name or array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function afterColumn($targetColumn) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->moveColumn($targetColumn, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * Move the most recently added column before the given target column. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param  string|array  $targetColumn  The target column name or array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function beforeColumn($targetColumn) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $this->moveColumn($targetColumn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * Move this column to be first in the columns list. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return bool|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function makeFirstColumn() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         if (! $this->columns()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $firstColumn = array_keys(array_slice($this->columns(), 0, 1))[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->beforeColumn($firstColumn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * Add the default column type to the given Column, inferring the type from the database column type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @param  array  $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @return array|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     public function addDefaultTypeToColumn($column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         if (array_key_exists('name', (array) $column)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $default_type = $this->inferFieldTypeFromDbColumnType($column['name']); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             return array_merge(['type' => $default_type], $column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * Remove a column from the CRUD panel by name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      * @param  string  $columnKey  The column key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     public function removeColumn($columnKey) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $columnsArray = $this->columns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         Arr::forget($columnsArray, $columnKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         $this->setOperationSetting('columns', $columnsArray); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * Remove multiple columns from the CRUD panel by name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @param  array  $columns  Array of column names. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     public function removeColumns($columns) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         if (! empty($columns)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             foreach ($columns as $columnKey) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                 $this->removeColumn($columnKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * Remove all columns from the CRUD panel. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     public function removeAllColumns() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         $this->setOperationSetting('columns', []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * Remove an attribute from one column's definition array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @param  string  $column  The name of the column. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      * @param  string  $attribute  The name of the attribute being removed. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     public function removeColumnAttribute($column, $attribute) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         $columns = $this->columns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |         unset($columns[$column][$attribute]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         $this->setOperationSetting('columns', $columns); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * Change attributes for multiple columns. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      * @param  array  $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * @param  array  $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     public function setColumnsDetails($columns, $attributes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         foreach ($columns as $columnKey) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |             $this->setColumnDetails($columnKey, $attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |      * Change attributes for a certain column. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |      * @param  string  $columnKey  Column key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |      * @param  array  $attributesAndValues | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |     public function setColumnDetails($columnKey, $attributesAndValues) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         $columnsArray = $this->columns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         if (isset($columnsArray[$columnKey])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |             foreach ($attributesAndValues as $attributeName => $attributeValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |                 $columnsArray[$columnKey][$attributeName] = $attributeValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |         $this->setOperationSetting('columns', $columnsArray); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |      * Alias for setColumnDetails(). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |      * Provides a consistent syntax with Fields, Buttons, Filters modify functionality. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |      * @param  string  $column  Column name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |      * @param  array  $attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |     public function modifyColumn($column, $attributes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |         $this->setColumnDetails($column, $attributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |      * Set label for a specific column. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |      * @param  string  $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |      * @param  string  $label | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |     public function setColumnLabel($column, $label) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |         $this->setColumnDetails($column, ['label' => $label]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |      * Get the relationships used in the CRUD columns. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |      * @return array Relationship names | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |     public function getColumnsRelationships() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |         $columns = $this->columns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |         return collect($columns)->pluck('entity')->reject(function ($value, $key) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |             return ! $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |         })->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |      * Order the CRUD columns. If certain columns are missing from the given order array, they will be pushed to the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |      * new columns array in the original order. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |      * @param  array  $order  An array of column names in the desired order. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |     public function orderColumns($order) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |         $orderedColumns = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |         foreach ($order as $columnName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |             if (array_key_exists($columnName, $this->columns())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |                 $orderedColumns[$columnName] = $this->columns()[$columnName]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |         if (empty($orderedColumns)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |         $remaining = array_diff_key($this->columns(), $orderedColumns); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |         $this->setOperationSetting('columns', array_merge($orderedColumns, $remaining)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |      * Get a column by the id, from the associative array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |      * @param  int  $column_number  Placement inside the columns array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |      * @return array Column details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |     public function findColumnById($column_number) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |         $result = array_slice($this->columns(), $column_number, 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |         return reset($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |      * Get the visibility priority for the actions column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |      * in the CRUD table view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |      * @return int The priority, from 1 to infinity. Lower is better. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |     public function getActionsColumnPriority() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |         return (int) $this->getOperationSetting('actionsColumnPriority') ?? 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |      * Set a certain priority for the actions column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |      * in the CRUD table view. Usually set to 10000 in order to hide it. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |      * @param  int  $number  The priority, from 1 to infinity. Lower is better. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |      * @return self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |     public function setActionsColumnPriority($number) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |         $this->setOperationSetting('actionsColumnPriority', (int) $number); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |      * Set actions column as dropdown | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |      * in the CRUD table view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |     public function displayActionsColumnAsDropdown(): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 |  |  |         $this->setOperationSetting('actionsColumnAsDropdown', true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |      * Check if a column exists, by any given attribute. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |      * @param  string  $attribute  Attribute name on that column definition array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 |  |  |      * @param  string  $value  Value of that attribute on that column definition array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 337 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 338 |  |  |     public function hasColumnWhere($attribute, $value) | 
            
                                                                        
                            
            
                                    
            
            
                | 339 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 340 |  |  |         $match = Arr::first($this->columns(), function ($column, $columnKey) use ($attribute, $value) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 341 |  |  |             return isset($column[$attribute]) && $column[$attribute] == $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 342 |  |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 343 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 344 |  |  |         return (bool) $match; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 |  |  |      * Get the first column where a given attribute has the given value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |      * @param  string  $attribute  Attribute name on that column definition array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 |  |  |      * @param  string  $value  Value of that attribute on that column definition array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |     public function firstColumnWhere($attribute, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |         return Arr::first($this->columns(), function ($column, $columnKey) use ($attribute, $value) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |             return isset($column[$attribute]) && $column[$attribute] == $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |      * The only REALLY MANDATORY attribute for a column is the 'name'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 |  |  |      * Everything else, Backpack can probably guess. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |      * This method checks that all necessary attributes are set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |      * If not, it tries to guess them. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |      * @param  string|array  $column  The column definition array OR column name as string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 |  |  |      * @return array Proper column definition array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |     public function makeSureColumnHasNeededAttributes($column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 |  |  |         $column = $this->makeSureColumnHasName($column); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 |  |  |         $column = $this->makeSureColumnHasKey($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |         $column = $this->makeSureColumnHasLabel($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 |  |  |         $column = $this->makeSureColumnHasEntity($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |         $column = $this->makeSureColumnHasModel($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |         $column = $this->makeSureColumnHasAttribute($column); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |         $column = $this->makeSureColumnHasRelationType($column); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 |  |  |         $column = $this->makeSureColumnHasType($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |         $column = $this->makeSureColumnHasPriority($column); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |         $column = $this->makeSureSubfieldsHaveNecessaryAttributes($column); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |         // check if the column exists in the database (as a db column) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 |  |  |         $column_exists_in_db = $this->hasDatabaseColumn($this->model->getTable(), $column['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 |  |  |         // make sure column has tableColumn, orderable and searchLogic | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |         $column['tableColumn'] = $column['tableColumn'] ?? $column_exists_in_db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 |  |  |         $column['orderable'] = $column['orderable'] ?? $column_exists_in_db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 |  |  |         $column['searchLogic'] = $column['searchLogic'] ?? $column_exists_in_db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 |  |  |         return $column; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 |  |  |      * Count the number of columns added so far. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |      * It will not take into account the action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 |  |  |      * columns (columns with buttons, checkbox). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 |  |  |      * @return int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 |  |  |     public function countColumnsWithoutActions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |         return collect($this->columns())->filter(function ($column, $key) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |             return ! isset($column['hasActions']) || $column['hasActions'] == false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |         })->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 |  |  |      * Create and return a CrudColumn object for that column name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  |      * Enables developers to use a fluent syntax to declare their columns, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 |  |  |      * in addition to the existing options: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |      * - CRUD::addColumn(['name' => 'price', 'type' => 'number']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 |  |  |      * - CRUD::column('price')->type('number'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 |  |  |      * And if the developer uses the CrudColumn object as Column in their CrudController: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 |  |  |      * - Column::name('price')->type('number'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  |      * @param  string  $name  The name of the column in the db, or model attribute. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 |  |  |      * @return CrudColumn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 |  |  |     public function column($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 |  |  |         return new CrudColumn($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 428 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 429 |  |  |  |