| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Dynamic\FlexSlider\Model; |
||||
| 4 | |||||
| 5 | use function GuzzleHttp\Psr7\parse_request; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 6 | use Sheadawson\Linkable\Forms\EmbeddedObjectField; |
||||
| 7 | use Sheadawson\Linkable\Forms\LinkField; |
||||
| 8 | use Sheadawson\Linkable\Models\EmbeddedObject; |
||||
| 9 | use Sheadawson\Linkable\Models\Link; |
||||
| 10 | use SilverStripe\AssetAdmin\Forms\UploadField; |
||||
| 11 | use SilverStripe\Assets\Image; |
||||
| 12 | use SilverStripe\CMS\Model\SiteTree; |
||||
| 13 | use SilverStripe\Dev\Debug; |
||||
| 14 | use SilverStripe\Forms\CompositeField; |
||||
| 15 | use SilverStripe\Forms\DropdownField; |
||||
| 16 | use SilverStripe\Forms\FieldList; |
||||
| 17 | use SilverStripe\Forms\TreeDropdownField; |
||||
| 18 | use SilverStripe\ORM\DataObject; |
||||
| 19 | use SilverStripe\Security\Permission; |
||||
| 20 | use SilverStripe\Security\PermissionProvider; |
||||
| 21 | use UncleCheese\DisplayLogic\Forms\Wrapper; |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * Class SlideImage |
||||
| 25 | * @package Dynamic\FlexSlider\Model |
||||
| 26 | * @property string $Name |
||||
| 27 | * @property string $Headline |
||||
| 28 | * @property string $Description |
||||
| 29 | * @property int $SortOrder |
||||
| 30 | * @property string $SlideType |
||||
| 31 | * @property int $ImageID |
||||
| 32 | * @property int $VideoID |
||||
| 33 | * @property int $PageID |
||||
| 34 | * @property int $PageLinkID |
||||
| 35 | * @property int $LinkID |
||||
| 36 | */ |
||||
| 37 | class SlideImage extends DataObject implements PermissionProvider |
||||
| 38 | { |
||||
| 39 | /** |
||||
| 40 | * @var string |
||||
| 41 | */ |
||||
| 42 | private static $singular_name = 'Slide'; |
||||
|
0 ignored issues
–
show
|
|||||
| 43 | |||||
| 44 | /** |
||||
| 45 | * @var string |
||||
| 46 | */ |
||||
| 47 | private static $plural_name = 'Slides'; |
||||
|
0 ignored issues
–
show
|
|||||
| 48 | |||||
| 49 | /** |
||||
| 50 | * @var array |
||||
| 51 | */ |
||||
| 52 | private static $db = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 53 | 'Name' => 'Varchar(255)', |
||||
| 54 | 'Headline' => 'Varchar(255)', |
||||
| 55 | 'Description' => 'Text', |
||||
| 56 | 'SortOrder' => 'Int', |
||||
| 57 | 'SlideType' => 'Varchar', |
||||
| 58 | ]; |
||||
| 59 | |||||
| 60 | /** |
||||
| 61 | * @var array |
||||
| 62 | */ |
||||
| 63 | private static $has_one = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 64 | 'Image' => Image::class, |
||||
| 65 | 'Video' => EmbeddedObject::class, |
||||
| 66 | 'Page' => \Page::class, |
||||
| 67 | 'PageLink' => SiteTree::class, |
||||
| 68 | 'SlideLink' => Link::class, |
||||
| 69 | ]; |
||||
| 70 | |||||
| 71 | /** |
||||
| 72 | * @var array |
||||
| 73 | */ |
||||
| 74 | private static $owns = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 75 | 'Image', |
||||
| 76 | ]; |
||||
| 77 | |||||
| 78 | /** |
||||
| 79 | * @var string |
||||
| 80 | */ |
||||
| 81 | private static $table_name = 'SlideImage'; |
||||
|
0 ignored issues
–
show
|
|||||
| 82 | |||||
| 83 | /** |
||||
| 84 | * @var string |
||||
| 85 | */ |
||||
| 86 | private static $default_sort = 'SortOrder'; |
||||
|
0 ignored issues
–
show
|
|||||
| 87 | |||||
| 88 | /** |
||||
| 89 | * Adds Publish button to SlideImage record |
||||
| 90 | * |
||||
| 91 | * @var bool |
||||
| 92 | */ |
||||
| 93 | private static $versioned_gridfield_extensions = true; |
||||
|
0 ignored issues
–
show
|
|||||
| 94 | |||||
| 95 | /** |
||||
| 96 | * @var array |
||||
| 97 | */ |
||||
| 98 | private static $defaults = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 99 | 'SlideType' => 'Image', |
||||
| 100 | ]; |
||||
| 101 | |||||
| 102 | /** |
||||
| 103 | * @var array |
||||
| 104 | */ |
||||
| 105 | private static $summary_fields = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 106 | 'Image.CMSThumbnail' => 'Image', |
||||
| 107 | 'Name' => 'Name', |
||||
| 108 | ]; |
||||
| 109 | |||||
| 110 | /** |
||||
| 111 | * @var array |
||||
| 112 | */ |
||||
| 113 | private static $searchable_fields = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 114 | 'Name', |
||||
| 115 | 'Headline', |
||||
| 116 | 'Description', |
||||
| 117 | ]; |
||||
| 118 | |||||
| 119 | /** |
||||
| 120 | * @var int |
||||
| 121 | */ |
||||
| 122 | private static $image_size_limit = 512000; |
||||
|
0 ignored issues
–
show
|
|||||
| 123 | |||||
| 124 | /** |
||||
| 125 | * @var array |
||||
| 126 | */ |
||||
| 127 | private static $slide_types = [ |
||||
|
0 ignored issues
–
show
|
|||||
| 128 | 'Image', |
||||
| 129 | 'Video', |
||||
| 130 | 'Text', |
||||
| 131 | ]; |
||||
| 132 | |||||
| 133 | /** |
||||
| 134 | * @param bool $includerelations |
||||
| 135 | * @return array |
||||
| 136 | */ |
||||
| 137 | public function fieldLabels($includerelations = true) |
||||
| 138 | { |
||||
| 139 | $labels = parent::fieldLabels($includerelations); |
||||
| 140 | |||||
| 141 | $labels['Name'] = _t(__CLASS__ . '.NAME', 'Name'); |
||||
| 142 | $labels['Headline'] = _t(__CLASS__ . '.HEADLINE', 'Headline'); |
||||
| 143 | $labels['Description'] = _t(__CLASS__ . '.DESCRIPTION', 'Description'); |
||||
| 144 | $labels['SlideLinkID'] = _t(__CLASS__ . '.PAGE_LINK', "Call to action link"); |
||||
| 145 | $labels['Image'] = _t(__CLASS__ . '.IMAGE', 'Image'); |
||||
| 146 | $labels['SlideType'] = _t(__CLASS__ . '.SlideType', 'Image or Video'); |
||||
| 147 | $labels['Video'] = _t(__CLASS__ . '.VideoLabel', 'Video URL'); |
||||
| 148 | |||||
| 149 | return $labels; |
||||
| 150 | } |
||||
| 151 | |||||
| 152 | /** |
||||
| 153 | * @return \SilverStripe\Forms\FieldList |
||||
| 154 | */ |
||||
| 155 | public function getCMSFields() |
||||
| 156 | { |
||||
| 157 | $this->beforeUpdateCMSFields(function ($fields) { |
||||
| 158 | $fields->removeByName([ |
||||
| 159 | 'ShowSlide', |
||||
| 160 | 'SortOrder', |
||||
| 161 | 'PageID', |
||||
| 162 | 'Image', |
||||
| 163 | 'SlideType', |
||||
| 164 | 'Video', |
||||
| 165 | 'VideoID', |
||||
| 166 | 'SlideLinkID', |
||||
| 167 | ]); |
||||
| 168 | |||||
| 169 | // Name |
||||
| 170 | $fields->dataFieldByName('Name') |
||||
| 171 | ->setDescription( |
||||
| 172 | _t(__CLASS__ . '.INTERNAL_USE', 'for internal reference only') |
||||
| 173 | ); |
||||
| 174 | |||||
| 175 | // Headline |
||||
| 176 | $fields->dataFieldByName('Headline') |
||||
| 177 | ->setDescription( |
||||
| 178 | _t(__CLASS__ . '.USED_IN_TEMPLATE', 'optional, used in template') |
||||
| 179 | ); |
||||
| 180 | |||||
| 181 | // Description |
||||
| 182 | $fields->dataFieldByName('Description') |
||||
| 183 | ->setDescription( |
||||
| 184 | _t(__CLASS__ . '.USED_IN_TEMPLATE', 'optional, used in template') |
||||
| 185 | ); |
||||
| 186 | |||||
| 187 | // Page link |
||||
| 188 | $fields->replaceField( |
||||
| 189 | 'PageLinkID', |
||||
| 190 | LinkField::create('SlideLinkID', $this->fieldLabel('SlideLinkID')) |
||||
| 191 | ); |
||||
| 192 | |||||
| 193 | // Image |
||||
| 194 | $image = UploadField::create('Image', $this->fieldLabel('Image')) |
||||
| 195 | ->setFolderName('Uploads/SlideImages'); |
||||
| 196 | |||||
| 197 | $image->getValidator()->setAllowedExtensions(['jpg', 'jpeg', 'png', 'gif']); |
||||
| 198 | |||||
| 199 | $fields->addFieldToTab( |
||||
| 200 | 'Root.Main', |
||||
| 201 | CompositeField::create(FieldList::create( |
||||
| 202 | DropdownField::create('SlideType', $this->fieldLabel('SlideType')) |
||||
| 203 | ->setSource($this->getTypeSource()), |
||||
| 204 | Wrapper::create( |
||||
| 205 | $image |
||||
| 206 | )->displayIf('SlideType')->isEqualTo('Image')->orIf('SlideType')->isEqualTo('Video')->end(), |
||||
| 207 | Wrapper::create( |
||||
| 208 | $videoField = EmbeddedObjectField::create('Video', $this->fieldLabel('Video')) |
||||
| 209 | ->setDescription(_t(__CLASS__ . '.VideoDescription', 'Supported links: YouTube, Vimeo')) |
||||
| 210 | )->displayIf('SlideType')->isEqualTo('Video')->end() |
||||
| 211 | ))->setName('MediaFields'), |
||||
| 212 | 'Description' |
||||
| 213 | ); |
||||
| 214 | }); |
||||
| 215 | |||||
| 216 | $fields = parent::getCMSFields(); |
||||
| 217 | |||||
| 218 | $this->extend('updateSlideImageFields', $fields); |
||||
| 219 | |||||
| 220 | return $fields; |
||||
| 221 | } |
||||
| 222 | |||||
| 223 | /** |
||||
| 224 | * @return \SilverStripe\ORM\ValidationResult |
||||
| 225 | */ |
||||
| 226 | public function validate() |
||||
| 227 | { |
||||
| 228 | $result = parent::validate(); |
||||
| 229 | |||||
| 230 | if (!$this->Name) { |
||||
| 231 | $result->addError( |
||||
| 232 | _t(__CLASS__ . '.NAME_REQUIRED', 'A Name is required before you can save') |
||||
| 233 | ); |
||||
| 234 | } |
||||
| 235 | |||||
| 236 | $types = $this->getTypeSource(); |
||||
| 237 | |||||
| 238 | if (isset($types['Video']) && $this->SlideType == 'Video' && !$this->VideoID) { |
||||
| 239 | $result->addError( |
||||
| 240 | _t(__CLASS__ . '.VIDEO_REQUIRED', 'An Video Link is required before you can save') |
||||
| 241 | ); |
||||
| 242 | } |
||||
| 243 | |||||
| 244 | if (isset($types['Image']) && $this->SlideType == 'Image' && !$this->ImageID) { |
||||
| 245 | $result->addError( |
||||
| 246 | _t(__CLASS__ . '.IMAGE_REQUIRED', 'An Image is required before you can save') |
||||
| 247 | ); |
||||
| 248 | } |
||||
| 249 | |||||
| 250 | if (isset($types['Text']) && $this->SlideType == 'Text' && !$this->Description) { |
||||
| 251 | $result->addError( |
||||
| 252 | _t(__CLASS__ . '.DESCRIPTION_REQUIRED', 'A Description is required before you can save') |
||||
| 253 | ); |
||||
| 254 | } |
||||
| 255 | |||||
| 256 | return $result; |
||||
| 257 | } |
||||
| 258 | |||||
| 259 | /** |
||||
| 260 | * @return array |
||||
| 261 | */ |
||||
| 262 | public function providePermissions() |
||||
| 263 | { |
||||
| 264 | return [ |
||||
| 265 | 'Slide_EDIT' => 'Slide Edit', |
||||
| 266 | 'Slide_DELETE' => 'Slide Delete', |
||||
| 267 | 'Slide_CREATE' => 'Slide Create', |
||||
| 268 | ]; |
||||
| 269 | } |
||||
| 270 | |||||
| 271 | /** |
||||
| 272 | * @param null $member |
||||
|
0 ignored issues
–
show
|
|||||
| 273 | * @param array $context |
||||
| 274 | * |
||||
| 275 | * @return bool|int |
||||
| 276 | */ |
||||
| 277 | public function canCreate($member = null, $context = []) |
||||
| 278 | { |
||||
| 279 | return Permission::check('Slide_CREATE', 'any', $member); |
||||
| 280 | } |
||||
| 281 | |||||
| 282 | /** |
||||
| 283 | * @param null $member |
||||
|
0 ignored issues
–
show
|
|||||
| 284 | * @param array $context |
||||
| 285 | * |
||||
| 286 | * @return bool|int |
||||
| 287 | */ |
||||
| 288 | public function canEdit($member = null, $context = []) |
||||
|
0 ignored issues
–
show
The parameter
$context is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 289 | { |
||||
| 290 | return Permission::check('Slide_EDIT', 'any', $member); |
||||
| 291 | } |
||||
| 292 | |||||
| 293 | /** |
||||
| 294 | * @param null $member |
||||
|
0 ignored issues
–
show
|
|||||
| 295 | * @param array $context |
||||
| 296 | * |
||||
| 297 | * @return bool|int |
||||
| 298 | */ |
||||
| 299 | public function canDelete($member = null, $context = []) |
||||
|
0 ignored issues
–
show
The parameter
$context is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 300 | { |
||||
| 301 | return Permission::check('Slide_DELETE', 'any', $member); |
||||
| 302 | } |
||||
| 303 | |||||
| 304 | /** |
||||
| 305 | * @param null $member |
||||
|
0 ignored issues
–
show
|
|||||
| 306 | * @param array $context |
||||
| 307 | * |
||||
| 308 | * @return bool |
||||
| 309 | */ |
||||
| 310 | public function canView($member = null, $context = []) |
||||
|
0 ignored issues
–
show
The parameter
$context is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 311 | { |
||||
| 312 | return true; |
||||
| 313 | } |
||||
| 314 | |||||
| 315 | /** |
||||
| 316 | * @return array |
||||
| 317 | */ |
||||
| 318 | public function getTypeSource() |
||||
| 319 | { |
||||
| 320 | $types = $this->config()->get('slide_types'); |
||||
| 321 | asort($types); |
||||
| 322 | return array_combine($types, $types); |
||||
| 323 | } |
||||
| 324 | |||||
| 325 | /** |
||||
| 326 | * @param null $template |
||||
|
0 ignored issues
–
show
|
|||||
| 327 | * @param null $customFields |
||||
|
0 ignored issues
–
show
|
|||||
| 328 | * @return \SilverStripe\ORM\FieldType\DBHTMLText |
||||
| 329 | */ |
||||
| 330 | public function renderWith($template = null, $customFields = null) |
||||
| 331 | { |
||||
| 332 | if ($template === null) { |
||||
|
0 ignored issues
–
show
|
|||||
| 333 | $template = static::class; |
||||
| 334 | $template = ($this->SlideType) ? $template . "_{$this->SlideType}" : ''; |
||||
| 335 | } |
||||
| 336 | |||||
| 337 | return parent::renderWith($template); |
||||
| 338 | } |
||||
| 339 | |||||
| 340 | /** |
||||
| 341 | * @return \SilverStripe\ORM\FieldType\DBHTMLText |
||||
| 342 | */ |
||||
| 343 | public function forTemplate() |
||||
| 344 | { |
||||
| 345 | return $this->renderWith(); |
||||
| 346 | } |
||||
| 347 | } |
||||
| 348 |