silverstripe-elemental-basics/src/Elements/InstagramElement.php

114 lines
2.8 KiB
PHP
Raw Normal View History

2021-06-08 10:38:35 +02:00
<?php
/**
* Created by PhpStorm.
* User: tony
* Date: 6/30/18
* Time: 11:54 PM
*/
namespace A2nt\ElementalBasics\Elements;
use DNADesign\Elemental\Models\BaseElement;
use SilverStripe\Core\Convert;
2022-05-10 13:08:53 +02:00
/**
* Class \A2nt\ElementalBasics\Elements\InstagramElement
*
* @property string $Username
* @property string $Tag
* @property boolean $DisplayProfile
* @property boolean $DisplayBiography
* @property boolean $DisplayGallery
* @property boolean $DisplayCaptions
*/
2021-06-08 10:38:35 +02:00
class InstagramElement extends BaseElement
{
2021-07-08 15:53:26 +02:00
private static $icon = 'font-icon-menu-files';
2021-06-08 10:38:35 +02:00
private static $singular_name = 'Instagram';
private static $plural_name = 'Instagram Elements';
private static $description = 'Displays Instagram posts';
private static $table_name = 'InstagramElement';
private static $db = [
'Username' => 'Varchar(255)',
2021-07-08 15:53:26 +02:00
'Tag' => 'Varchar(255)',
'DisplayProfile' => 'Boolean(0)',
'DisplayBiography' => 'Boolean(0)',
'DisplayGallery' => 'Boolean(0)',
'DisplayCaptions' => 'Boolean(0)',
2021-06-08 10:38:35 +02:00
];
private static $defaults = [
2021-07-08 15:53:26 +02:00
'DisplayGallery' => true,
2021-06-08 10:38:35 +02:00
];
public function getType()
{
return self::$singular_name;
}
/**
* @return array
*/
public function getAttributes(): array
{
2021-07-08 15:53:26 +02:00
return [
'data-username' => $this->Username,
'data-display-profile' => $this->DisplayProfile,
'data-display-biography' => $this->DisplayBiography,
'data-display-gallery' => $this->DisplayGallery,
'data-display-captions' => $this->DisplayCaptions,
'data-items' => 12,
];
2021-06-08 10:38:35 +02:00
}
/**
* Custom attributes to process.
*
* @param array $attributes
*
* @return string
*/
public function AttributesHTML($attributes = null): string
{
2021-07-08 15:53:26 +02:00
if (!$attributes) {
2021-06-08 10:38:35 +02:00
$attributes = $this->getAttributes();
}
$attributes = (array) $attributes;
$attributes = array_filter($attributes, static function ($v) {
return ($v || $v === 0 || $v === '0');
});
// Create markup
$parts = [];
foreach ($attributes as $name => $value) {
if ($value === true) {
$value = $name;
} else if (is_scalar($value)) {
$value = (string) $value;
} else {
$value = json_encode($value);
}
$parts[] = sprintf('%s=%s', Convert::raw2att($name), Convert::raw2att($value));
}
return implode(' ', $parts);
}
public function FeedLink()
{
2021-07-08 15:53:26 +02:00
return 'https://www.instagram.com/'.($this->Username ? $this->Username : 'explore/tags/'.$this->Tag).'/';
2021-06-08 10:38:35 +02:00
}
public function FeedTitle()
{
2021-07-08 15:53:26 +02:00
return ($this->Username ? '@'.$this->Username : '#'.$this->Tag);
2021-06-08 10:38:35 +02:00
}
}