1
0

BxBaseModGeneralFormsEntryHelper.php 14 KB

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