Jump to content

a-ok

Members
  • Posts

    812
  • Joined

  • Last visited

Everything posted by a-ok

  1. Apologies for the late reply. This makes sense... thanks for helping! How would you manage to dump $response? I can't seem to dump in either in a message or in Tracy to check the response. Thanks so much!
  2. @flydev I've tried this... if (function_exists('send_email')) { $this->message("Exists!"); } else { $this->message("Doesn't exist?"); } And the message 'Doesn't exist?' is being returned... hmmm
  3. Sorry @flydev if I'm not being clear. I meant the 'send_email()' function as seen in my snippet. My code is exactly as the snippet looks but when dumping the result global $mail; bd($mail); with this example, as opposed to yours, it's returning NULL and not sending the email... no errors though.
  4. Thanks for that, @flydev. It appears to work when I don't use the send_mail function... seems to work fine with your example. Any thoughts? I'm not getting any errors. $this->addHookAfter('Pages::saved', function(HookEvent $event) { $arguments = $event->arguments(); $page = $event->arguments(0); if ($page->template == 'user') { // Require relevent libraries require_once($this->config->paths->root . 'api/sendgrid/sendgrid-php.php'); // SendGrid API init $sgAPIKey = "XXX"; // Lucy // Set email confirmation settings $email_admin = 'XXX'; $email_customer = $page->email; $email_admin_subject = "You added a new user $page->name"; $email_customer_subject = 'Your login details'; $email_customer_body = 'test'; function send_email($from_email, $to_email, $subject, $body) { global $sgAPIKey; $from = new \SendGrid\Email(null, $from_email); $to = new \SendGrid\Email(null, $to_email); $content = new \SendGrid\Content("text/html", $body); $mail = new \SendGrid\Mail($from, $subject, $to, $content); $sg = new \SendGrid($sgAPIKey); $response = $sg->client->mail()->send()->post($mail); } send_email($email_admin, $email_customer, $email_customer_subject, $email_customer_body); } });
  5. Also does PW use a send_mail function? Call to undefined function ProcessWire\send_email() As I'm using my own? function send_email($from_email, $to_email, $subject, $body) { global $sgAPIKey; $from = new SendGrid\Email(null, $from_email); $to = new SendGrid\Email(null, $to_email); $content = new SendGrid\Content("text/html", $body); $mail = new SendGrid\Mail($from, $subject, $to, $content); $sg = new \SendGrid($sgAPIKey); $response = $sg->client->mail()->send()->post($mail); }
  6. Thanks guys... not sure how I didn't figure that. Why wouldn't $config work in this instance? I'm on 3.0.61
  7. I did try this but it didn't work which made me think that perhaps includes/requires couldn't be set in the hook... require_once($config->paths->root . '/api/sendgrid/sendgrid-php.php'); 1× PHP Notice: Undefined variable: config in /Users/***/Sites/Sites/spink/site/init.php:26 2× PHP Notice: Trying to get property of non-object in /Users/***/Sites/Sites/spink/site/init.php:26 1× PHP Warning: require_once(/api/sendgrid/sendgrid-php.php): failed to open stream: No such file or directory in /Users/***/Sites/Sites/spink/site/init.php:26
  8. Thanks! Yep was just intentional to show you a bit of the code... sorry! I think you're right about require_once() but unsure what the path should be when running from init.php if the /api/ folder is on the same level as /site/ and /wire/ Thanks again @adrian – see above. Must be a path thing...
  9. For one... my question. It doesn't seem to like using include or require in the hook...
  10. Thanks for the help! Where does $wft->include() run from? My /api/ folder is in the root beside /site/ and /wire/ can I specify $config->urls->root?
  11. If I am wanting to require some PHP (PHP libraries or a PHP file) in a hook within my init.php file, is this possible? Below is a snippet... $this->addHookAfter('Pages::saved', function(HookEvent $event) { $arguments = $event->arguments(); $page = $event->arguments(0); if ($page->template == user) { // Require relevent libraries require_once('../api/sendgrid/sendgrid-php.php'); // SendGrid API init $sgAPIKey = "XXXX"; // Set email confirmation settings $email_admin = 'xxxx@xxxx.com'; // Client $email_customer = $page->email; $email_admin_subject = "You added a new user $page->name"; $email_admin_customer = 'Your login details'; ob_start(); require_once '../api/confirmation__email--admin.php'; $email_admin_body = ob_get_contents(); ob_end_clean(); ob_start(); require_once '../api/confirmation__email--customer.php'; $email_customer_body = ob_get_contents(); ob_end_clean();
  12. Pages::saved or Pages::added doesn't seem to return the template being used when returning the page arguments. I'll give Tracy Captain Hook panel a go. Scrap that I was being THICK!
  13. Sorry what I meant is that I can only hook an 'added' method on Pages class and not on a Page class. Ah got you!
  14. Ohhhh interesting! But I guess I would need $page and not $pages if I wanted it to send off an email once a user has been added?
  15. I think @Sérgio Jardim's suggest may be the best solution... create a PageReference field 'property_permission' then add it to the user template so when a user is added the admin can set which pages they have access to... if ($user->property_permission->has("id=$page->id") || $user->property_permission->has("id=$page->parentID")) Thoughts? Also while anyone is here... can you hook into a new user creation? Preferably a hookAfter?
  16. I wonder if IDs may be better as page names can sometimes change... I think either this or @Sérgio Jardim's suggestion could work...
  17. I guess per template I could include the following... if ( (($page->id == 1030 || $page->parentID == 1030) && $user->hasRole("property-1")) || (($page->id == 1031 || $page->parentID == 1031) && $user->hasRole("property-2")) ) But I'd need to do this for every user role... but not the end of the world if it's the best option.
  18. Thanks, @adrian. One problem... if my pages are using the same template... when I run my check (if ($user->hasRole("property-1")), for example, if the user has that role won't they be able to see all the other properties too as the role isn't defining what pages a user can access?
  19. Wouldn't it just send them in a loop though? I have my login check at the top of each template (if user is logged in then show (and I can set my permissions here as you said) otherwise show login form) but if the user logs in but then doesn't have access it would then show the form again and then endless loop?
  20. I think so! So just create my roles as normal with view rights only then at the top of my template, for example... if page or page parent == and $user->hasRole("property-1") else 404?
  21. I think one of my problems may be that when a user is created it is auto assigned as 'guest' and you cannot remove 'guest' as a role of a user nor can you remove the 'view pages' capability of that role so regardless... the user will be able to access all pages. Hmm.
  22. I am creating a site which I want users to have access to specific pages. For example... this is my site structure: Properties - Property 1 - Gallery - Contact - Etc - Property 2 - Property 3 Each property uses the same template, which has an include file at the top to check if the user is logged in or not and if not show a login form (taken from https://processwire.com/talk/topic/107-custom-login/) which seems to work well. What I now want to do is add users (which will send off an email) and give that user access to a specific property (and its child pages). That same user could have access to more than one property or simply one property. If they try to access another property they don't have access to it will return them to the homepage, for example... or 404 error more probably. What do we think? Is it possible to have users have access to parent pages only rather than templates (as I'm using the same templates for each property). Ideally I'd like to use SendGrid to send off emails once a user is added so perhaps I could write some sort of hook when a user is added? Is it possible to add a hook when a user is added? I'm also going to use the Login Scheduler module (https://modules.processwire.com/modules/login-scheduler/) to set start/end times for user access. Any other advice would be appreciated.
  23. May be a dumb question in the end but I normally use `$config->urls->root;` when creating a 'return to homepage' link but what do I do when I'm using multi-language? I thought `$user->language->root;` but this seems to return the current page (albeit localised). Any thoughts?
×
×
  • Create New...