1
0

BxDolStudioForm.php 6.7 KB

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