BxDolStudioForm.php 7.2 KB

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