BxFdbFormQuestion.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Feedback Feedback
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once (BX_DOL_DIR_STUDIO_CLASSES . 'BxDolStudioForm.php');
  12. class BxFdbFormQuestionChecker extends BxDolFormChecker
  13. {
  14. function __construct($sHelper = '')
  15. {
  16. parent::__construct($sHelper);
  17. }
  18. public function getSubmittedValueAnswers($sKey, $sMethod, &$aSpecificValues = false)
  19. {
  20. $aValues = array(
  21. 'ids' => array(),
  22. 'vals' => array(),
  23. 'imps' => array()
  24. );
  25. $iIndex = (int)BxDolForm::getSubmittedValue($sKey . '_ind', $sMethod, $aSpecificValues);
  26. for($i = 0; $i < $iIndex; $i++) {
  27. $sValue = BxDolForm::getSubmittedValue($sKey . '_' . $i, $sMethod, $aSpecificValues);
  28. if($sValue === false)
  29. continue;
  30. $aValues['vals'][$i] = $sValue;
  31. $sId = BxDolForm::getSubmittedValue($sKey . '_id_' . $i, $sMethod, $aSpecificValues);
  32. if($sId !== false)
  33. $aValues['ids'][$i] = $sId;
  34. $sImp = BxDolForm::getSubmittedValue($sKey . '_imp_' . $i, $sMethod, $aSpecificValues);
  35. if($sImp !== false && strcmp($sValue, $sImp) == 0)
  36. $aValues['imps'][] = $sImp;
  37. }
  38. return $aValues;
  39. }
  40. }
  41. class BxFdbFormQuestionCheckerHelper extends BxDolStudioFormCheckerHelper
  42. {
  43. static public function checkAvailAnswers ($s, $sName)
  44. {
  45. $iCountMin = 2;
  46. if(self::_isEmptyArray($s) || count($s) < $iCountMin)
  47. return false;
  48. $iCount = 0;
  49. foreach($s as $k => $v)
  50. if(!empty($v))
  51. $iCount += 1;
  52. return $iCount >= $iCountMin;
  53. }
  54. }
  55. class BxFdbFormQuestion extends BxTemplStudioFormView
  56. {
  57. protected $_sModule;
  58. protected $_oModule;
  59. protected $_sJsObject;
  60. public function __construct($aInfo, $oTemplate = false)
  61. {
  62. parent::__construct($aInfo, $oTemplate);
  63. $this->_sModule = 'bx_feedback';
  64. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  65. $this->_sJsObject = $this->_oModule->_oConfig->getJsObject('question');
  66. }
  67. function initChecker ($aValues = array (), $aSpecificValues = array())
  68. {
  69. $CNF = &$this->_oModule->_oConfig->CNF;
  70. if(isset($this->aInputs[$CNF['FIELD_ANSWERS']]) && !empty($aValues['id'])) {
  71. $aAnswers = $this->_oModule->_oDb->getAnswers(array(
  72. 'type' => 'question_id',
  73. 'question_id' => $aValues['id']
  74. ));
  75. $aValue = array(
  76. 'ids' => array(),
  77. 'vals' => array(),
  78. 'imps' => array()
  79. );
  80. foreach($aAnswers as $i => $aAnswer) {
  81. $aValue['ids'][$i] = $aAnswer['id'];
  82. $aValue['vals'][$i] = $aAnswer['title'];
  83. if((int)$aAnswer['important'] == 1)
  84. $aValue['imps'][$i] = true;
  85. }
  86. $this->aInputs[$CNF['FIELD_ANSWERS']]['value'] = $aValue;
  87. }
  88. return parent::initChecker($aValues, $aSpecificValues);
  89. }
  90. public function insert ($aValsToAdd = array(), $isIgnore = false)
  91. {
  92. $CNF = &$this->_oModule->_oConfig->CNF;
  93. $iContentId = parent::insert($aValsToAdd, $isIgnore);
  94. if(!empty($iContentId))
  95. $this->processAnswersAdd($CNF['FIELD_ANSWERS'], $iContentId);
  96. return $iContentId;
  97. }
  98. public function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  99. {
  100. $CNF = &$this->_oModule->_oConfig->CNF;
  101. $iResult = parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  102. $this->processAnswersUpdate($CNF['FIELD_ANSWERS'], $iContentId);
  103. return $iResult;
  104. }
  105. public function processAnswersAdd ($sField, $iContentId = 0)
  106. {
  107. $CNF = &$this->_oModule->_oConfig->CNF;
  108. if(!isset($this->aInputs[$sField]))
  109. return true;
  110. $iIndex = (int)$this->getCleanValue($sField . '_ind');
  111. for($i = 0; $i < $iIndex; $i++) {
  112. $sValue = $this->getCleanValue($sField . '_' . $i);
  113. if(empty($sValue) || get_mb_len($sValue) == 0)
  114. continue;
  115. $sImp = $this->getCleanValue($sField . '_imp_' . $i);
  116. $this->_oModule->_oDb->insertAnswer(array(
  117. 'question_id' => $iContentId,
  118. 'title' => bx_process_input($sValue),
  119. 'important' => !empty($sImp) && $sImp == 'on' ? 1 : 0,
  120. 'order' => $i
  121. ));
  122. }
  123. return true;
  124. }
  125. public function processAnswersUpdate($sField, $iContentId = 0)
  126. {
  127. $CNF = &$this->_oModule->_oConfig->CNF;
  128. if(!isset($this->aInputs[$sField]))
  129. return true;
  130. $iIndex = (int)$this->getCleanValue($sField . '_ind');
  131. $aAnswersValues = $aAnswersIds = $aAnswersImps = array();
  132. for($i = 0; $i < $iIndex; $i++) {
  133. $sValue = $this->getCleanValue($sField . '_' . $i);
  134. if(empty($sValue))
  135. continue;
  136. $aAnswersValues[$i] = $sValue;
  137. $iId = $this->getCleanValue($sField . '_id_' . $i);
  138. if(!empty($iId))
  139. $aAnswersIds[$i] = $iId;
  140. $sImpName = $sField . '_imp_' . $i;
  141. $sImpValue = $this->getCleanValue($sImpName);
  142. if(!empty($sImpValue) && $sImpValue == 'on')
  143. $aAnswersImps[$i] = true;
  144. }
  145. //--- Remove deleted
  146. $aAnswersDb = $this->_oModule->_oDb->getAnswers(array('type' => 'question_id_pairs', 'question_id' => $iContentId));
  147. $this->_oModule->_oDb->deleteAnswerById(array_diff(array_keys($aAnswersDb), $aAnswersIds));
  148. //--- Update existed and remove empty
  149. foreach($aAnswersIds as $i => $iAnswerId) {
  150. $sAnswersValue = $aAnswersValues[$i];
  151. if($sAnswersValue != '' && get_mb_len($sAnswersValue) > 0)
  152. $this->_oModule->_oDb->updateAnswer(array(
  153. 'title' => bx_process_input($sAnswersValue),
  154. 'important' => isset($aAnswersImps[$i]) && $aAnswersImps[$i] === true ? 1 : 0
  155. ), array('id' => (int)$iAnswerId));
  156. else
  157. $this->_oModule->_oDb->deleteAnswer(array('id' => (int)$iAnswerId));
  158. }
  159. //--- Add new
  160. $iAnswersIds = count($aAnswersIds);
  161. $iAnswersValues = count($aAnswersValues);
  162. if($iAnswersValues > $iAnswersIds) {
  163. $iMaxOrder = (int)$this->_oModule->_oDb->getAnswers(array('type' => 'question_id_max_order', 'question_id' => $iContentId));
  164. for($i = $iAnswersIds; $i < $iAnswersValues; $i++)
  165. if($aAnswersValues[$i] != '' && get_mb_len($aAnswersValues[$i]) > 0)
  166. $this->_oModule->_oDb->insertAnswer(array(
  167. 'question_id' => $iContentId,
  168. 'title' => bx_process_input($aAnswersValues[$i]),
  169. 'important' => isset($aAnswersImps[$i]) && $aAnswersImps[$i] === true ? 1 : 0,
  170. 'order' => ++$iMaxOrder
  171. ));
  172. }
  173. return true;
  174. }
  175. protected function genCustomInputAnswers(&$aInput)
  176. {
  177. $iIndex = 0;
  178. $sAnswers = '';
  179. if(!empty($aInput['value']) && !empty($aInput['value']['vals']) && is_array($aInput['value']['vals'])) {
  180. $aImps = array();
  181. if(!empty($aInput['value']['imps']) && is_array($aInput['value']['imps']))
  182. $aImps = $aInput['value']['imps'];
  183. foreach($aInput['value']['vals'] as $i => $sValue)
  184. $sAnswers .= $this->_genAnswerSample($aInput, $iIndex++, array(
  185. 'value' => $sValue,
  186. 'values' => !empty($aInput['values']['vals'][$i]) && is_array($aInput['values']['vals'][$i]) ? $aInput['values']['vals'][$i] : '',
  187. 'id' => !empty($aInput['value']['ids'][$i]) ? (int)$aInput['value']['ids'][$i] : '',
  188. 'checked' => isset($aImps[$i]) && $aImps[$i] === true,
  189. ));
  190. }
  191. else
  192. for($i = 0; $i < 2; $i++)
  193. $sAnswers .= $this->_genAnswerSample($aInput, $iIndex++);
  194. return $this->_oModule->_oTemplate->parseHtmlByName('form_answers.html', array(
  195. 'index' => $this->genCustomInputAnswersIndex($aInput, $iIndex),
  196. 'sample' => $this->_genAnswerSample($aInput, '|x|'),
  197. 'answers' => $sAnswers,
  198. 'btn_add' => $this->genCustomInputAnswersButton($aInput)
  199. ));
  200. }
  201. protected function _genAnswerSample($aInput, $iIndex, $aParams = array())
  202. {
  203. $sClass = 'bx-form-input-answer';
  204. if(!empty($aParams['class']))
  205. $sClass .= ' ' . $aParams['class'];
  206. $mixedValue = !empty($aParams['value']) ? $aParams['value'] : '';
  207. $mixedValues = !empty($aParams['values']) ? $aParams['values'] : '';
  208. $sInputText = $this->genCustomInputAnswersText($aInput, $iIndex, $mixedValue, $mixedValues);
  209. if(!empty($aParams['id']))
  210. $sInputText .= $this->genCustomInputAnswersHidden($aInput, $iIndex, $aParams['id']);
  211. return $this->_oModule->_oTemplate->parseHtmlByName('form_answer.html', array(
  212. 'js_object' => $this->_sJsObject,
  213. 'class' => $sClass,
  214. 'input_text' => $sInputText,
  215. 'input_checkbox' => $this->genCustomInputAnswersCheckbox($aInput, $iIndex, 'on', (isset($aParams['checked']) && $aParams['checked'] === true))
  216. ));
  217. }
  218. protected function genCustomInputAnswersText($aInput, $iIndex, $mixedValue = '', $mixedValues = '')
  219. {
  220. $aInput['type'] = 'text_translatable';
  221. $aInput['name'] .= '_' . $iIndex;
  222. $aInput['caption'] = '';
  223. $aInput['value'] = $mixedValue;
  224. $aInput['values'] = $mixedValues;
  225. $aInput['db'] = array(
  226. 'pass' => 'Xss'
  227. );
  228. if(!is_array($aInput['attrs']))
  229. $aInput['attrs'] = array();
  230. $sJsMethod = 'javascript:' . $this->_sJsObject . '.answerOnType(event, \'' . $aInput['name'] . '\')';
  231. $aInput['attrs'] = array_merge($aInput['attrs'], array(
  232. 'bx-index' => $iIndex,
  233. 'class' => 'bx-def-margin-sec-top-auto',
  234. 'onkeyup' => $sJsMethod,
  235. 'onpaste' => $sJsMethod,
  236. 'onblur' => $sJsMethod
  237. ));
  238. return $this->genRow($aInput);
  239. }
  240. protected function genCustomInputAnswersCheckbox($aInput, $iIndex, $mixedValue = '', $bChecked = false)
  241. {
  242. $aInput['type'] = 'checkbox';
  243. $aInput['name'] .= '_imp_' . $iIndex;
  244. $aInput['caption'] = _t('_bx_feedback_form_question_input_answers_imp');
  245. $aInput['value'] = $mixedValue;
  246. $aInput['required'] = 0;
  247. if($bChecked)
  248. $aInput['checked'] = 'checked';
  249. if(!is_array($aInput['attrs']))
  250. $aInput['attrs'] = array();
  251. $aInput['attrs'] = array_merge($aInput['attrs'], array(
  252. 'bx-index' => $iIndex,
  253. ));
  254. return $this->genRow($aInput);
  255. }
  256. protected function genCustomInputAnswersHidden($aInput, $iIndex, $mixedValue = '')
  257. {
  258. $aInput['type'] = 'hidden';
  259. $aInput['name'] .= '_id_' . $iIndex;
  260. $aInput['value'] = $mixedValue;
  261. if(!is_array($aInput['attrs']))
  262. $aInput['attrs'] = array();
  263. $aInput['attrs'] = array_merge($aInput['attrs'], array(
  264. 'bx-index' => $iIndex,
  265. ));
  266. return $this->genInput($aInput);
  267. }
  268. protected function genCustomInputAnswersIndex($aInput, $mixedValue = '')
  269. {
  270. $aInput['type'] = 'hidden';
  271. $aInput['name'] .= '_ind';
  272. $aInput['value'] = $mixedValue;
  273. return $this->genInput($aInput);
  274. }
  275. protected function genCustomInputAnswersButton($aInput)
  276. {
  277. $sName = $aInput['name'];
  278. $aInput['type'] = 'button';
  279. $aInput['name'] .= '_add';
  280. $aInput['value'] = _t('_bx_feedback_form_question_input_answers_add');
  281. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  282. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('question') . ".answerAdd(this, '" . $sName . "');";
  283. return $this->genInputButton($aInput);
  284. }
  285. protected function _processTranslationsValuesByNameList($sName, $iIndex)
  286. {
  287. for($i = 0; $i < $iIndex; $i++)
  288. $this->aInputs[$sName]['values']['vals'][$i] = $this->_getTranslationsValuesByName($sName . '_' . $i);
  289. }
  290. }
  291. /** @} */