BxDolStudioForm.php 7.2 KB

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