Jump to:

10931 Posts in 2614 Topics by 1810 members

All other Modules

SilverStripe Forums » All other Modules » dynamic cms sitetree icon

Discuss all other Modules here.

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

Page: 1 2
Go to End
Author Topic: 2890 Views
  • James Bolitho
    Avatar
    Community Member
    33 Posts

    Re: dynamic cms sitetree icon Link to this post

    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

  • pelse
    Avatar
    Community Member
    9 Posts

    Re: dynamic cms sitetree icon Link to this post

    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.

  • James Bolitho
    Avatar
    Community Member
    33 Posts

    Re: dynamic cms sitetree icon Link to this post

    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

  • James Bolitho
    Avatar
    Community Member
    33 Posts

    Re: dynamic cms sitetree icon Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: dynamic cms sitetree icon Link to this post

    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.

  • James Bolitho
    Avatar
    Community Member
    33 Posts

    Re: dynamic cms sitetree icon Link to this post

    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

  • tineklancar
    Avatar
    Community Member
    1 Post

    Re: dynamic cms sitetree icon Link to this post

    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;
       }

    2890 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.