Web based family history software

question-circle Question Help with a custom Privacy Policy module

  • bluenellie
  • bluenellie's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • webtrees v2.2 / PHP 8.3 on VPS
More
2 weeks 5 days ago #1 by bluenellie
Help with a custom Privacy Policy module was created by bluenellie
Hello

Firstly let me say how impressed I am by webtrees. It's a great package, and I'm looking forward to exploring more of my Family History, with its assistance. Thank you Greg for all your hard work in its development.

I want to learn more about developing my own modules, and have been trying to implement a custom "privacy policy" module. I want to keep the statement about cookies, but to provide more information on the website owner. I have been using ~/app/Module/PrivacyPolicy.php to assist in creating my own, but I find I am unable to work out how to access the $uses_analytics variable in the getFooter() function to pass to the view template to produce the appropriate  comments relating to use of analytics.
Any guidance on this would be very much appreciated. Thank you
 

Please Log in or Create an account to join the conversation.

More
2 weeks 5 days ago #2 by Lars1963
Replied by Lars1963 on topic Help with a custom Privacy Policy module
you might want to try my custom policy:  github.com/LarsRabe/MyPrivacyPolicy

Please Log in or Create an account to join the conversation.

More
2 weeks 5 days ago - 2 weeks 5 days ago #3 by fisharebest
Replied by fisharebest on topic Help with a custom Privacy Policy module
> I am unable to work out how to access the $uses_analytics

The module uses this code to create a footer using a template file. The tempate files in webtrees have a .phtml extension. This is just my convention.

Code:
return view('modules/privacy-policy/footer', [ 'tree' => $tree, 'uses_analytics' => $this->analyticsModules($tree, $user)->isNotEmpty(), ]);

So, in the template file (resources/views/modules/privacy-policy/footer.phtml), two variables are available: $tree and $uses_analytics.

.phtml files are PHP scripts, so use these variables like any other PHP variable.

e.g.

Code:
<p class="cookie-statement"> <php if ($uses_analytics) : ?> Hey - we use analytics cookies <?php else : ?> OK - no cookies here <?php endif ?> </p>

Greg Roach - greg@subaqua.co.uk - @fisharebest@phpc.social - fisharebest.webtrees.net
Last edit: 2 weeks 5 days ago by fisharebest.

Please Log in or Create an account to join the conversation.

  • bluenellie
  • bluenellie's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • webtrees v2.2 / PHP 8.3 on VPS
More
2 weeks 4 days ago #4 by bluenellie
Replied by bluenellie on topic Help with a custom Privacy Policy module
Thank you, Lars, I've downloaded your module, and I can see it looks promising for what I want to achieve. I appreciate your help. :-)

Please Log in or Create an account to join the conversation.

  • bluenellie
  • bluenellie's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • webtrees v2.2 / PHP 8.3 on VPS
More
2 weeks 4 days ago #5 by bluenellie
Replied by bluenellie on topic Help with a custom Privacy Policy module
Greg, yes, I'm sorry, I do understand the clever way you've used the main scripts to drive the template files.

What I was trying to get at is how to access the current value for $uses_analytics prior to using it in this snippet from my attempt.

   <?php //if ($uses_analytics) : ?>
            This website uses cookies to learn about visitor behavior.
    <?php // endif ?>

As you see, I've commented it out, but I would like to:set it prior to invoking footer.phtml so that it accurately reflects whether I've configured "analytics" in the web interface. All the attempts I've made so far have involved more and more complications, and I figured I was getting into too much of a mess so I thought I'd seek help. Thank you for your time, and again thank you for webtrees.
 

Please Log in or Create an account to join the conversation.

More
2 weeks 2 days ago #6 by fisharebest
Replied by fisharebest on topic Help with a custom Privacy Policy module
I'm not sure what you are asking. In my first post, there were two code snippets (copied from webtrees).

The first (in the module) shows how to set the variable.

The second (in the view/template) shows how to read it.

Maybe show the code you've got, and it might be clearer?

Greg Roach - greg@subaqua.co.uk - @fisharebest@phpc.social - fisharebest.webtrees.net

Please Log in or Create an account to join the conversation.

  • bluenellie
  • bluenellie's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • webtrees v2.2 / PHP 8.3 on VPS
More
2 weeks 1 day ago - 2 weeks 1 day ago #7 by bluenellie
Replied by bluenellie on topic Help with a custom Privacy Policy module
Greg, thank you again for your reply.

Starting with a change to the example-footer module  github.com/webtrees/example-module-footer  and the code: 

Code:
    public function getFooter(ServerRequestInterface $request): string     {         $tree = $request->getAttribute('tree');         $url = route('module', [             'module' => $this->name(),             'action' => 'Page',             'tree'   => $tree ? $tree->name() : null,         ]);         return view($this->name() . '::footer', [              'url'            => $url,              'tree'           => $tree,              'uses_analytics' => $this->analyticsModules($tree, $user)->isNotEmpty(),         ]);


results in an error message that analyticsModules() is an undefined method.

Including the method leads to more errors, and more attempts on my part to address them, until I figured I must be missing something.

I'm now wondering whether I need to extend with ModuleAnalyticsInterface and use it and ModuleAnalyticsTrait in my source?

I'm going to try that later today, and if I'm unsuccessful, I will .zip up the code I'm working on and include it in another post.
Thanks.
 
 
Last edit: 2 weeks 1 day ago by bluenellie.

Please Log in or Create an account to join the conversation.

  • bertkoor
  • bertkoor's Avatar
  • Offline
  • Platinum Member
  • Platinum Member
  • Greetings from Utrecht, Holland
More
2 weeks 1 day ago - 2 weeks 1 day ago #8 by bertkoor
Replied by bertkoor on topic Help with a custom Privacy Policy module
The code fragment Greg shared comes from /app/modules/PrivacyPolicy.php and the 'missing' method analyticsModules() is at the bottom:
github.com/fisharebest/webtrees/blob/0c5...Policy.php#L120-L137

It in turn depends on $this->module_service which gets its value via the constructor:
github.com/fisharebest/webtrees/blob/0c5...cyPolicy.php#L41-L53

Maybe you had better copied this whole module as an example rather than the vanilla footer example.

stamboom.BertKoor.nl runs on webtrees v2.2.1
Last edit: 2 weeks 1 day ago by bertkoor.

Please Log in or Create an account to join the conversation.

  • bluenellie
  • bluenellie's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • webtrees v2.2 / PHP 8.3 on VPS
More
2 weeks 1 day ago #9 by bluenellie
Replied by bluenellie on topic Help with a custom Privacy Policy module
Thank you very much indeed for your response.

I have done as you suggest, and taken a copy of the module ~/app/modules/PrivacyPolicy.php

Its constructor needs to have 2 parameters, $module_service and $user_service:
:
Code:
public function __construct(ModuleService $module_service, UserService $user_service)  {        $this->module_service = $module_service;        $this->user_service   = $user_service;  }

I assume these 2 parameters, $module_service and $user_service will be needed, so how should I set their values?.

I found 2 code snippets (in separate sources):
Code:
$module_service = Registry::container()->get(ModuleService::class); $user_service = Registry::container()->get(UserService::class);

But I expect I need more than that.
 

Please Log in or Create an account to join the conversation.

More
2 weeks 8 hours ago #10 by fisharebest
Replied by fisharebest on topic Help with a custom Privacy Policy module
> I assume these 2 parameters, $module_service and $user_service will be needed, so how should I set their values?.

When the webtrees code creates an instance of your module, it will provide these objects automatically.

It's a technique known as "dependency injection", and is common in PHP frameworks/applications.

Greg Roach - greg@subaqua.co.uk - @fisharebest@phpc.social - fisharebest.webtrees.net

Please Log in or Create an account to join the conversation.

  • bluenellie
  • bluenellie's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • webtrees v2.2 / PHP 8.3 on VPS
More
1 week 3 days ago #11 by bluenellie
Replied by bluenellie on topic Help with a custom Privacy Policy module
I am happy to say that I now have my "Custom Privacy" module working to my satisfaction.

Thank you to Lars1963, fisharebest and bertkoor for their assistance along the way. :-)

And thanks of course to Greg, and other webtrees developers for a great package!
 

Please Log in or Create an account to join the conversation.

More
5 days 20 hours ago #12 by hermann
Replied by hermann on topic Help with a custom Privacy Policy module
Gratulation! Could you share or show.your result with us?

Hermann
Designer of the custom module "Extended Family"

webtrees 2.2.1 (all custom modules installed, PHP 8.3.12, MariaDB 10.6) @ ahnen.hartenthaler.eu

Please Log in or Create an account to join the conversation.

  • bluenellie
  • bluenellie's Avatar Topic Author
  • Offline
  • New Member
  • New Member
  • webtrees v2.2 / PHP 8.3 on VPS
More
3 days 13 hours ago #13 by bluenellie
Replied by bluenellie on topic Help with a custom Privacy Policy module

Gratulation! Could you share or show.your result with us?


 
Thank you! I was very relieved when I got it working! :-)

As to sharing it, although I have been a programmer for many years, I am still very much a novice when it comes to webtrees, but for what it's worth, here is my code for the main module:
Code:
<?php /**  * Custom "Privacy Policy" Footer, with a link to the policy statement.  * based on ~/app/Module/PrivacyPolicy.php and footer.phtml and page.phtml  * from ~/resources/views/modules/privacy-policy\*.phtml  */ declare(strict_types=1); namespace wishfulNamespace; use Fisharebest\Webtrees\Contracts\UserInterface; use Fisharebest\Webtrees\Services\ModuleService; use Fisharebest\Webtrees\Services\UserService; use Fisharebest\Webtrees\Tree; use Fisharebest\Webtrees\Validator; use Illuminate\Support\Collection; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Fisharebest\Webtrees\Module\AbstractModule; use Fisharebest\Webtrees\Module\ModuleCustomInterface; use Fisharebest\Webtrees\Module\ModuleCustomTrait; use Fisharebest\Webtrees\Module\ModuleFooterInterface; use Fisharebest\Webtrees\Module\ModuleFooterTrait; use Fisharebest\Webtrees\View; use Fisharebest\Webtrees\Module\ModuleAnalyticsInterface; use Fisharebest\Webtrees\Module\ModuleAnalyticsTrait; use Fisharebest\Webtrees\Registry; /**  * Class PrivacyPolicy - to comply with the GDPR and similar local laws.  */ class wishfulPrivacy extends AbstractModule implements ModuleCustomInterface, ModuleFooterInterface {     use ModuleCustomTrait;     use ModuleFooterTrait;     use ModuleAnalyticsTrait;     private ModuleService $module_service;     private UserService $user_service;          private bool $noLink = false; /**  * @return string */     public function title(): string     {         return ('Wishful Thinking&#039;s Privacy Policy');     } // ----------------------------------------------------------------------- /**   * Bootstrap the module */     public function boot(): void     {         $this->module_service = Registry::container()->get(ModuleService::class);         $this->user_service = Registry::container()->get(UserService::class); // ------------------------------------ Register a namespace for our views         View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');     } // ----------------------------------------------------------------------- /**   * A sentence describing what this module does.   *   * @return string */     public function description(): string     {         return ('This module provides an alternative Privacy Policy');     } // ----------------------------------------------------------------------- /**  * Where does this module store its resources  *  * @return string */     public function resourcesFolder(): string     {         return __DIR__ . '/resources/';     } // ----------------------------------------------------------------------- /**   * A footer, to be added at the bottom of every page.   *   * @param ServerRequestInterface $request   *   * @return string */     public function getFooter(ServerRequestInterface $request): string     { // ---- some other modules load as $tree = $request->getAttribute('tree'); // --------------------------- and $user = $request->getAttribute('user'); // ---------------- I don't know whether they would give different results         $tree = Validator::attributes($request)->treeOptional();         $user = Validator::attributes($request)->user(); // ------- I don't know why Greg includes this test, but must be important         if ($tree === null) {             return '';         }         $url = route('module', [             'module' => $this->name(),             'action' => 'Page',             'tree'   => $tree ? $tree->name() : null,         ]);         return view($this->name() . '::footer', [             'url'            => $url,             'tree'           => $tree,             'uses_analytics' => $this->analyticsModules($tree, $user)->isNotEmpty(),             'nolink'         => $this->noLink,         ]);     } // ----------------------------------------------------------------------- /**   * Generate the page that will be shown when we click the link in the footer.   *   * @param ServerRequestInterface $request   *   * @return ResponseInterface */     public function getPageAction(ServerRequestInterface $request): ResponseInterface     { // -------------------- see above, getFooter() for alternative invocations         $tree = Validator::attributes($request)->tree();         $user = Validator::attributes($request)->user();         $title = 'Wishful Thinking Privacy Policy';         $this->noLink = true; // ----- I don't need all these parameters - they are here for consistency         return $this->viewResponse($this->name() . '::page', [             'administrators' => $this->user_service->administrators(),             'analytics'      => $this->analyticsModules($tree, $user),             'title'          => $title,             'tree'           => $tree,         ]);     } // ----------------------------------------------------------------------- /**   * @param Tree          $tree   * @param UserInterface $user   *   * @return Collection<int,ModuleAnalyticsInterface> */     protected function analyticsModules(Tree $tree, UserInterface $user): Collection     {         return $this->module_service             ->findByComponent(ModuleAnalyticsInterface::class, $tree, $user)                 ->filter(static fn (ModuleAnalyticsInterface $module): bool => $module->isTracker());     } }; // -----------------------------------------------------------------------
 

Please Log in or Create an account to join the conversation.

Powered by Kunena Forum