1
0

BxBaseModGeneralFormsEntryHelper.php 14 KB

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