BxBaseModGeneralFormsEntryHelper.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. * Entry forms helper functions
  13. */
  14. class BxBaseModGeneralFormsEntryHelper extends BxDolProfileForms
  15. {
  16. protected $_oModule;
  17. public function __construct($oModule)
  18. {
  19. parent::__construct();
  20. $this->_oModule = $oModule;
  21. }
  22. public function getObjectStorage()
  23. {
  24. return BxDolStorage::getObjectInstance($this->_oModule->_oConfig->CNF['OBJECT_STORAGE']);
  25. }
  26. public function getObjectFormAdd ()
  27. {
  28. return BxDolForm::getObjectInstance($this->_oModule->_oConfig->CNF['OBJECT_FORM_ENTRY'], $this->_oModule->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'], $this->_oModule->_oTemplate);
  29. }
  30. public function getObjectFormEdit ($sDisplay = false)
  31. {
  32. $CNF = &$this->_oModule->_oConfig->CNF;
  33. if (false === $sDisplay)
  34. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'];
  35. return BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  36. }
  37. public function getObjectFormView ($sDisplay = false)
  38. {
  39. $CNF = &$this->_oModule->_oConfig->CNF;
  40. if (false === $sDisplay)
  41. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW'];
  42. return BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  43. }
  44. public function getObjectFormDelete ($sDisplay = false)
  45. {
  46. $CNF = &$this->_oModule->_oConfig->CNF;
  47. if (false === $sDisplay)
  48. $sDisplay = $CNF['OBJECT_FORM_ENTRY_DISPLAY_DELETE'];
  49. return BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $sDisplay, $this->_oModule->_oTemplate);
  50. }
  51. public function viewDataEntry ($iContentId)
  52. {
  53. // get content data and profile info
  54. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  55. if (!$aContentInfo)
  56. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  57. // check access
  58. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  59. return MsgBox($sMsg);
  60. return $this->_oModule->_oTemplate->entryText($aContentInfo);
  61. }
  62. public function addData ($iProfile, $aValues)
  63. {
  64. $CNF = &$this->_oModule->_oConfig->CNF;
  65. // check and display form
  66. $oForm = $this->getObjectFormAdd();
  67. if (!$oForm)
  68. return array('code' => 1, 'message' => '_sys_txt_error_occured');
  69. $oForm->aFormAttrs['method'] = BX_DOL_FORM_METHOD_SPECIFIC;
  70. $oForm->aParams['csrf']['disable'] = true;
  71. if(!empty($oForm->aParams['db']['submit_name']))
  72. $aValues[$oForm->aParams['db']['submit_name']] = $oForm->aInputs[$oForm->aParams['db']['submit_name']]['value'];
  73. $oForm->initChecker(array(), $aValues);
  74. if (!$oForm->isSubmittedAndValid())
  75. return array('code' => 2, 'message' => '_sys_txt_error_occured');
  76. // insert data into database
  77. $aValsToAdd = array ();
  78. if(isset($CNF['FIELD_AUTHOR']))
  79. $aValsToAdd[$CNF['FIELD_AUTHOR']] = $iProfile;
  80. $iContentId = $oForm->insert($aValsToAdd);
  81. if (!$iContentId) {
  82. if (!$oForm->isValid())
  83. return array('code' => 2, 'message' => '_sys_txt_error_occured');
  84. else
  85. return array('code' => 3, 'message' => '_sys_txt_error_entry_creation');
  86. }
  87. $sResult = $this->onDataAddAfter(BxDolProfile::getInstance($iProfile)->getAccountId(), $iContentId);
  88. if($sResult)
  89. return array('code' => 4, 'message' => $sResult);
  90. // process uploaded files
  91. if (isset($CNF['FIELD_PHOTO']))
  92. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, true);
  93. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  94. return array('code' => 0, 'message' => '', 'content' => $aContentInfo);
  95. }
  96. public function addDataForm ($sCheckFunction = false)
  97. {
  98. if (!$sCheckFunction)
  99. $sCheckFunction = 'checkAllowedAdd';
  100. $CNF = &$this->_oModule->_oConfig->CNF;
  101. // check access
  102. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction())) {
  103. $oProfile = BxDolProfile::getInstance();
  104. if ($oProfile && ($aProfileInfo = $oProfile->getInfo()) && $aProfileInfo['type'] == 'system' && is_subclass_of($this->_oModule, 'BxBaseModProfileModule') && $this->_oModule->serviceActAsProfile()) // special check for system profile is needed, because of incorrect error message
  105. return MsgBox(_t('_sys_txt_access_denied'));
  106. else
  107. return MsgBox($sMsg);
  108. }
  109. // check and display form
  110. $oForm = $this->getObjectFormAdd();
  111. if (!$oForm)
  112. return MsgBox(_t('_sys_txt_error_occured'));
  113. $oForm->initChecker();
  114. if (!$oForm->isSubmittedAndValid())
  115. return $oForm->getCode();
  116. // insert data into database
  117. $aValsToAdd = array ();
  118. $iContentId = $oForm->insert ($aValsToAdd);
  119. if (!$iContentId) {
  120. if (!$oForm->isValid())
  121. return $oForm->getCode();
  122. else
  123. return MsgBox(_t('_sys_txt_error_entry_creation'));
  124. }
  125. $sResult = $this->onDataAddAfter (getLoggedId(), $iContentId);
  126. if ($sResult)
  127. return $sResult;
  128. // process uploaded files
  129. if (isset($CNF['FIELD_PHOTO']))
  130. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, true);
  131. // perform action
  132. $this->_oModule->checkAllowedAdd(true);
  133. // redirect
  134. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  135. $this->redirectAfterAdd($aContentInfo);
  136. }
  137. public function redirectAfterAdd($aContentInfo)
  138. {
  139. $CNF = &$this->_oModule->_oConfig->CNF;
  140. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  141. }
  142. public function editDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false)
  143. {
  144. if (!$sCheckFunction)
  145. $sCheckFunction = 'checkAllowedEdit';
  146. $CNF = &$this->_oModule->_oConfig->CNF;
  147. // get content data and profile info
  148. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  149. if (!$aContentInfo)
  150. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  151. // check access
  152. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  153. return MsgBox($sMsg);
  154. // check and display form
  155. $oForm = $this->getObjectFormEdit($sDisplay);
  156. if (!$oForm)
  157. return MsgBox(_t('_sys_txt_error_occured'));
  158. $aSpecificValues = array();
  159. if (!empty($CNF['OBJECT_METATAGS'])) {
  160. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  161. if ($oMetatags->locationsIsEnabled())
  162. $aSpecificValues = $oMetatags->locationGet($iContentId, empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  163. }
  164. $oForm->initChecker($aContentInfo, $aSpecificValues);
  165. if (!$oForm->isSubmittedAndValid())
  166. return $oForm->getCode();
  167. // update data in the DB
  168. $aTrackTextFieldsChanges = null;
  169. $this->onDataEditBefore ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges);
  170. if (!$oForm->update ($aContentInfo[$CNF['FIELD_ID']], array(), $aTrackTextFieldsChanges)) {
  171. if (!$oForm->isValid())
  172. return $oForm->getCode();
  173. else
  174. return MsgBox(_t('_sys_txt_error_entry_update'));
  175. }
  176. $sResult = $this->onDataEditAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm);
  177. if ($sResult)
  178. return $sResult;
  179. // process uploaded files
  180. if (isset($CNF['FIELD_PHOTO']))
  181. $oForm->processFiles ($CNF['FIELD_PHOTO'], $iContentId, false);
  182. // perform action
  183. $this->_oModule->checkAllowedEdit($aContentInfo, true);
  184. // redirect
  185. $this->redirectAfterEdit($aContentInfo);
  186. }
  187. protected function redirectAfterEdit($aContentInfo)
  188. {
  189. $CNF = &$this->_oModule->_oConfig->CNF;
  190. $this->_redirectAndExit('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  191. }
  192. public function deleteDataForm ($iContentId, $sDisplay = false, $sCheckFunction = false)
  193. {
  194. if (!$sCheckFunction)
  195. $sCheckFunction = 'checkAllowedDelete';
  196. $CNF = &$this->_oModule->_oConfig->CNF;
  197. // get content data and profile info
  198. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  199. if (!$aContentInfo)
  200. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  201. // check access
  202. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->$sCheckFunction($aContentInfo)))
  203. return MsgBox($sMsg);
  204. // check and display form
  205. $oForm = $this->getObjectFormDelete($sDisplay);
  206. if (!$oForm)
  207. return MsgBox(_t('_sys_txt_error_occured'));
  208. $oForm->initChecker($aContentInfo);
  209. if (!$oForm->isSubmittedAndValid())
  210. return $oForm->getCode();
  211. if ($sError = $this->deleteData($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile, $oForm))
  212. return MsgBox($sError);
  213. // perform action
  214. $this->_oModule->checkAllowedDelete($aContentInfo, true);
  215. // redirect
  216. $this->redirectAfterDelete($aContentInfo);
  217. }
  218. protected function redirectAfterDelete($aContentInfo)
  219. {
  220. $CNF = &$this->_oModule->_oConfig->CNF;
  221. $this->_redirectAndExit($CNF['URL_HOME'], true, array(
  222. 'account_id' => getLoggedId(),
  223. 'profile_id' => bx_get_logged_profile_id(),
  224. ));
  225. }
  226. /**
  227. * Delete data entry
  228. * @param $iContentId entry id
  229. * @param $oForm optional content info array
  230. * @param $aContentInfo optional content info array
  231. * @param $oProfile optional content author profile
  232. * @return error string on error or empty string on success
  233. */
  234. public function deleteData ($iContentId, $aContentInfo = false, $oProfile = null, $oForm = null)
  235. {
  236. $CNF = &$this->_oModule->_oConfig->CNF;
  237. if (!$aContentInfo || !$oProfile)
  238. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  239. if (!$aContentInfo)
  240. return _t('_sys_txt_error_entry_is_not_defined');
  241. if (!$oForm)
  242. $oForm = $this->getObjectFormDelete();
  243. if (!$oForm->delete ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo))
  244. return _t('_sys_txt_error_entry_delete');
  245. if ($sResult = $this->onDataDeleteAfter ($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $oProfile))
  246. return $sResult;
  247. // create an alert
  248. bx_alert($this->_oModule->getName(), 'deleted', $aContentInfo[$CNF['FIELD_ID']]);
  249. return '';
  250. }
  251. public function viewDataForm ($iContentId, $sDisplay = false)
  252. {
  253. $CNF = &$this->_oModule->_oConfig->CNF;
  254. // get content data and profile info
  255. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  256. if (!$aContentInfo)
  257. return MsgBox(_t('_sys_txt_error_entry_is_not_defined'));
  258. // check access
  259. if ($sMsg = $this->_processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile))
  260. return MsgBox($sMsg);
  261. // get form
  262. $oForm = $this->getObjectFormView($sDisplay);
  263. if (!$oForm)
  264. return MsgBox(_t('_sys_txt_error_occured'));
  265. // process metatags
  266. if (!empty($CNF['OBJECT_METATAGS'])) {
  267. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  268. if ($oMetatags->keywordsIsEnabled()) {
  269. $aFields = $oMetatags->metaFields($aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_VIEW']);
  270. $oForm->setMetatagsKeywordsData($iContentId, $aFields, $oMetatags);
  271. }
  272. }
  273. // display profile
  274. $oForm->initChecker($aContentInfo);
  275. return $oForm->getCode();
  276. }
  277. protected function _processPermissionsCheckForViewDataForm ($aContentInfo, $oProfile)
  278. {
  279. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = $this->_oModule->checkAllowedView($aContentInfo)))
  280. return $sMsg;
  281. return '';
  282. }
  283. public function onDataDeleteAfter ($iContentId, $aContentInfo, $oProfile)
  284. {
  285. return '';
  286. }
  287. public function onDataEditBefore ($iContentId, $aContentInfo, &$aTrackTextFieldsChanges)
  288. {
  289. }
  290. public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
  291. {
  292. $CNF = &$this->_oModule->_oConfig->CNF;
  293. if (!empty($CNF['OBJECT_METATAGS'])) { // && isset($aTrackTextFieldsChanges['changed_fields'][$CNF['FIELD_TEXT']])) { // TODO: check if aTrackTextFieldsChanges works
  294. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  295. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  296. $oMetatags->metaAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']);
  297. if (isset($CNF['FIELD_LOCATION_PREFIX']) && isset($oForm->aInputs[$CNF['FIELD_LOCATION_PREFIX']]) && $oMetatags->locationsIsEnabled())
  298. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], empty($CNF['FIELD_LOCATION_PREFIX']) ? '' : $CNF['FIELD_LOCATION_PREFIX']);
  299. }
  300. return '';
  301. }
  302. public function onDataAddAfter ($iAccountId, $iContentId)
  303. {
  304. $this->_processMetas($iAccountId, $iContentId);
  305. return '';
  306. }
  307. protected function _processMetas($iAccountId, $iContentId)
  308. {
  309. $CNF = &$this->_oModule->_oConfig->CNF;
  310. if (!empty($CNF['OBJECT_METATAGS'])) {
  311. list ($oProfile, $aContentInfo) = $this->_getProfileAndContentData($iContentId);
  312. $oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  313. if ($aContentInfo)
  314. $oMetatags->metaAddAuto($aContentInfo[$CNF['FIELD_ID']], $aContentInfo, $CNF, $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  315. if ($oMetatags->locationsIsEnabled() && $aContentInfo)
  316. $oMetatags->locationsAddFromForm($aContentInfo[$CNF['FIELD_ID']], $CNF['FIELD_LOCATION_PREFIX']);
  317. }
  318. }
  319. protected function prepareCustomRedirectUrl($s, $aContentInfo)
  320. {
  321. $CNF = &$this->_oModule->_oConfig->CNF;
  322. $oProfile = BxDolProfile::getInstanceByContentAndType($aContentInfo[$CNF['FIELD_ID']], $this->_oModule->getName());
  323. $aMarkers = array(
  324. '{profile_id}',
  325. '{content_id}',
  326. '{module}',
  327. );
  328. $aReplacements = array(
  329. $oProfile ? $oProfile->id() : 0,
  330. $aContentInfo[$CNF['FIELD_ID']],
  331. $this->_oModule->getName(),
  332. );
  333. $s = str_replace($aMarkers, $aReplacements, $s);
  334. $s = BxDolPermalinks::getInstance()->permalink($s);
  335. if (false === strpos($s, 'http://') && false === strpos($s, 'https://'))
  336. $s = BX_DOL_URL_ROOT . $s;
  337. return $s;
  338. }
  339. }
  340. /** @} */