BxDolStudioForm.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, $iValue = 0) {
  40. $iId = 0;
  41. if($this->aInputs[$sName]['type'] != 'image_uploader')
  42. return $iId;
  43. $aInput = $this->aInputs[$sName];
  44. if(!empty($_FILES[$sName]['tmp_name'])) {
  45. $iProfileId = getLoggedId();
  46. bx_import('BxDolStorage');
  47. $sStorage = isset($aInput['storage_object']) && $aInput['storage_object'] != '' ? $aInput['storage_object'] : BX_DOL_STORAGE_OBJ_IMAGES;
  48. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  49. if((int)$iValue != 0 && !$oStorage->deleteFile($iValue))
  50. return _t('_adm_err_form_view_iu_delete');
  51. $iId = $oStorage->storeFileFromForm($_FILES[$aInput['name']], false, $iProfileId);
  52. if($iId === false)
  53. return _t('_adm_err_form_view_iu_save') . $oStorage->getErrorString();
  54. $oStorage->afterUploadCleanup($iId, $iProfileId);
  55. }
  56. return (int)$iId;
  57. }
  58. protected function processTranslations($sType = 'insert') {
  59. $aType2Method = array(
  60. 'insert' => 'addLanguageString',
  61. 'update' => 'updateLanguageString'
  62. );
  63. bx_import('BxDolStudioLanguagesUtils');
  64. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  65. $aLanguages = $oLanguage->getLanguagesInfo();
  66. foreach($this->aInputs as $sName => $aInput)
  67. if(in_array($aInput['type'], array('text_translatable', 'textarea_translatable'))) {
  68. $sKey = $this->getCleanValue($sName);
  69. foreach($aLanguages as $aLanguage) {
  70. $sString = BxDolForm::getSubmittedValue($sName . '-' . $aLanguage['name'], $this->aFormAttrs['method']);
  71. if($sString !== false)
  72. $oLanguage->$aType2Method[$sType]($sKey, $sString, $aLanguage['id']);
  73. }
  74. }
  75. }
  76. protected function processTranslationsKey($sType = 'insert') {
  77. bx_import('BxDolStudioLanguagesUtils');
  78. $sLanguage = BxDolStudioLanguagesUtils::getInstance()->getCurrentLangName(false);
  79. foreach($this->aInputs as $sName => $aInput)
  80. if(in_array($aInput['type'], array('text_translatable', 'textarea_translatable'))) {
  81. $sKey = $this->getTranslationsKey($sType, $sName, $this->getCleanValue($sName));
  82. BxDolForm::setSubmittedValue($sName, $sKey, $this->aFormAttrs['method']);
  83. }
  84. }
  85. protected function processTranslationsValue () {
  86. bx_import('BxDolStudioLanguagesUtils');
  87. $aLanguages = BxDolStudioLanguagesUtils::getInstance()->getLanguages();
  88. foreach($this->aInputs as $sName => $aInput)
  89. if(in_array($aInput['type'], array('text_translatable', 'textarea_translatable')))
  90. foreach($aLanguages as $sLangName => $sLangTitle)
  91. $this->aInputs[$sName]['values'][$sLangName] = BxDolForm::getSubmittedValue($sName . '-' . $sLangName, $this->aFormAttrs['method']);
  92. }
  93. protected function getTranslationsKey($sType, $sName, $sValue) {
  94. $iRand = time();
  95. $sPrefixDefault = "_sys_form_input";
  96. $sName = BxDolStudioUtils::getSystemName(trim($sName));
  97. $sResult = '';
  98. switch($sType) {
  99. case 'insert':
  100. bx_import('BxDolStudioUtils');
  101. $sValue = !empty($sValue) ? BxDolStudioUtils::getSystemName($sValue) : $sPrefixDefault;
  102. $sResult = $sValue . '_' . $sName . '_' . $iRand;
  103. break;
  104. case 'update':
  105. $sResult = !empty($sValue) ? $sValue : $sPrefixDefault . '_' . $sName . '_' . $iRand;
  106. break;
  107. }
  108. return $sResult;
  109. }
  110. }
  111. class BxDolStudioCheckerHelper extends BxDolFormCheckerHelper {
  112. function checkAvailTranslatable($sVal, $aName, $sMethod = BX_DOL_STUDIO_METHOD_DEFAULT, $bAll = true) {
  113. if(empty($sMethod) || empty($aName))
  114. return false;
  115. bx_import('BxDolStudioLanguagesUtils');
  116. $aLanguages = BxDolStudioLanguagesUtils::getInstance()->getLanguages();
  117. foreach($aLanguages as $sLangName => $sLangTitle) {
  118. $sValue = BxDolForm::getSubmittedValue($aName . '-' . $sLangName, $sMethod);
  119. $bValue = parent::checkAvail($sValue);
  120. if($bAll && !$bValue)
  121. return false;
  122. if(!$bAll && $bValue)
  123. return true;
  124. }
  125. return $bAll ? true : false;
  126. }
  127. function checkLengthTranslatable($sVal, $iLenMin, $iLenMax, $aName, $sMethod = BX_DOL_STUDIO_METHOD_DEFAULT, $bAll = true) {
  128. if(empty($sMethod) || empty($aName))
  129. return false;
  130. bx_import('BxDolStudioLanguagesUtils');
  131. $aLanguages = BxDolStudioLanguagesUtils::getInstance()->getLanguages();
  132. foreach($aLanguages as $sLangName => $sLangTitle) {
  133. $sValue = BxDolForm::getSubmittedValue($aName . '-' . $sLangName, $sMethod);
  134. $bValue = parent::checkLength($sValue, $iLenMin, $iLenMax);
  135. if($bAll && !$bValue)
  136. return false;
  137. if(!$bAll && $bValue)
  138. return true;
  139. }
  140. return $bAll ? true : false;
  141. }
  142. }
  143. /** @} */