1
0

BxDolStudioForm.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 UnaStudio UNA Studio
  7. * @{
  8. */
  9. define('BX_DOL_STUDIO_METHOD_DEFAULT', 'post');
  10. class BxDolStudioForm extends BxBaseFormView
  11. {
  12. function __construct($aInfo, $oTemplate)
  13. {
  14. parent::__construct($aInfo, $oTemplate !== false ? $oTemplate : BxDolStudioTemplate::getInstance());
  15. $this->_sCheckerHelper = isset($this->aParams['checker_helper']) ? $this->aParams['checker_helper'] : 'BxDolStudioFormCheckerHelper';
  16. }
  17. function initChecker($aValues = array (), $aSpecificValues = array())
  18. {
  19. parent::initChecker($aValues, $aSpecificValues);
  20. if($this->isSubmitted() && !$this->_isValid)
  21. $this->processTranslationsValue();
  22. }
  23. function insert($aValsToAdd = array(), $isIgnore = false)
  24. {
  25. $sAction = 'insert';
  26. $this->processTranslationsKey($sAction);
  27. $mixedResult = parent::insert($aValsToAdd, $isIgnore);
  28. if($mixedResult !== false)
  29. $this->processTranslations($sAction);
  30. return $mixedResult;
  31. }
  32. function update($val, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  33. {
  34. $sAction = 'update';
  35. $this->processTranslationsKey($sAction);
  36. $mixedResult = parent::update($val, $aValsToAdd, $aTrackTextFieldsChanges);
  37. if($mixedResult === false)
  38. return $mixedResult;
  39. $mixedResult += (int)$this->processTranslations($sAction);
  40. return $mixedResult;
  41. }
  42. function updateWithVisibility($iId)
  43. {
  44. $iVisibleFor = BxDolStudioUtils::getVisibilityValue($this->getCleanValue('visible_for'), $this->getCleanValue('visible_for_levels'));
  45. BxDolForm::setSubmittedValue('visible_for_levels', $iVisibleFor, $this->aFormAttrs['method']);
  46. unset($this->aInputs['visible_for']);
  47. return $this->update($iId);
  48. }
  49. function processImageUploaderSave($sName, $iId = 0)
  50. {
  51. if($this->aInputs[$sName]['type'] != 'image_uploader')
  52. return $iId;
  53. $aInput = $this->aInputs[$sName];
  54. if(!empty($_FILES[$sName]['tmp_name'])) {
  55. $iProfileId = getLoggedId();
  56. $sStorage = isset($aInput['storage_object']) && $aInput['storage_object'] != '' ? $aInput['storage_object'] : BX_DOL_STORAGE_OBJ_IMAGES;
  57. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  58. if((int)$iId != 0 && !$oStorage->deleteFile($iId))
  59. return _t('_adm_err_form_view_iu_delete');
  60. $iId = $oStorage->storeFileFromForm($_FILES[$aInput['name']], false, $iProfileId);
  61. if($iId === false)
  62. return _t('_adm_err_form_view_iu_save') . $oStorage->getErrorString();
  63. $oStorage->afterUploadCleanup($iId, $iProfileId);
  64. }
  65. return (int)$iId;
  66. }
  67. function getCleanValue ($sName)
  68. {
  69. $aResult = parent::getCleanValue($sName);
  70. $a = isset($this->aInputs[$sName]) ? $this->aInputs[$sName] : false;
  71. if($a && !empty($a['reverse']) && !empty($a['values']))
  72. $aResult = array_diff(array_keys($a['values']), (is_array($aResult) ? $aResult : array()));
  73. return $aResult;
  74. }
  75. protected function processTranslations($sType = 'insert')
  76. {
  77. $aType2Method = array(
  78. 'insert' => 'addLanguageString',
  79. 'update' => 'updateLanguageString'
  80. );
  81. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  82. $aLanguages = $oLanguage->getLanguagesInfo();
  83. $bResult = false;
  84. foreach($this->aInputs as $sName => $aInput) {
  85. if(!in_array($aInput['type'], array('text_translatable', 'textarea_translatable')))
  86. continue;
  87. $sKey = $this->getCleanValue($sName);
  88. foreach($aLanguages as $aLanguage) {
  89. $sString = BxDolForm::getSubmittedValue($sName . '-' . $aLanguage['name'], $this->aFormAttrs['method']);
  90. if($sString === false)
  91. continue;
  92. if($oLanguage->{$aType2Method[$sType]}($sKey, $sString, $aLanguage['id']))
  93. $bResult = true;
  94. }
  95. }
  96. return $bResult;
  97. }
  98. protected function processTranslationsKey($sType = 'insert')
  99. {
  100. $sLanguage = BxDolStudioLanguagesUtils::getInstance()->getCurrentLangName(false);
  101. foreach($this->aInputs as $sName => $aInput) {
  102. if(!in_array($aInput['type'], array('text_translatable', 'textarea_translatable')))
  103. continue;
  104. $sKey = $this->getTranslationsKey($sType, $sName, $this->getCleanValue($sName));
  105. BxDolForm::setSubmittedValue($sName, $sKey, $this->aFormAttrs['method']);
  106. }
  107. }
  108. protected function processTranslationsValue ()
  109. {
  110. $aLanguages = BxDolStudioLanguagesUtils::getInstance()->getLanguages();
  111. foreach($this->aInputs as $sName => $aInput) {
  112. if(!in_array($aInput['type'], array('text_translatable', 'textarea_translatable')))
  113. continue;
  114. foreach($aLanguages as $sLangName => $sLangTitle)
  115. $this->aInputs[$sName]['values'][$sLangName] = BxDolForm::getSubmittedValue($sName . '-' . $sLangName, $this->aFormAttrs['method']);
  116. }
  117. }
  118. protected function getTranslationsKey($sType, $sName, $sValue)
  119. {
  120. $iRand = time();
  121. $sPrefixDefault = "_sys_form_input";
  122. $sName = BxDolStudioUtils::getSystemName(trim($sName));
  123. $sResult = '';
  124. switch($sType) {
  125. case 'insert':
  126. $sValue = !empty($sValue) ? BxDolStudioUtils::getSystemName($sValue) : $sPrefixDefault;
  127. $sResult = $sValue . '_' . $sName . '_' . $iRand;
  128. break;
  129. case 'update':
  130. $sResult = !empty($sValue) ? $sValue : $sPrefixDefault . '_' . $sName . '_' . $iRand;
  131. break;
  132. }
  133. return $sResult;
  134. }
  135. }
  136. class BxDolStudioFormCheckerHelper extends BxDolFormCheckerHelper
  137. {
  138. function checkAvailTranslatable($sVal, $aName, $sMethod = BX_DOL_STUDIO_METHOD_DEFAULT, $bAll = true)
  139. {
  140. if(empty($sMethod) || empty($aName))
  141. return false;
  142. $aLanguages = BxDolStudioLanguagesUtils::getInstance()->getLanguages();
  143. foreach($aLanguages as $sLangName => $sLangTitle) {
  144. $sValue = BxDolForm::getSubmittedValue($aName . '-' . $sLangName, $sMethod);
  145. $bValue = parent::checkAvail($sValue);
  146. if($bAll && !$bValue)
  147. return false;
  148. if(!$bAll && $bValue)
  149. return true;
  150. }
  151. return $bAll ? true : false;
  152. }
  153. function checkLengthTranslatable($sVal, $iLenMin, $iLenMax, $aName, $sMethod = BX_DOL_STUDIO_METHOD_DEFAULT, $bAll = true)
  154. {
  155. if(empty($sMethod) || empty($aName))
  156. return false;
  157. $aLanguages = BxDolStudioLanguagesUtils::getInstance()->getLanguages();
  158. foreach($aLanguages as $sLangName => $sLangTitle) {
  159. $sValue = BxDolForm::getSubmittedValue($aName . '-' . $sLangName, $sMethod);
  160. $bValue = parent::checkLength($sValue, $iLenMin, $iLenMax);
  161. if($bAll && !$bValue)
  162. return false;
  163. if(!$bAll && $bValue)
  164. return true;
  165. }
  166. return $bAll ? true : false;
  167. }
  168. function checkTemplate($sVal)
  169. {
  170. return strlen($sVal) > 0 && BxDolModuleQuery::getInstance()->isEnabled($sVal);
  171. }
  172. }
  173. /** @} */