We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5683
    • 96 Posts
    Here is a simple Snippet that allows web users to join or leave web groups.
    In the configuration section you must specify which web groups are allowed to be joined/left.

    Only basic functionality is provided; you are welcome to expand it! wink

    //
    // WebGroupJoin v0.1 for MODx
    // (Tested on MODx 0.9.1)
    //
    // By Luca Allulli, February 2006
    // License: GNU GPL
    //
    
    //Begin configuration
    
    	//Web groups that web users are allowed to join/leave
    		$webUserGroups=array("Teachers", "Students");
    		
    	//Message strings
    		$submitButton="Subscribe";
    		$successful="Successfully subscribed.";
    		
    //End configuration
    
    $output="";
    $uid=$modx->getLoginUserID();
    
    if(!isset($uid)) return "";
    
    $sql = 'SELECT webgroup FROM'.$modx->getFullTableName("web_groups").' WHERE webuser='.$uid.';';
    
    $joinedGroups=$modx->db->getColumn("webgroup", $sql);
    
    
    $sql = "SELECT id, name FROM".$modx->getFullTableName("webgroup_names")." WHERE name IN ('".implode("','",$webUserGroups)."');";
    
    $rs=$modx->db->query($sql);
    
    while($row=$modx->fetchRow($rs)) {
    	$id=$row['id'];
    	$webGroupName[$id]=$row['name'];
    	$webGroupJoined[$id]=in_array($id, $joinedGroups);
    }
    	
    if(!isset($webGroupName)) return "";
    	
    if(!isset($_POST['posted'])) {
    	//Ask user for input
    	
    	$output.='<form action="index.php?id='.$modx->documentIdentifier.'" method="post">';
    	$output.='<input type="hidden" name="posted" value="yes" />';
    	foreach($webGroupName as $id => $name) {
    		$output.='<input type="checkbox" name="group'.$id.'" ';
    		if($webGroupJoined[$id])
    			$output.='checked="checked" ';
    		$output.='" />'.$webGroupName[$id].'<br />';		
    	}
    	$output.='<input type="submit" value="'.$submitButton.'" /></form>'."\n";
    
    } else {
    	//Process input
    	
    	foreach($webGroupJoined as $id => $joined) {
    		if(isset($_POST['group'.$id]) && !$joined) {
    			$sql = 'INSERT INTO '.$modx->getFullTableName("web_groups").' (id, webgroup, webuser) VALUES (NULL, '.$id.', '.$uid.');';
    			
    			$rs=$modx->db->query($sql);
    			
    		}
    		if(!isset($_POST['group'.$id]) && $joined) {
    			$sql = 'DELETE FROM '.$modx->getFullTableName("web_groups").' WHERE webgroup='.$id.' AND webuser='.$uid.';';
    			
    			$rs=$modx->db->query($sql);
    		}
    	}
    	$output.=$successful;
    }
    
    
    return $output;
    
    • This would be most useful with a newsletter snippet!
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 5683
        • 96 Posts
        Quote from: sottwell at Feb 17, 2006, 05:53 PM

        This would be most useful with a newsletter snippet!
        You got it grin
          • 32241
          • 1,495 Posts
          Ehmmm... It’s been implemented already wink Check out Newsletter snippets that I released...

          Good job though...
            Wendy Novianto
            [font=Verdana]PT DJAMOER Technology Media
            [font=Verdana]Xituz Media
            • 5683
            • 96 Posts
            Quote from: Djamoer at Feb 17, 2006, 06:43 PM

            Ehmmm... It’s been implemented already wink Check out Newsletter snippets that I released...
            Ooopps....
            Your snippet is certainly more complete wink

            Anyway, a standalone snippet to join webgroups could still make some sense... and it was instructive for me to implement it wink
            • Honestly, I really like the idea of a group of function-specific snippets that can be chained together or included in each respective template file to build out apps. MemberCheck is another example of this type of functionality.
                Ryan Thrash, MODX Co-Founder
                Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
              • That is the UNIX way!
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org