Skip to content

Instantly share code, notes, and snippets.

@sartas
Created February 16, 2014 09:08
Show Gist options
  • Select an option

  • Save sartas/9031489 to your computer and use it in GitHub Desktop.

Select an option

Save sartas/9031489 to your computer and use it in GitHub Desktop.
bitrixlib
<?php
/**
* User: marsel
* Date: 16.02.14
* Time: 11:56
*/
namespace bitrixlib;
require_once(dirname(__FILE__).'/query.php');
class Medialibrary extends Query
{
public static $per_page = array(25, 50, 100, 150);
/**
* Альбомы по родителям
* @param int $parent_id
* @return array
*/
public static function AlbumsByParent($parent_id = 0)
{
$parent_id = (int)$parent_id;
$sql = "
SELECT * FROM
b_medialib_collection
WHERE PARENT_ID = {$parent_id}
;";
return self::Query($sql);
}
/**
*
* @param array $album_ids
* @param int $deep
* @param int $page
* @param int $per_page
* @return array
*/
public static function PhotoByAlbums($album_ids = array(), $deep = 0, $page = 1, $per_page = 25)
{
if (!$album_ids || $page < 1 || !in_array($per_page, self::$per_page))
{
return array();
}
foreach ($album_ids as &$id)
{
$id = (int)$id;
}
// глубина вложенности
if ($deep > 0)
{
// $deep = (int)$deep;
$album_ids_add = self::AlbumIDsByParents($album_ids);
$album_ids = array_merge($album_ids, $album_ids_add);
}
$album_ids = implode(',', $album_ids);
$limit = $per_page;
$offset = $per_page * ($page - 1);
$sql = "
SELECT file.* FROM
b_medialib_collection_item as collection
LEFT JOIN b_medialib_item as photo on collection.ITEM_ID = photo.ID
LEFT JOIN b_file as file on photo.SOURCE_ID = file.ID
WHERE collection.COLLECTION_ID IN ({$album_ids})
ORDER BY TIMESTAMP_X
LIMIT {$offset},{$limit}
;";
$result = array();
$result['items'] = self::Query($sql);
$sql = "
SELECT count(collection.ITEM_ID) as count FROM
b_medialib_collection_item as collection
WHERE collection.COLLECTION_ID IN ({$album_ids})
;";
$count = self::Query($sql, self::FETCH_ONE);
$result['count'] = $count['count'];
return $result;
}
/**
* Массив id альбомов по id родителей
* @param array $parent_ids
* @return array
*/
public static function AlbumIDsByParents($parent_ids = array())
{
if (!$parent_ids)
{
return array();
}
foreach ($parent_ids as &$id)
{
$id = (int)$id;
}
$parent_ids = implode(',', $parent_ids);
$sql = "
SELECT ID as id FROM
b_medialib_collection
WHERE PARENT_ID IN ({$parent_ids})
;";
return self::Query($sql,self::FETCH_COLUMN);
}
}
<?php
/**
* Created by PhpStorm.
* User: marsel
* Date: 16.02.14
* Time: 14:16
*/
namespace bitrixlib;
class Query {
const FETCH_ONE = 'one';
const FETCH_ALL = 'all';
const FETCH_COLUMN = 'column';
public static function Query($sql, $fetch_type = 'all')
{
global $DB;
$res = $DB->Query($sql);
switch ($fetch_type)
{
case 'one':
$data = self::FetchOne($res);
break;
case 'all':
$data = self::FetchAll($res);
break;
case 'column':
$data = self::FetchColumn($res);
break;
}
return $data;
}
public static function FetchOne($res)
{
return $res->Fetch();
}
public static function FetchAll($res)
{
$data = array();
while ($row = $res->Fetch())
{
if (isset($row['ID']))
{
$data[$row['ID']] = $row;
}
else
{
$data[] = $row;
}
}
return $data;
}
public static function FetchColumn($res)
{
$data = array();
while ($row = $res->Fetch())
{
$data[] = array_shift($row);
}
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment