Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

dynamic cms sitetree icon


Go to End


15 Posts   5741 Views

Avatar
James Bolitho

Community Member, 33 Posts

20 December 2011 at 8:45am

Hi,

I have found myself in the same situation Keeny is describing. I know this is an oldish post and I have looked around but I haven't been able to work out a way to solve this yet. Has anyone achieved this? Would you be kind enough to share or point in the right direction.

Cheers,

Jim

Avatar
pelse

Community Member, 14 Posts

20 December 2011 at 11:18pm

Hi,

i have found a solution.
You can add the __construct function to the Page Class.

function __construct($record = null, $isSingleton = false) {    
    parent::__construct($record, $isSingleton);
    if((bool)$record['YOURSETTING']) Object::set_static('Page', 'icon', 'PATH/TO/TREEICONS/ICON');    
}

That´s it.

Avatar
James Bolitho

Community Member, 33 Posts

21 December 2011 at 1:57am

Hi Pelse,

Thanks for this but I cannot seem to get this to work. I am probably missing something obvious. Here is my code:

class SimpleShopCategoryPage extends Page 
{
.......
        public static $db = array(
		'CategoryPageCopy' => "Boolean"
	);
.......
       function __construct($record = null, $isSingleton = false) { 
		parent::__construct($record, $isSingleton); 
		if((bool)$record['CategoryPageCopy']) Object::set_static('SimpleShopCategoryPage', 'icon', 'SimpleShop/images/treeicons/storecopy'); 
	}

What am I doing wrong? Thanks for your help.

Cheers,

Jim

Avatar
James Bolitho

Community Member, 33 Posts

24 December 2011 at 6:14am

Hi,

I have had another look at this and the reason it is not working is because the if clause is returning false.

I did the following:

echo $record['CategoryPageCopy'];

and it returned the following value: 11111111

Can anyone explain why this is doing this as I thought it would return 1 or 0 depending on whether the checkbox was ticked? I am now a little confused.

Thanks,

Jim

Avatar
Willr

Forum Moderator, 5523 Posts

9 January 2012 at 7:11pm

Can anyone explain why this is doing this as I thought it would return 1 or 0 depending on whether the checkbox was ticked? I am now a little confused.

I would hazard a guess that your constructor is being called multiple times, hence the multiple 1's. If you echo "1." you'll likely see 1.1.1.1.1.

Avatar
James Bolitho

Community Member, 33 Posts

7 March 2012 at 5:24am

Hi,

Thanks Willr for the response. I have created a round about solution for this and I thought I would share how I did it. It doesn't replace the page icon but adds a new class to the list items on a site tree if a checkbox on this particular page class is ticked allowing you to add styling to make it stand out on the site tree.

Here is the code:

1) Add to config file details of custom extension, js and css to be included on CMS Main page.

mysite/_config.php

//Change icon background if copy category
LeftAndMain::require_javascript('mysite/javascript/treeaddclass.js');
LeftAndMain::require_css('mysite/css/treeicon.css');
//custom extension
LeftAndMain::add_extension('LeftAndMain', 'ActsLeftAndMain');

2) Custom extension to do a database query and return all page id's where the checkbox has been checked.

mysite/code/ActsLeftAndMain.php

class ActsLeftAndMain extends Extension {
	function getData(){
		$records = DB::query ("SELECT `CategoryPage`.`ID` FROM `CategoryPage` WHERE `CategoryPage`.`CategoryPageCopy` = 1");
		if($records) {
			$i = 0;
			$length = count($records)-1;
			foreach($records as $singlerecord){
				if($i == $length) {
					$set = $singlerecord['ID'];
				} else {
					$set .= ",";
					$set .= $singlerecord['ID'];	
				}
				$i++;
			}
			return $set;
		}	
	}
}

3) Create a modified CMSMain_right.ss template to output data.

mysite/templates/CMSMain_right.ss

<div id="CategoryPageCopyIDs" style="height: 0px; display: none;">$getData</div>
<div id="form_actions_right" class="ajaxActions">
</div>

<% if EditForm %>
	$EditForm
<% else %>
	<form id="Form_EditForm" action="admin?executeForm=EditForm" method="post" enctype="multipart/form-data">
		<h1>$ApplicationName</h1>

		<p><% _t('WELCOMETO','Welcome to') %> $ApplicationName! <% _t('CHOOSEPAGE','Please choose a page from the left.') %></p>
	</form>
<% end_if %>

<p id="statusMessage" style="visibility:hidden"></p>

4) Add some jQuery to grab info from page and add new or remove css class to the sitetree nodes.

mysite/javascript/treeaddclass.js

(function(jQuery) {
jQuery(document).ready(function() {
	
	//Gets the ID's of category pages
	var IDs = jQuery('div#CategoryPageCopyIDs').html();
	
	if(IDs){
		var IDsString = jQuery.parseJSON('[' + IDs + ']');			
		jQuery.each(IDsString, function() {
			jQuery('li#record-'+ this).addClass('copyIcon');
		});
	}
	
	jQuery("input#Form_EditForm_action_publish").live("click", function() {				
		var ID = jQuery('input#Form_EditForm_ID').val();
		if(jQuery('#Form_EditForm input#Form_EditForm_CategoryPageCopy').is(':checked')) {
			jQuery('li#record-'+ ID).addClass('copyIcon');	
		} else {
			jQuery('li#record-'+ ID).removeClass('copyIcon');
		}
	})
	
})
})(jQuery);

5) Add styling to the sitetree nodes.

mysite/css/treeicon.css

ul.tree li.copyIcon {
    background: #E8F3FC;
    border-radius: 10px;
   /* CSS3 tweaks for modern browsers */
   -moz-border-radius:10px;
   -webkit-border-radius:10px;
}

That will allow you to easily tell if you have a checkbox ticked on a certain page and updates when save and publish is pressed or the window is reloaded. I hope this helps others.

Thanks,

Jim

Avatar
tineklancar

Community Member, 1 Post

22 June 2012 at 8:36pm

I found another (simpler) solution to this one, in the page class you want his just override CMSTreeClasses function to add the classes you like and modify it with css

function CMSTreeClasses($controller) {

Requirements::css("mysite/css/TreeSelector.css");

$classes = parent::CMSTreeClasses($controller);

if($this->Status == "Published")
$classes .= " yourCustomCssClass";

return $classes;
}

Go to Top