Hi, I’m using WebLoginPE to manage a private gallery. It’s working really well on all fronts. I would however like to give my user the option to check a box on the profile update page, which will add then to an additional webgroup.
I’ve tried a variety of form inputs (e.g. below) as well as &customFields and &inputHandler, but can’t seem to get it to write to the db. Any insight would be GREATLY appreciated!
<label for="wlpeUserProfileWebGroup">Uprgade
<input id="wlpeUserProfileWebGroup" type="checkbox" name="webgroup" value="2" />
</label>
Thanks,
Nick
-
☆ A M B ☆
- 24,524 Posts
As far as I know, WLPE doesn’t have any option for doing that, it only allows for putting the user in the group(s) specified in the snippet parameters on creation. You’ll need a plugin to add the user to the group using the OnWebSaveUser event. The user ID and the webgroup ID need to be checked in the modx_web_groups table to make sure the user doesn’t already belong to that group (actually you could even prevent that checkbox from being in the form, or have it checked, if the user is already in the group), then add the user ID and group ID to the table.
I think it would be best to also add this as a custom field in the custom table, that way checkbox can easily be "checked" via the WLPE API if the user already is in the group.
some times ago, but not for the latest wlpe I hacked wlpe a little to add something like this to it.
I don’t know if this will work for you and for the actual version of wlpe.
see:
http://modxcms.com/forums/index.php/topic,18272.msg140731.html#msg140731
Thanks Susan & Bruno,
I really appreciate your replies! I’m a designer so battle a bit with of the code;)
Bruno, I’ve tried implementing your code but haven’t quite got it working. From my understanding of your ViewUserProfile code, I have added the following form element in the $wlpeProfileTpl template (seeing as, from my experience, this is the template used when a user updates their records):
<label for="wlpeUserProfileWebgroup_brandSupporter">brandSupporter
<input id="wlpeUserProfileWebgroup_brandSupporter" type="radio" name="webgroup_brandSupporter" value="[+form.webgroup_brandSupporter+]" />
</label>
I’m definitely don’t completely have my head around the way WebLoginPE works... Do you have any further insight?
Thanks again,
Nick
good point to create a plugin for that, Susan.
you can try this plugin. Seems to work, without hacking wlpe:
<?php//do not copy this line!!! Only for styling in this forum!
//add [#wlpeWebGroupCheckboxes#] to your userProfil-Form where you want these checkboxes
//check OnWebSaveUser and OnWebPagePrerender
//Plugin-Config:
//&allowedgroups=Editable Webgroups (commaseparated list);text;
$template = '
<input type="hidden" name="webgroup_[+groupname+]" value="off" />
<label for="wlpeUserProfileWebGroup_[+groupname+]">Ugrade to [+groupname+]
<input id="wlpeUserProfileWebGroup_[+groupname+]" type="checkbox" name="webgroup_[+groupname+]" value="on" [+checked+] />
</label>
';
$allowedgroups = explode(',', $allowedgroups);
$internalKey = $modx->getLoginUserID();
$e = $modx->event;
//print_r($e);
switch($e->name)
{
case 'OnWebSaveUser':
//echo '<pre>'.print_r($_POST,true).'</pre>';
//get all webgroups
$allGroups = array ();
$tableName = $modx->getFullTableName('webgroup_names');
$sql = "SELECT * FROM $tableName";
if ($rs = $modx->db->query($sql))
{
while ($row = $modx->db->getRow($rs))
{
$allGroups[$row['name']] = $row['id'];
}
}
$tableName = $modx->getFullTableName('web_groups');
foreach ($allowedgroups as $webgroup)
{
//get groupid
$groupid = $allGroups[$webgroup];
if ( isset ($_POST['webgroup_'.$webgroup]) && $_POST['webgroup_'.$webgroup] == 'off')
{
//remove from webgroup
$deleteFromGroups = $modx->db->query("DELETE FROM ".$tableName." WHERE `webuser`='".$internalKey."' AND `webgroup`='".$groupid."'");
}
if ( isset ($_POST['webgroup_'.$webgroup]) && $_POST['webgroup_'.$webgroup] == 'on')
{
//$deleteFromGroups = $modx->db->query("DELETE FROM ".$web_groups." WHERE `webuser`='".$internalKey."' AND `webgroup`='".$webgroup."'");
//add to webgroup if not allready there
$groupCheck = $modx->db->query("SELECT * FROM ".$tableName." WHERE `webuser`='".$internalKey."' AND `webgroup`='".$groupid."'");
$limit = $modx->db->getRecordCount($groupCheck);
if ($limit == 0)
{
$addtogroup = $modx->db->query("INSERT INTO ".$tableName."(`webuser`, `webgroup`) VALUES ('".$internalKey."', '".$groupid."')");
}
}
}
break;
case 'OnWebPagePrerender':
//create checkboxes for adding or removing to webgroups
$output = '';
foreach ($allowedgroups as $groupname)
{
$modx->isMemberOfWebGroup( array ($groupname));
$checked = $modx->isMemberOfWebGroup( array ($groupname))?'checked="checked"':'';
$output .= str_replace( array ('[+groupname+]', '[+checked+]'), array ($groupname, $checked), $template);
}
//$modx->setPlaceholder('wlpeWebGroupCheckboxes',$template);
//replace placeholder [#wlpeWebGroupCheckboxes#] in wlpe-userProfile-Template
$modx->documentOutput = str_replace('[#wlpeWebGroupCheckboxes#]', $output, $modx->documentOutput);
break;
}
-
☆ A M B ☆
- 24,524 Posts
I have posted a report on Jira about having the web_groups table use the webuser and webgroup fields together as the primary key, thus preventing duplication of webuser to webgroup assignments.
didn’t it work, when you first add this line into your plugin-config-textarea:
&allowedgroups=Editable Webgroups (commaseparated list);text;
then write into the new generated input-field ’Editable Webgroups’:
the plugin should then generate checkboxes for each webgroup which you have configured, here only one for brandSupporter
Hi Bruno,
Yes, I did as you say...add the "&allowedgroups=Editable Webgroups" in the plugin-config-textarea. If I put the group id in, it would write a 0 to the db. If I entered brandSupporter, it gave me a parser error (modx wants an integer). I noticed that $groupname was empty, not matter what I entered.
I tried putting $groupNames[$groupname] in place of $gn, but it wouldn’t work...even though the value is the same...strange.
Anyway, I know my fix is not very elegant, but it works and I am very happy for now. I’ll come back to it once the deadline is passed.
I really appreciate all your help!
Nick