/starter/htdocs/core/modules/apps/components/FeedbackForm.class.php
PHP | 260 lines | 156 code | 35 blank | 69 comment | 15 complexity | ae61cbf72e3b3cdc73c69ec31d1e0d6e MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
- <?php
- /**
- * ???????? ????? FeedbackForm
- *
- * @package energine
- * @subpackage share
- * @author dr.Pavka
- * @copyright Energine 2007
- * @version $Id$
- */
- /**
- * ????? ???????? ?????
- *
- * @package energine
- * @subpackage share
- * @author dr.Pavka
- */
- class FeedbackForm extends DBDataSet {
- /**
- * ??????????? ??????
- *
- * @param string $name
- * @param string $module
- * @param array $params
- * @access public
- */
- public function __construct($name, $module, array $params = null) {
- parent::__construct($name, $module, $params);
- //$tableName = $this->getParam('tableName');
- /*if(!($tableName)){
- $this->setTableName('apps_feedback');
- }else {
- $this->setTableName($tableName);
- }*/
- $this->setType(self::COMPONENT_TYPE_FORM_ADD);
- $this->setAction('send');
- $this->setTitle($this->translate('TXT_FEEDBACK_FORM'));
- $this->addTranslation('TXT_ENTER_CAPTCHA');
- }
- /**
- * ????????????? ???????? active
- *
- * @return int
- * @access protected
- */
- protected function defineParams() {
- $result = array_merge(parent::defineParams(),
- array(
- 'active' => true,
- 'textBlock' => false,
- 'tableName' => 'apps_feedback',
- 'recipientEmail' => false,
- 'userSubject' => 'TXT_SUBJ_FEEDBACK_USER',
- 'userBody' => 'TXT_BODY_FEEDBACK_USER',
- 'adminSubject' => 'TXT_SUBJ_FEEDBACK_ADMIN',
- 'adminBody' => 'TXT_BODY_FEEDBACK_ADMIN',
- 'noCaptcha' => false
- ));
- return $result;
- }
- /**
- * ????????? ??????
- *
- * @return mixed
- * @access protected
- */
- protected function saveData($data) {
- $result = false;
- //??????? ?????? ???????? ??????
- $dataDescriptionObject = new DataDescription();
- //???????? ???????? ????? ??? ??????
- $configDataDescription =
- $this->getConfig()->getStateConfig($this->getPreviousState());
- //???? ? ??????? ???? ???????? ????? ??? ?????? - ????????? ??
- if (isset($configDataDescription->fields)) {
- $dataDescriptionObject->loadXML($configDataDescription->fields);
- }
- //??????? ?????? ???????? ?????? ?????? ?? ??
- $DBDataDescription = new DataDescription();
- //????????? ? ???? ???? ? ????????
- $DBDataDescription->load($this->loadDataDescription());
- $this->setDataDescription($dataDescriptionObject->intersect($DBDataDescription));
- $dataObject = new Data();
- $dataObject->load($data);
- $this->setData($dataObject);
- //??????? ??????
- $this->saver = new Saver();
- //????????????? ??? ?????
- $this->saver->setMode(self::COMPONENT_TYPE_FORM_ADD);
- $this->saver->setDataDescription($this->getDataDescription());
- $this->saver->setData($this->getData());
- if ($this->saver->validate() === true) {
- $this->saver->setFilter($this->getFilter());
- $this->saver->save();
- $result = $this->saver->getResult();
- }
- else {
- //??????????? ?????? exception ??????? ??????????????? ? ?????? save
- throw new SystemException('ERR_VALIDATE_FORM', SystemException::ERR_WARNING, $this->saver->getErrors());
- }
- return $result;
- }
- /**
- * ?????????? ????????? ? ??, ?????????? ??????????? ???????????? ? ??????????????
- *
- * @return void
- * @access protected
- */
- protected function send() {
- if(!isset($_POST[$this->getTableName()])){
- E()->getResponse()->redirectToCurrentSection();
- }
- try {
- $data[$this->getTableName()] = $_POST[$this->getTableName()];
-
- if (!$this->document->getUser()->isAuthenticated() && !$this->getParam('noCaptcha')) {
- $this->checkCaptcha();
- }
- if ($result = $this->saveData($data)) {
- $data = $data[$this->getTableName()];
- $senderEmail = '';
- if (isset($data['feed_email'])) {
- $senderEmail = $data['feed_email'];
- } else {
- $data['feed_email'] =
- $this->translate('TXT_NO_EMAIL_ENTERED');
- }
- $this->dbh->modify(QAL::UPDATE, $this->getTableName(), array('feed_date' => date('Y-m-d H:i:s')), array($this->getPK() => $result));
- if ($senderEmail) {
- $mailer = new Mail();
- $mailer->setFrom($this->getConfigValue('mail.from'))->
- setSubject($this->translate($this->getParam('userSubject')))->
- setText($this->translate($this->getParam('userBody')), $data)->
- addTo($senderEmail, $senderEmail)
- ->send();
- }
- try {
- $mailer = new Mail();
- $recipientID = false;
- if (isset($data['feed_type']) &&
- intval($data['feed_type'])) {
- $recipientID = $data['feed_type'];
- }
- $mailer->setFrom($this->getConfigValue('mail.from'))->
- setSubject($this->translate($this->getParam('adminSubject')))->
- setText($this->translate($this->getParam('adminBody')), $data)->
- addTo($this->getRecipientEmail($recipientID))->send();
- }
- catch (Exception $e) {
- }
- }
- $this->prepare();
- if ($this->getParam('textBlock') && ($textBlock =
- $this->document->componentManager->getBlockByName($this->getParam('textBlock')))) {
- $textBlock->disable();
- }
- $this->response->redirectToCurrentSection('success/');
- }
- catch (Exception $e) {
- $this->failure($e->getMessage(), $data[$this->getTableName()]);
- }
- }
- /**
- * ???????? ?????? ??????????
- *
- * @return string
- * @access private
- */
- protected function getRecipientEmail($options = false) {
- return $this->getParam('recipientEmail');
- }
- /*
- * ?????????? ? ??????? ??????? ? captcha
- */
- protected function failure($errorMessage, $data) {
- $this->getConfig()->setCurrentState('main');
- $this->prepare();
- $eFD = new FieldDescription('error_message');
- $eFD->setMode(FieldDescription::FIELD_MODE_READ);
- $eFD->setType(FieldDescription::FIELD_TYPE_CUSTOM);
- $this->getDataDescription()->addFieldDescription($eFD);
- $this->getData()->load(array(array_merge(array('error_message' => $errorMessage), $data)));
- $this->getDataDescription()->getFieldDescriptionByName('error_message')->removeProperty('title');
- }
- /*
- * ????????? ?????
- */
- protected function checkCaptcha() {
- require_once('core/kernel/recaptchalib.php');
- $privatekey = $this->getConfigValue('recaptcha.private');
- $resp = recaptcha_check_answer($privatekey,
- $_SERVER["REMOTE_ADDR"],
- $_POST["recaptcha_challenge_field"],
- $_POST["recaptcha_response_field"]);
- if (!$resp->is_valid) {
- throw new SystemException($this->translate('TXT_BAD_CAPTCHA'), SystemException::ERR_CRITICAL);
- }
- }
- protected function prepare() {
- parent::prepare();
- if (
- $this->document->getUser()->isAuthenticated()
- &&
- ($captcha =
- $this->getDataDescription()->getFieldDescriptionByName('captcha'))
- ) {
- $this->getDataDescription()->removeFieldDescription($captcha);
- }
- }
- protected function success() {
- $this->setBuilder($this->createBuilder());
- $dataDescription = new DataDescription();
- $ddi = new FieldDescription('result');
- $ddi->setType(FieldDescription::FIELD_TYPE_TEXT);
- $ddi->setMode(FieldDescription::FIELD_MODE_READ);
- $ddi->removeProperty('title');
- $dataDescription->addFieldDescription($ddi);
- $data = new Data();
- $di = new Field('result');
- $di->setData($this->translate('TXT_FEEDBACK_SUCCESS_SEND'));
- $data->addField($di);
- $this->setDataDescription($dataDescription);
- $this->setData($data);
- }
- }