1
0

BxBaseModGeneralFormEntry.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 $MODULE;
  17. protected $_oModule;
  18. protected $_aMetatagsFieldsWithKeywords = array();
  19. protected $_oMetatagsObject = null;
  20. protected $_oMetatagsContentId = 0;
  21. protected $_sGhostTemplate = 'form_ghost_template.html';
  22. public function __construct($aInfo, $oTemplate = false)
  23. {
  24. parent::__construct($aInfo, $oTemplate);
  25. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  26. $CNF = &$this->_oModule->_oConfig->CNF;
  27. if (isset($CNF['FIELD_ADDED']) && isset($this->aInputs[$CNF['FIELD_ADDED']])) {
  28. $this->aInputs[$CNF['FIELD_ADDED']]['date_filter'] = BX_DATA_INT;
  29. $this->aInputs[$CNF['FIELD_ADDED']]['date_format'] = BX_FORMAT_DATE;
  30. }
  31. if (isset($CNF['FIELD_CHANGED']) && isset($this->aInputs[$CNF['FIELD_CHANGED']])) {
  32. $this->aInputs[$CNF['FIELD_CHANGED']]['date_filter'] = BX_DATA_INT;
  33. $this->aInputs[$CNF['FIELD_CHANGED']]['date_format'] = BX_FORMAT_DATE;
  34. }
  35. if (isset($this->aInputs[$CNF['FIELD_TEXT']]) && isset($CNF['FIELD_TEXT_ID'])) {
  36. $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] = array_merge (
  37. array ('id' => $CNF['FIELD_TEXT_ID']),
  38. is_array($this->aInputs[$CNF['FIELD_TEXT']]['attrs']) ? $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] : array ()
  39. );
  40. }
  41. if (isset($CNF['FIELD_LOCATION_PREFIX']) && isset($this->aInputs[$CNF['FIELD_LOCATION_PREFIX']])) {
  42. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['manual_input'] = true;
  43. }
  44. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  45. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_object'] = $CNF['OBJECT_STORAGE'];
  46. $this->aInputs[$CNF['FIELD_PHOTO']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_PHOTO']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_PHOTO']]['value']) : $CNF['OBJECT_UPLOADERS'];
  47. $this->aInputs[$CNF['FIELD_PHOTO']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW'];
  48. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_private'] = 0;
  49. $this->aInputs[$CNF['FIELD_PHOTO']]['multiple'] = true;
  50. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = 0;
  51. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = '';
  52. }
  53. }
  54. function initChecker ($aValues = array (), $aSpecificValues = array())
  55. {
  56. $CNF = &$this->_oModule->_oConfig->CNF;
  57. 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())
  58. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['value'] = $oMetatags->locationsString($aValues[$CNF['FIELD_ID']], false);
  59. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  60. $aContentInfo = false;
  61. if ($aValues && !empty($aValues['id'])) {
  62. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($aValues['id']);
  63. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = $aValues['id'];
  64. }
  65. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplate, $this->_getPhotoGhostTmplVars($aContentInfo));
  66. }
  67. parent::initChecker ($aValues, $aSpecificValues);
  68. }
  69. public function insert ($aValsToAdd = array(), $isIgnore = false)
  70. {
  71. $CNF = &$this->_oModule->_oConfig->CNF;
  72. if (isset($CNF['FIELD_AUTHOR']) && empty($aValsToAdd[$CNF['FIELD_AUTHOR']]))
  73. $aValsToAdd[$CNF['FIELD_AUTHOR']] = bx_get_logged_profile_id ();
  74. if (isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']]) && empty($this->getCleanValue($CNF['FIELD_ADDED'])))
  75. $aValsToAdd[$CNF['FIELD_ADDED']] = time();
  76. if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  77. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  78. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb() && isset($CNF['FIELD_THUMB'])) {
  79. $aThumb = isset($_POST[$CNF['FIELD_THUMB']]) ? bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT) : false;
  80. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  81. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  82. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  83. }
  84. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  85. if(!empty($iContentId) && isset($CNF['FIELD_PHOTO']))
  86. $this->processFiles ($CNF['FIELD_PHOTO'], $iContentId, true);
  87. return $iContentId;
  88. }
  89. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  90. {
  91. $CNF = &$this->_oModule->_oConfig->CNF;
  92. if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  93. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  94. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb() && isset($CNF['FIELD_THUMB'])) {
  95. $aThumb = bx_process_input (bx_get($CNF['FIELD_THUMB']), BX_DATA_INT);
  96. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  97. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  98. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  99. }
  100. $iRet = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  101. if (isset($CNF['FIELD_PHOTO']))
  102. $this->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
  103. return $iRet;
  104. }
  105. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  106. {
  107. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_oModule->_isModerator());
  108. }
  109. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  110. {
  111. $CNF = &$this->_oModule->_oConfig->CNF;
  112. return array (
  113. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  114. 'content_id' => $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  115. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  116. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  117. 'bx_if:set_thumb' => array (
  118. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb(),
  119. 'content' => array (
  120. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  121. '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')
  122. ),
  123. ),
  124. );
  125. }
  126. function delete ($iContentId, $aContentInfo = array())
  127. {
  128. $CNF = &$this->_oModule->_oConfig->CNF;
  129. // delete associated files
  130. if (!empty($CNF['OBJECT_STORAGE'])) {
  131. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  132. if ($oStorage)
  133. $oStorage->queueFilesForDeletionFromGhosts($aContentInfo[$CNF['FIELD_AUTHOR']], $iContentId);
  134. }
  135. // delete associated objects data
  136. if (!empty($CNF['OBJECT_VIEWS'])) {
  137. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId);
  138. if ($o) $o->onObjectDelete();
  139. }
  140. if (!empty($CNF['OBJECT_VOTES'])) {
  141. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  142. if ($o) $o->onObjectDelete();
  143. }
  144. if (!empty($CNF['OBJECT_COMMENTS'])) {
  145. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  146. if ($o) $o->onObjectDelete();
  147. }
  148. if (!empty($CNF['OBJECT_METATAGS'])) {
  149. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  150. $oMetatags->onDeleteContent($iContentId);
  151. }
  152. // delete db record
  153. return parent::delete($iContentId);
  154. }
  155. protected function _isAdmin ($iContentId = 0)
  156. {
  157. return $this->_oModule->_isModerator();
  158. }
  159. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  160. {
  161. $CNF = &$this->_oModule->_oConfig->CNF;
  162. if (!isset($this->aInputs[$sFieldFile]))
  163. return true;
  164. $mixedFileIds = $this->getCleanValue($sFieldFile);
  165. if(!$mixedFileIds)
  166. return true;
  167. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  168. if (!$oStorage)
  169. return false;
  170. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  171. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  172. if (!$aGhostFiles)
  173. return true;
  174. foreach ($aGhostFiles as $aFile) {
  175. if (is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  176. continue;
  177. if ($aFile['private'])
  178. $oStorage->setFilePrivate ($aFile['id'], 1);
  179. if ($iContentId)
  180. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  181. }
  182. return true;
  183. }
  184. function _deleteFile ($iFileId, $sStorage = '')
  185. {
  186. $CNF = &$this->_oModule->_oConfig->CNF;
  187. if (!$iFileId)
  188. return true;
  189. $sStorage = !empty($sStorage) ? $sStorage : $CNF['OBJECT_STORAGE'];
  190. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  191. if (!$oStorage)
  192. return false;
  193. if (!$oStorage->getFile($iFileId))
  194. return true;
  195. $iProfileId = bx_get_logged_profile_id();
  196. return $oStorage->deleteFile($iFileId, $iProfileId);
  197. }
  198. function addCssJs ()
  199. {
  200. if (!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) {
  201. if (self::$_isCssJsAdded)
  202. return;
  203. $this->_oModule->_oTemplate->addCss('forms.css');
  204. $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
  205. }
  206. return parent::addCssJs ();
  207. }
  208. function genViewRowValue(&$aInput)
  209. {
  210. $s = parent::genViewRowValue($aInput);
  211. if ($this->_oMetatagsObject && in_array($aInput['name'], $this->_aMetatagsFieldsWithKeywords) && $s)
  212. $s = $this->_oMetatagsObject->keywordsParse($this->_oMetatagsContentId, $s);
  213. return $s;
  214. }
  215. function setMetatagsKeywordsData($iId, $a, $o)
  216. {
  217. $this->_oMetatagsContentId = $iId;
  218. $this->_aMetatagsFieldsWithKeywords = $a;
  219. $this->_oMetatagsObject = $o;
  220. }
  221. function getContentOwnerProfileId ($iContentId)
  222. {
  223. $CNF = &$this->_oModule->_oConfig->CNF;
  224. // file owner must be author of the content or profile itself in case of profile based module
  225. if ($iContentId) {
  226. if ($this->_oModule instanceof iBxDolProfileService) {
  227. $oProfile = BxDolProfile::getInstanceByContentAndType($iContentId, $this->MODULE);
  228. }
  229. else {
  230. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  231. $oProfile = $aContentInfo ? BxDolProfile::getInstance($aContentInfo[$CNF['FIELD_AUTHOR']]) : null;
  232. }
  233. $iProfileId = $oProfile ? $oProfile->id() : bx_get_logged_profile_id();
  234. }
  235. else {
  236. $iProfileId = bx_get_logged_profile_id();
  237. }
  238. return $iProfileId;
  239. }
  240. }
  241. /** @} */