Colors array() configurable for each style installed

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Colors array() configurable for each style installed

Post by 3Di »

Hi all,
what's interesting me a lot today is how to make some colors configurable in ACP for each and every style installed.

Let's see, I do have the ACP side with 3 colorpickers which works accordingly to

- config array()

Code: Select all

			/* Highlight's colors */
			array('config.add', array('hlposts_colors', json_encode(array(
							'bckg' => '#b6e0b6',
							'text' => '#2c3645',
							'bord' => '#2ea7a4',
						)
					)
				)
			),
I read it in ACP module

Code: Select all

/* Let's read the style configuration */
$color_configs = json_decode($config['hlposts_colors'], true);
Then the colorpickers do their job in the HTML5 side and we store back the chosen colors to the config array()

Code: Select all

/* Styles' specific vars */
$color_configs['bckg'] = trim($request->variable('bckg', $color_configs['bckg']));
$color_configs['text'] = trim($request->variable('text', $color_configs['text']));
$color_configs['bord'] = trim($request->variable('bord', $color_configs['bord']));
$config->set('hlposts_colors', json_encode($color_configs));
There is a CSS with fake colors

Code: Select all

/* Colors are here just to be overriden via ACP configuration */
.hlposts-panel {
	margin-bottom: 10px;
	font-size: 0.9em;
	padding: 5px 10px;
	border-radius: 7px;
	
	/*hlposts_colors bckg */
	background-color: #84cc84;
	
	/*hlposts_colors text */
	color: #28313F;
	
	/*hlposts_colors bord */
	box-shadow: 1px 2px 3px #000000;
}
which will be in part overriden via template vars located in overall_header_stylesheets_after.html

Code: Select all

<style>

/*
Colors are set according to user configuration in ACP.
*/

/* Panel */

/* Items color */
.hlposts-panel {
	background-color: {{ HLPOSTS_BCKG }};
	color: {{ HLPOSTS_TEXT }};
	box-shadow: 1px 2px 3px {{ HLPOSTS_BORD }};
}

</style>
Those template vars have been declared using template->assign_vars() within the core.page_header_after PHP event, ..

Code: Select all

		/* Let's read the style configuration */
		$color_configs = json_decode($this->config['hlposts_colors'], true);

		$this->template->assign_vars(array(
			'HLPOSTS_BCKG'					=>	$color_configs['bckg'],
			'HLPOSTS_TEXT'					=>	$color_configs['text'],
			'HLPOSTS_BORD'					=>	$color_configs['bord'],
Ok, so far so good, everything works like a charm, just a note, it works the same for each and every style, the question is..

what should I do to make all of this configurable on a per installed style basis?

TIA :)
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
hubaishan
Translator
Posts: 155
Joined: Sun Apr 10, 2011 2:47 pm
Name: Saeed Hubaishan
Contact:

Re: Colors array() configurable for each style installed

Post by hubaishan »

I think you need to use config_text because it hold more characters.

Code: Select all

/* Highlight's colors */
			array('config_text.add', array('hlposts_colors', json_encode(
				array('prosilver' => array(
						'bckg' => '#b6e0b6',
						'text' => '#2c3645',
						'bord' => '#2ea7a4',
							),
						),
					)
				)
			),
I config page make a SELECT for installed styles there are a core function useful for this style_select()
then:

Code: Select all

 /* Styles' specific vars */
$color_configs[$request->variable('style', 'prosilver')]['bckg'] = trim($request->variable('bckg', $color_configs['prosilver']['bckg']));
$color_configs[$request->variable('style', 'prosilver')]['text'] = trim($request->variable('text', $color_configs['prosilver']['text']));
$color_configs[$request->variable('style', 'prosilver')]['bord'] = trim($request->variable('bord', $color_configs['prosilver']['bord']));
$config_text->set('hlposts_colors', json_encode($color_configs));
and then

Code: Select all

	/* Let's read the style configuration */
		$color_configs = json_decode($this->config_text['hlposts_colors'], true);
		$styles=$this->template->get_user_style();
		$user_style = '';
		foreach($styles as $style)
		{
			if (key_exist($style, $color_configs)
			{
				$user_style=$style;
				break;
			}
		}
		$this->template->assign_vars(array(
			'HLPOSTS_BCKG'					=>	$color_configs[$user_style]['bckg'],
			'HLPOSTS_TEXT'					=>	$color_configs[$user_style]['text'],
			'HLPOSTS_BORD'					=>	$color_configs[$user_style]['bord'],
hubaishan
Translator
Posts: 155
Joined: Sun Apr 10, 2011 2:47 pm
Name: Saeed Hubaishan
Contact:

Re: Colors array() configurable for each style installed

Post by hubaishan »

In config page you need javascript to change color values when SELECT styles changes
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Colors array() configurable for each style installed

Post by 3Di »

That's what's in my thoughts already, thanks for chiming in. :)

But I see there is something missing.. that's my doubt, and I don't know what, though.

I think something more elegant could be done using Twig and CSSs.. I could be wrong though.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: Colors array() configurable for each style installed

Post by 3Di »

And yes, we got it working and fully functional at the end. Thanks. :)

For the posterity, the method is in this extension contained.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
Post Reply

Return to “Extension Writers Discussion”