Your IP : 3.18.112.171
<?php
namespace Bitrix\Landing;
use \Bitrix\Main\Localization\Loc;
use \Bitrix\Main\Config\Option;
use \Bitrix\Main\Application;
Loc::loadMessages(__FILE__);
class Manager
{
/**
* Selected template theme id.
* @var string
*/
private static $themeId = '';
/**
* Get main instance of \CMain.
* @return \CMain
*/
public static function getApplication()
{
return $GLOBALS['APPLICATION'];
}
/**
* Get main instance of \CUser.
* @return \CUser
*/
public static function getUserInstance()
{
return $GLOBALS['USER'];
}
/**
* Get current user id.
* @return int
*/
public static function getUserId()
{
return self::getUserInstance()->getId();
}
/**
* Get option from module settings.
* @param string $code Option code.
* @param mixed $default Default value.
* @return type
*/
public static function getOption($code, $default = null)
{
return Option::get('landing', $code, $default);
}
/**
* Famous document root.
* @return string
*/
public static function getDocRoot()
{
static $docRoot = null;
if ($docRoot === null)
{
$context = \Bitrix\Main\Application::getInstance()->getContext();
$server = $context->getServer();
$docRoot = $server->getDocumentRoot();
}
return $docRoot;
}
/**
* Get path for publication sites.
* @param string|int Site id or site code.
* @return string
*/
public static function getPublicationPath($siteCode)
{
$path = '/pub/site/#id#/';
return str_replace('#id#', $siteCode, $path);
}
/**
* Add some class to some marker.
* @param string $marker Marker.
* @param string $class Class.
* @return void
*/
public static function setPageClass($marker, $class)
{
$class = trim($class);
if ($class)
{
$application = self::getApplication();
$existClass = $application->getPageProperty($marker);
$application->setPageProperty(
$marker,
$existClass . ($existClass != '' ? ' ' : '') . $class
);
}
}
/**
* Get themes from template dir.
* @param string $tplId Site template id.
* @return array
*/
public static function getThemes($tplId)
{
$themes = array();
$path = self::getDocRoot() . getLocalPath('templates/' . $tplId) . '/themes/';
if (($handle = opendir($path)))
{
while ((($entry = readdir($handle)) !== false))
{
if ($entry != '.' && $entry != '..')
{
$themes[] = pathinfo($entry, PATHINFO_FILENAME);
}
}
}
return $themes;
}
/**
* Get site template id for landing view.
* @return string
*/
public static function getTemplateId()
{
static $tplId = null;
if ($tplId === null)
{
$tplId = self::getOption('site_template_id', 'landing24');
}
return $tplId;
}
/**
* Set new colored theme id.
* @param string $themeId Theme id.
* @return void
*/
public static function setThemeId($themeId)
{
self::$themeId = $themeId;
}
/**
* Set current selected or default color theme.
* @return void
*/
public static function setTheme()
{
$tplId = self::getTemplateId();
$themes = Manager::getThemes($tplId);
if (
!self::$themeId ||
!in_array(self::$themeId, $themes)
)
{
self::setThemeId(array_pop($themes));
}
if (self::$themeId)
{
$themePath = \getLocalPath('templates/' . $tplId, BX_PERSONAL_ROOT) . '/themes/' . self::$themeId;
$themePathAbsolute = self::getDocRoot() . $themePath;
if (is_dir($themePathAbsolute))
{
if ($handle = opendir($themePathAbsolute))
{
while (($file = readdir($handle)) !== false)
{
if ($file != '.' && $file != '..')
{
\Bitrix\Main\Page\Asset::getInstance()->addCSS($themePath . '/' . $file);
}
}
closedir($handle);
}
}
elseif (is_file($themePathAbsolute . '.css'))
{
\Bitrix\Main\Page\Asset::getInstance()->addCSS($themePath . '.css');
}
}
}
/**
* Save picture to db.
* @param mixed $file File array or path to file.
* @param string $ext File extension (if can't detected by file name).
* @param array $params Some file params.
* @return array|false Local file array or false on error.
*/
public static function savePicture($file, $ext = false, $params = array())
{
// url of picture
if (!is_array($file))
{
$httpClient = new \Bitrix\Main\Web\HttpClient();
$httpClient->setTimeout(5);
$httpClient->setStreamTimeout(5);
$urlComponents = parse_url($file);
$dimensionDelta = 10;// delta for resize
// detect tmp file name
if ($urlComponents && $urlComponents['path'] != '')
{
$tempPath = \CFile::GetTempName('', bx_basename($urlComponents['path']));
}
else
{
$tempPath = \CFileGetTempName('', bx_basename($file));
}
if ($ext !== false && in_array($ext, explode(',', \CFile::GetImageExtensions())))
{
if (substr($tempPath, -3) != $ext)
{
$tempPath = $tempPath . '.' . $ext;
}
}
// download and save
if ($httpClient->download($file, $tempPath))
{
$fileName = $httpClient->getHeaders()->getFilename();
$file = \CFile::MakeFileArray($tempPath);
if ($file && $fileName)
{
$file['name'] = $fileName;
}
// resize if need
if (
isset($params['width']) &&
isset($params['height'])
)
{
$params['width'] += intval($params['width'] / 100 * $dimensionDelta);
$params['height'] += intval($params['height'] / 100 * $dimensionDelta);
\CFile::ResizeImage($file, $params);
}
}
}
// post array
if (\CFile::CheckImageFile($file, 0, 0, 0, array('IMAGE')) === null)
{
$module = 'landing';
$file['MODULE_ID'] = $module;
$file = \CFile::SaveFile($file, $module);
if ($file)
{
$file = \CFile::GetFileArray($file);
}
if ($file)
{
return $file;
}
}
return false;
}
}