| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\CKANRegistry\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\CKANRegistry\Forms\ResultConditionsField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Forms\FieldGroup; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Forms\FieldList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Forms\NumericField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Forms\ReadonlyField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\ORM\DataObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Represents a generic field on a CKAN Resource, e.g. a column in a spreadsheet. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * It is intentionally generic, as the resource may not be a tabular one, e.g. geospatial data to be rendered in a map. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @property Resource Resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @method static ResourceField create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @property string OriginalLabel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @property string Type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @property string ReadableLabel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @property bool ShowInResultsView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @property bool ShowInDetailView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @property bool RemoveDuplicates | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @property int Position | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @property string DisplayConditions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | class ResourceField extends DataObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private static $table_name = 'CKANResourceField'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     private static $db = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         'OriginalLabel' => 'Varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         'Type' => 'Varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         'ReadableLabel' => 'Varchar', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         'ShowInResultsView' => 'Boolean', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         'ShowInDetailView' => 'Boolean(1)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         'RemoveDuplicates' => 'Boolean', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         'Position' => 'Int', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         'DisplayConditions' => 'Text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private static $has_one = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         'Resource' => Resource::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     private static $summary_fields = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         'Position', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         'ReadableLabel', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         'OriginalLabel', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         'Type', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         'ShowInResultsView', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         'ShowInDetailView', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function getCMSFields() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->beforeUpdateCMSFields(function (FieldList $fields) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             $originalLabel = ReadonlyField::create('OriginalLabel') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 ->setDescription(_t( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     __CLASS__ . '.ORIGINAL_LABEL_DESCRIPTION', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                     'Title of this field as provided by the CKAN resource' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $fields->replaceField('OriginalLabel', $originalLabel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $readableLabel = $fields->dataFieldByName('ReadableLabel'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $readableLabel->setAttribute('placeholder', $this->OriginalLabel); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $fields->removeByName('Type'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $positionField = NumericField::create('Position') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 ->setTitle(_t(__CLASS__ . '.ORDER_LABEL', 'Presented order')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 ->setDescription(_t( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     __CLASS__ . '.ORDER_DENOMINATOR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     'of {count} columns', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                     ['count' => static::get()->filter('ResourceID', $this->ResourceID)->count()] | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 )) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 ->addExtraClass('ckan-resource__order'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $fields->replaceField('Position', $positionField); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $summary = $fields->dataFieldByName('ShowInResultsView') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 ->addExtraClass('visibility-options__option'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $detail = $fields->dataFieldByName('ShowInDetailView') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 ->addExtraClass('visibility-options__option'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $duplicates = $fields->dataFieldByName('RemoveDuplicates') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 ->setTitle( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                     _t(__CLASS__ . '.REMOVE_DUPLICATES_LABEL', 'Only show one value if duplicates exist') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 ->addExtraClass('visibility-options__option'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             // Present the visibility fields in a group | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $fields->removeByName(['ShowInResultsView', 'ShowInDetailView', 'RemoveDuplicates']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $visibilityOptions = FieldGroup::create('Visibility', [$summary, $detail, $duplicates]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 ->addExtraClass('visibility-options'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $fields->insertBefore($visibilityOptions, 'Position'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             $fields->removeByName('ResourceID'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $fields->replaceField( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 'DisplayConditions', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 ResultConditionsField::create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                     'DisplayConditions', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                     _t(__CLASS__ . '.RESULT_CONDITIONS', 'Result conditions') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             // See https://github.com/silverstripe/silverstripe-framework/issues/8696 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             foreach ([$summary, $detail, $readableLabel, $originalLabel] as $field) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 $field->setTitle(ucfirst(strtolower($field->Title()))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         return parent::getCMSFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * Use the readable label for GridField CRUD operation result messages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     public function getTitle() | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         return $this->ReadableLabel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  |