| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\CKANRegistry\Forms; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\CKANRegistry\Model\Resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Forms\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Forms\TextField; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * A PresentedOptionsField renders either a list of options that can be chosen for a {@link DropdownFilter}, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * a text area allowing free text entry per line. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * The values of these options are serialised and saved as JSON. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class PresentedOptionsField extends TextField | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     const SELECT_TYPE_ALL = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     const SELECT_TYPE_CUSTOM = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     const SELECT_TYPE_DEFAULT = self::SELECT_TYPE_ALL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $schemaComponent = 'PresentedOptions'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * The resource that this options field will suggest options from | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var Resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected $resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         Resource $resource, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $title = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $value = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $maxLength = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         Form $form = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->setResource($resource); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         parent::__construct($name, $title, $value, $maxLength, $form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->addExtraClass('ckan-presented-options__container'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function Type() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return 'ckan-presented-options'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function getSchemaDataDefaults() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $data = parent::getSchemaDataDefaults(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $data['data']['options'] = self::getOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $data['data']['selectTypeDefault'] = self::SELECT_TYPE_DEFAULT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $data['data']['selectTypes'] = self::getSelectTypes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $data['data']['endpoint'] = $this->getResource()->Endpoint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $data['data']['resource'] = $this->getResource()->Identifier; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $data['data']['fieldMap'] = $this->getResource()->Fields()->map('ID', 'Name')->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Get a list of options for filtering with a human readable (translated) label | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return array[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public static function getSelectTypes() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             ['value' => self::SELECT_TYPE_ALL, 'title' => _t(__CLASS__ . '.SELECT_ALL', 'Select from all options')], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             ['value' => self::SELECT_TYPE_CUSTOM, 'title' => _t(__CLASS__ . '.SELECT_CUSTOM', 'Manually add options')], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @todo remove mock data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public static function getOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             'One-by-one', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'Group session', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'Phone', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             'Helpline', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             'Other', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             'Something scrollable', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return Resource | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     public function getResource() | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         return $this->resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @param Resource $resouce | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function setResource(Resource $resource) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->resource = $resource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 118 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  |