Forum

Thread tagged as: Question

How to: Single list of employees, sorted by department as categories, with multi...

Hi there,

Goal

I’m working on a team-page for a company where about 40 employees are sorted into departments. Their department affiliation is changing a lot due to restructuring. So I decided to make a category set of „departments“, where the client can change/add/create new categories if they need, and choose from the categories for each employee. The employee list is a multiple-item region.

All of this is placed on a single team-page. What I want is a list of departments, and the employees in that department. I got it working so that the heading of the department is only shown if it is different from the last one. Re-ordering employees makes the list work almost completely.

Problem

Some employees are in multiple departments, and this is where my current solution stops working: When I add two or more categories to an employee, using the following template, the heading (h3) is repeated, and the employee is added after the last of his/her categories:

team.php:

<?php perch_content('Büroteam'); ?>

templates/content/buero-team.html:

<perch:before>
  <div class="container container--team">
</perch:before>

<perch:if different="team-abteilungen">
  <perch:categories id="team-abteilungen" label="Abteilung" set="team-abteilungen" required="true" /><h3 class="abteilung" id="<perch:category id="catSlug" label="Kurzname" />"><perch:category id="catTitle"  /></h3></perch:categories>
</perch:if>

  <div class="person">
    <img src="<perch:content type="image" id="foto" label="Foto" required="true" />" />
    <div class="contact-info">
      <h3><perch:content type="text" id="vorname" label="Vorname" required="true" /> <perch:content type="text" id="nachname" label="Nachname" required="true" title="true" /></h3>
      <perch:if exists="position">
        <p><perch:content type="text" id="position" label="Position/Aufgaben" /></p>
      </perch:if>
    </div> <!-- .contact-info -->
  </div> <!-- .person -->

<perch:after>
  </div>
</perch:after>

Question

How can I make a list of all employees, where I don’t have to add the same person multiple times with a different category selected, but can select multiples from my categories so that the employee gets repeated automatically for each category he/she is in? So this:

Jane Doe: Category 1 and 3
John Doe: Category 1 and 2
Mark Doe: Category 3

should become this:

Category 1
Jane Doe
John Doe

Category 2
John Doe

Category 3
Mark Doe
Jane Doe

I’m already using perch_categories() to display a list of all categories on top of the page (for jumping to a department quickly), but I couldn’t get the categories list to work with additional parameters for sorting etc. They just didn’t display.

Best regards, Jannis

Jannis Borgers

Jannis Borgers 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you'll probably need to loop through the categories and fetch the linked employees for each as you go.

Thanks Drew!

Can you tell me how I can do that?

I found this forum entry: https://forum.grabaperch.com/forum/04-16-2015-display-all-categories-with-listdetail-items

But the solution is not completely revealed there. You wrote …

  1. Grab the perch_categories() call into an array using skip-template
  2. Loop through that array calling perch_content_custom() using the value from the array for the category to filter by

… what do I need to do in both steps? I tried adding this to my team.php, but the list with categories and items disappears completely when I do that:

            <?php
                perch_categories(array(
          'set'=>'Team Abteilungen',
          'filter'=> 'catTitle',
          'match' => 'eq',
          'value' => 'categoryname',
                    'skip-template' => true
        ));

                foreach($categories as $category) {
                   perch_content_custom('Büroteam', array(
                       'template' => 'team-buero.html',
                       'category' => $category['catPath']
                   ));
                }
                ?>

Ah, I forgot to actually store the array. The items are repeating now:

team.php:

            <?php
                $categories = perch_categories(array(
                    'set'=>'Team Abteilungen',
                    'skip-template' => true
                ));

                foreach($categories as $category) {
                    perch_content_custom('Büroteam', array(
                        'template' => 'team-buero.html',
                        'category' => $category['catPath']
                    ));
                }
                ?>

templates/content/team-buero.html:

<perch:before>
    <div class="container container--team">
</perch:before>

<perch:if different="team-abteilungen">
    <perch:categories id="team-abteilungen" label="Abteilung" set="team-abteilungen" required="true" /><h3 class="abteilung" id="<perch:category id="catSlug" label="Kurzname" />"><perch:category id="catTitle"    /></h3></perch:categories>
</perch:if>

    <div class="person">
        <img src="<perch:content type="image" id="foto" label="Foto" required="true" />" />
        <div class="contact-info">
            <h3><perch:content type="text" id="vorname" label="Vorname" required="true" /> <perch:content type="text" id="nachname" label="Nachname" required="true" title="true" /></h3>
            <perch:if exists="position">
                <p><perch:content type="text" id="position" label="Position/Aufgaben" /></p>
            </perch:if>
            <perch:if exists="25jahre">
                <p class="team-hinweis hinweis-<perch:content id="25jahre" type="checkbox" label="25 Jahre" value="25jahre" />">Ãœber 25 Jahre bei uns</p>
            </perch:if>
            <perch:if exists="vdb">
                <p class="team-hinweis hinweis-<perch:content id="vdb" type="checkbox" label="VDB" value="vdb" />">VDB-zertifizierte Blitzschutzfachkraft</p>
            </perch:if>
            <perch:if exists="bdsf">
                <p class="team-hinweis hinweis-<perch:content id="bdsf" type="checkbox" label="BDSF" value="bdsf" />">Sachverständiger (BDSF)</p>
            </perch:if>
            <perch:if exists="email">
                <a class="button mail" href="mailto:<perch:content type="text" id="email" label="E-Mail" />"><perch:content type="text" id="email" label="E-Mail" /></a>
            </perch:if>
        </div> <!-- .contact-info -->
    </div> <!-- .person -->

<perch:after>
    </div>
</perch:after>

The only thing wrong is that category names are repeated, so every item is preceded by all it’s categories. Of course „different“ doesn’t work here. This:

Jane Doe: Category 1
John Doe: Category 2
Mark Doe: Category 2 and 3

becomes this:

Category 1
Jane Doe

Category 2
John Doe

Category 2 and 3
Mark Doe

Category 2 and 3
Mark Doe

While this is still an issue (I can’t get it to work, see above), this is new:

I just noticed that there is no way to let my clients add and edit categories at all. Even when I give their user role all privileges, the Categories tab doesn’t show up in Perch when I log in as that user.

Is this desired behavior, a bug, or me?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Your team-buero.html template outputs the categories. I think you can use suppress="true" prevent them being output, or use a shortened template for this particular view.

How many apps do you have installed?

Thanks Drew. I went with the second solution (it didn’t occur to me to use a separate template). I have no apps installed, just a German translation.

I managed to get what I want: A list of all employees categories, with the employees linked to that category showing below, duplicating if necessary.

team.php:

        <?php

        // show all categories, with links to the anchors generated below
        perch_categories(array(
            'set' => 'Team Abteilungen',
            'template' => 'departments.html'
        ));

        // store the employee categories in an array for later use, not for direct output
        $categories = perch_categories(array(
            'set'=>'Team Abteilungen',
            'skip-template' => true
        ));

        // loop through the stored categories one after another
        foreach($categories as $category) {

            // show the title of the current category …
            perch_category($category['catPath'],array(
                'template'=>'departments-separate.html'
            ));

            // … and show the employees which that category linked to them
            perch_content_custom('Team', array(
                'template' => 'team.html',
                'category' => $category['catPath']
            ));

        }
        ?>

templates/categories/departments.html (this shows a clickable list at the top, which jumps to the clicked department):

<perch:before>
  <ul class="team-list">
</perch:before>

<li class="team-list__item">
    <a class="team-list__link" href="#<perch:category id="catSlug" />">
        <h4 class="team-list__headline"><perch:category id="catTitle" type="smarttext" label="Abteilungsname" required="true" /></h4>
        <perch:category id="catSlug" type="slug" for="catTitle" suppress="true" />
        <p class="team-list__desc"><perch:category id="desc" type="text" label="Abteilungsbeschreibung" /></p>
    </a>
</li>

<perch:after>
  </ul>
</perch:after>

templates/categories/departments-separate.html (these are the category headings, I exclude the company board because this it is shown using a different template):

<perch:if id="catTitle" match="neq" value="Firmenleitung">
<h3 class="abteilung" id="<perch:category id="catSlug" label="Kurzname" />"><perch:category id="catTitle"   /></h3>
</perch:if>

templates/content/team.html (this shows the employees):

<perch:before>
    <div class="container container--team">
</perch:before>

<!-- The department has to be called somewhere in the template, so it will show up in Perch. Suppressed so it doesn’t show in the template itself (although this could be of some use in some cases) -->
<perch:categories id="team-abteilungen" label="Abteilung" set="team-abteilungen" required="true" />
    <perch:category id="catTitle" suppress="true"/>
</perch:categories>

    <div class="person">
        <img src="<perch:content type="image" id="foto" label="Foto" required="true" />" />
        <div class="contact-info">
            <h3><perch:content type="text" id="vorname" label="Vorname" required="true" /> <perch:content type="text" id="nachname" label="Nachname" required="true" title="true" /></h3>
            <perch:if exists="position">
                <p><perch:content type="text" id="position" label="Position/Aufgaben" /></p>
            </perch:if>
            <perch:if exists="email">
                <a class="button mail" href="mailto:<perch:content type="text" id="email" label="E-Mail" />"><perch:content type="text" id="email" label="E-Mail" /></a>
            </perch:if>
        </div> <!-- .contact-info -->
    </div> <!-- .person -->

<perch:after>
    </div>
</perch:after>

Just wanted to say thanks to Jannis for putting up the code and walking through it as it helped me a lot and saved me several hours of frustration.

The perch documentation is largely very good but this would be a very welcome inclusion in the official docs as I note that several people have asked this question now and I find the categories documentation quite confusing for a newbie.

Oh wow, thanks for the nice feedback, Mike!

Just as a follow-up, this is the end result: https://ried-blitzschutz.de/ueber-uns/das-team.php

The client is happy because they move people in between departments quite often and they don’t want to wait until I can make the change on the site. I’m happy because I don’t have to deal with duplicating 10 lines of markup every couple of weeks and dealing with client revisions. The client already wants to expand the website with Perch to add project showcases that they can manage. And I can do that without having to worry about a whole templating system or a theme, because this website is still mostly static HTML and I can just add Perch where it is needed.

So thank you, Rachel and Drew, for giving us Perch! I’m the happiest customer right now! :)

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's great to hear - thanks!