1
0

BxBaseModGeneralFormPoll.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 processAnswersAdd ($sField, $iContentId = 0)
  52. {
  53. $CNF = &$this->_oModule->_oConfig->CNF;
  54. if(!isset($this->aInputs[$sField]))
  55. return true;
  56. $aAnswers = $this->getCleanValue($sField);
  57. if(empty($aAnswers) || !is_array($aAnswers))
  58. return true;
  59. foreach($aAnswers as $iIndex => $sAnswer)
  60. if($sAnswer != '' && get_mb_len($sAnswer) > 0)
  61. $this->_oModule->_oDb->insertPollAnswer(array(
  62. 'poll_id' => $iContentId,
  63. 'title' => bx_process_input($sAnswer),
  64. 'order' => $iIndex
  65. ));
  66. return true;
  67. }
  68. public function processAnswersUpdate($sField, $iContentId = 0)
  69. {
  70. $CNF = &$this->_oModule->_oConfig->CNF;
  71. if(!isset($this->aInputs[$sField]))
  72. return true;
  73. $aAnswersIds = $this->getCleanValue($sField . '_ids');
  74. $aAnswersValues = $this->getCleanValue($sField);
  75. //--- Remove deleted
  76. $aAnswersDb = $this->_oModule->_oDb->getPollAnswers(array('type' => 'poll_id_pairs', 'poll_id' => $iContentId));
  77. $this->_oModule->_oDb->deletePollAnswersByIds(array_diff(array_keys($aAnswersDb), $aAnswersIds));
  78. //--- Update existed and remove empty
  79. foreach($aAnswersIds as $iIndex => $iId)
  80. if($aAnswersValues[$iIndex] != '' && get_mb_len($aAnswersValues[$iIndex]) > 0)
  81. $this->_oModule->_oDb->updatePollAnswers(array('title' => bx_process_input($aAnswersValues[$iIndex])), array('id' => (int)$iId));
  82. else
  83. $this->_oModule->_oDb->deletePollAnswers(array('id' => (int)$iId));
  84. //--- Add new
  85. $iAnswersIds = count($aAnswersIds);
  86. $iAnswersValues = count($aAnswersValues);
  87. if($iAnswersValues > $iAnswersIds) {
  88. $iMaxOrder = (int)$this->_oModule->_oDb->getPollAnswers(array('type' => 'poll_id_max_order', 'poll_id' => $iContentId));
  89. $aAnswersValues = array_slice($aAnswersValues, $iAnswersIds);
  90. foreach($aAnswersValues as $sAnswersValue)
  91. if($sAnswersValue != '' && get_mb_len($sAnswersValue) > 0)
  92. $this->_oModule->_oDb->insertPollAnswer(array(
  93. 'poll_id' => $iContentId,
  94. 'title' => bx_process_input($sAnswersValue),
  95. 'order' => ++$iMaxOrder
  96. ));
  97. }
  98. return true;
  99. }
  100. protected function genCustomInputAnswers(&$aInput)
  101. {
  102. $sJsObject = $this->_oModule->_oConfig->getJsObject('poll');
  103. $aTmplVarsAnswers = array(
  104. array('class' => 'bx-fi-answer-blank', 'js_object' => $sJsObject, 'input_text' => $this->genCustomInputAnswersText($aInput, '', true)),
  105. );
  106. if(!empty($aInput['value']) && is_array($aInput['value']))
  107. foreach($aInput['value'] as $iKey => $sValue) {
  108. $sInput = $this->genCustomInputAnswersText($aInput, $sValue);
  109. if(!empty($aInput['value_ids'][$iKey]))
  110. $sInput .= $this->genCustomInputAnswersHidden($aInput, (int)$aInput['value_ids'][$iKey]);
  111. $aTmplVarsAnswers[] = array('class' => '', 'js_object' => $sJsObject, 'input_text' => $sInput);
  112. }
  113. else
  114. $aTmplVarsAnswers = array_merge($aTmplVarsAnswers, array(
  115. array('class' => '', 'js_object' => $sJsObject, 'input_text' => $this->genCustomInputAnswersText($aInput)),
  116. array('class' => '', 'js_object' => $sJsObject, 'input_text' => $this->genCustomInputAnswersText($aInput))
  117. ));
  118. return $this->_oModule->_oTemplate->parseHtmlByName('poll_form_answers.html', array(
  119. 'bx_repeat:answers' => $aTmplVarsAnswers,
  120. 'btn_add' => $this->genCustomInputAnswersButton($aInput)
  121. ));
  122. }
  123. protected function genCustomInputAnswersText($aInput, $mixedValue = '', $bDisabled = false)
  124. {
  125. $aInput['type'] = 'text';
  126. $aInput['name'] .= '[]';
  127. $aInput['value'] = $mixedValue;
  128. $aInput['attrs']['class'] = 'bx-def-margin-sec-top-auto';
  129. if($bDisabled)
  130. $aInput['attrs']['disabled'] = 'disabled';
  131. return $this->genInput($aInput);
  132. }
  133. protected function genCustomInputAnswersHidden($aInput, $mixedValue = '')
  134. {
  135. $aInput['type'] = 'hidden';
  136. $aInput['name'] .= '_ids[]';
  137. $aInput['value'] = $mixedValue;
  138. return $this->genInput($aInput);
  139. }
  140. protected function genCustomInputAnswersButton($aInput)
  141. {
  142. $CNF = &$this->_oModule->_oConfig->CNF;
  143. $sName = $aInput['name'];
  144. $aInput['type'] = 'button';
  145. $aInput['name'] .= '_add';
  146. $aInput['value'] = _t($CNF['T']['txt_poll_form_answers_add']);
  147. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  148. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('poll') . ".addPollAnswer(this, '" . $sName . "');";
  149. return $this->genInputButton($aInput);
  150. }
  151. }
  152. class BxBaseModGeneralFormPollCheckerHelper extends BxDolFormCheckerHelper
  153. {
  154. static public function checkAvailAnswers ($s)
  155. {
  156. return !self::_isEmptyArray($s) && count($s) >= 2;
  157. }
  158. }
  159. /** @} */