public function updateCMSFields(FieldList $fields)
 {
     //Only remove fields if remove is enabled and Simplify isn't disabled
     if (SimplifyPermissionProvider::getRemoveEnabled() && !SimplifyPermission::check("SIMPLIFY_DISABLED")) {
         //Get all SimplifyPermissions that refer to this DAO class
         $hideFields = SimplifyPermission::getPermissionsByPage($this->owner->class);
         //Remove them..
         if ($hideFields) {
             foreach ($hideFields as $hideField) {
                 $dataFieldOnly = false;
                 //TODO: Make better - fix to prevent Content tab being removed when Content field is being removed
                 if ($hideField->HideName == "Content") {
                     if ($hideField->HideType != "Tab" && $hideField->HideType != "TabSet") {
                         $dataFieldOnly = true;
                     }
                 }
                 $fields->removeByName($hideField->HideName, $dataFieldOnly);
             }
         }
     }
 }
    /**
     * Add the Simplify Tab to the Group edit page
     * 
     * @param FieldSet $fields	List of CMS fields to update 
     */
    public function updateCMSFields(FieldList $fields)
    {
        if ($this->owner->class == "Group") {
            //print_r($this->owner->Permissions());
            $groupID = $this->owner->ID;
            //Only remove fields if Simplify isn't disabled
            if (!SimplifyPermission::check("SIMPLIFY_DISABLED")) {
                //Check if any Simplify Permissions are disabling Secutity Group fields..
                if (SimplifyPermission::check("SIMPLIFY_SECURITY_HIDE_MEMBERS")) {
                    $fields->removeByName("Members");
                }
                if (SimplifyPermission::check("SIMPLIFY_SECURITY_HIDE_PERMISSIONS")) {
                    $fields->removeByName("Permissions");
                }
                if (SimplifyPermission::check("SIMPLIFY_SECURITY_HIDE_IP")) {
                    $fields->removeByName("IP Addresses");
                }
                //If the permisison to hide the Simplify tab itself is set, exit now - as we don't want to create the tab
                if (SimplifyPermission::check("SIMPLIFY_SECURITY_HIDE_SIMPLIFY")) {
                    return;
                }
            }
            //Create the Simplify TabSet
            $fields->addFieldToTab("Root", new TabSet("Simplify"));
            //loop Through permissions and build header tabs and checkboxes
            foreach (SimplifyPermissionProvider::mergedPermissions() as $title => $grouping) {
                $tab = str_replace(" ", "", $title);
                $fields->findOrMakeTab("Root.Simplify.{$tab}");
                foreach ($grouping as $code => $label) {
                    //See if perm exists
                    $perm = DataObject::get_one("Permission", "\"Code\"='{$code}' AND \"GroupID\"={$groupID}");
                    $setChecked = "";
                    if ($perm) {
                        $checked = 1;
                        $setChecked = "checked='checked'";
                    } else {
                        $checked = 0;
                    }
                    //$fields->addFieldToTab("Root.Simplify.{$tab}", new CheckboxField($code."|".$groupID, $label, $checked));
                    //TODO: Should be line above, have to hack for now with onclick to avoid race cond.
                    $fields->addFieldToTab("Root.Simplify.{$tab}", new LiteralField($code . "|" . $groupID, "<p id='{$code}|{$groupID}' class='checkbox'>\n\t\t\t\t\t\t\t<input type='checkbox' value='1' {$setChecked} name='{$code}' onclick='Simplify.PermissionToggle(this)'/>\n\t\t\t\t\t\t\t<label class='right' for='{$code}'>{$label}</label>\t\t\t\t\t\t\t\n\t\t\t\t\t\t</p>"));
                }
            }
            //Add button lists to the HTML Editor tab
            $fields->addFieldsToTab("Root.Simplify.HTMLEditor", array(new TextField("HTMLEditorLine1", "Line 1"), new TextField("HTMLEditorLine2", "Line 2"), new TextField("HTMLEditorLine3", "Line 3"), new LiteralField("HTMLNote", "\n\t\t\t\t\t<p class='simpHead'>Default Buttons</p>\n\t\t\t\t\t<p class='simpHead'>Line 1</p>\n\t\t\t\t\t<p class='default1'>" . implode(",", self::$editor_buttons[1]) . "</p>\n\t\t\t\t\t<p class='simpHead'>Line 2</p>\n\t\t\t\t\t<p class='default2'>" . implode(",", self::$editor_buttons[2]) . "</p>\n\t\t\t\t\t<p class='simpHead'>Line 3</p>\n\t\t\t\t\t<p class='default3'>" . implode(",", self::$editor_buttons[3]) . "</p>\n\t\t\t\t\t<p><button class='simplifyHtmlDefaults action' type='button'>Reset to defaults</button></p>\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t")));
            //Add select/deselect all to Page Creation
            $pageCreation = $fields->findOrMakeTab("Root.Simplify.PageCreation");
            $firstField = $pageCreation->Fields()->First();
            $fields->addFieldToTab("Root.Simplify.PageCreation", new LiteralField("SelectDeselect", "<button class='simplifyPageCreationAll'>Select all</button><button class='simplifyPageCreationNone'>Deselect all</button>"), $firstField->getName());
            //TODO - these are future niceities..impl them!
            //$fields->addFieldToTab("Root.Simplify", new CheckboxField("FieldTreeInherit", "Inherit Permissions"));
            //$fields->addFieldToTab("Root.Simplify", new CheckboxField("FieldTreeDisable", "Disable instead of hiding"));
            //Create Field Tree tab
            //This is hacky until SS 2.4 comes with its improved admin JS
            //Tree is loaded by AJAX button as its too big to load whole tree
            //TODO: Eventually replace with tree with AJAX branches
            $fields->addFieldsToTab("Root.Simplify.Fields", array(new LiteralField("FieldTreeNote", "<p>Hide the following Pages, Tabs and Fields from this group (in the CMS).</p>"), new LiteralField("TreeActivator", '<div id="TreeActivator"> 
							<input class="simplifyGroupCode" type="hidden" name="groupCode" value="' . $this->owner->Code . '"> 
							<button onclick="Simplify.TreeActivator()" type="button" class="action">Load Field Tree...</button>
					</div>')));
        }
    }
 /**
  * Checks Simplify default and custom permission, also combines the CSS and JS.
  * Called from LeftAndMain
  * 
  * @return null
  */
 function init()
 {
     //add global js + css required by Simplify - these aren't permissions, just support code
     //DEPREACATED?
     //TODO - what does/did this do? No longer works with new 3.x JS
     // Look at simplify_global to see use of MultiselecttreeFIX
     //$treeJS = "simplify/javascript/simplify_multiselect_tree.js";
     //LeftAndMain::require_javascript($treeJS);
     //self::$js_files[] = $treeJS;
     $globalJS = "simplify/javascript/simplify_global.js";
     Requirements::javascript($globalJS);
     self::$js_files[] = $globalJS;
     //TODO -BUG FIX ME - uses old JS, need to fix this to work with new 3.x CMS JS
     //Block the JS file used to perform Permission/Full admin rights toggle - replace it with our own
     /*Requirements::block(SAPPHIRE_DIR . '/javascript/PermissionCheckboxSetField.js');
     		$checkboxJS = "simplify/javascript/simplify_PermissionCheckboxSetField.js";
     		LeftAndMain::require_javascript($checkboxJS);
     		self::$js_files[] = $checkboxJS;
     		*/
     $globalCSS = "simplify/css/simplify_global.css";
     Requirements::css($globalCSS);
     self::$css_files[] = $globalCSS;
     //Set defaults if initial load (on ALL groups)
     SimplifyGroupDecorator::set_html_editor_defaults();
     //only apply Simplify perms is they're not disabled
     if (!SimplifyPermission::check("SIMPLIFY_DISABLED")) {
         $permissionProvider = new SimplifyPermissionProvider();
         //check default permissions
         self::checkPermissions($permissionProvider->providePermissions(), 'simplify/css/', 'simplify/javascript/');
         //check custom (user) permissions
         self::checkPermissions(SimplifyPermissionProvider::getCustomPermissions(), SimplifyPermissionProvider::getCustomCSSPath(), SimplifyPermissionProvider::getCustomJSPath());
         //Some permissions require code - execute that here
         //Hide the help menu
         if (SimplifyPermission::check("SIMPLIFY_HIDE_HELP")) {
             CMSMenu::remove_menu_item('Help');
         }
         //Get the HTML Editor button lists for this user and customise the editor
         if (SimplifyPermission::check("SIMPLIFY_CUSTOM_HTML_EDITOR")) {
             //Put all lines into an array, iterate over each - if they have content it will be "button1,button2,button3"
             //explode this into an array and use HtmlEditorconfig to set the line
             //TODO: this gets the first group the member belongs to - they may belong to many
             //need to sort out how this is handled
             $group = Member::currentUser()->Groups()->First();
             $lines = array($group->HTMLEditorLine1, $group->HTMLEditorLine2, $group->HTMLEditorLine3);
             $config = HtmlEditorConfig::get('cms');
             $i = 1;
             foreach ($lines as $line) {
                 $lineArray = array();
                 if ($line) {
                     $lineArray = explode(",", $line);
                 }
                 $config->setButtonsForLine($i, $lineArray);
                 $i++;
             }
         }
         //Hide page classes from create dropdown if set
         //First, get all no create permissions for the current user
         $groupList = Member::currentUser()->Groups()->getIdList();
         $groupCSV = implode(", ", $groupList);
         $perms = DataObject::get("Permission", "\"Code\" like 'SIMPLIFY_NO_CREATE_%' AND \"GroupID\" IN ({$groupCSV})");
         if ($perms) {
             foreach ($perms as $perm) {
                 //TODO - do this more elegantly
                 $page = str_replace("SIMPLIFY_NO_CREATE_", "", $perm->Code);
                 //This 'hack' uses the hide_ancestor static to remove itself
                 //See SiteTree::page_type_classes() for the call
                 Config::inst()->update($page, "hide_ancestor", $page);
                 //Note that this hides it from Behaviour/Page type which is bad -
                 //SimplifyDataObjectDecorator updateCMSFields ensures it is set
             }
         }
         //TODO: this seems to fail, only adds the global css + js - fix
         //Combine js and css for live deployment
         /*
         Requirements::combine_files(
         	'assets/simplify.css',
         	self::$css_files
         );
         
         Requirements::combine_files(
         	'assets/simplify.js',
         	self::$js_files
         );
         */
     }
 }
 /** 
  * 
  * Custom getChildrenAsUL - specific for Pages/Tabsets/Tabs/Fields
  * TODO this is very slow - improve it!
  * TODO could load branches via AJAX instead
  */
 public static function getChildrenAsUL($fields, $level = 0, $ulExtraAttributes = null, $parentPage, &$itemCount = 0)
 {
     $output = "";
     $hasNextLevel = false;
     //Set to true to remove any node from being displayed. Its children still will be.
     $removeNode = false;
     //Remove Root, as its not really needed and confuses this tree
     if (is_a($fields, "FieldSet") && is_a($fields->First(), "TabSet")) {
         $firstField = $fields->First();
         $firstField = method_exists($firstField, "Name") ? $firstField->Name() : "";
         if ($firstField == "Root") {
             $removeNode = true;
         }
     }
     if (!$removeNode) {
         $output = "<ul {$ulExtraAttributes}>\n";
     }
     $ulExtraAttributes = null;
     foreach ($fields as $field) {
         $css = '';
         $display = '';
         $recurse = false;
         $name = '';
         $type = '';
         //Handle Page classes and children (getCMSFields)
         if (is_a($field, "Page")) {
             $css .= "tree-page ";
             $recurse = true;
             $name = $field->class;
             $display = $field->class;
             $parentPage = $field->class;
             $children = $field->getCMSFields(null);
         } else {
             //Handle TabSet classes and children (Tabs)
             if (is_a($field, "TabSet")) {
                 $css .= "tree-tabset ";
                 $recurse = true;
                 $display = method_exists($field, "Name") ? $field->Name() : $field->class;
                 $name = $display;
                 $children = $field->Tabs();
             } else {
                 //Handle Tab classes and children (Fields)
                 if (is_a($field, "Tab")) {
                     $css .= "tree-tab ";
                     $recurse = true;
                     $display = method_exists($field, "Name") ? $field->Name() : $field->class;
                     $name = $display;
                     $children = $field->Fields();
                 } else {
                     //Handle all FormField subclasses - excluding LiteralField
                     //If the class doesn't have a Title, display the class instead
                     //If the class has a Name, display that in brackets afterwards (maybe, comm for now)
                     if (is_subclass_of($field, "FormField") and !is_a($field, "LiteralField")) {
                         $title = method_exists($field, "Title") ? $field->Title() : $field->class;
                         $name = method_exists($field, "Name") ? $field->Name() : $field->class;
                         if (!$title) {
                             $title = $field->class;
                         }
                         $css .= "tree-field ";
                         $display = $title . "(" . $field->class . ")";
                     } else {
                         //Handle LiteralField classes - the content is HTML, so convert to raw first
                         if (is_a($field, "LiteralField")) {
                             $css .= "tree-literal ";
                             $name = method_exists($field, "Name") ? $field->Name() : $field->class;
                             $display = Convert::xml2raw($field->getContent());
                         } else {
                             //If the item isn't any of the above classes, we don't know what it is...
                             $css .= "tree-unknown ";
                             $name = method_exists($field, "Name") ? $field->Name() : $field->class;
                             $display = $field->class . " is an unknown type...";
                         }
                     }
                 }
             }
         }
         //Find out if this field has a SimplifyPermission entry for the given group
         if (SimplifyPermission::checkField($parentPage, $name, $field->class, self::$group)) {
             $css .= 'selected ';
         }
         //Build the page|field|type|group key
         $code = $parentPage . "|" . $name . "|" . $field->class . "|" . self::$group->ID;
         //Build the node
         if (!$removeNode) {
             $output .= "<li class='{$css}'><a href='#' rel='{$code}'>{$display}</a>\n";
         }
         //Do the recursive call
         if ($recurse) {
             $output .= self::getChildrenAsUL($children, $level + 1, $ulExtraAttributes, $parentPage);
         }
         if (!$removeNode) {
             $output .= "</li>\n";
         }
         $itemCount++;
     }
     if (!$removeNode) {
         $output .= "</ul>\n";
     }
     return $output;
 }