1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Get Style Properties in phpcode

Discussion in 'XenForo Development Discussions' started by ragtek, May 11, 2011.

  1. ragtek

    ragtek Guest

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

    How can i do this?
    DroidHost likes this.
  2. ragtek

    ragtek Guest

    ok found it

    /** @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) )
  3. cclaerhout

    cclaerhout Well-Known Member

    I found this method:

        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;
  4. cclaerhout

    cclaerhout Well-Known Member

    I continue this thread because the method of Ragtek(...):

    /** @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:

            //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']);
                    $background = $style->getStylePropertyByDefinitionAndStyle($background_id, 0);
                $background = $background ['property_value'];
  5. cclaerhout

    cclaerhout Well-Known Member

    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...
  6. cclaerhout

    cclaerhout Well-Known Member

    FOUND IT !

    //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
  7. cclaerhout

    cclaerhout Well-Known Member

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

Share This Page