Hi everyone,
I'm trying to make a widget for users to subscribe to a newsletter.
Here's the code:
<?php
/**
* Widget form the NETForms Newsletter
* the user only need to input his email adress
*/
class NewsletterWidget extends Widget {
static $db = array(
"TitleNLW" => "Text",
"TextNLW" => "Text",
"PlaceHolderTextNLW" => "Text",
"SubmitButtonTextNLW" => "Text",
"UserKeyNLW" => "Text",
"MailingListKey" => "Text"
);
static $cmsTitle = "Widget NewsletterWidget";
static $description = "Widget for a user to subscribe to the NETForms newsletter";
function getCMSFields() {
return new FieldSet(
new TextField("TitleNLW", "Title on top of widget"),
new TextField("TextNLW", "Text description under the title"),
new TextField("PlaceHolderTextNLW", "Text inside the submit field that will disapear when the user put something else"),
new TextField("SubmitButtonTextNLW", "Text for the submit button"),
new TextField("UserKeyNLW", "NETForms user key"),
new TextField("MailingListKey", "NETForms mailing list key")
);
}
}
class NewsletterWidget_Controller extends Widget_Controller {
public static $allowed_actions = array(
"NewsletterForm"
);
function NewsletterForm() {
$header = '';
$textButton = '';
$fields = new FieldSet(
new HeaderField($header, $header, 4),
new EmailField('Email')
);
$actions = new FieldSet(
new FormAction('SendNewsletterForm', $textButton)
);
$validator = new RequiredFields('Email');
return new Form($this, 'NewsletterForm', $fields, $actions, $validator);
}
function SendNewsletterForm($data, $form) {
$userKeyTest = 'a1434f';
$mailListKeyTest = '98550b77-4e';
// get email from user entry
// $email = $data['Email'];
$url = 'https://api.netscoop.ca/service.asmx/saveContact?userkey='.$userKeyTest.'&greeting=&firstname=&lastname=&email='.$email.'&externalkey=&mailinglistkey='.$mailListKeyTest;
// create a new cURL resource
$curl = curl_init();
// set URL and other appropriate options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_CAINFO, getcwd() . "/somesite/somecertificate.crt");
// grab URL and pass it to the browser
curl_exec($curl);
curl_close($curl);
// go back to home page
Director::redirectBack();
}
}
When I try to submit an email, I get this (in dev mode)
[User Error] No widget found
POST /widget/58/NewsletterForm
Line 247 in C:\web\sitename\sapphire\core\control\ContentController.php
Source
238
239 // find widget
240 $widget = null;
241 foreach($widgetAreaRelations as $widgetAreaRelation) {
242 if($widget) break;
243 $widget = $this->dataRecord->$widgetAreaRelation()->Widgets(
244 sprintf('"Widget"."ID" = %d', $SQL_id)
245 )->First();
246 }
247 if(!$widget) user_error('No widget found', E_USER_ERROR);
248
249 // find controller
250 $controllerClass = '';
251 foreach(array_reverse(ClassInfo::ancestry($widget->class)) as $widgetClass) {
252 $controllerClass = "{$widgetClass}_Controller";
253 if(class_exists($controllerClass)) break;
Trace
No widget found
Line 247 of ContentController.php
ContentController->handleWidget(SS_HTTPRequest)
Line 143 of RequestHandler.php
RequestHandler->handleRequest(SS_HTTPRequest)
Line 147 of Controller.php
Controller->handleRequest(SS_HTTPRequest)
Line 199 of ContentController.php
ContentController->handleRequest(SS_HTTPRequest)
Line 67 of ModelAsController.php
ModelAsController->handleRequest(SS_HTTPRequest)
Line 282 of Director.php
Director::handleRequest(SS_HTTPRequest,Session)
Line 125 of Director.php
Director::direct(/widget/58/NewsletterForm)
Line 127 of main.php
I've seen a lot of people struggling with form inside a widget ... anyone have any idea?