BxPollsFormEntry.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. /**
  12. * Create/Edit entry form
  13. */
  14. class BxPollsFormEntry extends BxBaseModTextFormEntry
  15. {
  16. public function __construct($aInfo, $oTemplate = false)
  17. {
  18. $this->MODULE = 'bx_polls';
  19. parent::__construct($aInfo, $oTemplate);
  20. }
  21. public function getCode($bDynamicMode = false)
  22. {
  23. $this->_oModule->_oTemplate->addJs(array('form.js'));
  24. return $this->_oModule->_oTemplate->getJsCode('form') . parent::getCode($bDynamicMode);
  25. }
  26. function initChecker ($aValues = array (), $aSpecificValues = array())
  27. {
  28. $CNF = &$this->_oModule->_oConfig->CNF;
  29. if(isset($this->aInputs[$CNF['FIELD_SUBENTRIES']]) && !empty($aValues['id'])) {
  30. $aSubentries = $this->_oModule->_oDb->getSubentries(array(
  31. 'type' => 'entry_id_pairs',
  32. 'entry_id' => $aValues['id']
  33. ));
  34. $this->aInputs[$CNF['FIELD_SUBENTRIES']]['value'] = array_values($aSubentries);
  35. $this->aInputs[$CNF['FIELD_SUBENTRIES']]['value_ids'] = array_keys($aSubentries);
  36. }
  37. return parent::initChecker($aValues, $aSpecificValues);
  38. }
  39. public function insert ($aValsToAdd = array(), $isIgnore = false)
  40. {
  41. $CNF = &$this->_oModule->_oConfig->CNF;
  42. $iContentId = parent::insert($aValsToAdd, $isIgnore);
  43. if(!empty($iContentId))
  44. $this->processSubentriesAdd($CNF['FIELD_SUBENTRIES'], $iContentId);
  45. return $iContentId;
  46. }
  47. public function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  48. {
  49. $CNF = &$this->_oModule->_oConfig->CNF;
  50. $iResult = parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  51. $this->processSubentriesUpdate($CNF['FIELD_SUBENTRIES'], $iContentId);
  52. return $iResult;
  53. }
  54. public function processSubentriesAdd ($sField, $iContentId = 0)
  55. {
  56. $CNF = &$this->_oModule->_oConfig->CNF;
  57. if(!isset($this->aInputs[$sField]))
  58. return true;
  59. $aSubentries = $this->getCleanValue($sField);
  60. if(empty($aSubentries) || !is_array($aSubentries))
  61. return true;
  62. foreach($aSubentries as $iIndex => $sSubentry)
  63. if(!empty($sSubentry))
  64. $this->_oModule->_oDb->insertSubentry(array(
  65. 'entry_id' => $iContentId,
  66. 'title' => bx_process_input($sSubentry),
  67. 'order' => $iIndex
  68. ));
  69. return true;
  70. }
  71. public function processSubentriesUpdate($sField, $iContentId = 0)
  72. {
  73. $CNF = &$this->_oModule->_oConfig->CNF;
  74. if(!isset($this->aInputs[$sField]))
  75. return true;
  76. //--- Update existed and remove empty
  77. $aSubentriesIds = $this->getCleanValue($sField . '_ids');
  78. $aSubentriesValues = $this->getCleanValue($sField);
  79. foreach($aSubentriesIds as $iIndex => $iId)
  80. if(!empty($aSubentriesValues[$iIndex]))
  81. $this->_oModule->_oDb->updateSubentry(array('title' => bx_process_input($aSubentriesValues[$iIndex])), array('id' => (int)$iId));
  82. else
  83. $this->_oModule->_oDb->deleteSubentry(array('id' => (int)$iId));
  84. //--- Add new
  85. $iSubentriesIds = count($aSubentriesIds);
  86. $iSubentriesValues = count($aSubentriesValues);
  87. if($iSubentriesValues > $iSubentriesIds) {
  88. $iMaxOrder = (int)$this->_oModule->_oDb->getSubentries(array('type' => 'entry_id_max_order', 'entry_id' => $iContentId));
  89. $aSubentriesValues = array_slice($aSubentriesValues, $iSubentriesIds);
  90. foreach($aSubentriesValues as $sSubentriesValue)
  91. if(!empty($sSubentriesValue))
  92. $this->_oModule->_oDb->insertSubentry(array(
  93. 'entry_id' => $iContentId,
  94. 'title' => bx_process_input($sSubentriesValue),
  95. 'order' => ++$iMaxOrder
  96. ));
  97. }
  98. return true;
  99. }
  100. protected function genCustomInputSubentries(&$aInput)
  101. {
  102. $sResult = '';
  103. if(empty($aInput['value']) || !is_array($aInput['value'])) {
  104. $sResult .= $this->genCustomInputSubentriesText($aInput);
  105. $sResult .= $this->genCustomInputSubentriesText($aInput);
  106. }
  107. else
  108. foreach($aInput['value'] as $iKey => $sValue) {
  109. $sResult .= $this->genCustomInputSubentriesText($aInput, $sValue);
  110. if(!empty($aInput['value_ids'][$iKey]))
  111. $sResult .= $this->genCustomInputSubentriesHidden($aInput, (int)$aInput['value_ids'][$iKey]);
  112. }
  113. $sResult .= $this->genCustomInputSubentriesButton($aInput);
  114. return $sResult;
  115. }
  116. protected function genCustomInputSubentriesText($aInput, $mixedValue = '')
  117. {
  118. $aInput['type'] = 'text';
  119. $aInput['name'] .= '[]';
  120. $aInput['value'] = $mixedValue;
  121. $aInput['attrs']['class'] = 'bx-def-margin-sec-top-auto';
  122. return $this->genInput($aInput);
  123. }
  124. protected function genCustomInputSubentriesHidden($aInput, $mixedValue = '')
  125. {
  126. $aInput['type'] = 'hidden';
  127. $aInput['name'] .= '_ids[]';
  128. $aInput['value'] = $mixedValue;
  129. return $this->genInput($aInput);
  130. }
  131. protected function genCustomInputSubentriesButton($aInput)
  132. {
  133. $aInput['type'] = 'button';
  134. $aInput['name'] .= '_add';
  135. $aInput['value'] = _t('_bx_polls_form_entry_input_subentries_add');
  136. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  137. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('form') . '.addMore(this);';
  138. return $this->genInputButton($aInput);
  139. }
  140. }
  141. /** @} */