Skip to content

Generic Form Snippet w/ Group Restrictions

<?php

// Written By: Tom Maneri (tmaneri@pint.com)

/**************************************
    
    Generic Form Snippet w/ Group Restrictions
    
    This snippet can be used for any form through the PWP.
    If the form is not a site group form, make sure that
    the $defaultFieldCategory variable is set.
    
    The main sections to change will be in SETTINGS and DISPLAY.
    Don't change the CONTENT GENERATION section unless you know
    what you are doing!

    Check the Biogen Idec edit profile page for an example.

**************************************/

/**************************************
    SETTINGS
**************************************/
 
    // Page header
    $pageTitle = "Profile Settings";

    // default field category if there isn't one present
    $defaultFieldCategory = $pageTitle;
    
    /**************************************
        RESTRICTIONS
    **************************************/
    
        // Restrict component groups to specific site group ids.
        // ',' represents OR and '&' represents AND.
     
        // Only allow users that are in site group (1 AND 2) OR 3
        // $restrictedFieldCategory["Extra Settings"] = "1&2,3";

        // Only allow users that are in site group 33
        $restrictedFieldCategory["My Investigator Information"] = "33";


/**************************************
    CONTENT GENERATION
**************************************/

    // get the viewers site group id list
    $currentUserGIDs = explode( ",", userGetValue( 'siteGroupIdList' ) );

    // check all restricted field categories for view site group list matches
    foreach( $restrictedFieldCategory as $heading => $gids )
    {
        $hidden = true;
        $gids = explode(",", $gids);
        foreach( $gids as $gid )
        {
            if( strpos( "&", $gid ) !== false )
            {
                $reqGids = explode( "&", $gid );
                $i = count( $reqGids );
            }
            else
            {
                $reqGids = $gid;
                $i = 1;
            }
            $j = 0;
            foreach( $reqGids as $reqGid )
            {
                if( !in_array($reqGid, $currentUserGIDs) )
                {
                    break;
                }
                $j++;
            }
            if( $j == $i )
            {
                $hidden = false;
                break;
            }
        }
        if( $hidden ) $hiddenTabs["$heading"] = true;

    }

    // prepare the default field category incase we need it
        $defaultFieldCategory = str_replace ( " ", "_", $defaultFieldCategory );
        $$defaultFieldCategory = array();
        ${$defaultFieldCategory."Errors"} = false;


    // build the array of field categories and an array of fields for each
    $headers = array();
    $inHiddenSection = false;
    foreach( $components as $component )
    {
        if( $component->getIsHeading() )
        {
            $heading = $component->display();
            
            if( $hiddenTabs["$heading"] )
                $inHiddenSection = true;
            else
                $inHiddenSection = false;
            
            if( !$inHiddenSection )
            {
                $currentFieldCategory = str_replace ( " ", "_", $heading );
                $headers[] = $currentFieldCategory;
                $$currentFieldCategory = array();
                ${$currentFieldCategory."Errors"} = false;
            }
        }
        else if( !$inHiddenSection ) {
            
            // If we havn't come across a field category, set the current field
            // category to the default
            if( !isset($currentFieldCategory) || !isset($$currentFieldCategory) )
            {
                $currentFieldCategory = str_replace ( " ", "_", $defaultFieldCategory );
                if( !in_array( $currentFieldCategory, $headers ) )
                    $headers[] = $currentFieldCategory;
            }        
            
            $label = trim($component->getLabel());
            
            $content = "";

            $content .= "<tr><td style=\"text-align: right; padding: 0 3px 3px 0;\">";
            if( $component->getRequired() )
                $content .= '<span class="required">* </span>';    
            if( $label )
                $content .= $label . ':';
            $content .= '</td><td style="padding-bottom: 3px;">';
            $content .= $component->display();
            
            if ($component->getHasError()) {
            
                ${$currentFieldCategory."Errors"} = true;
            
                $errMessage = $component->getErrorMessage();
                if(!is_array($errMessage))
                {
                    $content .= "<span class=\"required\"><small>";
                    if (is_array($errMessage))
                        $content .= "<li>" . arrayToList($errMessage, '</li><li>') . "</li>";
                    else
                        $content .= "&nbsp; $errMessage";
                    $content .= "</small></span>";
                }
            }

            $content .= "</td></tr>";        

            //add this component content to the current field category array
            array_push( $$currentFieldCategory, $content );
        }
    }
?>


<?php
/**************************************
    DISPLAY
**************************************/
?>

<div class="form">
<form name="##$name##" id="##$name##" action="##$action##" method="post">

<?php
foreach ($hiddenFields as $hiddenField) {
	echo "<input type=\"hidden\" name=\"$hiddenField[name]\" ";
    echo "id=\"$hiddenField[id]\" value=\"";
    echo addslashes($hiddenField['value']) . "\" />";
}
?>

<h1>##$pageTitle##</h1>
<div class="tabs" id="profile">

	<ul class="yui-nav clearfix">
    
    <?php
    //special case classes
    $ifFirstTabClass = "selected";
    $hasErrorTabClass = "required";
    
    //loop through all of the headers to create the li elements
    $isFirst = true;
    foreach( $headers as $header )
    {
        //get the heading by replacing '_' with ' ' from the header id
        $heading = str_replace ( "_", " ", $header );
        
        //check if there is an error in this group of components
        $headingHasError = ${$header."Errors"};

        //build the li and echo
        $li = "<li id='{$header}Tab'";
        $li .= ( $isFirst ? " class='$ifFirstTabClass'" : "" );
        $li .= "><a href='#'";
        $li .= ( $headingHasError ? " class='$hasErrorTabClass'" : "" );
        $li .= ">$heading</a></li>";
        echo $li;
        
        //we've done at least the first, set to false
        $isFirst = false;
    }
    ?>
    
	</ul>
	<div class="yui-content">
        <?php
        //loop through all of the headers
        foreach( $headers as $header )
        {
        ?>
            <table cellpadding="0" cellspacing="0">
                <tr><td>        
                <?php
                //loop through all of the components for this header
                foreach( $$header as $result )
                    echo $result;
                ?>
                </td></tr>
            </table>
        <?php
        }
        ?>
    </div><!-- END: yui-content -->
</div><!-- END: tabs -->

<div style="padding: 5px;">
    <input type="submit" name="apply" value="Apply Changes" class="button" />
</div>

</form>
</div><!-- END: form -->

<script type="text/javascript" 
        src="http://yui.yahooapis.com/2.5.2/build/element/element-beta-min.js"></script> 
<script type="text/javascript"
        src="http://yui.yahooapis.com/2.5.2/build/tabview/tabview-min.js"></script> 
<script type="text/javascript">

YAHOO.util.Event.onAvailable('profile', function(e){
   PINT.tabs = new YAHOO.widget.TabView('profile');
});

</script>