vendor/oneup/contao-mailchimp/src/MailChimp/Module/ModuleSubscribe.php line 142

Open in your IDE?
  1. <?php
  2. namespace Oneup\Contao\MailChimp\Module;
  3. use Oneup\Contao\MailChimp\Model\MailChimpModel;
  4. use Contao\Module;
  5. use Contao\System;
  6. use Contao\Environment;
  7. use Contao\Input;
  8. use Contao\BackendTemplate;
  9. use Haste\Form\Form;
  10. use Oneup\MailChimp\Client;
  11. class ModuleSubscribe extends Module
  12. {
  13.     protected $strTemplate 'mod_mailchimp_subscribe';
  14.     /** @var Client */
  15.     protected $mailChimp;
  16.     protected $objMailChimp;
  17.     protected $mailChimpListId;
  18.     public function generate()
  19.     {
  20.         if (TL_MODE == 'BE') {
  21.             /** @var BackendTemplate|object $objTemplate */
  22.             $objTemplate = new BackendTemplate('be_wildcard');
  23.             $objTemplate->wildcard '### '.utf8_strtoupper($GLOBALS['TL_LANG']['FMD']['mailchimp_subscribe'][0]).' ###';
  24.             return $objTemplate->parse();
  25.         }
  26.         $this->objMailChimp MailChimpModel::findByPk($this->mailchimpList);
  27.         $this->mailChimp = new Client($this->objMailChimp->listApiKey);
  28.         $this->mailChimpListId $this->objMailChimp->listId;
  29.         return parent::generate();
  30.     }
  31.     protected function compile()
  32.     {
  33.         System::loadLanguageFile('tl_module');
  34.         $objForm = new Form('mailchimp-subscribe''POST', function (Form $objHaste) {
  35.             return Input::post('FORM_SUBMIT') === $objHaste->getFormId();
  36.         });
  37.         $objForm->setFormActionFromUri(Environment::get('request'));
  38.         $eval = [
  39.             'mandatory' => true,
  40.             'rgxp' => 'email',
  41.         ];
  42.         if ((int) $this->mailchimpShowPlaceholder) {
  43.             $eval['placeholder'] = $GLOBALS['TL_LANG']['tl_module']['mailchimp']['placeholderEmail'];
  44.         }
  45.         $objForm->addFormField('email', [
  46.             'label' => $GLOBALS['TL_LANG']['tl_module']['mailchimp']['labelEmail'],
  47.             'inputType' => 'text',
  48.             'eval' => $eval,
  49.         ]);
  50.         $fields json_decode($this->objMailChimp->fields);
  51.         $mergeVarTags = [];
  52.         if (is_array($fields)) {
  53.             foreach ($fields as $field) {
  54.                 $addedName $this->addFieldToForm($field$objForm);
  55.                 if (null !== $addedName) {
  56.                     $mergeVarTags[] = $addedName;
  57.                 }
  58.             }
  59.         }
  60.         $objForm->addFormField('submit', [
  61.             'label' => $GLOBALS['TL_LANG']['tl_module']['mailchimp']['labelSubmit'],
  62.             'inputType' => 'submit'
  63.         ]);
  64.         $objForm->addContaoHiddenFields();
  65.         $this->Template->error false;
  66.         if ($objForm->validate()) {
  67.             $arrData $objForm->fetchAll();
  68.             $mergeVars = [];
  69.             foreach ($mergeVarTags as $tag) {
  70.                 $mergeVars[$tag] = $arrData[$tag];
  71.             }
  72.             $subscribed $this->mailChimp->subscribeToList(
  73.                 $this->mailChimpListId,
  74.                 $arrData['email'],
  75.                 $mergeVars,
  76.                 (boolean) $this->mailchimpOptin
  77.             );
  78.             if ($subscribed) {
  79.                 $this->jumpToOrReload($this->mailchimpJumpTo);
  80.             } else {
  81.                 $this->Template->error true;
  82.                 $this->Template->errorMsg $GLOBALS['TL_LANG']['tl_module']['mailchimp']['subscribeError'];
  83.             }
  84.         }
  85.         $form = new \stdClass();
  86.         $objForm->addToObject($form);
  87.         $this->Template->form $form;
  88.     }
  89.     /**
  90.      * Return the name of the field.
  91.      *
  92.      * @param $field
  93.      * @param  Form  $form
  94.      * @return mixed
  95.      */
  96.     protected function addFieldToForm($fieldForm $form)
  97.     {
  98.         if (!in_array($field->type, ['text''number''website''address''dropdown''radio''url''date''birthday''phone'])) {
  99.             return null;
  100.         }
  101.         switch ($field->type) {
  102.             case 'text':
  103.             case 'address':
  104.             case 'date':
  105.             case 'birthday':
  106.             case 'phone':
  107.                 $inputType 'text';
  108.                 $eval = [
  109.                     'mandatory' => $field->required,
  110.                 ];
  111.                 if (($maxLength = (int) $field->options->size) > 0) {
  112.                     $eval['maxlength'] = $maxLength;
  113.                 }
  114.                 if ((int) $this->mailchimpShowPlaceholder) {
  115.                     $eval['placeholder'] = $field->name;
  116.                 }
  117.                 if (false === (bool) $field->public) {
  118.                     $inputType 'hidden';
  119.                 }
  120.                 $form->addFormField($field->tag, [
  121.                     'label' => $field->name,
  122.                     'inputType' => $inputType,
  123.                     'eval' => $eval,
  124.                     'default' => $field->default,
  125.                 ]);
  126.                 break;
  127.             case 'dropdown':
  128.                 $inputType 'select';
  129.                 if (false === (bool) $field->public) {
  130.                     $inputType 'hidden';
  131.                 }
  132.                 $form->addFormField($field->tag, [
  133.                     'label' => $field->name,
  134.                     'inputType' => $inputType,
  135.                     'options' => $field->options->choices,
  136.                     'eval' => [
  137.                         'mandatory' => $field->required,
  138.                     ],
  139.                     'default' => $field->default,
  140.                 ]);
  141.                 break;
  142.             case 'radio':
  143.                 $inputType 'radio';
  144.                 if (false === (bool) $field->public) {
  145.                     $inputType 'hidden';
  146.                 }
  147.                 $form->addFormField($field->tag, [
  148.                     'label' => $field->name,
  149.                     'inputType' => $inputType,
  150.                     'options' => $field->options->choices,
  151.                     'eval' => [
  152.                         'mandatory' => $field->required,
  153.                     ],
  154.                     'default' => $field->default,
  155.                 ]);
  156.                 break;
  157.             case 'number':
  158.                 $inputType 'text';
  159.                 if (false === (bool) $field->public) {
  160.                     $inputType 'hidden';
  161.                 }
  162.                 $eval = [
  163.                     'rgxp' => 'digit',
  164.                     'mandatory' => $field->required,
  165.                 ];
  166.                 if ((int) $this->mailchimpShowPlaceholder) {
  167.                     $eval['placeholder'] = $field->name;
  168.                 }
  169.                 $form->addFormField($field->tag, [
  170.                     'label' => $field->name,
  171.                     'inputType' => $inputType,
  172.                     'eval' => $eval,
  173.                     'default' => $field->default,
  174.                 ]);
  175.                 break;
  176.             case 'url':
  177.                 $inputType 'text';
  178.                 if (false === (bool) $field->public) {
  179.                     $inputType 'hidden';
  180.                 }
  181.                 $eval = [
  182.                     'rgxp' => 'url',
  183.                     'mandatory' => $field->required,
  184.                 ];
  185.                 if ((int) $this->mailchimpShowPlaceholder) {
  186.                     $eval['placeholder'] = $field->name;
  187.                 }
  188.                 $form->addFormField($field->tag, [
  189.                     'label' => $field->name,
  190.                     'inputType' => $inputType,
  191.                     'eval' => $eval,
  192.                     'default' => $field->default,
  193.                 ]);
  194.                 break;
  195.         }
  196.         return $field->tag;
  197.     }
  198. }