<?php

/**
 * This file contains examples for using the SendReach PHP-SDK.
 */
// require the setup which has registered the autoloader
require_once dirname(__FILE__) . '/setup.php';
// CREATE THE ENDPOINT
$endpoint = new MailWizzApi_Endpoint_ListFields();
/*===================================================================================*/
// GET ALL ITEMS
$response = $endpoint->getFields('LIST-UNIQUE-ID');
// DISPLAY RESPONSE
echo '<pre>';
print_r($response->body);
echo '</pre>';
 /**
  * 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;
 }