Views with parameters, is it possible?
Seems simple.  Build the VIEW without the last 5 lines.  Then use those 5 lines when you use the VIEW as if it were a TABLE.

If those JOINs are not "many:one", you will get an inflated COUNT(*).