Changeset View
Changeset View
Standalone View
Standalone View
core/units/helpers/skin_helper.php
Show All 11 Lines | |||||
* See http://www.in-portal.org/license for copyright notices and details. | * See http://www.in-portal.org/license for copyright notices and details. | ||||
*/ | */ | ||||
defined('FULL_PATH') or die('restricted access!'); | defined('FULL_PATH') or die('restricted access!'); | ||||
class SkinHelper extends kHelper { | class SkinHelper extends kHelper { | ||||
/** | /** | ||||
* Regular expression, used to detect admin skin file among others | |||||
*/ | |||||
const SKIN_REGEXP = '/admin-(.*)-([\d]+).css/'; | |||||
/** | |||||
* Allows to read some skin fields and build link to admin skin file. | * Allows to read some skin fields and build link to admin skin file. | ||||
* Will automatically compile skin file, when missing. | * Will automatically compile skin file, when missing. | ||||
* | * | ||||
* @param Array $params | * @param array $params Tag params. | ||||
* | |||||
* @return string | * @return string | ||||
*/ | */ | ||||
function AdminSkinTag($params) | public function AdminSkinTag(array $params) | ||||
Lint: CodingStandard.NamingConventions.ValidFunctionName.ScopeNotCamelCaps: Public method name "SkinHelper::AdminSkinTag" is not in camel caps format | |||||
{ | { | ||||
// Returns given field of skin. | |||||
if (array_key_exists('type', $params)) { | if ( array_key_exists('type', $params) ) { | ||||
// returns given field of skin | |||||
return $this->_getStyleField( $params['type'] ); | return $this->_getStyleField($params['type']); | ||||
} | } | ||||
$style_info = $this->_getStyleInfo(); | |||||
if (file_exists( $this->getSkinPath() )) { | if ( file_exists($this->getSkinPath()) ) { | ||||
// returns last compiled skin | |||||
$ret = $this->getSkinPath(true); | $ret = $this->getSkinPath(true); | ||||
} | } | ||||
else { | else { | ||||
// search for previously compiled skin | |||||
$last_compiled = $this->_getLastCompiled( mb_strtolower($style_info['Name']) ); | |||||
if ($last_compiled) { | |||||
// found | |||||
$ret = $this->getSkinPath(true, $last_compiled); | |||||
} | |||||
else { | |||||
// not found (try to compile on the fly) | |||||
/** @var kDBItem $skin */ | /** @var kDBItem $skin */ | ||||
$skin = $this->Application->recallObject('skin.-item', null, Array ('skip_autoload' => true)); | $skin = $this->Application->recallObject('skin.-item', null, array('skip_autoload' => true)); | ||||
$skin->Load(1, 'IsPrimary'); | $skin->Load(1, 'IsPrimary'); | ||||
$last_compiled = $this->compile($skin); | $ret = $this->compile($skin) ? $this->getSkinPath(true) : ''; | ||||
$ret = $last_compiled ? $this->getSkinPath(true, $last_compiled) : ''; | |||||
} | |||||
} | } | ||||
if (array_key_exists('file_only', $params) && $params['file_only']) { | if ( array_key_exists('file_only', $params) && $params['file_only'] ) { | ||||
return $ret; | return $ret; | ||||
} | } | ||||
$ret .= '?ts=' . adodb_date('Y-m-d_H:i:s', filemtime($this->getSkinPath())); | |||||
return '<link rel="stylesheet" rev="stylesheet" href="' . $ret . '" type="text/css" media="screen"/>'; | return '<link rel="stylesheet" rev="stylesheet" href="' . $ret . '" type="text/css" media="screen"/>'; | ||||
} | } | ||||
/** | /** | ||||
* Compiles given skin object | * Compiles given skin object | ||||
* | * | ||||
* @param kDBItem $object | * @param kDBItem $object Skin. | ||||
* | |||||
* @return integer | |||||
*/ | */ | ||||
function compile(&$object) | public function compile(kDBItem $object) | ||||
{ | { | ||||
$ret = $object->GetDBField('CSS'); | $ret = $object->GetDBField('CSS'); | ||||
$options = $object->GetDBField('Options'); | $options = $object->GetDBField('Options'); | ||||
$options = unserialize($options); | $options = unserialize($options); | ||||
$options['base_url'] = Array ('Value' => rtrim(BASE_PATH, '/')); | $options['base_url'] = array('Value' => rtrim(BASE_PATH, '/')); | ||||
foreach ($options as $key => $row) { | foreach ( $options as $key => $row ) { | ||||
$ret = str_replace('@@' . $key . '@@', $row['Value'], $ret); | $ret = str_replace('@@' . $key . '@@', $row['Value'], $ret); | ||||
} | } | ||||
$compile_ts = adodb_mktime(); | $compile_ts = adodb_mktime(); | ||||
$css_file = $this->_getStylesheetPath() . DIRECTORY_SEPARATOR . 'admin-' . mb_strtolower($object->GetDBField('Name')) . '-' . $compile_ts . '.css'; | $style_name = $this->getSkinFilename($object->GetDBField('Name')); | ||||
$css_file = $this->_getStylesheetPath() . DIRECTORY_SEPARATOR . $style_name; | |||||
$fp = fopen($css_file, 'w'); | if ( file_put_contents($css_file, $ret) === false ) { | ||||
if (!$fp) { | |||||
return false; | return false; | ||||
} | } | ||||
$prev_css = $this->_getStylesheetPath() . '/admin-' . mb_strtolower($object->GetDBField('Name')) . '-' . $object->GetDBField('LastCompiled') . '.css'; | |||||
if (file_exists($prev_css)) { | |||||
unlink($prev_css); | |||||
} | |||||
fwrite($fp, $ret); | |||||
fclose($fp); | |||||
$sql = 'UPDATE ' . $object->TableName . ' | $sql = 'UPDATE ' . $object->TableName . ' | ||||
SET LastCompiled = ' . $compile_ts . ' | SET LastCompiled = ' . $compile_ts . ' | ||||
WHERE ' . $object->IDField . ' = ' . $object->GetID(); | WHERE ' . $object->IDField . ' = ' . $object->GetID(); | ||||
$this->Conn->Query($sql); | $this->Conn->Query($sql); | ||||
$this->Application->incrementCacheSerial('skin'); | $this->Application->incrementCacheSerial('skin'); | ||||
$this->Application->incrementCacheSerial('skin', $object->GetID()); | $this->Application->incrementCacheSerial('skin', $object->GetID()); | ||||
return $compile_ts; | return $compile_ts; | ||||
} | } | ||||
/** | /** | ||||
* Returns fields of primary admin skin | * Returns fields of primary admin skin | ||||
* | * | ||||
* @return Array | * @return array | ||||
* @access protected | |||||
*/ | */ | ||||
protected function _getStyleInfo() | protected function _getStyleInfo() | ||||
{ | { | ||||
$cache_key = 'primary_skin_info[%SkinSerial%]'; | $cache_key = 'primary_skin_info[%SkinSerial%]'; | ||||
$ret = $this->Application->getCache($cache_key); | $ret = $this->Application->getCache($cache_key); | ||||
if ($ret === false) { | if ( $ret === false ) { | ||||
$this->Conn->nextQueryCachable = true; | $this->Conn->nextQueryCachable = true; | ||||
$sql = 'SELECT * | $sql = 'SELECT * | ||||
FROM ' . TABLE_PREFIX . 'AdminSkins | FROM ' . TABLE_PREFIX . 'AdminSkins | ||||
WHERE IsPrimary = 1'; | WHERE IsPrimary = 1'; | ||||
$ret = $this->Conn->GetRow($sql); | $ret = $this->Conn->GetRow($sql); | ||||
$this->Application->setCache($cache_key, $ret); | $this->Application->setCache($cache_key, $ret); | ||||
} | } | ||||
return $ret; | return $ret; | ||||
} | } | ||||
/** | /** | ||||
* Returns requested field value of primary admin skin | * Returns requested field value of primary admin skin | ||||
* | * | ||||
* @param string $field | * @param string $field Field. | ||||
* | |||||
* @return string | * @return string | ||||
*/ | */ | ||||
function _getStyleField($field) | protected function _getStyleField($field) | ||||
Protected method name "SkinHelper::_getStyleField" must not be prefixed with an underscore Lint: CodingStandard.NamingConventions.ValidFunctionName.PublicUnderscore: Protected method name "SkinHelper::_getStyleField" must not be prefixed with an underscore | |||||
{ | { | ||||
// Old style method of calling. | |||||
if ($field == 'logo') { | if ( $field == 'logo' ) { | ||||
// old style method of calling | |||||
$field = 'Logo'; | $field = 'Logo'; | ||||
} | } | ||||
$style_info = $this->_getStyleInfo(); | $style_info = $this->_getStyleInfo(); | ||||
if (!$style_info[$field]) { | if ( !$style_info[$field] ) { | ||||
return ''; | return ''; | ||||
} | } | ||||
$image_fields = Array ('Logo', 'LogoBottom', 'LogoLogin'); | $image_fields = array('Logo', 'LogoBottom', 'LogoLogin'); | ||||
if (in_array($field, $image_fields)) { | if ( in_array($field, $image_fields) ) { | ||||
return $this->_getStylesheetPath(true) . '/' . $style_info[$field]; | return $this->_getStylesheetPath(true) . '/' . $style_info[$field]; | ||||
} | } | ||||
return $style_info[$field]; | return $style_info[$field]; | ||||
} | } | ||||
/** | /** | ||||
* Returns path, where compiled skin and it's image files are stored | * Returns path, where compiled skin and it's image files are stored | ||||
* | * | ||||
* @param bool $url | * @param boolean $url Return url or path. | ||||
* | |||||
* @return string | * @return string | ||||
* @access protected | |||||
*/ | */ | ||||
protected function _getStylesheetPath($url = false) | protected function _getStylesheetPath($url = false) | ||||
{ | { | ||||
if ($url) { | if ( $url ) { | ||||
return $this->Application->BaseURL( str_replace(DIRECTORY_SEPARATOR, '/', WRITEBALE_BASE) ) . 'user_files'; | $sub_folder = str_replace(DIRECTORY_SEPARATOR, '/', WRITEBALE_BASE) . '/user_files'; | ||||
return $this->Application->BaseURL() . ltrim($sub_folder, '/'); | |||||
} | } | ||||
return WRITEABLE . DIRECTORY_SEPARATOR . 'user_files'; | return WRITEABLE . DIRECTORY_SEPARATOR . 'user_files'; | ||||
} | } | ||||
/** | /** | ||||
* Returns full path to primary admin skin using given or last compiled date | * Returns full path to primary admin skin using given or last compiled date | ||||
* | * | ||||
* @param string|bool $url | * @param boolean $url Return url or path.. | ||||
* @param int $compile_date | * | ||||
* @return string | * @return string | ||||
* @access public | |||||
*/ | */ | ||||
public function getSkinPath($url = false, $compile_date = null) | public function getSkinPath($url = false) | ||||
{ | { | ||||
$style_info = $this->_getStyleInfo(); | $style_info = $this->_getStyleInfo(); | ||||
$style_name = $this->getSkinFilename($style_info['Name']); | |||||
if ( !isset($compile_date) ) { | |||||
$compile_date = $style_info['LastCompiled']; | |||||
} | |||||
$style_name = 'admin-' . mb_strtolower($style_info['Name']) . '-' . $compile_date . '.css'; | |||||
return $this->_getStylesheetPath($url) . ($url ? '/' : DIRECTORY_SEPARATOR) . $style_name; | return $this->_getStylesheetPath($url) . ($url ? '/' : DIRECTORY_SEPARATOR) . $style_name; | ||||
} | } | ||||
/** | /** | ||||
* Returns maximal compilation date for given skin name | * Returns skin filename without path. | ||||
* | |||||
* @param string $skin_name Skin name. | |||||
* | * | ||||
* @param string $style_name | * @return string | ||||
* @return int | |||||
*/ | */ | ||||
function _getLastCompiled($style_name) | protected function getSkinFilename($skin_name) | ||||
{ | { | ||||
$last_compiled = 0; | return 'admin-' . mb_strtolower($skin_name) . '.css'; | ||||
$iterator = new DirectoryIterator( $this->_getStylesheetPath() . DIRECTORY_SEPARATOR ); | |||||
/** @var DirectoryIterator $file_info */ | |||||
foreach ($iterator as $file_info) { | |||||
if ( !$file_info->isFile() ) { | |||||
continue; | |||||
} | |||||
$regs = $this->isSkinFile( $file_info->getFilename() ); | |||||
if ( $regs && $regs[1] == $style_name && $regs[2] > $last_compiled ) { | |||||
$last_compiled = max($last_compiled, $regs[2]); | |||||
} | |||||
} | |||||
return $last_compiled; | |||||
} | } | ||||
/** | /** | ||||
* Deletes all compiled versions of all skins | * Deletes all compiled versions of all skins | ||||
* | * | ||||
* @return void | |||||
*/ | */ | ||||
function deleteCompiled() | public function deleteCompiled() | ||||
{ | { | ||||
$iterator = new DirectoryIterator( $this->_getStylesheetPath() . DIRECTORY_SEPARATOR ); | $files = glob($this->_getStylesheetPath() . DIRECTORY_SEPARATOR . 'admin-*.css'); | ||||
/** @var DirectoryIterator $file_info */ | |||||
foreach ($iterator as $file_info) { | if ( $files ) { | ||||
if ( $file_info->isFile() && $this->isSkinFile( $file_info->getFilename() ) ) { | array_map('unlink', $files); | ||||
unlink( $file_info->getPathname() ); | |||||
} | |||||
} | } | ||||
} | } | ||||
/** | |||||
* Determines if given file is admin skin file | |||||
* | |||||
* @param string $filename | |||||
* @return array|bool | |||||
* @access protected | |||||
*/ | |||||
protected function isSkinFile($filename) | |||||
{ | |||||
if ( preg_match(self::SKIN_REGEXP, $filename, $regs) ) { | |||||
return $regs; | |||||
} | |||||
return false; | |||||
} | } | ||||
Expected 0 spaces before closing brace; 4 found Lint: CodingStandard.Classes.ClassDeclaration.SpaceBeforeCloseBrace: Expected 0 spaces before closing brace; 4 found | |||||
} | |||||
No newline at end of file |
Public method name "SkinHelper::AdminSkinTag" is not in camel caps format