diff --git a/IblockTable.php b/IblockTable.php new file mode 100644 index 0000000..2a86e28 --- /dev/null +++ b/IblockTable.php @@ -0,0 +1,110 @@ +id = $params['id']; + } + if (!empty($params['code'])){ + $this->code = $params['code']; + } + if (!empty($params['ttl'])){ + $this->ttl = $params['ttl']; + } + if (!empty($params['cache_id'])){ + $this->cache_id = $params['cache_id']; + } + if (!empty($params['cache_path'])){ + $this->cache_path = $params['cache_path']; + } + } + + /** + * method that return raw uncached data from database + * + * @param array $params array of bitrix CIBlockElement::getList() parameters + * proccess_callback(callable) - callback_function that can modify element result + * function($arr) { + * do something + * return $arr; + * } + * @return array + * @throws + **/ + + public function getList(array $params) : array { + \CModule::includeModule('iblock'); + if (empty($params['filter']['iblock_id'])){ + $params['filter']['IBLOCK_ID'] = $this->id; + } + $db_res = \CIBlockElement::GetList( + ($params['sort']) ? $params['sort'] : [], + $params['filter'], + (isset($params['group_by']) && is_array($params['group_by'])) ? $params['group_by'] : false, + (isset($params['nav_start_params']) && is_array($params['nav_start_params'])) ? $params['nav_start_params'] : false, + (!empty($params['select'])) ? $params['select'] : ['ID'] + ); + $result = []; + while($arr = $db_res->GetNext()){ + if (isset($params['process_callback']) && is_callable($params['process_callback'])) + { + $arr = $params['process_callback']($arr); + } + $result[] = $arr; + } + return $result; + } + + /** + * method that return a cached data + * + * @param array $queryParams array of getList() parameters + * + * @param array $cacheParams array of cache parameters + * ttl - time to live + * cache_id - id of a cache + * cache_path - folder where to save data + * @return array + * @throws + **/ + public function getCachedList(array $queryParams, array $cacheParams): array { + $ttl = ($cacheParams['ttl']) ? $cacheParams['ttl'] : $this->ttl; + $cache_id = ($cacheParams['cache_id']) ? $cacheParams['cache_id'] : $this->cache_id; + $cache_path = ($cacheParams['cache_path']) ? $cacheParams['cache_path'] : $this->cache_path; + $result = []; + $obCache = new \CPHPCache(); + if( $obCache->InitCache($ttl, $cache_id, $cache_path) ) { + $vars = $obCache->GetVars(); + $result = $vars['result']; + } + elseif( $obCache->StartDataCache() ) { + $result = $this->getList($queryParams); + $obCache->EndDataCache(array('result' => $result)); + } + return $result; + } + +} \ No newline at end of file diff --git a/getLastNews.php b/getLastNews.php new file mode 100644 index 0000000..f0d2c7d --- /dev/null +++ b/getLastNews.php @@ -0,0 +1,77 @@ + +
+ channel->item); + + +$show_result = []; + +for($i = 0; $i < $items_count; $i++){ + $item = $xml_data->channel->item[$i]; + $show_result[] = [ + 'name' => (string)$item->title, + 'link' => (string)$item->link, + 'pub_date' => (string)$item->pubDate, + 'description' => (string)$item->description + ]; +} + +unset($item); + +$row_delimiter = '
'; +if (php_sapi_name() == 'cli'){ + $row_delimiter = "\n"; +} + +// xml данные идут по убыванию даты можно взять просто первые пять + +// sort +usort($show_result, 'sort_by_date'); + + +//show +foreach(array_slice($show_result, 0, $show_count) as $item){ + echo "title:{$item['name']}{$row_delimiter}". + "link:{$item['link']}{$row_delimiter}". + "publicate date:{$item['pub_date']}{$row_delimiter}". + "description:{$item['description']}".$row_delimiter. + $row_delimiter; +}