BxDolStudioForm.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. function getCleanValue ($sName)
  70. {
  71. $aResult = parent::getCleanValue($sName);
  72. $a = isset($this->aInputs[$sName]) ? $this->aInputs[$sName] : false;
  73. if($a && !empty($a['reverse']) && !empty($a['values']))
  74. $aResult = array_diff(array_keys($a['values']), (is_array($aResult) ? $aResult : array()));
  75. return $aResult;
  76. }
  77. protected function processTranslations($sType = 'insert')
  78. {
  79. $aType2Method = array(
  80. 'insert' => 'addLanguageString',
  81. 'update' => 'updateLanguageString'
  82. );
  83. bx_import('BxDolStudioLanguagesUtils');
  84. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  85. $aLanguages = $oLanguage->getLanguagesInfo();
  86. foreach($this->aInputs as $sName => $aInput)
  87. if(in_array($aInput['type'], array('text_translatable', 'textarea_translatable'))) {
  88. $sKey = $this->getCleanValue($sName);
  89. foreach($aLanguages as $aLanguage) {
  90. $sString = BxDolForm::getSubmittedValue($sName . '-' . $aLanguage['name'], $this->aFormAttrs['method']);
  91. if($sString !== false)
  92. $oLanguage->$aType2Method[$sType]($sKey, $sString, $aLanguage['id']);
  93. }
  94. }
  95. }
  96. protected function processTranslationsKey($sType = 'insert')
  97. {
  98. bx_import('BxDolStudioLanguagesUtils');
  99. $sLanguage = BxDolStudioLanguagesUtils::getInstance()->getCurrentLangName(false);
  100. foreach($this->aInputs as $sName => $aInput)
  101. if(in_array($aInput['type'], array('text_translatable', 'textarea_translatable'))) {
  102. $sKey = $this->getTranslationsKey($sType, $sName, $this->getCleanValue($sName));
  103. BxDolForm::setSubmittedValue($sName, $sKey, $this->aFormAttrs['method']);
  104. }
  105. }
  106. protected function processTranslationsValue ()
  107. {
  108. bx_import('BxDolStudioLanguagesUtils');
  109. $aLanguages = BxDolStudioLanguagesUtils::getInstance()->getLanguages();
  110. foreach($this->aInputs as $sName => $aInput)
  111. if(in_array($aInput['type'], array('text_translatable', 'textarea_translatable')))
  112. foreach($aLanguages as $sLangName => $sLangTitle)
  113. $this->aInputs[$sName]['values'][$sLangName] = BxDolForm::getSubmittedValue($sName . '-' . $sLangName, $this->aFormAttrs['method']);
  114. }
  115. protected function getTranslationsKey($sType, $sName, $sValue)
  116. {
  117. $iRand = time();
  118. $sPrefixDefault = "_sys_form_input";
  119. $sName = BxDolStudioUtils::getSystemName(trim($sName));
  120. $sResult = '';
  121. switch($sType) {
  122. case 'insert':
  123. bx_import('BxDolStudioUtils');
  124. $sValue = !empty($sValue) ? BxDolStudioUtils::getSystemName($sValue) : $sPrefixDefault;
  125. $sResult = $sValue . '_' . $sName . '_' . $iRand;
  126. break;
  127. case 'update':
  128. $sResult = !empty($sValue) ? $sValue : $sPrefixDefault . '_' . $sName . '_' . $iRand;
  129. break;
  130. }
  131. return $sResult;
  132. }
  133. }
  134. class BxDolStudioFormCheckerHelper extends BxDolFormCheckerHelper
  135. {
  136. function checkAvailTranslatable($sVal, $aName, $sMethod = BX_DOL_STUDIO_METHOD_DEFAULT, $bAll = true)
  137. {
  138. if(empty($sMethod) || empty($aName))
  139. return false;
  140. bx_import('BxDolStudioLanguagesUtils');
  141. $aLanguages = BxDolStudioLanguagesUtils::getInstance()->getLanguages();
  142. foreach($aLanguages as $sLangName => $sLangTitle) {
  143. $sValue = BxDolForm::getSubmittedValue($aName . '-' . $sLangName, $sMethod);
  144. $bValue = parent::checkAvail($sValue);
  145. if($bAll && !$bValue)
  146. return false;
  147. if(!$bAll && $bValue)
  148. return true;
  149. }
  150. return $bAll ? true : false;
  151. }
  152. function checkLengthTranslatable($sVal, $iLenMin, $iLenMax, $aName, $sMethod = BX_DOL_STUDIO_METHOD_DEFAULT, $bAll = true)
  153. {
  154. if(empty($sMethod) || empty($aName))
  155. return false;
  156. bx_import('BxDolStudioLanguagesUtils');
  157. $aLanguages = BxDolStudioLanguagesUtils::getInstance()->getLanguages();
  158. foreach($aLanguages as $sLangName => $sLangTitle) {
  159. $sValue = BxDolForm::getSubmittedValue($aName . '-' . $sLangName, $sMethod);
  160. $bValue = parent::checkLength($sValue, $iLenMin, $iLenMax);
  161. if($bAll && !$bValue)
  162. return false;
  163. if(!$bAll && $bValue)
  164. return true;
  165. }
  166. return $bAll ? true : false;
  167. }
  168. function checkTemplate($sVal)
  169. {
  170. bx_import('BxDolModuleQuery');
  171. return strlen($sVal) > 0 && BxDolModuleQuery::getInstance()->isEnabled($sVal);
  172. }
  173. }
  174. /** @} */