-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = '»'; | ||
|
||
/** | ||
* prev label for pagination links | ||
* @var string | ||
*/ | ||
public $paginationPrevLabel = '«'; | ||
|
||
/** | ||
* 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; | ||
|
||
|
@@ -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) { | ||
|
@@ -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 | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = '»'; | ||
public $paginationPrevLabel = '«'; | ||
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'; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters