BxBaseModGeneralFormPoll.php 8.1 KB

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