* @author Serban George Cristian <*****@*****.**>
 * @link http://www.mailwizz.com/
 * @copyright 2013 http://www.mailwizz.com/
 */
exit('COMMENT ME TO TEST THE EXAMPLES!');
// require the autoloader class
require_once dirname(__FILE__) . '/../MailWizzApi/Autoloader.php';
// register the autoloader.
MailWizzApi_Autoloader::register();
// if using a framework that already uses an autoloading mechanism, like Yii for example,
// you can register the autoloader like:
// Yii::registerAutoloader(array('MailWizzApi_Autoloader', 'autoloader'), true);
/**
 * Notes: 
 * If SSL present on the webhost, the api can be accessed via SSL as well (https://...).
 * A self signed SSL certificate will work just fine.
 * If the MailWizz powered website doesn't use clean urls, 
 * make sure your apiUrl has the index.php part of url included, i.e: 
 * http://www.mailwizz-powered-website.tld/api/index.php
 * 
 * Configuration components:
 * The api for the MailWizz EMA is designed to return proper etags when GET requests are made.
 * We can use this to cache the request response in order to decrease loading time therefore improving performance.
 * In this case, we will need to use a cache component that will store the responses and a file cache will do it just fine.
 * Please see MailWizzApi/Cache for a list of available cache components and their usage.
 */
// configuration object
$config = new MailWizzApi_Config(array('apiUrl' => 'http://www.mailwizz-powered-website.tld/api', 'publicKey' => 'PUBLIC-KEY', 'privateKey' => 'PRIVATE-KEY', 'components' => array('cache' => array('class' => 'MailWizzApi_Cache_File', 'filesPath' => dirname(__FILE__) . '/../MailWizzApi/Cache/data/cache'))));
// now inject the configuration and we are ready to make api calls
MailWizzApi_Base::setConfig($config);
 /**
  * Return a list of callbacks/event handlers for the given event
  * 
  * @param string $eventName
  * @return MailWizzApi_Params
  */
 public function getEventHandlers($eventName)
 {
     if (!self::$_eventHandlers instanceof MailWizzApi_Params) {
         self::$_eventHandlers = new MailWizzApi_Params(self::$_eventHandlers);
     }
     if (!self::$_eventHandlers->contains($eventName) || !self::$_eventHandlers->itemAt($eventName) instanceof MailWizzApi_Params) {
         self::$_eventHandlers->add($eventName, new MailWizzApi_Params());
     }
     return self::$_eventHandlers->itemAt($eventName);
 }
 /**
  * Helper method to generate the html form that will be pushed in the widgets area in frontend.
  * It exists so that we don't have to generate the html at each page load.
  */
 protected function generateForm(array $instance)
 {
     if (empty($instance['list_uid']) || empty($instance['public_key']) || empty($instance['private_key'])) {
         return;
     }
     $oldSdkConfig = MailWizzApi_Base::getConfig();
     MailWizzApi_Base::setConfig(mwznb_build_sdk_config($instance['api_url'], $instance['public_key'], $instance['private_key']));
     $endpoint = new MailWizzApi_Endpoint_ListFields();
     $response = $endpoint->getFields($instance['list_uid']);
     $response = $response->body->toArray();
     mwznb_restore_sdk_config($oldSdkConfig);
     unset($oldSdkConfig);
     if (!isset($response['status']) || $response['status'] != 'success' || empty($response['data']['records'])) {
         return;
     }
     $freshFields = $response['data']['records'];
     $selectedFields = !empty($instance['selected_fields']) ? $instance['selected_fields'] : array();
     $rowTemplate = '<div class="form-group"><label>[LABEL] [REQUIRED_SPAN]</label><input type="text" class="form-control" name="[TAG]" placeholder="[HELP_TEXT]" value="" [REQUIRED]/></div>';
     $output = array();
     foreach ($freshFields as $field) {
         $searchReplace = array('[LABEL]' => $field['label'], '[REQUIRED]' => $field['required'] != 'yes' ? '' : 'required', '[REQUIRED_SPAN]' => $field['required'] != 'yes' ? '' : '<span class="required">*</span>', '[TAG]' => $field['tag'], '[HELP_TEXT]' => $field['help_text']);
         if (in_array($field['tag'], $selectedFields) || $field['required'] == 'yes') {
             $output[] = str_replace(array_keys($searchReplace), array_values($searchReplace), $rowTemplate);
         }
     }
     $out = '<form method="post" data-uid="' . $instance['uid'] . '">' . "\n\n";
     $out .= implode("\n\n", $output);
     $out .= "\n\n";
     $out .= '<div class="clearfix"><!-- --></div><div class="actions pull-right"><button type="submit" class="btn btn-default btn-submit">Subscribe</button></div><div class="clearfix"><!-- --></div>';
     $out .= "\n\n" . '</form>';
     return $out;
 }