BxBaseModGeneralFormsEntryHelper.php 14 KB

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