Jump to content

Developing the function "favorite pages of an user"


LAPS
 Share

Recommended Posts

Hi all,

in my site I would like to list the favorite pages of a logged in user (I'm using the PW LoginRegister module).

To make that, I thought about adding a button on every page of the site so that the logged in user can click it (running a AJAX function) and save somehow his favorite pages (e.g. storing the pages ID to the database).

My question are: what is the PW "approach" and how to make something like that?

Link to comment
Share on other sites

Here is way to get most viewed posts today:

https://processwire.com/talk/topic/2868-count-views-of-post/?do=findComment&comment=28641

for a pagelist you have to asign a page field to a user and you save added pages via click like you described.

This module could also be interesting:

http://modules.processwire.com/modules/page-bookmarks/

regards mr-fan

  • Like 2
Link to comment
Share on other sites

  • 2 months later...

LAPS, I am also using the LoginRegister module along with Kongondo Blog module, and wanted to add Favorite functionality for blog posts and users.

This related thread gave me a good start: https://processwire.com/talk/topic/18618-best-way-to-handle-saving-an-item-as-a-favorite/

Guessing there will be more users than posts (and the pages may be sorted by favorite count in the future), I decided on using a page field "blog_favs" with input "templates : user" attached to my template "post". And I decided the Add/Remove functionality would be URL query based. Here is the code that I implemented in /site/Modules/MarkupBlog/MarkupBlog.module
 

	/**
	 * Render favs
	 *
	 * Used by renderPostHead().
	 *
	 * @access private
	 * @param PageArray $blog_favs is field in blog_post template.
	 * @return string $out Markup of favs
	 * 
	 */
	private function renderFavs($page, $small = false) {
		$options = $this->options;
		$out = "";
		
	    // if page field not available, break
		if ( !$page->blog_favs ) { return $out; }
		
		// if post display is summary snippet skip full UI
		// if user logged in, use full UI and functionality
		if ( $small != true && $this->wire('user')->isLoggedin() ) {
			$uid            = $this->wire('user')->id; // uid is int
			$favExists      = 0;
			// check query string for Add/Remove function
			$queryCheck     = $this->wire('sanitizer')->entities($this->wire('input')->queryString);
			$queryAddFav    = "add_fav={$page->id}";
			$queryRemoveFav = "remove_fav={$page->id}";
			$doSaveFav      = 0; // on AddFav request, assume we are going to save fav
			$doRemoveFav    = 0; // on RemoveFav request, assume we are going to remove fav
			
			if ( $queryCheck ) {
			    if ( $queryCheck == $queryAddFav )    { $doSaveFav   = 1; }
			    if ( $queryCheck == $queryRemoveFav ) { $doRemoveFav = 1; }
			}

			// only loop through blog_favs if there are entries // only loop once
			if ( count($page->blog_favs) > 0 ) {
				foreach( $page->blog_favs as $fav ) {
					$fav_int = (int)(string)$fav; // convert saved object to string then to int (!)
					if ( $fav_int == $uid ) { 
						if ( $doRemoveFav == 1 ) { 
							$page->of(false);
							$page->blog_favs->remove($fav);
							$page->save('blog_favs');
							$out .= "<div>" . $options['post_removed_fav_text'] . "</div>";
						} else  {
							$favExists = 1;
						}
						break; 
					}
				}
			}
			
			// if AddFav, only if user not already faved this post
			if ( $doSaveFav == 1 && $favExists == 0 ) {
				$page->of(false);
				$page->blog_favs->add($uid); // $uid is int
				$page->save('blog_favs');
				$favExists = 1;
				$out .= "<div>" . $options['post_added_fav_text'] . "</div>";
            }
			
			// show Add or Remove links for user
			if ( $favExists == 0 ) {
				$out .= "<div><a href='{$page->url}?{$queryAddFav}'>" . $options['post_add_fav_text'] . "</a></div>";
			} else {
				$out .= "<div><a href='{$page->url}?{$queryRemoveFav}'>" . $options['post_remove_fav_text'] . "</a></div>";
			}
		}

		// always display basic UI Favorites Counter (this is at end to capture add/remove changes)
		$out .= "<span class='favs'>" . $options['post_favs_text'] . " " . count($page->blog_favs) . "</span>";

		return $out;
	}

I make no claims on performance or code elegance, but if any of this is helpful to you, enjoy!!!

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...