1
0

BxBaseModGeneralFormEntry.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup BaseGeneral Base classes for modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Create/edit entry form
  13. */
  14. class BxBaseModGeneralFormEntry extends BxTemplFormView
  15. {
  16. protected static $_isCssJsGeneralModuleAdded = false;
  17. protected $MODULE;
  18. protected $_oModule;
  19. protected $_aMetatagsFieldsWithKeywords = array();
  20. protected $_oMetatagsObject = null;
  21. protected $_oMetatagsContentId = 0;
  22. protected $_sGhostTemplate = 'form_ghost_template.html';
  23. public function __construct($aInfo, $oTemplate = false)
  24. {
  25. parent::__construct($aInfo, $oTemplate);
  26. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  27. $CNF = &$this->_oModule->_oConfig->CNF;
  28. if (isset($CNF['FIELD_ADDED']) && isset($this->aInputs[$CNF['FIELD_ADDED']])) {
  29. $this->aInputs[$CNF['FIELD_ADDED']]['date_filter'] = BX_DATA_INT;
  30. $this->aInputs[$CNF['FIELD_ADDED']]['date_format'] = BX_FORMAT_DATE;
  31. }
  32. if (isset($CNF['FIELD_CHANGED']) && isset($this->aInputs[$CNF['FIELD_CHANGED']])) {
  33. $this->aInputs[$CNF['FIELD_CHANGED']]['date_filter'] = BX_DATA_INT;
  34. $this->aInputs[$CNF['FIELD_CHANGED']]['date_format'] = BX_FORMAT_DATE;
  35. }
  36. if (isset($CNF['FIELD_TEXT']) && isset($this->aInputs[$CNF['FIELD_TEXT']]) && isset($CNF['FIELD_TEXT_ID'])) {
  37. $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] = array_merge (
  38. array ('id' => $CNF['FIELD_TEXT_ID']),
  39. is_array($this->aInputs[$CNF['FIELD_TEXT']]['attrs']) ? $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] : array ()
  40. );
  41. }
  42. if (isset($CNF['FIELD_LOCATION_PREFIX']) && isset($this->aInputs[$CNF['FIELD_LOCATION_PREFIX']])) {
  43. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['manual_input'] = true;
  44. }
  45. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  46. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_object'] = $CNF['OBJECT_STORAGE'];
  47. $this->aInputs[$CNF['FIELD_PHOTO']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_PHOTO']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_PHOTO']]['value']) : $CNF['OBJECT_UPLOADERS'];
  48. $this->aInputs[$CNF['FIELD_PHOTO']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW'];
  49. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_private'] = 0;
  50. $this->aInputs[$CNF['FIELD_PHOTO']]['multiple'] = true;
  51. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = 0;
  52. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = '';
  53. }
  54. if (isset($CNF['FIELD_LABELS']) && isset($this->aInputs[$CNF['FIELD_LABELS']])) {
  55. $this->aInputs[$CNF['FIELD_LABELS']]['values'] = array_map('trim', getParam($CNF['PARAM_LABELS']) ? explode(',', getParam($CNF['PARAM_LABELS'])) : array());
  56. }
  57. if (isset($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']]) && $oPrivacy = BxDolPrivacy::getObjectInstance($CNF['OBJECT_PRIVACY_VIEW'])) {
  58. $aSave = array('db' => array('pass' => 'Xss'));
  59. array_walk($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']], function ($a, $k, $aSave) {
  60. if (in_array($k, array('info', 'caption', 'value')))
  61. $aSave[0][$k] = $a;
  62. }, array(&$aSave));
  63. $aGroupChooser = $oPrivacy->getGroupChooser($CNF['OBJECT_PRIVACY_VIEW']);
  64. $this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']] = array_merge($this->aInputs[$CNF['FIELD_ALLOW_VIEW_TO']], $aGroupChooser, $aSave);
  65. }
  66. }
  67. function initChecker ($aValues = array (), $aSpecificValues = array())
  68. {
  69. $CNF = &$this->_oModule->_oConfig->CNF;
  70. if (!empty($CNF['FIELD_LOCATION_PREFIX']) && isset($this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]) && isset($aValues[$CNF['FIELD_ID']]) && !empty($CNF['OBJECT_METATAGS']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) && $oMetatags->locationsIsEnabled())
  71. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['value'] = $oMetatags->locationsString($aValues[$CNF['FIELD_ID']], false);
  72. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  73. $aContentInfo = false;
  74. if ($aValues && !empty($aValues['id'])) {
  75. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($aValues['id']);
  76. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = $aValues['id'];
  77. }
  78. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplate, $this->_getPhotoGhostTmplVars($aContentInfo));
  79. }
  80. if (isset($CNF['FIELD_LABELS']) && isset($this->aInputs[$CNF['FIELD_LABELS']]) && !empty($aValues['id']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) && $oMetatags->keywordsIsEnabled() && ($aLabels = $oMetatags->keywordsGet($aValues['id']))) {
  81. $this->aInputs[$CNF['FIELD_LABELS']]['content_id'] = $aValues['id'];
  82. $this->aInputs[$CNF['FIELD_LABELS']]['meta_object'] = $CNF['OBJECT_METATAGS'];
  83. $this->aInputs[$CNF['FIELD_LABELS']]['value'] = array_intersect($aLabels, $this->aInputs[$CNF['FIELD_LABELS']]['values']);
  84. }
  85. if (isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]) && isset($aValues[$CNF['FIELD_AUTHOR']])) {
  86. $this->aInputs[$CNF['FIELD_ANONYMOUS']]['checked'] = $aValues[$CNF['FIELD_AUTHOR']] < 0;
  87. }
  88. parent::initChecker ($aValues, $aSpecificValues);
  89. }
  90. public function insert ($aValsToAdd = array(), $isIgnore = false)
  91. {
  92. $CNF = &$this->_oModule->_oConfig->CNF;
  93. if (isset($CNF['FIELD_AUTHOR']) && empty($aValsToAdd[$CNF['FIELD_AUTHOR']]))
  94. $aValsToAdd[$CNF['FIELD_AUTHOR']] = (isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']]) && $this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1) * bx_get_logged_profile_id ();
  95. if (isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']]) && empty($this->getCleanValue($CNF['FIELD_ADDED'])))
  96. $aValsToAdd[$CNF['FIELD_ADDED']] = time();
  97. if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  98. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  99. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb() && isset($CNF['FIELD_THUMB'])) {
  100. $aThumb = isset($_POST[$CNF['FIELD_THUMB']]) ? bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT) : false;
  101. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  102. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  103. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  104. }
  105. return parent::insert ($aValsToAdd, $isIgnore);
  106. }
  107. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  108. {
  109. $CNF = &$this->_oModule->_oConfig->CNF;
  110. if (isset($CNF['FIELD_AUTHOR']) && isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']])) {
  111. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($iContentId);
  112. $aValsToAdd[$CNF['FIELD_AUTHOR']] = ($this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1) * abs($aContentInfo[$CNF['FIELD_AUTHOR']]);
  113. }
  114. if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  115. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  116. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($iContentId) && isset($CNF['FIELD_THUMB'])) {
  117. $aThumb = bx_process_input (bx_get($CNF['FIELD_THUMB']), BX_DATA_INT);
  118. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  119. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  120. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  121. }
  122. return parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  123. }
  124. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  125. {
  126. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  127. }
  128. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  129. {
  130. $CNF = &$this->_oModule->_oConfig->CNF;
  131. return array (
  132. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  133. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  134. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  135. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  136. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  137. 'bx_if:set_thumb' => array (
  138. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($this->aInputs[$CNF['FIELD_PHOTO']]['content_id']),
  139. 'content' => array (
  140. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  141. 'txt_pict_use_as_thumb' => _t(!empty($CNF['T']['txt_pict_use_as_thumb']) ? $CNF['T']['txt_pict_use_as_thumb'] : '_sys_txt_form_entry_input_picture_use_as_thumb')
  142. ),
  143. ),
  144. );
  145. }
  146. function delete ($iContentId, $aContentInfo = array())
  147. {
  148. $CNF = &$this->_oModule->_oConfig->CNF;
  149. // delete associated files
  150. if (!empty($CNF['OBJECT_STORAGE'])) {
  151. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  152. if ($oStorage)
  153. $oStorage->queueFilesForDeletionFromGhosts($aContentInfo[$CNF['FIELD_AUTHOR']], $iContentId);
  154. }
  155. // delete associated objects data
  156. if (!empty($CNF['OBJECT_VIEWS'])) {
  157. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId);
  158. if ($o) $o->onObjectDelete();
  159. }
  160. if (!empty($CNF['OBJECT_VOTES'])) {
  161. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  162. if ($o) $o->onObjectDelete();
  163. }
  164. if (!empty($CNF['OBJECT_SCORES'])) {
  165. $o = BxDolScore::getObjectInstance($CNF['OBJECT_SCORES'], $iContentId);
  166. if ($o) $o->onObjectDelete();
  167. }
  168. if (!empty($CNF['OBJECT_COMMENTS'])) {
  169. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  170. if ($o) $o->onObjectDelete();
  171. }
  172. if (!empty($CNF['OBJECT_METATAGS'])) {
  173. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  174. $oMetatags->onDeleteContent($iContentId);
  175. }
  176. // delete db record
  177. return parent::delete($iContentId);
  178. }
  179. protected function _isAdmin ($iContentId = 0)
  180. {
  181. return $this->_oModule->_isModerator();
  182. }
  183. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  184. {
  185. $CNF = &$this->_oModule->_oConfig->CNF;
  186. if (!isset($this->aInputs[$sFieldFile]))
  187. return true;
  188. $mixedFileIds = $this->getCleanValue($sFieldFile);
  189. if(!$mixedFileIds)
  190. return true;
  191. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  192. if (!$oStorage)
  193. return false;
  194. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  195. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  196. if (!$aGhostFiles)
  197. return true;
  198. foreach ($aGhostFiles as $aFile) {
  199. if (is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  200. continue;
  201. if ($aFile['private'])
  202. $oStorage->setFilePrivate ($aFile['id'], 1);
  203. if ($iContentId)
  204. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  205. }
  206. return true;
  207. }
  208. function _deleteFile ($iFileId, $sStorage = '')
  209. {
  210. $CNF = &$this->_oModule->_oConfig->CNF;
  211. if (!$iFileId)
  212. return true;
  213. $sStorage = !empty($sStorage) ? $sStorage : $CNF['OBJECT_STORAGE'];
  214. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  215. if (!$oStorage)
  216. return false;
  217. if (!$oStorage->getFile($iFileId))
  218. return true;
  219. $iProfileId = bx_get_logged_profile_id();
  220. return $oStorage->deleteFile($iFileId, $iProfileId);
  221. }
  222. function addCssJs ()
  223. {
  224. if ((!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) && !self::$_isCssJsGeneralModuleAdded) {
  225. $this->_oModule->_oTemplate->addCss('forms.css');
  226. $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
  227. self::$_isCssJsGeneralModuleAdded = true;
  228. }
  229. return parent::addCssJs ();
  230. }
  231. function genViewRowValue(&$aInput)
  232. {
  233. $s = parent::genViewRowValue($aInput);
  234. if ($this->_oMetatagsObject && in_array($aInput['name'], $this->_aMetatagsFieldsWithKeywords) && $s)
  235. $s = $this->_oMetatagsObject->metaParse($this->_oMetatagsContentId, $s);
  236. return $s;
  237. }
  238. function setMetatagsKeywordsData($iId, $a, $o)
  239. {
  240. $this->_oMetatagsContentId = $iId;
  241. $this->_aMetatagsFieldsWithKeywords = $a;
  242. $this->_oMetatagsObject = $o;
  243. }
  244. function getContentOwnerProfileId ($iContentId)
  245. {
  246. return $this->_oModule->serviceGetContentOwnerProfileId($iContentId);
  247. }
  248. }
  249. /** @} */