This repository was archived by the owner on May 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathItems.php
98 lines (87 loc) · 2.47 KB
/
Items.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
declare(strict_types=1);
namespace Adrenth\RssFetcher\Components;
use Adrenth\RssFetcher\Models\Item;
use Cms\Classes\ComponentBase;
use InvalidArgumentException;
use October\Rain\Support\Collection;
/**
* Class Items
*
* @package Adrenth\RssFetcher\Components
*/
class Items extends ComponentBase
{
/**
* @var Collection
*/
public $items;
/**
* {@inheritdoc}
*/
public function componentDetails()
{
return [
'name' => 'adrenth.rssfetcher::lang.component.item_list.name',
'description' => 'adrenth.rssfetcher::lang.component.item_list.description'
];
}
/**
* {@inheritdoc}
*/
public function defineProperties(): array
{
return [
'maxItems' => [
'label' => 'adrenth.rssfetcher::lang.item.max_items',
'type' => 'string',
'default' => '10'
],
'sourceId' => [
'label' => 'adrenth.rssfetcher::lang.item.source_id',
'type' => 'string',
'default' => ''
]
];
}
/**
* {@inheritdoc}
*/
public function onRun()
{
$sourceId = (int) $this->property('sourceId');
$this->items = self::loadItems(
(int) $this->property('maxItems', 10),
$sourceId > 0 ? $sourceId : null
);
}
/**
* Load Items
*
* @param int $maxItems
* @param int $sourceId
* @return array
*/
public static function loadItems(int $maxItems, int $sourceId = null): array
{
try {
$items = Item::select(['adrenth_rssfetcher_items.*', 'adrenth_rssfetcher_sources.name AS source'])
->join(
'adrenth_rssfetcher_sources',
'adrenth_rssfetcher_items.source_id',
'=',
'adrenth_rssfetcher_sources.id'
)
->where('adrenth_rssfetcher_sources.is_enabled', '=', 1)
->where('adrenth_rssfetcher_items.is_published', '=', 1)
->orderBy('adrenth_rssfetcher_items.pub_date', 'desc')
->limit($maxItems);
if ($sourceId !== null && is_numeric($sourceId)) {
$items->where('adrenth_rssfetcher_items.source_id', '=', (int) $sourceId);
}
} catch (InvalidArgumentException $e) {
return [];
}
return $items->get()->toArray();
}
}