/**
  * provideI18nEntities
  * Creates an i18n entry for each permission in the namespace SIMPLIFY.permkey	 * 	  
  * @return All permission as entites in an associative array
  */
 function provideI18nEntities()
 {
     $entites = array();
     foreach (SimplifyPermissionProvider::providePermissions() as $permissionKey => $permission) {
         $entities["SIMPLIFY." . $permissionKey] = array($permission);
     }
     return $entities;
 }
 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);
             }
         }
     }
 }
 /**
  * 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
         );
         */
     }
 }
    /**
     * 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>')));
        }
    }
 public static function setRemoveEnabled($removeEnabled)
 {
     self::$removeEnabled = $removeEnabled;
 }
 /**
  *  Display a tree of all page types and their children, with perms
  *
  *  @param Group group show permissions related to this group
  *  @return string A fully formatted tree as a HTML list	
  */
 public static function PageTree($group = null)
 {
     self::$group = $group;
     //Get all the page types
     $classes = SiteTree::page_type_classes();
     $pageTreeList = array();
     //We don't want to remove the fields from this list, so disable the remove
     SimplifyPermissionProvider::setRemoveEnabled(false);
     //Get an instance of each page type, add to an array
     //TODO: This works, but only returns the initial field state of the objects
     //      Might be a better way to scrape ALL the possible fields?
     foreach ($classes as $class) {
         $instance = singleton($class);
         if ($instance instanceof HiddenClass) {
             continue;
         }
         if (!$instance->canCreate()) {
             continue;
         }
         $pageTreeList[] = $instance;
     }
     //Get the children of each page type and return it as an UL
     $pageTree = self::getChildrenAsUL($pageTreeList, 0, " id='perm-tree' class='tree' ", "SiteTree");
     //Re-enable the remove
     SimplifyPermissionProvider::setRemoveEnabled(true);
     return $pageTree;
 }