Forum

Thread tagged as: Question, Members

Using tags as a "Favourite" feature

I'm wondering if it's possible to use member tags as a way to allow members to "favourite" content.

Could members set their own tags by clicking a link on a page?

Stephen Turvey

Stephen Turvey 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, I don’t see why not.

My idea of the interaction is a grey heart icon which, upon clicking, would set the tag for that member and change the heart red (instagram style). I guess I would need some jquery/ajax magic to make that happen wouldn't I? Any pointers or things to look out for?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You'll want to call perch_member_add_tag()

https://docs.grabaperch.com/functions/members/perch-member-add-tag/

I would need a page where it outputs all the tags for each member so that they can have a bunch of links to all their favourited content. Is there a perch function for that?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Stephen,

I don't think there is a function in the Members addon that lists tags, but you should be able to work around that.

If members favourite blog posts, for example, you can:

  • retrieve all the blog posts
  • check for the posts the member has added to their favourites
  • then output a filtered list
// empty array for favourites
$favs = array();


// get all posts
$posts = perch_blog_custom([
    'skip-template' => true,
]);


// loop through posts 
foreach ($posts as $post){
    if (perch_member_has_tag($post['postSlug'])){
        // member has added this post to their favourites; add it to $favs array
        $favs[] = $post['postSlug'];
    }
}


// filter posts by postSlug values in $favs
perch_blog_custom([
    'template' => 'post_in_list.html',
    'filter' => 'postSlug',
    'match' => 'in',
    'value' => implode(',', $favs)
]);

The above is an example using blog posts and assumes the tags you add to the member are blog post slugs. You haven't specified what type of content you're using so you may need to modify the above to make it work for your use case.

Many thanks for this, Hussein.

Currently though the content isn't blog posts or even standard perch pages. The content is created on the fly by the users themselves as it's a classified advert website (think craigslist).

I'm only using perch in this project to make use of the members app (so users can log in and features can be hidden from non members) and the shop app (for upgrading adverts).

Do you think there's still a way around it with this in mind?

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

The content is created on the fly by the users themselves as it's a classified advert website (think craigslist).

It depends on how you've implemented this. I'm assuming you can retrieve a list of all the ads and that each ad has an ID or a slug. If so, you could do something similar to the above.

Yes, the app has a function like this:

public function getAdsByEmail($status=1, $email='')
    {
        $ads = R::findAll('ads',
                " status=:status AND email=:email ORDER BY created DESC",
                array(':status'=>$status, ':email'=>$email));
        return $ads;
    }

Then in the controller I can call it, for example:

$app->group('/my-page', function () use ($app) {

    $app->get('/', function () use ($app) {

        global $lang;
        global $perch_email;

        $j = new Ads();

        $ads = array();
        $ads['inactive'] = $j->getAdsByEmail(INACTIVE, $perch_email);
        $ads['active'] = $j->getAdsByEmail(ACTIVE, $perch_email);
        $ads['deactivated'] = $j->getAdsByEmail(DEACTIVATED, $perch_email);
        $ads['expired'] = $j->getAdsByEmail(EXPIRED, $perch_email);

        $seo_title = APP_NAME;
        $seo_desc = "";
        $seo_url = BASE_URL ."my-page/";

        $app->render(THEME_PATH . 'my-page.php',
                    array(
                        'lang' => $lang,
                        'seo_url'=>$seo_url,
                        'seo_title'=>$seo_title,
                        'seo_desc'=>$seo_desc,
                        'ads' => $ads
                    ));
    });

});
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

If you're building a custom app, you can do whatever your like.

The above solution roughly translates to:

$favs = array();
$ads = get_ads();

foreach ($ads as $ad){
  if (perch_member_has_tag($ad['ID'])){
      $favs[] = $ad['ID'];
  }
}

//output filtered ads
appname_filtered_ads($favs);

If I were you, I'd write a function that outputs a member's favourite ads to make my life easier:

if (perch_member_logged_in()) {
  appname_member_fav_ads();
}

How you get the favourite ads within the function is up to you. You can query the DB for example.