I am building a module and I set the cache in the form function, which will be retrieved in the insert_new_email function.
I can retrieve the cached data in the form function, but I can not in the insert_new_email function.
Below is the class I use for my mod.email_blast.php file
class email_blast {
public $return_data;
/**
* Constructor
*/
public function __construct()
{
$this->EE =& get_instance();
$this->EE->load->library('session');
if (!class_exists('MCAPI'))
{
require PATH_THIRD.'email_blast/libraries/MCAPI.php';
}
}
// ----------------------------------------------------------------
public function form() {
$service = $this->EE->TMPL->fetch_param('service', false);
$api_key = $this->EE->TMPL->fetch_param('api_key', false);
$list_id = $this->EE->TMPL->fetch_param('list_id', false);
if($api_key == FALSE || $list_id == FALSE) {
return "You are missing parameter(s)";
}
$this->EE->session->set_cache('email_blast', 'api_key', $api_key);
$this->EE->session->set_cache('email_blast', 'list_id', $list_id);
$this->EE->session->set_cache('email_blast', 'service', $service);
// Build an array to hold the form's hidden fields
$hidden_fields = array(
'ACT' => ee()->functions->fetch_action_id('Email_blast', 'insert_new_email'),
'RET' => ee()->functions->fetch_current_uri(),
'API_KEY' => $this->EE->session->cache('email_blast', 'api_key')
);
// Build an array with the form data
$form_data = array(
"id" => $this->EE->TMPL->form_id,
"class" => $this->EE->TMPL->form_class,
"hidden_fields" => $hidden_fields,
);
// Fetch contents of the tag pair, ie, the form contents
$tagdata = $this->EE->TMPL->tagdata;
$form = $this->EE->functions->form_declaration($form_data) .
$tagdata . "</form>";
return $form;
}
function insert_new_email()
{
$email = ee()->input->get_post('email');
$email = trim(strip_tags($email));
$api_key = $this->EE->session->cache('email_blast', 'api_key');
$api = new MCAPI($api_key);
$merge_vars = array ("OPTINIP" => $_SERVER['REMOTE_ADDR']);
$list_id = $this->EE->session->cache('email_blast', 'list_id');
$result = $api->listSubscribe($list_id, $email, $merge_vars, 'html', 'false', 'true', 'false', 'true');
$data = array(
'title' => 'Mailing list',
'heading' => 'Thank you',
'content' => $list_id,
'link' => array($_POST['RET'], 'Site name here'),
'email' => $email,
);
ee()->output->show_message($data);
}
}