CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Contact Summary page colours / colors in an extension using Jquery
Pages: [1]

Author Topic: Contact Summary page colours / colors in an extension using Jquery  (Read 509 times)

scrubba

  • I post occasionally
  • **
  • Posts: 31
  • Karma: 2
  • CiviCRM version: 4.6.2
  • CMS version: Wordpress 4.2
Contact Summary page colours / colors in an extension using Jquery
December 01, 2014, 04:20:34 pm
So I got inspired by the work of idea15
who added colour-coded custom data via wordpress admin using Jquery
http://idea15.wordpress.com/2014/08/26/how-to-colour-code-data-inputs-in-the-wordpress-admin-interface/

I had a client who asked for similar - but only needed it for the Contact summary page -
So decided to try and pull the idea15 approach into an extension.
I am just learning much of this,  - so maybe you civicrm folks have better ideas than mine - very happy for feedback, but maybe what follows could help someone..


The hook i used was just
http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_summary

SO I first created the basics of the extension the normal way by following -
http://wiki.civicrm.org/confluence/display/CRMDOC/Create+a+Module+Extension

then in the PHP file I just pointed to the above hook and an as yet not created colours.js file by adding the code:
Code: [Select]
function extensionname_civicrm_summary( $contactID, &$content, &$contentPlacement ) { 
{
CRM_Core_Resources::singleton()->addScriptFile('org.test.extensionname', 'colours.js');
}

You will have to rename 'extensionname' and 'org.test.extensionname' to your extension..

Then i created colours.js and saved it in the same extensions folder.. In it - i plonked the jquery -

Code: [Select]
cj(function ($) {
  'use strict';
  // Variables declared here will be globally available within this closure


//The following changes the colour of the External ID label - this uses the inline css method
  $("div.crm-label:contains(External ID)").css({"background-color": "yellow", "font-weight": "bold"})
   
//The following changes the background colours of the custom fields - if refers to the custom css classes you can find in plugins / files / civicrm / custom_css

  $("div.crm-custom-data:contains(Heavy Metal)").addClass('metal');
  $("div.crm-custom-data:contains(Industrial)").addClass('industrial');
  $("div.crm-custom-data:contains(Reggae)").addClass('reggae');
 
   
}

 
To explain - the three options "Heavy Metal / Industrial / Reggae" are in a custom field "Music taste" drop down menu that is set to display one option at a time on the contact summary page inline.

the bit that says ("div.crm-custom-data:contains(Heavy Metal)") just means if you see the word "Heavy Metal" as custom data - then apply this css class

So above there are two ways to pull css colours - Idea15 used css classes first created in a custom CSS file - dropped into a folder (you will have to create if not there) like 'custom_css'  - and then you need point to the right file at System Settings / Resource URLs.  This approach is probably best if you want to use the same class on different pages and forms etc

The example of my custom css looked like this:
Code: [Select]
/* The following classes change the background colour - for membership status */

.metal {
color: #000000 !important;
background-color: #FF3399 !important;
padding: 3px;
}

.industrial {
color: #000000 !important;
background-color: #00CCFF !important;
padding: 3px;
}

.reggae {
color: #000000 !important;
background-color: #FF9900 !important;
padding: 3px;
}

The other way to do it is shown by the line
$("div.crm-label:contains(External ID)").css({"background-color": "yellow", "font-weight": "bold"})

This just uses the .css jquery to add css right there in the code ..
if you do it this way its probably faster - the quick an dirty way - and you wont need a custom css file at all.
so if you want to do the same thing to the custom fields it would be something like -
$("div.crm-custom-data:contains(Heavy Metal)").css({"background-color": "red"})

The other thing to note is the two different selectors i used -
("div.crm-label:contains(External ID)") - works for labels on the summary page,
while
$("div.crm-custom-data:contains(Heavy Metal)")
works for the custom data

You can just change the words in the brackets to match your custom data or labels (titles)

There are endless ways to select the area you want to colour - My way is the same as idea15 - which is pretty lose - if i have say 'Heavy Metal' anywhere else on the summary page - say in a note - then it will change the colour of that too.

You can find heaps of selectors here;
http://www.w3schools.com/jquery/trysel.asp

Anyway - maybe there is a much easier way than this - but i like the extension format so i can turn it on and off quickly. As i said feedback welcome..







« Last Edit: December 01, 2014, 04:28:56 pm by scrubba »

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Contact Summary page colours / colors in an extension using Jquery
December 02, 2014, 12:38:27 am
Hi,

In general, I prefer matching on classes or id rather than rely on labels (eg. what if the admin change the style industrial to Industrial?) but the id (value) might not always be available from the html

As it's purely cosmetic, no big deal and fine solution. If it were more sensitive, I'd probably fetch the custom field (using the api) and inject the relevant css/js to be sure it will work even if the admin change the label.

Tanks for sharing.

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Michael McAndrew

  • Forum Godess / God
  • I live on this forum
  • *****
  • Posts: 1274
  • Karma: 55
    • Third Sector Design
  • CiviCRM version: various
  • CMS version: Nearly always Drupal
  • MySQL version: 5.5
  • PHP version: 5.3
Re: Contact Summary page colours / colors in an extension using Jquery
December 02, 2014, 02:21:15 am
Hey Scrubba - that is really cool work - you might want to write a blog post out of that since I think it deserves a bit more attention and it could be inspiring for others who want to do something similar / take your worth further - people who don't necessarily look in the forums.

Michael
Service providers: Grow your business, build your reputation and support CiviCRM. Become a partner today

idea15

  • I’m new here
  • *
  • Posts: 4
  • Karma: 2
  • CiviCRM version: 4.4.6
  • CMS version: WordPress
  • MySQL version: 5.5.32-cll
  • PHP version: 5.3.27
Re: Contact Summary page colours / colors in an extension using Jquery
December 02, 2014, 06:23:39 am
Wow! Really chuffed to see someone taking the trick I came up with and expanding on it. Keep up the good work!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • Contact Summary page colours / colors in an extension using Jquery

This forum was archived on 2017-11-26.