Skip to content

Commit

Permalink
refactor/cleanup/streamline widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
handcode committed Jun 9, 2021
1 parent 3c23149 commit 7a41708
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/views/default/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
?>
<div class="publication-default-tag publication-default-index">
<?= Cell::widget(['id' => 'publication_tag_top', 'requestParam' => 'publication_tag_top']) ?>
<?= TaggedPublication::widget(['tagId' => $tagId,'categoryId' => $categoryId, 'limit' => $limit]) ?>
<?= TaggedPublication::widget(['tagId' => $tagId,'categoryId' => $categoryId, 'limit' => $limit,'pagination' => true]) ?>
<?= Cell::widget(['id' => 'publication_tag_bottom', 'requestParam' => 'publication_tag_bottom']) ?>
</div>
102 changes: 95 additions & 7 deletions src/widgets/BasePublication.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,71 @@
use dmstr\modules\publication\models\crud\PublicationItemTranslation;
use dmstr\modules\publication\models\crud\PublicationItemXTag;
use dmstr\modules\publication\models\crud\query\PublicationItemQuery;
use Exception;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\data\Pagination;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\LinkPager;

/**
* Class BasePublication
* @package dmstr\modules\publication\widgets
* @author Elias Luhr <[email protected]>
*
* @property bool teaser
* @property int|null limit
* @property PublicationItemQuery itemsQuery
* @property int categoryId
*/
abstract class BasePublication extends Widget
{

/**
* @var PublicationItem|null
*/
public $item;

/**
* en|disable pagination in list views
*
* @var bool
*/
public $pagination = false;

/**
* next label for pagination links
* @var string
*/
public $paginationNextLabel = '&raquo;';

/**
* prev label for pagination links
* @var string
*/
public $paginationPrevLabel = '&laquo;';

/**
* page size for paginated lists
* @var int
*/
public $pageSize = 60;

/**
* Css class used within the wrapper container
* @var string
*/
public $wrapperCssClass = 'publication-widget publication-item-index';

/**
* @var bool
*/
public $teaser = true;

/**
* Limit num items
* @var
* @var int|null
*/
public $limit;

Expand Down Expand Up @@ -91,7 +131,7 @@ public function init()
}

if (!empty($this->tagId)) {
$this->itemsQuery->leftJoin(['x' => PublicationItemXTag::tableName()], PublicationItem::tableName() . '.id = x.item_id');
$this->itemsQuery->innerJoin(['x' => PublicationItemXTag::tableName()], PublicationItem::tableName() . '.id = x.item_id');
$this->itemsQuery->groupBy(PublicationItem::tableName() . '.id');

foreach ($this->filterConditions('tagId', 'tag_id') as $condition) {
Expand All @@ -111,14 +151,62 @@ public function init()

}

/**
* @throws Exception
* @return PublicationItem|string
*/
public function run()
{

if ($this->item) {
return Html::tag('div', $this->renderHtmlByPublicationItem($this->item), ['class' => $this->wrapperCssClass]);
}

$publicationItemsQuery = $this->itemsQuery;

$paginationHtml = null;

if ($this->pagination) {
$count = $publicationItemsQuery->count();
$pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->pageSize]);
$publicationItemsQuery->offset($pagination->offset);
$publicationItemsQuery->limit($pagination->limit);
$paginationHtml = Html::beginTag('div', ['class' => 'publication-pagination']) . LinkPager::widget(
[
'pagination' => $pagination,
'nextPageLabel' => $this->paginationNextLabel,
'prevPageLabel' => $this->paginationPrevLabel,
// 'firstPageLabel' => $this->paginationPrevLabel,
// 'lastPageLabel' => $this->paginationNextLabel,
]
) . Html::endTag('div');
}

return Html::tag('div', $this->renderItemsHtml($publicationItemsQuery->all()), ['class' => $this->wrapperCssClass]) . $paginationHtml;
}

protected function renderItemsHtml($publicationItems = [])
{
$html = '';
/** @var PublicationItem[] $publicationItems */
foreach ($publicationItems as $publicationItem) {
try {
$html .= $this->renderHtmlByPublicationItem($publicationItem);
} catch (Exception $e) {
Yii::error($e->getMessage(), __METHOD__);
}
}
return $html;
}

/**
* @param string $attributeName
* @param string $columnName
*
* @return array
*
* if we get one or a list of category_ids build constraint
* to exclude IDs they can be dafined as negative int
* to exclude IDs they can be defined as negative int
*/
protected function filterConditions(string $attributeName, string $columnName): array
{
Expand Down
61 changes: 1 addition & 60 deletions src/widgets/Publication.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,69 +22,10 @@
* @package dmstr\modules\publication\widgets
* @author Elias Luhr <[email protected]>
*
* @property PublicationItem item
* @property bool pagination
* @property string wrapperCssClass
* @property string paginationNextLabel
* @property string paginationPrevLabel
*/
class Publication extends BasePublication
{
public $item;
public $pagination = false;
public $paginationNextLabel = '&raquo;';
public $paginationPrevLabel = '&laquo;';
public $pageSize = 60;

private $wrapperCssClass = 'publication-widget publication-item-index';


/**
* @throws Exception
* @return PublicationItem|string
*/
public function run()
{

if ($this->item) {
return Html::tag('div', $this->renderHtmlByPublicationItem($this->item), ['class' => $this->wrapperCssClass]);
}

$publicationItemsQuery = $this->itemsQuery;

$paginationHtml = null;

if ($this->pagination) {
$count = $publicationItemsQuery->count();
$pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->pageSize]);
$publicationItemsQuery->offset($pagination->offset);
$publicationItemsQuery->limit($pagination->limit);
$paginationHtml = Html::beginTag('div', ['class' => 'publication-pagination']) . LinkPager::widget(
[
'pagination' => $pagination,
'nextPageLabel' => $this->paginationNextLabel,
'prevPageLabel' => $this->paginationPrevLabel,
// 'firstPageLabel' => $this->paginationPrevLabel,
// 'lastPageLabel' => $this->paginationNextLabel,
]
) . Html::endTag('div');
}

return Html::tag('div', $this->renderItemsHtml($publicationItemsQuery->all()), ['class' => $this->wrapperCssClass]) . $paginationHtml;
}

protected function renderItemsHtml($publicationItems = [])
{
$html = '';
/** @var PublicationItem[] $publicationItems */
foreach ($publicationItems as $publicationItem) {
try {
$html .= $this->renderHtmlByPublicationItem($publicationItem);
} catch (Exception $e) {
Yii::error($e->getMessage(), __METHOD__);
}
}
return $html;
}
public $wrapperCssClass = 'publication-widget publication-item-index';

}
15 changes: 3 additions & 12 deletions src/widgets/TaggedPublication.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@
class TaggedPublication extends BasePublication
{

public function run()
{
$html = '';
foreach ($this->itemsQuery->all() as $item) {
try {
$html .= $this->renderHtmlByPublicationItem($item);
} catch (Exception $e) {
Yii::error($e->getMessage(), __METHOD__);
}
}
return Html::tag('div', $html, ['class' => 'publication-widget publication-item-tagged']);
}
public $pagination = true;
public $wrapperCssClass = 'publication-widget publication-item-tagged';

}

0 comments on commit 7a41708

Please sign in to comment.