PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/starter/htdocs/core/modules/apps/components/FeedbackForm.class.php

http://energine.googlecode.com/
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
  1. <?php
  2. /**
  3. * ???????? ????? FeedbackForm
  4. *
  5. * @package energine
  6. * @subpackage share
  7. * @author dr.Pavka
  8. * @copyright Energine 2007
  9. * @version $Id$
  10. */
  11. /**
  12. * ????? ???????? ?????
  13. *
  14. * @package energine
  15. * @subpackage share
  16. * @author dr.Pavka
  17. */
  18. class FeedbackForm extends DBDataSet {
  19. /**
  20. * ??????????? ??????
  21. *
  22. * @param string $name
  23. * @param string $module
  24. * @param array $params
  25. * @access public
  26. */
  27. public function __construct($name, $module, array $params = null) {
  28. parent::__construct($name, $module, $params);
  29. //$tableName = $this->getParam('tableName');
  30. /*if(!($tableName)){
  31. $this->setTableName('apps_feedback');
  32. }else {
  33. $this->setTableName($tableName);
  34. }*/
  35. $this->setType(self::COMPONENT_TYPE_FORM_ADD);
  36. $this->setAction('send');
  37. $this->setTitle($this->translate('TXT_FEEDBACK_FORM'));
  38. $this->addTranslation('TXT_ENTER_CAPTCHA');
  39. }
  40. /**
  41. * ????????????? ???????? active
  42. *
  43. * @return int
  44. * @access protected
  45. */
  46. protected function defineParams() {
  47. $result = array_merge(parent::defineParams(),
  48. array(
  49. 'active' => true,
  50. 'textBlock' => false,
  51. 'tableName' => 'apps_feedback',
  52. 'recipientEmail' => false,
  53. 'userSubject' => 'TXT_SUBJ_FEEDBACK_USER',
  54. 'userBody' => 'TXT_BODY_FEEDBACK_USER',
  55. 'adminSubject' => 'TXT_SUBJ_FEEDBACK_ADMIN',
  56. 'adminBody' => 'TXT_BODY_FEEDBACK_ADMIN',
  57. 'noCaptcha' => false
  58. ));
  59. return $result;
  60. }
  61. /**
  62. * ????????? ??????
  63. *
  64. * @return mixed
  65. * @access protected
  66. */
  67. protected function saveData($data) {
  68. $result = false;
  69. //??????? ?????? ???????? ??????
  70. $dataDescriptionObject = new DataDescription();
  71. //???????? ???????? ????? ??? ??????
  72. $configDataDescription =
  73. $this->getConfig()->getStateConfig($this->getPreviousState());
  74. //???? ? ??????? ???? ???????? ????? ??? ?????? - ????????? ??
  75. if (isset($configDataDescription->fields)) {
  76. $dataDescriptionObject->loadXML($configDataDescription->fields);
  77. }
  78. //??????? ?????? ???????? ?????? ?????? ?? ??
  79. $DBDataDescription = new DataDescription();
  80. //????????? ? ???? ???? ? ????????
  81. $DBDataDescription->load($this->loadDataDescription());
  82. $this->setDataDescription($dataDescriptionObject->intersect($DBDataDescription));
  83. $dataObject = new Data();
  84. $dataObject->load($data);
  85. $this->setData($dataObject);
  86. //??????? ??????
  87. $this->saver = new Saver();
  88. //????????????? ??? ?????
  89. $this->saver->setMode(self::COMPONENT_TYPE_FORM_ADD);
  90. $this->saver->setDataDescription($this->getDataDescription());
  91. $this->saver->setData($this->getData());
  92. if ($this->saver->validate() === true) {
  93. $this->saver->setFilter($this->getFilter());
  94. $this->saver->save();
  95. $result = $this->saver->getResult();
  96. }
  97. else {
  98. //??????????? ?????? exception ??????? ??????????????? ? ?????? save
  99. throw new SystemException('ERR_VALIDATE_FORM', SystemException::ERR_WARNING, $this->saver->getErrors());
  100. }
  101. return $result;
  102. }
  103. /**
  104. * ?????????? ????????? ? ??, ?????????? ??????????? ???????????? ? ??????????????
  105. *
  106. * @return void
  107. * @access protected
  108. */
  109. protected function send() {
  110. if(!isset($_POST[$this->getTableName()])){
  111. E()->getResponse()->redirectToCurrentSection();
  112. }
  113. try {
  114. $data[$this->getTableName()] = $_POST[$this->getTableName()];
  115. if (!$this->document->getUser()->isAuthenticated() && !$this->getParam('noCaptcha')) {
  116. $this->checkCaptcha();
  117. }
  118. if ($result = $this->saveData($data)) {
  119. $data = $data[$this->getTableName()];
  120. $senderEmail = '';
  121. if (isset($data['feed_email'])) {
  122. $senderEmail = $data['feed_email'];
  123. } else {
  124. $data['feed_email'] =
  125. $this->translate('TXT_NO_EMAIL_ENTERED');
  126. }
  127. $this->dbh->modify(QAL::UPDATE, $this->getTableName(), array('feed_date' => date('Y-m-d H:i:s')), array($this->getPK() => $result));
  128. if ($senderEmail) {
  129. $mailer = new Mail();
  130. $mailer->setFrom($this->getConfigValue('mail.from'))->
  131. setSubject($this->translate($this->getParam('userSubject')))->
  132. setText($this->translate($this->getParam('userBody')), $data)->
  133. addTo($senderEmail, $senderEmail)
  134. ->send();
  135. }
  136. try {
  137. $mailer = new Mail();
  138. $recipientID = false;
  139. if (isset($data['feed_type']) &&
  140. intval($data['feed_type'])) {
  141. $recipientID = $data['feed_type'];
  142. }
  143. $mailer->setFrom($this->getConfigValue('mail.from'))->
  144. setSubject($this->translate($this->getParam('adminSubject')))->
  145. setText($this->translate($this->getParam('adminBody')), $data)->
  146. addTo($this->getRecipientEmail($recipientID))->send();
  147. }
  148. catch (Exception $e) {
  149. }
  150. }
  151. $this->prepare();
  152. if ($this->getParam('textBlock') && ($textBlock =
  153. $this->document->componentManager->getBlockByName($this->getParam('textBlock')))) {
  154. $textBlock->disable();
  155. }
  156. $this->response->redirectToCurrentSection('success/');
  157. }
  158. catch (Exception $e) {
  159. $this->failure($e->getMessage(), $data[$this->getTableName()]);
  160. }
  161. }
  162. /**
  163. * ???????? ?????? ??????????
  164. *
  165. * @return string
  166. * @access private
  167. */
  168. protected function getRecipientEmail($options = false) {
  169. return $this->getParam('recipientEmail');
  170. }
  171. /*
  172. * ?????????? ? ??????? ??????? ? captcha
  173. */
  174. protected function failure($errorMessage, $data) {
  175. $this->getConfig()->setCurrentState('main');
  176. $this->prepare();
  177. $eFD = new FieldDescription('error_message');
  178. $eFD->setMode(FieldDescription::FIELD_MODE_READ);
  179. $eFD->setType(FieldDescription::FIELD_TYPE_CUSTOM);
  180. $this->getDataDescription()->addFieldDescription($eFD);
  181. $this->getData()->load(array(array_merge(array('error_message' => $errorMessage), $data)));
  182. $this->getDataDescription()->getFieldDescriptionByName('error_message')->removeProperty('title');
  183. }
  184. /*
  185. * ????????? ?????
  186. */
  187. protected function checkCaptcha() {
  188. require_once('core/kernel/recaptchalib.php');
  189. $privatekey = $this->getConfigValue('recaptcha.private');
  190. $resp = recaptcha_check_answer($privatekey,
  191. $_SERVER["REMOTE_ADDR"],
  192. $_POST["recaptcha_challenge_field"],
  193. $_POST["recaptcha_response_field"]);
  194. if (!$resp->is_valid) {
  195. throw new SystemException($this->translate('TXT_BAD_CAPTCHA'), SystemException::ERR_CRITICAL);
  196. }
  197. }
  198. protected function prepare() {
  199. parent::prepare();
  200. if (
  201. $this->document->getUser()->isAuthenticated()
  202. &&
  203. ($captcha =
  204. $this->getDataDescription()->getFieldDescriptionByName('captcha'))
  205. ) {
  206. $this->getDataDescription()->removeFieldDescription($captcha);
  207. }
  208. }
  209. protected function success() {
  210. $this->setBuilder($this->createBuilder());
  211. $dataDescription = new DataDescription();
  212. $ddi = new FieldDescription('result');
  213. $ddi->setType(FieldDescription::FIELD_TYPE_TEXT);
  214. $ddi->setMode(FieldDescription::FIELD_MODE_READ);
  215. $ddi->removeProperty('title');
  216. $dataDescription->addFieldDescription($ddi);
  217. $data = new Data();
  218. $di = new Field('result');
  219. $di->setData($this->translate('TXT_FEEDBACK_SUCCESS_SEND'));
  220. $data->addField($di);
  221. $this->setDataDescription($dataDescription);
  222. $this->setData($data);
  223. }
  224. }