BxBaseModGeneralFormPoll.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 BaseGeneral Base classes for general modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModGeneralFormPoll extends BxTemplFormView
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. public function __construct($aInfo, $oTemplate = false)
  16. {
  17. parent::__construct($aInfo, $oTemplate);
  18. $this->_aFieldsCheckForSpam = ['answers'];
  19. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  20. }
  21. function initChecker ($aValues = array (), $aSpecificValues = array())
  22. {
  23. $CNF = &$this->_oModule->_oConfig->CNF;
  24. if(isset($this->aInputs[$CNF['FIELD_POLL_ANSWERS']]) && !empty($aValues['id'])) {
  25. $aAnswers = $this->_oModule->_oDb->getPollAnswers(array(
  26. 'type' => 'poll_id_pairs',
  27. 'poll_id' => $aValues['id']
  28. ));
  29. $this->aInputs[$CNF['FIELD_POLL_ANSWERS']]['value'] = array_values($aAnswers);
  30. $this->aInputs[$CNF['FIELD_POLL_ANSWERS']]['value_ids'] = array_keys($aAnswers);
  31. }
  32. return parent::initChecker($aValues, $aSpecificValues);
  33. }
  34. public function insert ($aValsToAdd = array(), $isIgnore = false)
  35. {
  36. $CNF = &$this->_oModule->_oConfig->CNF;
  37. if(empty($aValsToAdd[$CNF['FIELD_POLL_AUTHOR_ID']]))
  38. $aValsToAdd[$CNF['FIELD_POLL_AUTHOR_ID']] = bx_get_logged_profile_id();
  39. $iContentId = parent::insert($aValsToAdd, $isIgnore);
  40. if(!empty($iContentId))
  41. $this->processAnswersAdd($CNF['FIELD_POLL_ANSWERS'], $iContentId);
  42. return $iContentId;
  43. }
  44. public function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  45. {
  46. $CNF = &$this->_oModule->_oConfig->CNF;
  47. $iResult = parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  48. $this->processAnswersUpdate($CNF['FIELD_POLL_ANSWERS'], $iContentId);
  49. return $iResult;
  50. }
  51. public function getCleanValue ($sName)
  52. {
  53. $mixedValue = parent::getCleanValue($sName);
  54. if($this->_bIsApi && !empty($mixedValue) && in_array($sName, ['answers', 'answers_ids']))
  55. $mixedValue = explode(',', $mixedValue);
  56. return $mixedValue;
  57. }
  58. public function processAnswersAdd ($sField, $iContentId = 0)
  59. {
  60. $CNF = &$this->_oModule->_oConfig->CNF;
  61. if(!isset($this->aInputs[$sField]))
  62. return true;
  63. $aAnswers = $this->getCleanValue($sField);
  64. if(empty($aAnswers) || !is_array($aAnswers))
  65. return true;
  66. foreach($aAnswers as $iIndex => $sAnswer)
  67. if($sAnswer != '' && get_mb_len($sAnswer) > 0)
  68. $this->_oModule->_oDb->insertPollAnswer(array(
  69. 'poll_id' => $iContentId,
  70. 'title' => bx_process_input($sAnswer),
  71. 'order' => $iIndex
  72. ));
  73. return true;
  74. }
  75. public function processAnswersUpdate($sField, $iContentId = 0)
  76. {
  77. $CNF = &$this->_oModule->_oConfig->CNF;
  78. if(!isset($this->aInputs[$sField]))
  79. return true;
  80. $aAnswersIds = $this->getCleanValue($sField . '_ids');
  81. $aAnswersValues = $this->getCleanValue($sField);
  82. //--- Remove deleted
  83. $aAnswersDb = $this->_oModule->_oDb->getPollAnswers(array('type' => 'poll_id_pairs', 'poll_id' => $iContentId));
  84. $this->_oModule->_oDb->deletePollAnswersByIds(array_diff(array_keys($aAnswersDb), $aAnswersIds));
  85. //--- Update existed and remove empty
  86. foreach($aAnswersIds as $iIndex => $iId)
  87. if($aAnswersValues[$iIndex] != '' && get_mb_len($aAnswersValues[$iIndex]) > 0)
  88. $this->_oModule->_oDb->updatePollAnswers(array('title' => bx_process_input($aAnswersValues[$iIndex])), array('id' => (int)$iId));
  89. else
  90. $this->_oModule->_oDb->deletePollAnswers(array('id' => (int)$iId));
  91. //--- Add new
  92. $iAnswersIds = count($aAnswersIds);
  93. $iAnswersValues = count($aAnswersValues);
  94. if($iAnswersValues > $iAnswersIds) {
  95. $iMaxOrder = (int)$this->_oModule->_oDb->getPollAnswers(array('type' => 'poll_id_max_order', 'poll_id' => $iContentId));
  96. $aAnswersValues = array_slice($aAnswersValues, $iAnswersIds);
  97. foreach($aAnswersValues as $sAnswersValue)
  98. if($sAnswersValue != '' && get_mb_len($sAnswersValue) > 0)
  99. $this->_oModule->_oDb->insertPollAnswer(array(
  100. 'poll_id' => $iContentId,
  101. 'title' => bx_process_input($sAnswersValue),
  102. 'order' => ++$iMaxOrder
  103. ));
  104. }
  105. return true;
  106. }
  107. protected function genCustomInputAnswers(&$aInput)
  108. {
  109. if($this->_bIsApi)
  110. return array_merge($aInput, [
  111. 'type' => 'multi_field',
  112. 'subtype' => 'text'
  113. ]);
  114. $sJsObject = $this->_oModule->_oConfig->getJsObject('poll');
  115. $aTmplVarsAnswers = array(
  116. array('class' => 'bx-fi-answer-blank', 'js_object' => $sJsObject, 'input_text' => $this->genCustomInputAnswersText($aInput, '', true)),
  117. );
  118. if(!empty($aInput['value']) && is_array($aInput['value']))
  119. foreach($aInput['value'] as $iKey => $sValue) {
  120. $sInput = $this->genCustomInputAnswersText($aInput, $sValue);
  121. if(!empty($aInput['value_ids'][$iKey]))
  122. $sInput .= $this->genCustomInputAnswersHidden($aInput, (int)$aInput['value_ids'][$iKey]);
  123. $aTmplVarsAnswers[] = array('class' => '', 'js_object' => $sJsObject, 'input_text' => $sInput);
  124. }
  125. else
  126. $aTmplVarsAnswers = array_merge($aTmplVarsAnswers, array(
  127. array('class' => '', 'js_object' => $sJsObject, 'input_text' => $this->genCustomInputAnswersText($aInput)),
  128. array('class' => '', 'js_object' => $sJsObject, 'input_text' => $this->genCustomInputAnswersText($aInput))
  129. ));
  130. return $this->_oModule->_oTemplate->parseHtmlByName('poll_form_answers.html', array(
  131. 'bx_repeat:answers' => $aTmplVarsAnswers,
  132. 'btn_add' => $this->genCustomInputAnswersButton($aInput)
  133. ));
  134. }
  135. protected function genCustomInputAnswersText($aInput, $mixedValue = '', $bDisabled = false)
  136. {
  137. $aInput['type'] = 'text';
  138. $aInput['name'] .= '[]';
  139. $aInput['value'] = $mixedValue;
  140. $aInput['attrs']['class'] = 'bx-def-margin-sec-top-auto';
  141. if($bDisabled)
  142. $aInput['attrs']['disabled'] = 'disabled';
  143. return $this->genInput($aInput);
  144. }
  145. protected function genCustomInputAnswersHidden($aInput, $mixedValue = '')
  146. {
  147. $aInput['type'] = 'hidden';
  148. $aInput['name'] .= '_ids[]';
  149. $aInput['value'] = $mixedValue;
  150. return $this->genInput($aInput);
  151. }
  152. protected function genCustomInputAnswersButton($aInput)
  153. {
  154. $CNF = &$this->_oModule->_oConfig->CNF;
  155. $sName = $aInput['name'];
  156. $aInput['type'] = 'button';
  157. $aInput['name'] .= '_add';
  158. $aInput['value'] = _t($CNF['T']['txt_poll_form_answers_add']);
  159. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  160. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('poll') . ".addPollAnswer(this, '" . $sName . "');";
  161. return $this->genInputButton($aInput);
  162. }
  163. }
  164. class BxBaseModGeneralFormPollCheckerHelper extends BxDolFormCheckerHelper
  165. {
  166. static public function checkAvailAnswers ($s)
  167. {
  168. return bx_is_api() ? self::checkAvail($s) : !self::_isEmptyArray($s) && count($s) >= 2;
  169. }
  170. }
  171. /** @} */