BxPollsFormEntry.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 Polls Polls
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxPollsFormEntryCheckerHelper extends BxDolFormCheckerHelper
  12. {
  13. static public function checkAvailSubentries ($s)
  14. {
  15. if(bx_is_api() && is_string($s))
  16. $s = explode(',', $s);
  17. return !self::_isEmptyArray($s) && count($s) >= 2;
  18. }
  19. }
  20. /**
  21. * Create/Edit entry form
  22. */
  23. class BxPollsFormEntry extends BxBaseModTextFormEntry
  24. {
  25. public function __construct($aInfo, $oTemplate = false)
  26. {
  27. $this->MODULE = 'bx_polls';
  28. $this->_aFieldsCheckForSpam = array('subentries');
  29. parent::__construct($aInfo, $oTemplate);
  30. }
  31. public function getCode($bDynamicMode = false)
  32. {
  33. $sJs = $this->_oModule->_oTemplate->addJs(array('form.js'), $bDynamicMode);
  34. $sCode = '';
  35. if($bDynamicMode)
  36. $sCode .= $sJs;
  37. $sCode .= $this->_oModule->_oTemplate->getJsCode('form');
  38. $sCode .= parent::getCode($bDynamicMode);
  39. return $sCode;
  40. }
  41. function initChecker ($aValues = array (), $aSpecificValues = array())
  42. {
  43. $CNF = &$this->_oModule->_oConfig->CNF;
  44. if(isset($this->aInputs[$CNF['FIELD_SUBENTRIES']]) && !empty($aValues['id'])) {
  45. $aSubentries = $this->_oModule->_oDb->getSubentries(array(
  46. 'type' => 'entry_id_pairs',
  47. 'entry_id' => $aValues['id']
  48. ));
  49. $this->aInputs[$CNF['FIELD_SUBENTRIES']]['value'] = array_values($aSubentries);
  50. $this->aInputs[$CNF['FIELD_SUBENTRIES']]['value_ids'] = array_keys($aSubentries);
  51. }
  52. return parent::initChecker($aValues, $aSpecificValues);
  53. }
  54. public function insert ($aValsToAdd = array(), $isIgnore = false)
  55. {
  56. $CNF = &$this->_oModule->_oConfig->CNF;
  57. $iContentId = parent::insert($aValsToAdd, $isIgnore);
  58. if(!empty($iContentId))
  59. $this->processSubentriesAdd($CNF['FIELD_SUBENTRIES'], $iContentId);
  60. return $iContentId;
  61. }
  62. public function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  63. {
  64. $CNF = &$this->_oModule->_oConfig->CNF;
  65. $iResult = parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  66. $this->processSubentriesUpdate($CNF['FIELD_SUBENTRIES'], $iContentId);
  67. return $iResult;
  68. }
  69. public function getCleanValue ($sName)
  70. {
  71. $mixedValue = parent::getCleanValue($sName);
  72. if($this->_bIsApi && !empty($mixedValue) && in_array($sName, ['subentries', 'subentries_ids']))
  73. $mixedValue = explode(',', $mixedValue);
  74. return $mixedValue;
  75. }
  76. public function processSubentriesAdd ($sField, $iContentId = 0)
  77. {
  78. $CNF = &$this->_oModule->_oConfig->CNF;
  79. if(!isset($this->aInputs[$sField]))
  80. return true;
  81. $aSubentries = $this->getCleanValue($sField);
  82. if(empty($aSubentries) || !is_array($aSubentries))
  83. return true;
  84. foreach($aSubentries as $iIndex => $sSubentry)
  85. if($sSubentry != '' && get_mb_len($sSubentry) > 0)
  86. $this->_oModule->_oDb->insertSubentry(array(
  87. 'entry_id' => $iContentId,
  88. 'title' => bx_process_input($sSubentry),
  89. 'order' => $iIndex
  90. ));
  91. return true;
  92. }
  93. public function processSubentriesUpdate($sField, $iContentId = 0)
  94. {
  95. $CNF = &$this->_oModule->_oConfig->CNF;
  96. if(!isset($this->aInputs[$sField]))
  97. return true;
  98. $aSubentriesIds = $this->getCleanValue($sField . '_ids');
  99. $aSubentriesValues = $this->getCleanValue($sField);
  100. //--- Remove deleted
  101. $aSubentriesDb = $this->_oModule->_oDb->getSubentries(array('type' => 'entry_id_pairs', 'entry_id' => $iContentId));
  102. $this->_oModule->_oDb->deleteSubentryById(array_diff(array_keys($aSubentriesDb), $aSubentriesIds));
  103. //--- Update existed and remove empty
  104. foreach($aSubentriesIds as $iIndex => $iId)
  105. if($aSubentriesValues[$iIndex] != '' && get_mb_len($aSubentriesValues[$iIndex]) > 0)
  106. $this->_oModule->_oDb->updateSubentry(array('title' => bx_process_input($aSubentriesValues[$iIndex])), array('id' => (int)$iId));
  107. else
  108. $this->_oModule->_oDb->deleteSubentry(array('id' => (int)$iId));
  109. //--- Add new
  110. $iSubentriesIds = count($aSubentriesIds);
  111. $iSubentriesValues = count($aSubentriesValues);
  112. if($iSubentriesValues > $iSubentriesIds) {
  113. $iMaxOrder = (int)$this->_oModule->_oDb->getSubentries(array('type' => 'entry_id_max_order', 'entry_id' => $iContentId));
  114. $aSubentriesValues = array_slice($aSubentriesValues, $iSubentriesIds);
  115. foreach($aSubentriesValues as $sSubentriesValue)
  116. if($sSubentriesValue != '' && get_mb_len($sSubentriesValue) > 0)
  117. $this->_oModule->_oDb->insertSubentry(array(
  118. 'entry_id' => $iContentId,
  119. 'title' => bx_process_input($sSubentriesValue),
  120. 'order' => ++$iMaxOrder
  121. ));
  122. }
  123. return true;
  124. }
  125. protected function genCustomInputSubentries(&$aInput)
  126. {
  127. if($this->_bIsApi)
  128. return array_merge($aInput, [
  129. 'type' => 'multi_field',
  130. 'subtype' => 'text'
  131. ]);
  132. $sJsObject = $this->_oModule->_oConfig->getJsObject('form');
  133. if(!empty($aInput['value']) && is_array($aInput['value'])) {
  134. $aTmplVarsSubentries = array();
  135. foreach($aInput['value'] as $iKey => $sValue) {
  136. $sInput = $this->genCustomInputSubentriesText($aInput, $sValue);
  137. if(!empty($aInput['value_ids'][$iKey]))
  138. $sInput .= $this->genCustomInputSubentriesHidden($aInput, (int)$aInput['value_ids'][$iKey]);
  139. $aTmplVarsSubentries[] = array('js_object' => $sJsObject, 'input_text' => $sInput);
  140. }
  141. }
  142. else
  143. $aTmplVarsSubentries = array(
  144. array('js_object' => $sJsObject, 'input_text' => $this->genCustomInputSubentriesText($aInput)),
  145. array('js_object' => $sJsObject, 'input_text' => $this->genCustomInputSubentriesText($aInput))
  146. );
  147. return $this->_oModule->_oTemplate->parseHtmlByName('form_subentries.html', array(
  148. 'bx_repeat:subentries' => $aTmplVarsSubentries,
  149. 'btn_add' => $this->genCustomInputSubentriesButton($aInput)
  150. ));
  151. }
  152. protected function genCustomInputSubentriesText($aInput, $mixedValue = '')
  153. {
  154. $aInput['type'] = 'text';
  155. $aInput['name'] .= '[]';
  156. $aInput['value'] = $mixedValue;
  157. $aInput['attrs']['class'] = 'bx-def-margin-sec-top-auto';
  158. return $this->genInput($aInput);
  159. }
  160. protected function genCustomInputSubentriesHidden($aInput, $mixedValue = '')
  161. {
  162. $aInput['type'] = 'hidden';
  163. $aInput['name'] .= '_ids[]';
  164. $aInput['value'] = $mixedValue;
  165. return $this->genInput($aInput);
  166. }
  167. protected function genCustomInputSubentriesButton($aInput)
  168. {
  169. $sName = $aInput['name'];
  170. $aInput['type'] = 'button';
  171. $aInput['name'] .= '_add';
  172. $aInput['value'] = _t('_bx_polls_form_entry_input_subentries_add');
  173. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  174. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('form') . ".subentryAdd(this, '" . $sName . "');";
  175. return $this->genInputButton($aInput);
  176. }
  177. }
  178. /** @} */