| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\Widget; |
| 4 |
|
| 5 |
use ipl\Html\Attributes; |
| 6 |
use ipl\Html\BaseHtmlElement; |
| 7 |
use ipl\Html\HtmlElement; |
| 8 |
use ipl\Stdlib\Filter; |
| 9 |
use ipl\Stdlib\Filter\Rule; |
| 10 |
use ipl\Web\Url; |
| 11 |
|
| 12 |
class ContinueWith extends BaseHtmlElement |
| 13 |
{ |
| 14 |
protected $tag = 'span'; |
| 15 |
|
| 16 |
protected $defaultAttributes = ['class' => 'continue-with']; |
| 17 |
|
| 18 |
/** @var Url */ |
| 19 |
protected $url; |
| 20 |
|
| 21 |
/** @var Filter\Rule|callable */ |
| 22 |
protected $filter; |
| 23 |
|
| 24 |
/** @var string */ |
| 25 |
protected $title; |
| 26 |
|
| 27 |
/** @var string The title to use when the filter is empty */ |
| 28 |
protected string $emptyFilterTitle; |
| 29 |
|
| 30 |
/** @var ?string The reason why the widget is disabled */ |
| 31 |
protected ?string $disableReason = null; |
| 32 |
|
| 33 |
/** @var ?string The `data-base-target` attribute */ |
| 34 |
protected ?string $baseTarget = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* @deprecated Use {@see ContinueWith::create()} and {@see ContinueWith::createDisabled()} instead |
| 38 |
*/ |
| 39 |
public function __construct(?Url $url = null, $filter = null) |
| 40 |
{ |
| 41 |
$this->url = $url; |
| 42 |
$this->filter = $filter; |
| 43 |
|
| 44 |
$this->emptyFilterTitle = ''; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Create a `ContinueWith` widget |
| 49 |
* |
| 50 |
* @param Url $url The url to use |
| 51 |
* @param Rule|callable $filter The filter to apply |
| 52 |
* @param string $title The title of the widget |
| 53 |
* @param string $emptyFilterTitle The title to use when the filter is empty |
| 54 |
* |
| 55 |
* @return static |
| 56 |
*/ |
| 57 |
public static function create( |
| 58 |
Url $url, |
| 59 |
Filter\Rule|callable $filter, |
| 60 |
string $title, |
| 61 |
string $emptyFilterTitle |
| 62 |
): static { |
| 63 |
return (new static($url, $filter)) |
| 64 |
->setTitle($title) |
| 65 |
->setEmptyFilterTitle($emptyFilterTitle); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Create a disabled `ContinueWith` widget |
| 70 |
* |
| 71 |
* @param string $reason The reason why the widget is disabled |
| 72 |
* |
| 73 |
* @return static |
| 74 |
*/ |
| 75 |
public static function createDisabled(string $reason): static |
| 76 |
{ |
| 77 |
$instance = new static(); |
| 78 |
$instance->disableReason = $reason; |
| 79 |
|
| 80 |
return $instance; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get the `data-base-target` attribute |
| 85 |
* |
| 86 |
* @return ?string |
| 87 |
*/ |
| 88 |
public function getBaseTarget(): ?string |
| 89 |
{ |
| 90 |
return $this->baseTarget; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Set the `data-base-target` attribute |
| 95 |
* |
| 96 |
* @param string $target |
| 97 |
* |
| 98 |
* @return $this |
| 99 |
*/ |
| 100 |
public function setBaseTarget(string $target): static |
| 101 |
{ |
| 102 |
$this->baseTarget = $target; |
| 103 |
|
| 104 |
return $this; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Set the title to use when the filter is empty |
| 109 |
* |
| 110 |
* @param string $emptyFilterTitle |
| 111 |
* |
| 112 |
* @return $this |
| 113 |
*/ |
| 114 |
public function setEmptyFilterTitle(string $emptyFilterTitle): static |
| 115 |
{ |
| 116 |
$this->emptyFilterTitle = $emptyFilterTitle; |
| 117 |
|
| 118 |
return $this; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Set title for the anchor |
| 123 |
* |
| 124 |
* @param string $title |
| 125 |
* |
| 126 |
* @return $this |
| 127 |
*/ |
| 128 |
public function setTitle($title) |
| 129 |
{ |
| 130 |
$this->title = $title; |
| 131 |
|
| 132 |
return $this; |
| 133 |
} |
| 134 |
|
| 135 |
public function assemble() |
| 136 |
{ |
| 137 |
$filter = $this->filter; |
| 138 |
if (is_callable($filter)) { |
| 139 |
$filter = $filter(); /** @var Filter\Rule $filter */ |
| 140 |
} |
| 141 |
|
| 142 |
$baseFilter = $this->url?->getFilter(); |
| 143 |
if ($baseFilter && ((! $baseFilter instanceof Filter\Chain) || ! $baseFilter->isEmpty())) { |
| 144 |
$filter = Filter::all($baseFilter, $filter); |
| 145 |
} |
| 146 |
|
| 147 |
if ($this->disableReason || ($filter instanceof Filter\Chain && $filter->isEmpty())) { |
| 148 |
$this->addHtml(new HtmlElement( |
| 149 |
'span', |
| 150 |
Attributes::create([ |
| 151 |
'class' => ['control-button', 'disabled'], |
| 152 |
'title' => $this->disableReason ?? $this->emptyFilterTitle, |
| 153 |
]), |
| 154 |
new Icon('share') |
| 155 |
)); |
| 156 |
} else { |
| 157 |
$link = new ActionLink( |
| 158 |
null, |
| 159 |
$this->url->setFilter($filter), |
| 160 |
'share', |
| 161 |
['class' => 'control-button', 'title' => $this->title] |
| 162 |
); |
| 163 |
|
| 164 |
if ($this->getBaseTarget()) { |
| 165 |
$link->setBaseTarget($this->getBaseTarget()); |
| 166 |
} |
| 167 |
|
| 168 |
$this->addHtml($link); |
| 169 |
} |
| 170 |
} |
| 171 |
} |