Page 1 of 3

Setting Colour on font depending on their status!

Posted: 2021-12-24 13:11
by espo
Hi everyone,

i am trying to enter this code to get a different color for each state.
It doesn't work, it clears my table data leaving the page blank.
Using AppGini 5.94


}function slot_init(&$options, $memberInfo, &$args){
// modify the status field of the table view query to display 'no' invoices in bold red
$oldArray=$options->QueryFieldsTV;
$options->QueryFieldsTV='';
foreach($oldArray as $field => $caption){
if($field=='`slot`.`slot_mag`'){
$options->QueryFieldsTV['IF(`slot`.`slot_mag`=\'betting da lavorare\', \'<b style="color: green;">BETTING DA LAVORARE</b>\', IF(`slot`.`slot_mag`=\'ATS da lavorare\', \'<b style="color: red;">ATS da lavorare</b>\', IF(`slot`.`slot_mag`=\'saci vlt da lavorare\', \'<b style="color: blue;">SACI VLT da lavorare</b>\', `slot`.`slot_mag`)))']=$caption;
}else{
$options->QueryFieldsTV[$field]=$caption;
}
}
return TRUE;


Could anyone help me to solve the problem?

Many thanks in advance.

Re: Setting Colour on font depending on their status!

Posted: 2021-12-24 14:57
by pbottcher
Hi,

not sure what happens, but maybe your first } in front of the function is not correct.

Also you may try:

Code: Select all

function slot_init(&$options, $memberInfo, &$args){
// modify the status field of the table view query to display 'no' invoices in bold red
	$ar=array_flip($options->QueryFieldsTV);
	$ar['slot_mag']="CASE 
			WHEN `slot`.`slot_mag`='betting da lavorare' then '<b style=\"color: green;\">BETTING DA LAVORARE</b>' 
			WHEN `slot`.`slot_mag`='ATS da lavorare' then '<b style=\"color: red;\">ATS da lavorare</b>'
			WHEN `slot`.`slot_mag`='saci vlt da lavorare' then '<b style=\"color: blue;\">SACI VLT da lavorare</b>'
			ELSE `slot`.`slot_mag`
			END";
	$options->QueryFieldsTV=array_flip($ar);
}

Re: Setting Colour on font depending on their status!

Posted: 2021-12-27 07:51
by espo
Thank you so much for your answer.
I tried to insert your code, but if I insert the return true at the end it gives me this error:
Parse error: syntax error, unexpected '}', expecting end of file in /membri/logisticmag/slot/hooks/SLOT.php on line 16

I have to remove RETURN TRUE} ?
Should I enter { in another area of ​​the code ?

Thank you so much for your help.

Re: Setting Colour on font depending on their status!

Posted: 2021-12-27 09:11
by pbottcher
Hi,

well in this function I do not see any wrong bracket. Can you post your SLOT.php.

Re: Setting Colour on font depending on their status!

Posted: 2021-12-27 10:13
by espo
This is mine.
Thanks so much.

Code: Select all

<?php
	// For help on using hooks, please refer to https://bigprof.com/appgini/help/working-with-generated-web-database-application/hooks

	function slot_init(&$options, $memberInfo, &$args){
// modify the status field of the table view query to display 'no' invoices in bold red
	$ar=array_flip($options->QueryFieldsTV);
	$ar['slot_mag']="CASE 
			WHEN `slot`.`slot_mag`='betting da lavorare' then '<b style=\"color: green;\">BETTING da lavorare</b>' 
			WHEN `slot`.`slot_mag`='ATS da lavorare' then '<b style=\"color: red;\">ATS da lavorare</b>'/						
			WHEN `slot`.`slot_mag`='saci vlt da lavorare' then '<b style=\"color: blue;\">SACI VLT da lavorare</b>'
			ELSE `slot`.`slot_mag`
			END";
	$options->QueryFieldsTV=array_flip($ar);
}

//	return TRUE;
//}

	function SLOT_header($contentType, $memberInfo, &$args) {
		$header='';

		switch($contentType) {
			case 'tableview':
				$header='';
				break;

			case 'detailview':
				$header='';
				break;

			case 'tableview+detailview':
				$header='';
				break;

			case 'print-tableview':
				$header='';
				break;

			case 'print-detailview':
				$header='';
				break;

			case 'filters':
				$header='';
				break;
		}

		return $header;
	}

	function SLOT_footer($contentType, $memberInfo, &$args) {
		$footer='';

		switch($contentType) {
			case 'tableview':
				$footer='';
				break;

			case 'detailview':
				$footer='';
				break;

			case 'tableview+detailview':
				$footer='';
				break;

			case 'print-tableview':
				$footer='';
				break;

			case 'print-detailview':
				$footer='';
				break;

			case 'filters':
				$footer='';
				break;
		}

		return $footer;
	}

	function SLOT_before_insert(&$data, $memberInfo, &$args) {

		return TRUE;
	}

	function SLOT_after_insert($data, $memberInfo, &$args) {

		return TRUE;
	}

	function SLOT_before_update(&$data, $memberInfo, &$args) {

		return TRUE;
	}

	function SLOT_after_update($data, $memberInfo, &$args) {

		return TRUE;
	}

	function SLOT_before_delete($selectedID, &$skipChecks, $memberInfo, &$args) {

		return TRUE;
	}

	function SLOT_after_delete($selectedID, $memberInfo, &$args) {

	}

	function SLOT_dv($selectedID, $memberInfo, &$html, &$args) {

	}

	function SLOT_csv($query, $memberInfo, &$args) {

		return $query;
	}
	function SLOT_batch_actions(&$args) {

		return [];
	}
	

Re: Setting Colour on font depending on their status!

Posted: 2021-12-27 10:47
by pbottcher
You just need to remove the } before the return.

Code: Select all

	function slot_init(&$options, $memberInfo, &$args){
// modify the status field of the table view query to display 'no' invoices in bold red
	$ar=array_flip($options->QueryFieldsTV);
	$ar['slot_mag']="CASE 
			WHEN `slot`.`slot_mag`='betting da lavorare' then '<b style=\"color: green;\">BETTING da lavorare</b>' 
			WHEN `slot`.`slot_mag`='ATS da lavorare' then '<b style=\"color: red;\">ATS da lavorare</b>'/						
			WHEN `slot`.`slot_mag`='saci vlt da lavorare' then '<b style=\"color: blue;\">SACI VLT da lavorare</b>'
			ELSE `slot`.`slot_mag`
			END";
	$options->QueryFieldsTV=array_flip($ar);

	return TRUE;
}

Re: Setting Colour on font depending on their status!

Posted: 2021-12-27 11:36
by espo
Ok, now it shows the items on the screen without errors, but they don't have the color.

Thanks for your patience.

Re: Setting Colour on font depending on their status!

Posted: 2021-12-27 13:02
by espo
espo wrote:
2021-12-27 11:36
Ok, now it shows the items on the screen without errors, but they don't have the color.

Thanks for your patience.
Sorry but update.
I entered the code you sent me and this is the error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN `slot`.`slot_mag`='saci vlt da lavorare' then '<b style="color: blue;">SACI' at line 4

Re: Setting Colour on font depending on their status!

Posted: 2021-12-27 14:06
by pbottcher
Hi,

sorry there is a trailing /, please try

Code: Select all

	function slot_init(&$options, $memberInfo, &$args){
// modify the status field of the table view query to display 'no' invoices in bold red
	$ar=array_flip($options->QueryFieldsTV);
	$ar['slot_mag']="CASE 
			WHEN `slot`.`slot_mag`='betting da lavorare' then '<b style=\"color: green;\">BETTING da lavorare</b>' 
			WHEN `slot`.`slot_mag`='ATS da lavorare' then '<b style=\"color: red;\">ATS da lavorare</b>'					
			WHEN `slot`.`slot_mag`='saci vlt da lavorare' then '<b style=\"color: blue;\">SACI VLT da lavorare</b>'
			ELSE `slot`.`slot_mag`
			END";
	$options->QueryFieldsTV=array_flip($ar);

	return TRUE;
}

Re: Setting Colour on font depending on their status!

Posted: 2021-12-30 07:20
by espo
Hi and thanks for your help.
I entered the code by removing the "/".

Now I have this error:

Code: Select all

Unknown column 'slot.slot_mag' in 'field list'

Re: Setting Colour on font depending on their status!

Posted: 2021-12-30 09:46
by pbottcher
Hi,

well then you need to check what fields are with your table, as I do not know your setup.

Re: Setting Colour on font depending on their status!

Posted: 2021-12-30 10:36
by jsetzer
Many times debugging helps us, finding our own errors: You should dump out contents of fields.

I recommend...

Code: Select all

var_dump($options->QueryFieldsTV);
exit;
...before applying any changes on the $options array and check the array keys.
$ar['slot_mag']="CASE WHEN ... END";
I doubt the array key equals slot_mag. Without knowing your model, I guess the key equals `slot`.`slot_mag`, instead.

If I remember right, the array should contain the desired output value as key and the fieldname as value, not vice versa. For example...

Code: Select all

$options->QueryFieldsTV["'Test'"] = 'COLUMN_NAME';
This would output ...
Test
... in every cell of COLUMN_NAME column.

You may have switched key and value.

Re: Setting Colour on font depending on their status!

Posted: 2022-01-03 11:48
by espo
Happy New Year everyone and thanks for the replies.
Unfortunately I'm not very experienced and I try to explain my problem better.
I have always used the code I am attaching to change the state color in a field.
This code has always worked.
After updating APPGINI to version 5.97 the code no longer works and leaves me blank and non-editable fields.
The image is in the inserted link.
How can I solve this problem?

Code: Select all

function TEST_init(&$options, $memberInfo, &$args) {
   // modify the status field of the table view query to display 'no' invoices in bold red
     $oldArray=$options->QueryFieldsTV;
     $options->QueryFieldsTV='';
      foreach($oldArray as $field => $caption){
       if($field=='`TEST`.`stato`'){
           $options->QueryFieldsTV['IF(`TEST`.`stato`=\'BIANCO\', \'<b style="color: red;">BIANCO</b>\', 
									IF(`TEST`.`stato`=\'ROSSO\', \'<b style="color: green;">ROSSO</b>\', 
									IF(`TEST`.`stato`=\'VERDE\', \'<b style="color: #FF8243;">VERDE</b>\', 
									`TEST`.`stato`)))']=$caption;
        }else{
           $options->QueryFieldsTV[$field]=$caption;
        }
      }  
		return TRUE;
	}
https://ibb.co/6FdVLRp

My DB only has three fields

ID
FIRST NAME
STATE

Thank you very much!

Re: Setting Colour on font depending on their status!

Posted: 2022-01-03 11:58
by jsetzer
Please post the result of var_dump(). This should help you (and us, who try to help you) finding the mistake.

See my recommendation above.

Re: Setting Colour on font depending on their status!

Posted: 2022-01-03 12:01
by jsetzer
Additionally, there has been a change in AG recently for avoiding XSS attacks by injecting custom CSS. You should inspect the elements in you browser dev-tools and check if AppGini has removed style attributes as a security feature.

Re: Setting Colour on font depending on their status!

Posted: 2022-01-04 09:47
by espo
Sorry for my little knowledge ... but could you tell me where to insert

Code: Select all

var_dump ($ options-> QueryFieldsTV);
exit;
Thanks so much for your help

Re: Setting Colour on font depending on their status!

Posted: 2022-01-04 10:05
by jsetzer
You were talking about _init-function in TEST.php. So, exaxtky that is the place.

Assuming your table name equals TEST:

File: hooks/TEST.php

Code: Select all

function TEST_init(&$options, $memberInfo, &$args) {
  var_dump($options->QueryFieldsTV);
  exit;
}

Re: Setting Colour on font depending on their status!

Posted: 2022-01-04 10:12
by jsetzer
espo wrote:
2022-01-04 09:47

Code: Select all

var_dump ($ options-> QueryFieldsTV);
exit;
And please don't put any extra spaces.

In your code, extra spaces after $ and after -> which have to be removed.

Re: Setting Colour on font depending on their status!

Posted: 2022-01-04 10:53
by espo
Thanks again for the help.

Code: Select all

function TEST_init(&$options, $memberInfo, &$args) {
   // modify the status field of the table view query to display 'no' invoices in bold red
    var_dump($options->QueryFieldsTV);
  exit;

     $oldArray=$options->QueryFieldsTV;
     $options->QueryFieldsTV='';
      foreach($oldArray as $field => $caption){
       if($field=='`TEST`.`stato`'){
          $options->QueryFieldsTV['IF(`TEST`.`stato`=\'BIANCO\', \'<b style="color: red;">BIANCO</b>\',
									IF(`TEST`.`stato`=\'ROSSO\', \'<b style="color: green;">ROSSO</b>\', 
									IF(`TEST`.`stato`=\'VERDE\', \'<b style="color: #FF8243;">VERDE</b>\', 
									`TEST`.`stato`)))']=$caption;
      }else{
           $options->QueryFieldsTV[$field]=$caption;
        }
      }  
		return TRUE;
	}
I insert the code and the result of the page ../view.php

array(3) { ["`TEST`.`id`"]=> string(2) "id" ["`TEST`.`NOME`"]=> string(4) "NOME" ["`TEST`.`STATO`"]=> string(5) "STATO" }

Re: Setting Colour on font depending on their status!

Posted: 2022-01-04 13:36
by jsetzer
(1) As mentioned before, AppGini removes custom style attributes for avoiding XSS attacks. Even if you get your syntax right in PHP init-hook, the css-style will be removed.

(2) Also, as mentioned before, check the final HTML code in your browser's dev-tools. After customizing the TV column in init-hook and after AppGini has removed CSS styles for security reasons you will end up with sometihng like this:

Code: Select all

<span xss="removed">Test</span>
(3) Anyway, custom CSS-styling in TV seems to be important for a few users. I consider writing a blog post on this topic. Stay tuned.

Re: Setting Colour on font depending on their status!

Posted: 2022-01-04 14:51
by jsetzer
If someone is interested in customizing TV columns, I have just published Part 1 of a series of tutorials in my Blog:

:arrow: https://appgini.bizzworxx.de/appgini/tv ... nge-value/

If you are interested, just have a look. There are screenshots and complete source codes inside. I am going to continue writing blog articles about customizing TV the next hours if I find enough time. And I will also adress the XSS-security problems and show solutions.

Re: Setting Colour on font depending on their status!

Posted: 2022-01-04 15:12
by pbottcher
Hi,

you check

if($field=='`TEST`.`stato`'){

but in your datadump there is ["`TEST`.`STATO`"]=> string(5) "STATO"

So maybe the issue is related to the case-sensitivity.

Re: Setting Colour on font depending on their status!

Posted: 2022-01-04 15:35
by jsetzer
+1 @pböttcher

That's exactly the reason why I recommend (and prefer by myself) dumping out the exact keys and values. I see it quite often that programmers assume some prerequisites but do not check them.

Re: Setting Colour on font depending on their status!

Posted: 2022-01-05 10:02
by espo
Hello,

I also checked by entering capital words, but it doesn't work anyway.
I believe it is an XSS security issue at this point.
Stay tuned for updates ...

Thanks a lot to everyone.

Re: Setting Colour on font depending on their status!

Posted: 2022-01-06 10:25
by jsetzer
To see if your style has been removed or not, you can simply inspect the cell value in your browser's development tools:

chrome_T36pq9yG0I.png
chrome_T36pq9yG0I.png (49.17 KiB) Viewed 10317 times

If you can see xss="removed" there, well, your style has been removed for security reasons.

See also: Part 3 of my Blog series about customizing Table View cell values here:
https://appgini.bizzworxx.de/appgini/tv ... css-style/