BxDolStudioForm.php 7.3 KB

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