Author Topic: Subscription to mailing list via URL during registration  (Read 934 times)

Offline tdhaynes

  • Jr. Member
  • **
  • Posts: 5
    • View Profile
Subscription to mailing list via URL during registration
« on: 15/July/2011, 06:06:45 AM »
I am using an nBill registration form but need to integrate a subscription to a mailing list via a URL when the form is submitted.
As I am already collecting a username and email address, is it possible to add another field (checkbox??) to ask if user wants to subscribe to a newsletter.

If the checkbox is selected I would then like to create a URL using the name & email values.  This URL would need to be posted at the same time that the form is submitted.  Below is the structure of the URL required
http://www.mywebsite.com/index.php?option=com_acymailing&ctrl=sub&task=optin&hiddenlists=1&user[email]=mymail@gmail.com&user[name]=Trevor

I'm guessing this code would go into the Page Submit Code but am a little lost on how to construct this.

Offline netshine

  • Administrator
  • Hero Member
  • *****
  • Posts: 4,952
    • View Profile
Re: Subscription to mailing list via URL during registration
« Reply #1 on: 15/July/2011, 09:46:08 AM »
If you put the code in the page submit code, it will be executed whenever the page is submitted - even if the form as a whole is not submitted, and if the user goes backwards and forwards through your form pages (assuming there is more than 1), it could be called multiple times. If you only want the code to be executed when an order has been successfully placed, you should use the order creation code setting.

Anyway, I think the following code would do what you want:

Code: [Select]
$params['option'] = 'com_acymailing';
$params['ctrl'] = 'sub';
$params['task'] = 'optin';
$params['hiddenlists'] = '1';
$params['user[email]'] = 'mymail@gmail.com';
$params['user[name]'] = 'Trevor';
include_once(nbf_cms::$interop->nbill_admin_base_path . '/framework/classes/nbill.http.class.php');
$result = nbf_http::get_http('mywebsite.com', '/index.php', $params);

Offline tdhaynes

  • Jr. Member
  • **
  • Posts: 5
    • View Profile
Re: Subscription to mailing list via URL during registration
« Reply #2 on: 15/July/2011, 10:37:33 AM »
Thank you so much for fantastic support, unfortunately it didn't work.
No error reported but the record was not added to the mailing list.  

I don't know if it has anything to do with using and odd domain (I'm developing on a local MAMP server and my test domain is http://clientsite.com-2011/index.php - Note: The URL does work correctly when pasted into the address bar of my browser

Further how do I use form variables, do I use the field number, eg ##71## in place of your static value.
Finally is it possible to execute this code only if the submitter selects a check box.

Once again I am very grateful for your support and absolutely love your component.
« Last Edit: 15/July/2011, 10:55:01 AM by tdhaynes »

Offline netshine

  • Administrator
  • Hero Member
  • *****
  • Posts: 4,952
    • View Profile
Re: Subscription to mailing list via URL during registration
« Reply #3 on: 15/July/2011, 01:31:26 PM »
If you are running in one of the submit code settings, you can access the form values using the $_POST or $_REQUEST array. If you use the order creation code setting it is more complicated and you have to lookup the values on the database based on the order ID. The same principle applies to checkboxes as to other fields. I am assuming some knowledge of PHP here - if you are not a programmer, you might need professional assistance.

If the URL you are calling is on the same website as nBill, you are probably better off populating the $_GET and/or $_REQUEST parameters expected by the acymailing component, and just including the acymailing.php file directly, eg:

$_REQUEST['option'] = 'com_acymailing';
$_REQUEST['ctrl'] = 'sub';
$_REQUEST['task'] = 'optin';
$_REQUEST['hiddenlists'] = '1';
$_REQUEST['user[email]'] = 'mymail@gmail.com';
$_REQUEST['user[name]'] = 'Trevor';
//You might need to repeat the above lines using $_GET or $_POST instead of $_REQUEST, depending on where com_acymailing picks up the values from.
include_once('/path/to/your/site/components/com_acymailing/acymailing.php');

Offline tdhaynes

  • Jr. Member
  • **
  • Posts: 5
    • View Profile
Re: Subscription to mailing list via URL during registration
« Reply #4 on: 17/July/2011, 03:36:30 AM »
Once again thank you for your terrific support.

The code you suggested didn't work but with the help of the developer documentation ( http://www.acyba.com/en/support/documentation/64-acymailing-developer-documentation.html ) for AcyMailing and youguiding me in the right directing I have put together the code below which works perfectly for me but as I am not a programmer I'm sure it can be improved (i.e. read the database to see what lists are available and generate checkboxes etc to allow subscribers to choose, but this goes beyond my present need).

I would appreciate a quick check by netshine if you have a spare moment.  If you cant see any major problems I'll post this on Acyba's forum ( http://www.acyba.com/en/support/forum.html ) for anyone else wanting to integrate the 2 components.

Code below added to order form in Submit Code field on Advanced tab.

// CREATE SUBSCRIBER
if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_acymailing'.DS.'helpers'.DS.'helper.php')){
return false;
}
$myUser = null;
$myUser->email = $_POST['NBILL_CORE_email_address'];
$myUser->name = $_POST['NBILL_CORE_first_name'].' '.$_POST['NBILL_CORE_last_name']; //this information is optional

$subscriberClass = acymailing::get('class.subscriber');
$subid = $subscriberClass->save($myUser); //this function will return you the ID of the user inserted in the AcyMailing table

// SUBSCRIBE TO LISTS

$subscribe = array(1); //Id of the lists you want the user to be subscribed to (can be empty)
$remove = array(); //Id of the lists you want the user to be removed from (can be empty)
$memberid = $_POST['NBILL_CORE_email_address']; //ID of the Joomla User or user e-mail (this code supposes that the user is already inserted in AcyMailing!)

if(!include_once(rtrim(JPATH_ADMINISTRATOR,DS).DS.'components'.DS.'com_acymailing'.DS.'helpers'.DS.'helper.php')){
echo 'This plugin can not work without the AcyMailing Component';
return false;
}

$userClass = acymailing::get('class.subscriber');

$newSubscription = array();
if(!empty($subscribe)){
foreach($subscribe as $listId){
$newList = null;
$newList['status'] = 1;
$newSubscription[$listId] = $newList;
}
}
if(!empty($remove)){
foreach($remove as $listId){
$newList = null;
$newList['status'] = 0;
$newSubscription[$listId] = $newList;
}
}

if(empty($newSubscription)) return; //there is nothing to do...

$subid = $userClass->subid($memberid); //this function returns the ID of the user stored in the AcyMailing table from a Joomla User ID or an e-mail address
if(empty($subid)) return false; //we didn't find the user in the AcyMailing tables

$userClass->saveSubscription($subid,$newSubscription);

Offline netshine

  • Administrator
  • Hero Member
  • *****
  • Posts: 4,952
    • View Profile
Re: Subscription to mailing list via URL during registration
« Reply #5 on: 17/July/2011, 08:43:15 AM »
The only thing I would advise here is to sanitise the user input. Instead of accessing data directly from the $_POST array, you can use the nbf_common::get_param() function to do this (strips out any HTML or script tags and escapes quote marks to avoid XSS and SQL injection attacks). For example:

$myUser->email = nbf_common::get_param($_POST, 'NBILL_CORE_email_address');