BxDolStudioForm.php 7.9 KB

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