Source of file SimplifyDataObjectDecorator.php

Size: 4,061 Bytes - Last Modified: 2021-12-24T06:48:44+00:00

/var/www/docs.ssmods.com/process/src/code/SimplifyDataObjectDecorator.php

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
<?php
/**
 * SimplifyDataObjectDecorator
 * Decorates DataObject to remove fields from a Page
 * TODO: Might be better to make this more specific??
 * 
 * @package simplify
 */
class SimplifyDataObjectDecorator extends SiteTreeExtension
{

    public static $many_many = array(
        "SimplifyHideDeleteGroups" => "Group",
        "SimplifyHideUnpublishGroups" => "Group"
    );

    public function updateCMSActions(FieldList $actions)
    {
        
        //Remove delete and/or unpublish buttons if set for this users group
        // Do this by getting the list of Groups set in the Access  tab, looping through
        // them and comparing them to the current members group
        $hideDeleteGroups = $this->owner->SimplifyHideDeleteGroups();
        $hideUnpublishGroups = $this->owner->SimplifyHideUnpublishGroups();
        $memberGroup = Member::currentUser()->Groups()->First();
        
        foreach ($hideDeleteGroups as $deleteGroup) {
            if ($deleteGroup->Code == $memberGroup->Code) {
                $actions->removeByName("action_delete");
            }
        }
        
        foreach ($hideUnpublishGroups as $unpublishGroup) {
            if ($unpublishGroup->Code == $memberGroup->Code) {
                $actions->removeByName("action_unpublish");
            }
        }
    }

    public function updateSettingsFields(FieldList $fields)
    {
        //Add extra fields to Settings tab to hide action buttons
        $fields->addFieldsToTab("Root.Settings", array(
            new HeaderField('SimplifyHideDelete', _t('Smplify.HIDEDELETEHEADER', "Simplify - Hide Delete button from these users"), 2),
            new TreeMultiselectField("SimplifyHideDeleteGroups", 'Hide Delete Groups'),
            new HeaderField('SimplifyHideUnpublish', _t('Smplify.HIDEUNPUBLISHHEADER', "Simplify - Hide Unpublish button from these users"), 2),
            new TreeMultiselectField("SimplifyHideUnpublishGroups", 'Hide Unpublish Groups')
        ));
    }


    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);
                }
            }
        }
    }
    
    
    /**
     * 
     *This attaches custom classes to the tree items - use this to hide pages
     * 
     */
    public function markingClasses()
    {
        //Every DataObject has the Hierarchy extension attached to it,
        //call its markingClasses() method first to determine $classes
        $hierarchyExtension = $this->owner->getExtensionInstance('Hierarchy');
        $hierarchyExtension->setOwner($this->owner);
        $classes = $hierarchyExtension->markingClasses();

        //Hide pages that have been selected in the top level of the Simplify fields tree -
        //These will be where the field = page, ie; HidePage = HideName
        $hideThisPage = DataObject::get("SimplifyPermission",
            "\"HidePage\" = \"HideName\" AND \"HidePage\" = '{$this->owner->class}'");
            
        if ($hideThisPage) {
            $classes .= " hide";
        }
        return $classes;
    }
}