Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions test/Elements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Test;

use Bitrix\Main\Loader;
use Bitrix\Main\Data\Cache;

if (!Loader::includeModule('iblock')) {
throw new Exception('Iblock module ist installed.');
}

class Elements
{
protected $id = 0;

public function __construct($id = 0)
{
if ($id) {
$this->id = $id;
}

if (!$this->id) {
throw new Exception('Iblock ID is undefined.');
}
}

public function getElements($params = [])
{
if (array_key_exists('cacheTime', $params)) {
$cacheTime = $params['cacheTime'];
unset($params['cacheTime']);
} else {
$cacheTime = 3600;
}

$order = $params['order'] ? $params['order'] : [];
$select = $params['select'] ? $params['select'] : ['*'];
$filter = $params['filter'] ? $params['filter'] : [];
$group = $params['group'] ? $params['group'] : false;
$nav = $params['nav'] ? $params['nav'] : false;

if (!$filter['IBLOCK_ID']) {
$filter['IBLOCK_ID'] = $this->id;
}

$cache = Cache::createInstance();
if ($cache->initCache($cacheTime, serialize($params), '/cache_elements')) {
$result = $cache->getVars();
} elseif ($cache->startDataCache()) {
$result = [];

$res = \CIBlockElement::GetList($order, $filter, $group, $nav, $select);

while ($element = $res->GetNextElement()) {
$item = $element->GetFields();
$item['PROPERTIES'] = $element->GetProperties();
$result[$item['ID']] = $item;
}

if (!empty($result)) {
$cache->endDataCache($result);
} else {
$cache->abortDataCache();
}
}

return $result;
}

protected function getCacheDir()
{
return get_class($this);
}
}
43 changes: 43 additions & 0 deletions test/getLastNews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

class RssReader
{
protected $items = [];

public function __construct($url)
{
if (!$url) {
throw new Exception('Url is undefined.');
}

$this->loadRss($url);
}

private function loadRss($url)
{
$rss = file_get_contents($url);
$rss = simplexml_load_string($rss);

foreach ($rss->channel->item as $item) {
$pubDate = strtotime($item->pubDate);
$this->items[$pubDate] = $item;
}
}

public function getItems($count = 0)
{
if ($count > 0) {
return array_slice($this->items, 0, $count);
} else {
return $this->items;
}
}
}

$rss = new RssReader('https://lenta.ru/rss');

foreach ($rss->getItems(5) as $item) {
echo "\n Название: " . $item->title;
echo "\n Ссылка: " . $item->link;
echo "\n Описание: " . $item->description;
}
26 changes: 26 additions & 0 deletions test/testWeb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<? require($_SERVER['DOCUMENT_ROOT'] . '/bitrix/header.php'); ?>

<?
include($_SERVER['DOCUMENT_ROOT'] . '/include/pride/Elements.php');

$banners = new \Test\Elements(12);

//get all properties with params
$items = $banners->getElements([
'select' => ['NAME', 'ID', 'IBLOCK_ID', 'PROPERTY_*'],
'order' => ['SORT' => 'ASC'],
]);

print_r($items);

//get single property
$items = $banners->getElements([
'select' => ['NAME', 'PROPERTY_HREF'],
'order' => ['SORT' => 'ASC'],
]);

print_r($items);

?>

<? require($_SERVER['DOCUMENT_ROOT'] . '/bitrix/footer.php'); ?>