Snippet: Rename Groups by Campus

  • By Michael Garrison One Year Ago

 There was a request in Slack the other day, to be able to rename groups according to the campus they belong to. We ended up doing this using a combination of LavaV2's RockEntity and SQL commands. It was such a strong example of the power of these new capabilities I thought it was worth sharing here. Take this just as an example; I don't think anyone will actually want to do precisely what this function does, but there a lot of concepts you can adapt from this springboard.

Moreover, this doesn't have any error checking, and doesn't protect you from groups with non-standard names (quote marks or apostrophes in the names, etc). If you tried this, you'd need to take precautions that this is run only by people who know what they're doing, and who have checked to make sure that none of the group names will inject any errors. Once again, it's just an example of the power of LavaV2.

We created a new page under Power Tools (you don't want non-admins to stumble across this page, after all). Then we added an HTML Content block to the page, enabled RockEntity and SQL commands on the block and set the cache to 0 seconds.

Here are the contents:

{% if PageParameter.GroupTypeId %}
    {% group where:'GroupTypeId == {{PageParameter.GroupTypeId}} && CampusId _! ""' %}
        {% for group in groupItems %}
            <span class="label label-warning">{{ group.Name }}</span> renamed to <span class="label label-success">{{ group.Campus.Name }}-{{ group.Name }}</span>
                {% sql %}
                UPDATE [Group] SET [Name]='{{ group.Campus.Name }}-{{ group.Name }}' WHERE [Id]={{ group.Id }}
                {% endsql %}
            <br />
        {% endfor %}
    {% endgroup %}
{% else %}
    <select name="GroupTypeSelector" id="GroupTypeSelector" onchange="window.location.href=window.location.pathname + '?GroupTypeId=' + this.value;">  
        <option value="">(select)</option> 
        {% grouptype WHERE:'Name _! ""' %}
            {% for type in grouptypeItems %}
                <option value="{{ type.Id }}">{{ type.Name }}</option>
            {% endfor %}
        {% endgrouptype %}
{% endif %}

Let's step through this to see what it does:

First, we need to check and see whether a GroupTypeId parameter is provided in the URL- the first half of the code is what runs if it is, and the second half is what runs if the page has just been loaded (indicated by the lack of the GroupTypeId parameter).

The first half:

If there's a GroupTypeId parameter (line 1), we use a RockEntity command to loop through all groups whose GroupTypeId matches what's given in the page parameter, which are also assigned to a campus (line 2). Then for each such group (line 3), it outputs the current group name and what it's going to be renamed to (line 4). Then it runs a SQL command that updates the name of the group according to the current group name and prepends the name of the campus to which the group belongs (lines 5-7). Then we output a linebreak (line 8) and close out the loops we're in (lines 9-10).

The second half:

If there's NO GroupTypeId parameter, it means we need to enable the user to provide one. So we start an HTML form dropdown control, which reloads the current page with the selected GroupTypeId appended to the end when the value is changed (line 12). We provide a "dummy" value that serves to prompt the user what to do (line 13). Then we use a RockEntity command to loop through all GroupTypes where the name isn't blank (line 14). Then for each such grouptype (line 15), we put another option in the dropdown, where the value is the GroupTypeId and the content (which is what the user sees) is the name of the GroupType (line 16). Then close out the loops (lines 17-18) and close the dropdown control (line 19)


Finally, we close out the conditional (line 20)- we've handled both conditions of the page's load (with or without the GroupTypeId identified as a URL parameter). 

So when the page is first loaded, we get a dropdown with all of the grouptypes available:


Selecting one reloads the current page with (for instance) ?GroupTypeId=25 appended to the end of the URL (if you chose Small Group).

Assuming you did choose Small Group, the page would reload and you might see something like this:


(In the Rock Demo site, I'd updated just these three stock groups to be assigned to "Main Campus"). So you can see it only affected groups assigned to a campus, of the given GroupTypeId.

Once again, this isn't what I'd call a "complete solution" - it's not intelligent enough to remove the campus name if you run the page on the same grouptype again- so you'd end up with names like "Main Campus-Main Campus-Adults Group". And like I said, this doesn't protect you from SQL injection from unexpected group names. It's just a demo of the power of Lava, starting with Rock v6!

Spark Development Network
Flagstaff, AZ

Michael Garrison recently left his job in Architecture to become one of the "new guys" at Spark (the "Core Team"), but he's still helping out Christ's Church of Flagstaff and other non-profits with tech needs in his off-hours, trying to make computers do what computers do best, so that people are freed to do what we do best: relate with people!