_idItem) { $this->_idItem = abs(crc32(__CLASS__)); } if ('split' != $this->getConfig('nameType')) { $this->_userFieldRules['fullName'] = [$this, 'prepareUserName']; } return $this; } public function getFormItem($options = []) { $options = array_merge( $options, [ 'action' => Qs_SiteMap::findFirst([], ['type' => 'Form_Newsletter_', 'action' => 'insert'], [], 'url'), ] ); $form = parent::_getNewForm($options); $form->prependId(); $item = [ 'tpl' => $this->getTemplate('text.tpl'), 'text' => $form->render(), 'formId' => $form->getId(), 'isSideBlock' => true, 'thankYouMessage' => $this->_getThankYouMessage(), ]; if (empty($item['config'])) { $item['config'] = $this->getConfig()->toArray(); } $item['templatePath'] = $this->_getTemplatePath(); $docItem = new Qs_Doc_Item($item); $this->_doc->addItemResources($docItem); return $docItem; } public function _doNew() { $item = $this->getFormItem(); $item->isSideBlock = false; $this->_doc->addItem($item); $this->_postNew(); return $this; } protected function prepareUserName(array $userData) { return $userData['firstName'] . ' ' . $userData['lastName']; } protected function _getSideBlockForm($blockData) { $formOptions = $this->_getFormOptions('new'); $formOptions['attribs']['id'] .= '_sideblock-' . Qs_Array::get($blockData, 'id', 0); return $this->_getNewForm($formOptions); } public function getSideBlockItem(array $blockData = []) { $item = []; $this->_sideBlockData = $blockData; $this->_initAction(); if ('insert' == $this->_action) { $this->exec(); } else { $form = $this->_getSideBlockForm($blockData); $form->setDefaults(); $item = [ 'tpl' => $this->getTemplate('text.tpl'), 'formId' => $form->getId(), 'isSideBlock' => true, 'thankYouMessage' => $this->_getThankYouMessage(), 'text' => $form->render(), ]; } return $item; } protected function _doInsertAjax() { $this->_doInsert(); } protected function _doInsert() { if (!empty($this->_sideBlockData)) { $form = $this->_getSideBlockForm($this->_sideBlockData); } else { $form = $this->_getNewForm(); } $result = $form->validateAjax(); if ($result['isValid']) { $this->_initFromForm($form); if (false === $this->_getDataObj()->insert()) { $result['idValid'] = false; $result['formErrors'] = $this->_getDataObj()->getErrors(); } else { $this->_postInsert(); } } $this->_displayJson($result); } protected function _initFromForm(Qs_Form $form) { $data = $form->getValues(); if (empty($data['fullName'])) { $data['fullName'] = $data['firstName'] . ' ' . $data['lastName']; } $this->_getDataObj()->initFromForm($data); return $this; } protected function _getMailData(array $data) { $mailData = [ 'name' => htmlspecialchars($data['fullName']), 'email' => htmlspecialchars($data['email']), 'link' => Qs_SiteMap::findFirst(null, ['type' => 'Form_Newsletter_Admin_'], null, 'url') . '?action=view&id=' . $data['id'], ]; return $mailData; } protected function _postInsert() { $data = $this->_getDataObj()->clearData()->getData(); $mailchimp = new Qs_Mailchimp(); $mailchimp->subscribeMember($data); $this->_sendAdminNotification($data); $this->_sendUserNotification($data); $this->_log(); return $this; } protected function _sendUserNotification(array $data) { $to = $data['email']; $body = App_Settings_Obj::get($this->_settingsUserPrefix . 'Body'); if (empty($to) || empty($body)) { return false; } $subject = App_Settings_Obj::get($this->_settingsUserPrefix . 'Subject'); $from = App_Settings_Obj::getEmailFrom($this->_settingsUserPrefix . 'From'); $mailData = $this->_getMailData($data); foreach ($mailData as $field => $value) { $body = str_replace('{' . $field . '}', $value, $body); } $options = [ 'from' => $from, 'to' => $to, 'subject' => $subject, 'body' => $body, ]; $this->_sendMail($options); return $this; } protected function _addFormItem(Qs_Form $form) { $item['tpl'] = $this->getTemplate('text.tpl'); $item['header'] = $this->getConfig('formHeader'); $item['thankYouMessage'] = $this->_getThankYouMessage(); $item['text'] = $form->render(); $this->_addItem($item); return $this; } }