Get Style Properties in phpcode

  • Thread starter Thread starter ragtek
  • Start date Start date
R

ragtek

Guest
I want to access the Style Properties ( @pageBackground ) in my model.

How can i do this?
 
ok found it

PHP:
/** @var $styleModel XenForo_Model_StyleProperty */
            $styleModel = XenForo_Model::create('XenForo_Model_StyleProperty');
              $visitor = XenForo_Visitor::getInstance();

            $background = $styleModel->getStylePropertyDefinitionByNameAndStyle('pageBackground', $visitor['style_id']);
            $background = $background['property_value'];

But any chance to get the value as hexdecimal?
I need this for a javascript
I've tried it with the XenForo_Helper_Color Methods, but i got always the same value back ( rgb(240,240,240) )
 
I found this method:

Code:
    public static function rgb2hex($color)
    {
        //Match R, G, B values
        preg_match('#^rgb\((?P<r>\d{1,3}).+?(?P<g>\d{1,3}).+?(?P<b>\d{1,3})\)$#i', $color, $rgb);
        //Convert them in hexa
        //Code source: http://forum.codecall.net/php-tutorials/22589-rgb-hex-colors-hex-colors-rgb-php.html                
        $output = sprintf("%x", ($rgb['r'] << 16) + ($rgb['g'] << 8) + $rgb['b']);
        
               return $output;
    }
 
I continue this thread because the method of Ragtek(...):

Code:
/** @var $styleModel XenForo_Model_StyleProperty */
            $styleModel = XenForo_Model::create('XenForo_Model_StyleProperty');
              $visitor = XenForo_Visitor::getInstance();
 
            $background = $styleModel->getStylePropertyDefinitionByNameAndStyle('pageBackground', $visitor['style_id']);
            $background = $background['property_value'];

(...) only works with the master style. I don't know why but the function 'getStylePropertyDefinitionByNameAndStyle' doesn't allow to get value for styles other than the master one (id:0), even if this value is defined both in the master and 'child' theme. I've found a solution but I'm wondering if there was an easiest way to perform this. 'My' method:

EDIT: DO NOT USE THE CODE BELOW
Code:
        //Get styles options
        $style = XenForo_Model::create('XenForo_Model_StyleProperty');
        //Get visitor information (to know which style he is using
        $visitor = XenForo_Visitor::getInstance();
        //Get Slideshow Background value
            $background_id = $style->getStylePropertyDefinitionByNameAndStyle('sedo_picasa_background', 0);
            $background_id = $background_id['property_definition_id'];
            $background = $style->getStylePropertyByDefinitionAndStyle($background_id, $visitor['style_id']);
            if(empty($background))
            {
                $background = $style->getStylePropertyByDefinitionAndStyle($background_id, 0);
            }
            $background = $background ['property_value'];
 
Ok... the above fix is an horrible one. It consumes too much DB queries. I need to find a way to get css value from the cache...
 
FOUND IT !

Code:
//Get styles datas
$style = XenForo_Application::get('styles');
 
//Get visitor style ID
$visitor = XenForo_Visitor::getInstance();
$styleid = $visitor['style_id'];
 
//Get current style properties
$properties = $style[$styleid]['properties'];
$properties = unserialize($properties);
 
//Get your CSS value
$your_css_value = $properties['your_css_name'];

Query used: 0
:)
 
...
.......
PHP:
$myproperty = XenForo_Template_Helper_Core::styleProperty('myproperty');
=> i have missed this one... the value will automatically change per user style
 
Top Bottom