BxBaseModGeneralFormEntry.php 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  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 static $_isCssJsGeneralModuleAdded = false;
  17. protected $MODULE;
  18. protected $_oModule;
  19. protected $_aMetatagsFieldsWithKeywords = array();
  20. protected $_oMetatagsObject = null;
  21. protected $_oMetatagsContentId = 0;
  22. protected $_sGhostTemplate = 'form_ghost_template.html';
  23. protected $_aTrackFieldsChanges;
  24. protected $_iContentId;
  25. protected $_bAllowChangeUserForAdmins;
  26. public function __construct($aInfo, $oTemplate = false)
  27. {
  28. if (!isset($this->_bAllowChangeUserForAdmins))
  29. $this->_bAllowChangeUserForAdmins = true;
  30. parent::__construct($aInfo, $oTemplate);
  31. $this->_oModule = BxDolModule::getInstance($this->MODULE);
  32. $this->_aTrackFieldsChanges = array();
  33. $CNF = &$this->_oModule->_oConfig->CNF;
  34. if (isset($CNF['FIELD_ADDED']) && isset($this->aInputs[$CNF['FIELD_ADDED']])) {
  35. $this->aInputs[$CNF['FIELD_ADDED']]['date_filter'] = BX_DATA_INT;
  36. $this->aInputs[$CNF['FIELD_ADDED']]['date_format'] = BX_FORMAT_DATE;
  37. }
  38. if (isset($CNF['FIELD_CHANGED']) && isset($this->aInputs[$CNF['FIELD_CHANGED']])) {
  39. $this->aInputs[$CNF['FIELD_CHANGED']]['date_filter'] = BX_DATA_INT;
  40. $this->aInputs[$CNF['FIELD_CHANGED']]['date_format'] = BX_FORMAT_DATE;
  41. }
  42. if (isset($CNF['FIELD_TEXT']) && isset($this->aInputs[$CNF['FIELD_TEXT']]) && isset($CNF['FIELD_TEXT_ID'])) {
  43. $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] = array_merge (
  44. array ('id' => $CNF['FIELD_TEXT_ID']),
  45. is_array($this->aInputs[$CNF['FIELD_TEXT']]['attrs']) ? $this->aInputs[$CNF['FIELD_TEXT']]['attrs'] : array ()
  46. );
  47. }
  48. if (isset($CNF['FIELD_LABELS']) && isset($this->aInputs[$CNF['FIELD_LABELS']]) && !empty($CNF['OBJECT_METATAGS']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) && $oMetatags->keywordsIsEnabled()) {
  49. $this->aInputs[$CNF['FIELD_LABELS']]['meta_object'] = $CNF['OBJECT_METATAGS'];
  50. }
  51. if (isset($CNF['FIELD_LOCATION_PREFIX']) && isset($this->aInputs[$CNF['FIELD_LOCATION_PREFIX']])) {
  52. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['manual_input'] = true;
  53. }
  54. // add ability to change author by admins in some apps
  55. if (!empty($CNF['FIELD_AUTHOR']) && ($this->_oModule->_isModerator() || $this->_oModule->_isAdministrator()) && $this->_isChangeUserForAdmins($this->aParams['display']))
  56. $this->aInputs = array_merge([
  57. $CNF['FIELD_AUTHOR'] => [
  58. 'type' => 'custom',
  59. 'name' => $CNF['FIELD_AUTHOR'],
  60. 'db' => ['pass' => 'Xss'],
  61. 'caption' => _t('_sys_form_input_caption_author')
  62. ],
  63. ], $this->aInputs);
  64. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  65. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_object'] = $CNF['OBJECT_STORAGE'];
  66. $this->aInputs[$CNF['FIELD_PHOTO']]['uploaders'] = !empty($this->aInputs[$CNF['FIELD_PHOTO']]['value']) ? unserialize($this->aInputs[$CNF['FIELD_PHOTO']]['value']) : $CNF['OBJECT_UPLOADERS'];
  67. $this->aInputs[$CNF['FIELD_PHOTO']]['images_transcoder'] = $CNF['OBJECT_IMAGES_TRANSCODER_PREVIEW'];
  68. $this->aInputs[$CNF['FIELD_PHOTO']]['storage_private'] = 0;
  69. $this->aInputs[$CNF['FIELD_PHOTO']]['multiple'] = true;
  70. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = 0;
  71. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = '';
  72. }
  73. $aPrivacyFields = $this->_getPrivacyFields();
  74. foreach($aPrivacyFields as $sField => $sObject)
  75. $this->_preparePrivacyField($sField, $sObject);
  76. foreach($this->aInputs as $sKey => $aInput) {
  77. if ($aInput['type'] == 'nested_form'){
  78. $this->genGhostTemplateForInputNestedForm ($this->aInputs[$sKey]);
  79. }
  80. }
  81. }
  82. protected function _isChangeUserForAdmins($sDisplay)
  83. {
  84. $CNF = &$this->_oModule->_oConfig->CNF;
  85. if(!$this->_bAllowChangeUserForAdmins)
  86. return false;
  87. if(isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']) && $sDisplay == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])
  88. return true;
  89. if(isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']) && $sDisplay == $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'])
  90. return true;
  91. return false;
  92. }
  93. function getCode($bDynamicMode = false)
  94. {
  95. $CNF = &$this->_oModule->_oConfig->CNF;
  96. if(!$bDynamicMode && bx_is_dynamic_request())
  97. $bDynamicMode = true;
  98. $this->_replaceMarkersInControls();
  99. $sResult = parent::getCode($bDynamicMode);
  100. $aPrivacyFields = $this->_getPrivacyFields();
  101. foreach($aPrivacyFields as $sField => $sObject)
  102. $this->_addCssJsPrivacyField($sField, $sObject, $bDynamicMode);
  103. if(isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true) {
  104. $sInclude2 = '';
  105. $sInclude2 .= $this->_oModule->_oTemplate->addJs(array('BxDolCategories.js'), $bDynamicMode);
  106. $sInclude2 .= $this->_oModule->_oTemplate->addCss(array('categories.css'), $bDynamicMode);
  107. $sResult .= ($bDynamicMode ? $sInclude2 : '') . $this->_oModule->_oTemplate->getJsCode('categories');
  108. }
  109. return $sResult;
  110. }
  111. /**
  112. * Nested forms processing
  113. */
  114. function genInputNestedForm (&$aInput, $sInfo = '', $sError = '')
  115. {
  116. $sUniqId = genRndPwd (8, false);
  117. $aNestedForms = array();
  118. if (!$this->isSubmitted()){
  119. $aGhostTemplateParams = is_object($aInput['ghost_template']) ? $aInput['ghost_template']->aParams : $aInput['ghost_template']['params'];
  120. $aNestedValues = $this->_oModule->_oDb->getNestedBy(array(
  121. 'type' => 'content_id',
  122. 'id' => $this->_iContentId,
  123. 'key_name' => $aGhostTemplateParams['db']['key']
  124. ), $aGhostTemplateParams['db']['table']);
  125. foreach($aNestedValues as $aNestedValue) {
  126. $aNestedValuesRv = array();
  127. $oForm = $this->getNestedFormObject($aInput);
  128. foreach($aNestedValue as $aNestedKey => $aNestedItem) {
  129. if ($oForm->aParams['db']['key'] == $aNestedKey)
  130. $aNestedValuesRv[$aNestedKey . '[]'] = $aNestedItem;
  131. else
  132. $aNestedValuesRv[$aNestedKey] = $aNestedItem;
  133. }
  134. $oForm->initChecker($aNestedValuesRv);
  135. array_push(
  136. $aNestedForms,
  137. array(
  138. 'key_value' => $aNestedValuesRv[$oForm->aParams['db']['key'] . '[]'],
  139. 'key_name' => $aInput['name'],
  140. 'form_code' => $oForm->genRows(),
  141. 'js_instance_name' => 'oBxNestedForm_' . $sUniqId,
  142. 'nested_type' => $aInput['name'],
  143. )
  144. );
  145. }
  146. }
  147. else{
  148. $aInput['ghost_templates'] = $aInput['ghost_template'];
  149. $this->genGhostTemplateForInputNestedForm ($aInput);
  150. foreach($aInput['ghost_templates'] as $oForm) {
  151. if (isset($oForm->aInputs)){
  152. array_push(
  153. $aNestedForms,
  154. array(
  155. 'key_value' => $oForm->aInputs[$aInput['name']]['value'],
  156. 'key_name' => $aInput['name'],
  157. 'form_code' => $oForm->genRows(),
  158. 'js_instance_name' => 'oBxNestedForm_' . $sUniqId,
  159. 'nested_type' => $aInput['name'],
  160. )
  161. );
  162. }
  163. }
  164. }
  165. return $this->oTemplate->parseHtmlByName('form_field_nested_form.html', array(
  166. 'bx_repeat:items' => $aNestedForms,
  167. 'info' => $sInfo,
  168. 'error' => $sError,
  169. 'uniq_id' => $sUniqId,
  170. 'js_instance_name' => 'oBxNestedForm_' . $sUniqId,
  171. 'options' => json_encode(array(
  172. 'uniq_id' => $sUniqId,
  173. 'js_instance_name' => 'oBxNestedForm_' . $sUniqId,
  174. 'template_ghost' => $this->genGhostTemplate($aInput),
  175. 'form_name' => $aInput['value'],
  176. 'action_uri' => $this->_oModule->_oConfig->getBaseUri(),
  177. 'nested_type' => $aInput['name'],
  178. )),
  179. ));
  180. }
  181. function genNestedForm (&$aInput)
  182. {
  183. $sResult = '';
  184. $aGhostTemplateParams = is_object($aInput['ghost_template']) ? $aInput['ghost_template']->aParams : $aInput['ghost_template']['params'];
  185. $aNestedValues = $this->_oModule->_oDb->getNestedBy([
  186. 'type' => 'content_id',
  187. 'id' => $this->_iContentId,
  188. 'key_name' => $aGhostTemplateParams['db']['key']
  189. ], $aGhostTemplateParams['db']['table']);
  190. foreach($aNestedValues as $aNestedValue) {
  191. $oForm = $this->getNestedFormObject($aInput, true);
  192. $aNestedValuesRv = [];
  193. foreach($aNestedValue as $aNestedKey => $aNestedItem) {
  194. if ($oForm->aParams['db']['key'] == $aNestedKey)
  195. $aNestedValuesRv[$aNestedKey . '[]'] = $aNestedItem;
  196. else
  197. $aNestedValuesRv[$aNestedKey] = $aNestedItem;
  198. }
  199. $oForm->initChecker($aNestedValuesRv);
  200. $sValue = '';
  201. if($aInput['rateable']) {
  202. $iId = BxDolFormQuery::getFormField($this->id, $aInput['name'], $this->_iContentId, $aNestedValuesRv[$oForm->aParams['db']['key'] . '[]']);
  203. $sVoteElement = '';
  204. $sVoteCounter = '';
  205. if(($oVote = BxDolVote::getObjectInstance($aInput['rateable'], $iId)) !== null) {
  206. $sVoteElement = $oVote->getElementInline(['show_counter_empty' => true, 'show_counter' => false, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode]);
  207. $sVoteCounter = $oVote->getCounter(['show_counter_empty' => true, 'show_counter' => true, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode]);
  208. }
  209. $sValue = $this->oTemplate->parseHtmlByName('form_view_rateable_row.html', [
  210. 'value' => $oForm->getCode(),
  211. 'rate_btn' => $sVoteElement,
  212. 'rate' => $sVoteCounter,
  213. ]);
  214. }
  215. else
  216. $sValue = $oForm->getCode();
  217. $sResult .= $this->oTemplate->parseHtmlByName('form_view_nested_row.html', [
  218. 'value' => $sValue
  219. ]);
  220. }
  221. if(!$sResult)
  222. return '';
  223. $aTmplVarsIcon = [];
  224. if(!empty($aInput['icon'])) {
  225. list($sIcon, $sIconUrl, $sIconA, $sIconHtml) = BxTemplFunctions::getInstanceWithTemplate($this->oTemplate)->getIcon($aInput['icon']);
  226. $aTmplVarsIcon = [
  227. 'bx_if:icon' => [
  228. 'condition' => (bool)$sIcon,
  229. 'content' => ['icon' => $sIcon],
  230. ],
  231. 'bx_if:icon-html' => [
  232. 'condition' => (bool)$sIconHtml,
  233. 'content' => ['icon' => $sIconHtml],
  234. ],
  235. 'bx_if:image_inline' => [
  236. 'condition' => false,
  237. 'content' => ['image' => ''],
  238. ]
  239. ];
  240. }
  241. return $this->oTemplate->parseHtmlByName('form_view_row.html', [
  242. 'type' => $aInput['type'],
  243. 'class' => '',
  244. 'bx_if:show_icon' => [
  245. 'condition' => !empty($aTmplVarsIcon),
  246. 'content' => $aTmplVarsIcon
  247. ],
  248. 'caption' => isset($aInput['caption']) ? bx_process_output($aInput['caption']) : '',
  249. 'class_value' => '',
  250. 'value' => $sResult
  251. ]);
  252. }
  253. protected function genGhostTemplateForInputNestedForm (&$aInput)
  254. {
  255. $oForm = $this->getNestedFormObject($aInput);
  256. $aInput['ghost_template'] = array (
  257. 'params' => array(
  258. 'nested_form_template' => 'form_field_nested_form_wrapper.html',
  259. 'db' => array(
  260. 'table' => $oForm->aParams['db']['table'],
  261. 'key' => $oForm->aParams['db']['key'],
  262. 'submit_name' => $oForm->aParams['db']['key'],
  263. ),
  264. ),
  265. 'inputs' => $oForm->aInputs
  266. );
  267. }
  268. function getNestedFormObject (&$aInput, $bViewMode = false)
  269. {
  270. $oForm = BxDolForm::getObjectInstance($aInput['value'], $aInput['value'] . ($bViewMode ? '_view' : ''));
  271. foreach($oForm->aInputs as $sKey => $aInput2) {
  272. $sName = $oForm->aInputs[$sKey]['name'];
  273. if (strpos($sName,'[]') === false)
  274. $oForm->aInputs[$sKey]['name'] = $sName . '[]';
  275. }
  276. return $oForm;
  277. }
  278. /**
  279. * Add field(s) which will be tracked during content update.
  280. */
  281. public function addTrackFields($mixedFields, $mixedContent)
  282. {
  283. if(empty($mixedContent) || empty($mixedFields))
  284. return;
  285. if(!is_array($mixedContent)) {
  286. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  287. if(empty($mixedContent) || !is_array($mixedContent))
  288. return;
  289. }
  290. if(!is_array($mixedFields))
  291. $mixedFields = array($mixedFields);
  292. foreach($mixedFields as $sField) {
  293. if(!isset($mixedContent[$sField]))
  294. continue;
  295. $this->_aTrackFieldsChanges[$sField] = array(
  296. 'old' => $mixedContent[$sField],
  297. );
  298. }
  299. }
  300. /**
  301. * Checks if the field was changed.
  302. */
  303. public function isTrackFieldChanged($sField, $bReturnValues = false)
  304. {
  305. if(!isset($this->_aTrackFieldsChanges[$sField]) || $this->_aTrackFieldsChanges[$sField] === false)
  306. return false;
  307. return $bReturnValues ? $this->_aTrackFieldsChanges[$sField] : true;
  308. }
  309. function initChecker ($aValues = array (), $aSpecificValues = array())
  310. {
  311. $CNF = &$this->_oModule->_oConfig->CNF;
  312. $bValues = $aValues && !empty($aValues['id']);
  313. $this->_iContentId = isset($aValues['id']) ? $aValues['id'] : false;
  314. if (!empty($CNF['URI_VIEW_ENTRY']) && $this->_iContentId)
  315. $this->addMarkers([
  316. 'edit_cancel_url' => bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $this->_iContentId))
  317. ]);
  318. 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())
  319. $this->aInputs[$CNF['FIELD_LOCATION_PREFIX']]['value'] = $oMetatags->locationsString($aValues[$CNF['FIELD_ID']], false);
  320. if (isset($CNF['FIELD_PHOTO']) && isset($this->aInputs[$CNF['FIELD_PHOTO']])) {
  321. $aContentInfo = false;
  322. if ($aValues && !empty($aValues['id'])) {
  323. $aContentInfo = $this->_oModule->_oDb->getContentInfoById ($aValues['id']);
  324. $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'] = $aValues['id'];
  325. }
  326. $this->aInputs[$CNF['FIELD_PHOTO']]['ghost_template'] = $this->_oModule->_oTemplate->parseHtmlByName($this->_sGhostTemplate, $this->_getPhotoGhostTmplVars($aContentInfo));
  327. }
  328. if (isset($CNF['FIELD_LABELS']) && isset($this->aInputs[$CNF['FIELD_LABELS']]) && !empty($CNF['OBJECT_METATAGS']) && ($oMetatags = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS'])) && $oMetatags->keywordsIsEnabled() && !empty($aValues['id'])) {
  329. $this->aInputs[$CNF['FIELD_LABELS']]['content_id'] = $aValues['id'];
  330. if(($aLabels = $oMetatags->keywordsGet($aValues['id'])))
  331. $this->aInputs[$CNF['FIELD_LABELS']]['value'] = array_intersect($aLabels, BxDolLabel::getInstance()->getLabels(array('type' => 'values')));
  332. }
  333. if (isset($CNF['FIELD_ANONYMOUS'], $this->aInputs[$CNF['FIELD_ANONYMOUS']]) && isset($CNF['FIELD_AUTHOR'], $aValues[$CNF['FIELD_AUTHOR']])) {
  334. $this->aInputs[$CNF['FIELD_ANONYMOUS']]['checked'] = $aValues[$CNF['FIELD_AUTHOR']] < 0;
  335. }
  336. if (isset($CNF['FIELD_MULTICAT']) && isset($this->aInputs[$CNF['FIELD_MULTICAT']]) && $CNF['PARAM_MULTICAT_ENABLED']) {
  337. if ($bValues)
  338. $this->aInputs[$CNF['FIELD_MULTICAT']]['content_id'] = $aValues['id'];
  339. }
  340. $aPrivacyFields = $this->_getPrivacyFields();
  341. foreach($aPrivacyFields as $sField => $sObject)
  342. $this->_preloadPrivacyField($sField, $sObject, $aValues);
  343. if(isset($CNF['FIELD_AUTHOR'], $this->aInputs[$CNF['FIELD_AUTHOR']])) {
  344. if (isset($aValues[$CNF['FIELD_AUTHOR']]) && $aValues[$CNF['FIELD_AUTHOR']])
  345. $aValues[$CNF['FIELD_AUTHOR']] = abs($aValues[$CNF['FIELD_AUTHOR']]);
  346. else
  347. $aValues[$CNF['FIELD_AUTHOR']] = bx_get_logged_profile_id();
  348. }
  349. parent::initChecker ($aValues, $aSpecificValues);
  350. $this->_processContextBeforeAdd();
  351. foreach($aPrivacyFields as $sField => $sObject)
  352. $this->_validatePrivacyField($sField, $sObject, $aValues);
  353. }
  354. public function insert ($aValsToAdd = array(), $isIgnore = false)
  355. {
  356. $CNF = &$this->_oModule->_oConfig->CNF;
  357. $iAuthor = -1;
  358. if(isset($CNF['FIELD_AUTHOR'])) {
  359. $sFa = $CNF['FIELD_AUTHOR'];
  360. $iAnonymous = isset($CNF['FIELD_ANONYMOUS'], $this->aInputs[$CNF['FIELD_ANONYMOUS']]) && $this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1;
  361. if(empty($aValsToAdd[$sFa])) {
  362. if(!isset($this->aInputs[$sFa]) || empty($this->getCleanValue($sFa)))
  363. $iAuthor = bx_get_logged_profile_id();
  364. else
  365. $iAuthor = $this->getCleanValue($sFa);
  366. $aValsToAdd[$sFa] = $iAnonymous * $iAuthor;
  367. }
  368. else {
  369. $iAuthor = $aValsToAdd[$sFa];
  370. $aValsToAdd[$sFa] *= $iAnonymous;
  371. }
  372. }
  373. if (isset($CNF['FIELD_ADDED']) && empty($aValsToAdd[$CNF['FIELD_ADDED']]) && empty($this->getCleanValue($CNF['FIELD_ADDED'])))
  374. $aValsToAdd[$CNF['FIELD_ADDED']] = time();
  375. if (isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  376. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  377. if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb() && isset($CNF['FIELD_THUMB'])) {
  378. $aThumb = isset($_POST[$CNF['FIELD_THUMB']]) ? bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT) : false;
  379. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  380. if (!empty($aThumb)){
  381. if(is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  382. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  383. else
  384. $aValsToAdd[$CNF['FIELD_THUMB']] = $aThumb;
  385. }
  386. }
  387. if(!empty($CNF['OBJECT_METATAGS']))
  388. $this->_processMetas($aValsToAdd);
  389. if(isset($CNF['FIELD_STATUS']) && !empty($CNF['FIELDS_DELAYED_PROCESSING']) && !empty($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4'])) {
  390. $oTranscoder = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  391. $aFields = $CNF['FIELDS_DELAYED_PROCESSING'];
  392. if(is_string($aFields))
  393. $aFields = explode(',', $aFields);
  394. foreach($aFields as $sField) {
  395. $mixedFieldValues = $this->getCleanValue($sField);
  396. if(empty($mixedFieldValues))
  397. continue;
  398. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sField]['storage_object']);
  399. if(!$oStorage)
  400. continue;
  401. if(!is_array($mixedFieldValues))
  402. $mixedFieldValues = array($mixedFieldValues);
  403. foreach($mixedFieldValues as $mixedFieldValue) {
  404. $aInfo = $oStorage->getFile((int)$mixedFieldValue);
  405. if(empty($aInfo) || !is_array($aInfo))
  406. continue;
  407. if(strncmp($aInfo['mime_type'], 'video/', 6) != 0)
  408. continue;
  409. if($oTranscoder->isFileReady((int)$mixedFieldValue))
  410. continue;
  411. $aValsToAdd[$CNF['FIELD_STATUS']] = 'awaiting';
  412. break 2;
  413. }
  414. }
  415. }
  416. if(isset($CNF['FIELD_STATUS_ADMIN']) && empty($aValsToAdd[$CNF['FIELD_STATUS_ADMIN']]) && !$this->_oModule->_isModerator() && !$this->_oModule->_oConfig->isAutoApproveEnabled())
  417. $aValsToAdd[$CNF['FIELD_STATUS_ADMIN']] = BX_BASE_MOD_GENERAL_STATUS_PENDING;
  418. $bMulticatEnabled = $this->_isMulticatEnabled();
  419. if ($bMulticatEnabled)
  420. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  421. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  422. if(empty($iContentId))
  423. return $iContentId;
  424. foreach($this->aInputs as $aInput) {
  425. if ($aInput['type'] == 'nested_form') {
  426. if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) {
  427. foreach ($aInput['ghost_template'] as $oFormNested) {
  428. $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId));
  429. if ($aInput['rateable']) {
  430. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId);
  431. }
  432. }
  433. }
  434. }
  435. }
  436. if($this->_bIsApi && ($sKeyF = 'FIELD_ALLOW_VIEW_TO') && isset($CNF[$sKeyF]) && ($mixedAllowViewTo = $this->getCleanValue($CNF[$sKeyF])) !== false) {
  437. $aAllowViewToItems = [];
  438. if(($mixedAllowViewToItems = $this->getCleanValue($CNF[$sKeyF] . '_items')) !== false)
  439. $aAllowViewToItems = explode(',', $mixedAllowViewToItems);
  440. if(($sKeyO = 'OBJECT_PRIVACY_VIEW') && isset($CNF[$sKeyO]) && ($sObject = $CNF[$sKeyO]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($sObject)) !== false)
  441. $oPrivacy->addGroupCustom([
  442. 'profile_id' => $iAuthor,
  443. 'content_id' => 0,
  444. 'object' => $sObject,
  445. 'group_id' => (int)$mixedAllowViewTo
  446. ], $aAllowViewToItems);
  447. }
  448. if($bMulticatEnabled)
  449. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  450. foreach($this->aInputs as $aInput)
  451. if(isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form')
  452. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName());
  453. if(($iContextNid = $this->getCleanValue('context_nid')) !== false)
  454. $this->_processContextAfterAdd($iContentId, $iContextNid, (int)$this->getCleanValue('context_usage'));
  455. return $iContentId;
  456. }
  457. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  458. {
  459. $CNF = &$this->_oModule->_oConfig->CNF;
  460. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  461. $iAuthor = -1;
  462. if(isset($CNF['FIELD_AUTHOR'])) {
  463. $sFa = $CNF['FIELD_AUTHOR'];
  464. if(!empty($aValsToAdd[$sFa]))
  465. $iAuthor = $aValsToAdd[$sFa];
  466. else if(isset($this->aInputs[$sFa]) && !empty($this->getCleanValue($sFa)))
  467. $iAuthor = $this->getCleanValue($sFa);
  468. else if(!empty($aContentInfo[$sFa]))
  469. $iAuthor = $aContentInfo[$sFa];
  470. else
  471. $iAuthor = bx_get_logged_profile_id();
  472. $iAuthor = abs($iAuthor);
  473. $iAnonymous = isset($CNF['FIELD_ANONYMOUS'], $this->aInputs[$CNF['FIELD_ANONYMOUS']]) && $this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1;
  474. $aValsToAdd[$sFa] = $iAnonymous * $iAuthor;
  475. }
  476. if(isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  477. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  478. if(CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($iContentId) && isset($CNF['FIELD_THUMB']) && ($aThumb = bx_get($CNF['FIELD_THUMB'])) !== false) {
  479. $aThumb = bx_process_input($aThumb, BX_DATA_INT);
  480. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  481. if(!empty($aThumb)) {
  482. if(is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  483. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  484. else
  485. $aValsToAdd[$CNF['FIELD_THUMB']] = $aThumb;
  486. }
  487. }
  488. if(!empty($CNF['OBJECT_METATAGS']))
  489. $this->_processMetas($aValsToAdd);
  490. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  491. $aValsToAdd[$CNF['FIELD_STATUS']] = 'active';
  492. $bMulticatEnabled = $this->_isMulticatEnabled();
  493. if ($bMulticatEnabled)
  494. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  495. $mixedResult = parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  496. foreach($this->aInputs as $aInput) {
  497. if ($aInput['type'] == 'nested_form'){
  498. if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) {
  499. foreach ($aInput['ghost_template'] as $oFormNested) {
  500. $aSpecificValues = $oFormNested->getSpecificValues();
  501. $iNestedContentId = $oFormNested->getSubmittedValue($aInput['name'], BX_DOL_FORM_METHOD_SPECIFIC, $aSpecificValues);
  502. if (empty($iNestedContentId)){
  503. $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId));
  504. if ($aInput['rateable']){
  505. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId);
  506. }
  507. }
  508. else{
  509. $oFormNested->update($iNestedContentId, array('content_id' => $iContentId));
  510. }
  511. }
  512. }
  513. }
  514. }
  515. if ($bMulticatEnabled)
  516. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  517. foreach($this->aInputs as $aInput) {
  518. if (isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form'){
  519. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName());
  520. }
  521. }
  522. if($mixedResult !== false) {
  523. $this->_processTrackFields($iContentId);
  524. if($this->_bIsApi && ($sKeyF = 'FIELD_ALLOW_VIEW_TO') && isset($CNF[$sKeyF]) && ($mixedAllowViewTo = $this->getCleanValue($CNF[$sKeyF])) !== false) {
  525. $aAllowViewToItems = [];
  526. if(($mixedAllowViewToItems = $this->getCleanValue($CNF[$sKeyF] . '_items')) !== false)
  527. $aAllowViewToItems = explode(',', $mixedAllowViewToItems);
  528. if(($sKeyO = 'OBJECT_PRIVACY_VIEW') && isset($CNF[$sKeyO]) && ($sObject = $CNF[$sKeyO]) && ($oPrivacy = BxDolPrivacy::getObjectInstance($sObject)) !== false) {
  529. $oPrivacy->editGroupCustom([
  530. 'profile_id' => $iAuthor,
  531. 'content_id' => $iContentId,
  532. 'object' => $sObject,
  533. 'group_id' => (int)$mixedAllowViewTo
  534. ], $aAllowViewToItems);
  535. }
  536. }
  537. }
  538. return $mixedResult;
  539. }
  540. function getHtmlEditorQueryParams($aInput)
  541. {
  542. $aQueryParams = parent::getHtmlEditorQueryParams($aInput);
  543. if (isset($this->MODULE)){
  544. $aQueryParams['m'] = $this->MODULE;
  545. }
  546. if (isset($this->_iContentId) && $this->_iContentId){
  547. $aQueryParams['cid'] = $this->_iContentId;
  548. }
  549. $aQueryParams['fi'] = '';
  550. /**
  551. * @hooks
  552. * @hookdef hook-system-editor_query_params 'system', 'editor_query_params' - hook to override http(s) request's query params, which is used in HTML editor
  553. * - $unit_name - equals `system`
  554. * - $action - equals `editor_query_params`
  555. * - $object_id - not used
  556. * - $sender_id - not used
  557. * - $extra_params - array of additional params with the following array keys:
  558. * - `form` - [object] an instance of form, @see BxDolForm
  559. * - `override_result` - [array] by ref, query string params, can be overridden in hook processing
  560. * @hook @ref hook-system-editor_query_params
  561. */
  562. bx_alert('system', 'editor_query_params', 0, 0, [
  563. 'form' => $this,
  564. 'override_result' => &$aQueryParams
  565. ]);
  566. return $aQueryParams;
  567. }
  568. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  569. {
  570. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  571. }
  572. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  573. {
  574. $CNF = &$this->_oModule->_oConfig->CNF;
  575. return array (
  576. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  577. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  578. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  579. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  580. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  581. 'bx_if:set_thumb' => array (
  582. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($this->aInputs[$CNF['FIELD_PHOTO']]['content_id']),
  583. 'content' => array (
  584. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  585. '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')
  586. ),
  587. ),
  588. );
  589. }
  590. function delete ($iContentId, $aContentInfo = array())
  591. {
  592. $CNF = &$this->_oModule->_oConfig->CNF;
  593. // delete associated files
  594. $a = array('OBJECT_STORAGE', 'OBJECT_STORAGE_FILES', 'OBJECT_STORAGE_PHOTOS', 'OBJECT_STORAGE_VIDEOS', 'OBJECT_STORAGE_SOUNDS');
  595. foreach ($a as $k) {
  596. if (!empty($CNF[$k])) {
  597. $oStorage = BxDolStorage::getObjectInstance($CNF[$k]);
  598. if ($oStorage)
  599. $oStorage->queueFilesForDeletionFromGhosts($aContentInfo[$CNF['FIELD_AUTHOR']], $iContentId);
  600. }
  601. }
  602. // delete associated objects data
  603. if (!empty($CNF['OBJECT_VIEWS'])) {
  604. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId);
  605. if ($o) $o->onObjectDelete();
  606. }
  607. if (!empty($CNF['OBJECT_VOTES'])) {
  608. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  609. if ($o) $o->onObjectDelete();
  610. }
  611. if (!empty($CNF['OBJECT_SCORES'])) {
  612. $o = BxDolScore::getObjectInstance($CNF['OBJECT_SCORES'], $iContentId);
  613. if ($o) $o->onObjectDelete();
  614. }
  615. if (!empty($CNF['OBJECT_COMMENTS'])) {
  616. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  617. if ($o) $o->onObjectDelete();
  618. }
  619. if (!empty($CNF['OBJECT_REPORTS'])) {
  620. $o = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS'], $iContentId);
  621. if ($o) $o->onObjectDelete();
  622. }
  623. if (!empty($CNF['OBJECT_METATAGS'])) {
  624. $o = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  625. if ($o) $o->onDeleteContent($iContentId);
  626. }
  627. // delete SEO links
  628. BxDolPage::deleteSeoLink ($this->_oModule->getName(), $this->_oModule->getName(), $iContentId);
  629. // delete db record
  630. BxDolFormQuery::removeFormField($this->id, $iContentId);
  631. return parent::delete($iContentId);
  632. }
  633. protected function _isAdmin ($iContentId = 0)
  634. {
  635. return $this->_oModule->_isModerator();
  636. }
  637. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  638. {
  639. $CNF = &$this->_oModule->_oConfig->CNF;
  640. if (!isset($this->aInputs[$sFieldFile]))
  641. return true;
  642. $mixedFileIds = $this->getCleanValue($sFieldFile);
  643. if(!$mixedFileIds)
  644. return true;
  645. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  646. if (!$oStorage)
  647. return false;
  648. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  649. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  650. if(!empty($aGhostFiles) && is_array($aGhostFiles))
  651. foreach($aGhostFiles as $aFile) {
  652. if(is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  653. continue;
  654. if($aFile['private'])
  655. $oStorage->setFilePrivate ($aFile['id'], 1);
  656. if($iContentId)
  657. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  658. }
  659. if($this->_bAllowChangeUserForAdmins && ($this->_oModule->_isModerator() || $this->_oModule->_isAdministrator()) && ($iLoggedId = bx_get_logged_profile_id()) != $iProfileId) {
  660. $aGhostFiles = $oStorage->getGhosts($iLoggedId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  661. if(!empty($aGhostFiles) && is_array($aGhostFiles))
  662. foreach($aGhostFiles as $aFile) {
  663. if(is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  664. continue;
  665. if($aFile['private'])
  666. $oStorage->setFilePrivate ($aFile['id'], 1);
  667. if($iContentId)
  668. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  669. }
  670. }
  671. return true;
  672. }
  673. function _deleteFile ($iFileId, $sStorage = '')
  674. {
  675. $CNF = &$this->_oModule->_oConfig->CNF;
  676. if (!$iFileId)
  677. return true;
  678. $sStorage = !empty($sStorage) ? $sStorage : $CNF['OBJECT_STORAGE'];
  679. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  680. if (!$oStorage)
  681. return false;
  682. if (!$oStorage->getFile($iFileId))
  683. return true;
  684. $iProfileId = bx_get_logged_profile_id();
  685. return $oStorage->deleteFile($iFileId, $iProfileId);
  686. }
  687. function addCssJs ()
  688. {
  689. if ((!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) && !self::$_isCssJsGeneralModuleAdded) {
  690. $this->_oModule->_oTemplate->addCss('forms.css');
  691. $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
  692. self::$_isCssJsGeneralModuleAdded = true;
  693. }
  694. return parent::addCssJs ();
  695. }
  696. function genViewRowValue(&$aInput)
  697. {
  698. $s = parent::genViewRowValue($aInput);
  699. if ($this->_oMetatagsObject && in_array($aInput['name'], $this->_aMetatagsFieldsWithKeywords) && $s)
  700. $s = $this->_oMetatagsObject->metaParse($this->_oMetatagsContentId, $s);
  701. return $s;
  702. }
  703. function genViewRowWrapped(&$aInput)
  704. {
  705. $sResult = parent::genViewRowWrapped($aInput);
  706. if(!$aInput['rateable'])
  707. return $sResult;
  708. $sValue = $this->genViewRowValue($aInput);
  709. if(null === $sValue)
  710. return '';
  711. // process rateable fields
  712. $iId = BxDolFormQuery::getFormField($this->id, $aInput['name'], $this->_iContentId);
  713. if(($oVote = BxDolVote::getObjectInstance($aInput['rateable'], $iId)) !== null) {
  714. $sVoteElement = $oVote->getElementInline(['show_counter_empty' => true, 'show_counter' => false, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode]);
  715. $sVoteCounter = $oVote->getCounter(['show_counter_empty' => true, 'show_counter' => true, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode]);
  716. return $this->oTemplate->parseHtmlByName('form_view_rateable_row.html', [
  717. 'value' => $sResult,
  718. 'rate_btn' => $sVoteElement,
  719. 'rate' => $sVoteCounter,
  720. ]);
  721. }
  722. return $sResult;
  723. }
  724. function setMetatagsKeywordsData($iId, $a, $o)
  725. {
  726. $this->_oMetatagsContentId = $iId;
  727. $this->_aMetatagsFieldsWithKeywords = $a;
  728. $this->_oMetatagsObject = $o;
  729. }
  730. function getContentOwnerProfileId ($iContentId)
  731. {
  732. return $this->_oModule->serviceGetContentOwnerProfileId($iContentId);
  733. }
  734. protected function _processTrackFields($mixedContent)
  735. {
  736. if(empty($mixedContent) || empty($this->_aTrackFieldsChanges))
  737. return;
  738. if(!is_array($mixedContent)) {
  739. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  740. if(empty($mixedContent) || !is_array($mixedContent))
  741. return;
  742. }
  743. foreach($this->_aTrackFieldsChanges as $sField => $aValues)
  744. if($mixedContent[$sField] == $aValues['old'])
  745. $this->_aTrackFieldsChanges[$sField] = false;
  746. else
  747. $this->_aTrackFieldsChanges[$sField]['new'] = $mixedContent[$sField];
  748. }
  749. protected function _processMetas(&$aValsToAdd)
  750. {
  751. $CNF = &$this->_oModule->_oConfig->CNF;
  752. $sKey = 'FIELD_LABELS';
  753. if(isset($CNF[$sKey]) && isset($this->aInputs[$CNF[$sKey]]) && empty($aValsToAdd[$CNF[$sKey]])) {
  754. $aLabels = $this->getCleanValue($CNF[$sKey]);
  755. if(!empty($aLabels) && is_array($aLabels))
  756. $aValsToAdd[$CNF[$sKey]] = serialize($aLabels);
  757. else
  758. $aValsToAdd[$CNF[$sKey]] = '';
  759. }
  760. $sKey1 = 'FIELD_LOCATION';
  761. $sKey2 = 'FIELD_LOCATION_PREFIX';
  762. if(isset($CNF[$sKey1]) && isset($CNF[$sKey2]) && isset($this->aInputs[$CNF[$sKey1]]) && empty($aValsToAdd[$CNF[$sKey1]])) {
  763. $aLocation = BxDolMetatags::locationsRetrieveFromForm($CNF[$sKey2], $this);
  764. if(!empty($aLocation) && is_array($aLocation))
  765. $aValsToAdd[$CNF[$sKey1]] = serialize(BxDolMetatags::locationsParseComponents($aLocation));
  766. }
  767. }
  768. protected function _getPrivacyFields($aKeysF2O = array())
  769. {
  770. $CNF = &$this->_oModule->_oConfig->CNF;
  771. if(isset($CNF['PRIVACY_FIELD_TO_OBJECT']) && is_array($CNF['PRIVACY_FIELD_TO_OBJECT']))
  772. return $CNF['PRIVACY_FIELD_TO_OBJECT'];
  773. if(empty($aKeysF2O))
  774. $aKeysF2O = array('FIELD_ALLOW_VIEW_TO' => 'OBJECT_PRIVACY_VIEW');
  775. $aResult = array();
  776. foreach($aKeysF2O as $sKeyField => $sKeyObject) {
  777. if(!isset($CNF[$sKeyField]) || !isset($this->aInputs[$CNF[$sKeyField]]) || !isset($CNF[$sKeyObject]))
  778. continue;
  779. $aResult[$CNF[$sKeyField]] = $CNF[$sKeyObject];
  780. }
  781. return $aResult;
  782. }
  783. function _addCssJsPrivacyField($sField, $sPrivacyObject, $bDynamicMode = false)
  784. {
  785. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  786. if(!$oPrivacy)
  787. return;
  788. $this->aInputs[$sField]['content'] = $oPrivacy->addCssJs($bDynamicMode) . $this->aInputs[$sField]['content'];
  789. }
  790. protected function _preparePrivacyField($sField, $sPrivacyObject)
  791. {
  792. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  793. if(!$oPrivacy)
  794. return;
  795. $aSave = array('db' => array('pass' => 'Xss'));
  796. array_walk($this->aInputs[$sField], function ($a, $k, $aSave) {
  797. if (in_array($k, array('info', 'caption', 'value')))
  798. $aSave[0][$k] = $a;
  799. }, array(&$aSave));
  800. $aGroupChooser = $oPrivacy->getGroupChooser($sPrivacyObject, 0, array(
  801. 'object' => $this->aParams['object'],
  802. 'display' => $this->aParams['display']
  803. ));
  804. if (bx_get('context') && isset($aGroupChooser['values'])) {
  805. $bFound = false;
  806. foreach ($aGroupChooser['values'] as $k => $r) {
  807. if (!isset($r['key']) || abs((int)bx_get('context')) !== abs((int)$r['key']))
  808. continue;
  809. $bFound = true;
  810. break;
  811. }
  812. if ($bFound) {
  813. $aSave['type'] = 'hidden';
  814. $aSave['value'] = -abs((int)bx_get('context'));
  815. }
  816. }
  817. $this->aInputs[$sField] = array_merge($this->aInputs[$sField], $aGroupChooser, $aSave);
  818. }
  819. protected function _preloadPrivacyField($sField, $sPrivacyObject, $aValues)
  820. {
  821. $CNF = &$this->_oModule->_oConfig->CNF;
  822. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  823. if(!$oPrivacy)
  824. return;
  825. $iContentId = !empty($aValues[$CNF['FIELD_ID']]) ? (int)$aValues[$CNF['FIELD_ID']] : 0;
  826. $iProfileId = !empty($iContentId) ? (int)$this->getContentOwnerProfileId($iContentId) : bx_get_logged_profile_id();
  827. $iGroupId = !empty($aValues[$sField]) ? $aValues[$sField] : 0;
  828. if(!$this->_bIsApi) {
  829. if(!isset($this->aInputs[$sField]['content']))
  830. $this->aInputs[$sField]['content'] = '';
  831. $this->aInputs[$sField]['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iProfileId, [
  832. 'content_id' => $iContentId,
  833. 'group_id' => $iGroupId,
  834. 'html_ids' => [
  835. 'form' => $this->getId()
  836. ]
  837. ]);
  838. }
  839. else
  840. $this->aInputs[$sField]['subvalue'] = $oPrivacy->initGroupChooserAPI($sPrivacyObject, $iProfileId, [
  841. 'content_id' => $iContentId,
  842. 'group_id' => $iGroupId,
  843. ]);
  844. }
  845. protected function _validatePrivacyField($sField, $sPrivacyObject, $aValues)
  846. {
  847. $mixedValue = $this->aInputs[$sField]['value'];
  848. $bValue = false;
  849. foreach($this->aInputs[$sField]['values'] as $aValue)
  850. if(isset($aValue['key']) && $aValue['key'] == $mixedValue) {
  851. $bValue = true;
  852. break;
  853. }
  854. if(!$bValue) {
  855. $sTitle = '';
  856. if(is_numeric($mixedValue) && (int)$mixedValue < 0 && ($oContext = BxDolProfile::getInstance(abs((int)$mixedValue))) !== false)
  857. $sTitle = $oContext->getDisplayName();
  858. else
  859. $sTitle = _t('_sys_ps_group_title_unknown');
  860. $this->aInputs[$sField]['values'] = array_merge(array(
  861. array('key' => $mixedValue, 'value' => $sTitle),
  862. array('key' => '', 'value' => '----', 'attrs' => array('disabled' => 'disabled'))
  863. ), $this->aInputs[$sField]['values']);
  864. }
  865. }
  866. protected function _processContextBeforeAdd()
  867. {
  868. $CNF = &$this->_oModule->_oConfig->CNF;
  869. $sKey = 'FIELD_ALLOW_VIEW_TO';
  870. if(!isset($CNF[$sKey], $this->aInputs[$CNF[$sKey]]))
  871. return;
  872. if(($iContextId = bx_get('context_pid')) !== false)
  873. $this->aInputs[$CNF[$sKey]] = array_merge($this->aInputs[$CNF[$sKey]], [
  874. 'type' => 'hidden',
  875. 'value' => -abs((int)$iContextId)
  876. ]);
  877. $sKey = 'context_nid';
  878. if(($iNodeId = bx_get($sKey)) !== false && !isset($this->aInputs[$sKey])) {
  879. $this->aInputs[$sKey] = [
  880. 'name' => $sKey,
  881. 'type' => 'hidden',
  882. 'value' => (int)$iNodeId
  883. ];
  884. }
  885. $sKey = 'context_usage';
  886. if(($iUsage = bx_get($sKey)) !== false && !isset($this->aInputs[$sKey])) {
  887. $this->aInputs[$sKey] = [
  888. 'name' => $sKey,
  889. 'type' => 'hidden',
  890. 'value' => (int)$iUsage
  891. ];
  892. }
  893. }
  894. protected function _processContextAfterAdd($iContentId, $iContextNid, $iContextUsage = 0)
  895. {
  896. $CNF = &$this->_oModule->_oConfig->CNF;
  897. $sKey = 'FIELD_ALLOW_VIEW_TO';
  898. if(!isset($CNF[$sKey]))
  899. return;
  900. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  901. if(!is_numeric($aContentInfo[$CNF[$sKey]]) || (int)$aContentInfo[$CNF[$sKey]] > 0)
  902. return;
  903. $iContextPid = abs($aContentInfo[$CNF[$sKey]]);
  904. $oContext = BxDolProfile::getInstance($iContextPid);
  905. if(!$oContext)
  906. return;
  907. $sModule = $oContext->getModule();
  908. $sMethod = 'on_content_added';
  909. if(bx_is_srv($sModule, $sMethod))
  910. bx_srv($sModule, $sMethod, [$this->MODULE, $iContentId, $oContext->getContentId(), $iContextNid, $iContextUsage]);
  911. }
  912. /**
  913. *
  914. * MultiCategories related methods.
  915. *
  916. */
  917. protected function processMulticatBefore($sFieldName, &$aValsToAdd){
  918. if (isset($this->aInputs[$sFieldName])){
  919. $this->aInputs[$sFieldName]['value'] = array_unique(array_filter($this->aInputs[$sFieldName]['value'], function($sTmp){
  920. return trim($sTmp);
  921. }));
  922. $aValsToAdd[$sFieldName] = implode(',', $this->aInputs[$sFieldName]['value']);
  923. }
  924. }
  925. protected function processMulticatAfter($sFieldName, $iContentId){
  926. $CNF = &$this->_oModule->_oConfig->CNF;
  927. $bAutoActivation = (isset($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) && getParam($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) == 'on') ? true : false;
  928. $oCategories = BxDolCategories::getInstance();
  929. if (isset($this->aInputs[$sFieldName])){
  930. $oCategories->delete($this->_oModule->getName(), $iContentId);
  931. foreach($this->aInputs[$sFieldName]['value'] as $sValue) {
  932. $oCategories->add($this->_oModule->getName(), bx_get_logged_profile_id(), $sValue, $iContentId, $bAutoActivation);
  933. }
  934. }
  935. }
  936. protected function genCustomViewRowValueMulticat(&$aInput)
  937. {
  938. $oCategories = BxDolCategories::getInstance();
  939. $aValues = $oCategories->getData(array('type' => 'by_module_and_object', 'module' => $this->_oModule->getName(), 'object_id' => (!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0 )));
  940. if (count($aValues) > 0){
  941. $aVars = array('bx_repeat:cats' => array());
  942. foreach ($aValues as $sValue) {
  943. $aVars['bx_repeat:cats'][] = array(
  944. 'url' => $oCategories->getUrl($this->_oModule->getName(), $sValue),
  945. 'name' => _t($sValue),
  946. 'bx_if:more' => array(
  947. 'condition' => $sValue === end($aValues) ? false : true,
  948. 'content' => array('1')
  949. ),
  950. );
  951. }
  952. if (!$aVars['bx_repeat:cats'])
  953. return '';
  954. return $this->_oModule->_oTemplate->parseHtmlByName('category_list_inline.html', $aVars);
  955. }
  956. return '';
  957. }
  958. protected function genCustomInputMulticat(&$aInput)
  959. {
  960. $sJsObject = $this->_oModule->_oConfig->getJsObject('categories');
  961. $aValuesForSelect = BxDolCategories::getInstance()->getData(array('type' => 'by_module_and_author', 'module' => $this->_oModule->getName(), 'author' => bx_get_logged_profile_id()));
  962. $aSelectedItems = array();
  963. if (isset($aInput['value']) && is_array($aInput['value']))
  964. $aInput['value'] = array_filter($aInput['value']);
  965. if(!empty($aInput['value'])) {
  966. if (!is_array($aInput['value']))
  967. $aValues = BxDolCategories::getInstance()->getData(array('type' => 'by_module_and_object', 'module' => $this->_oModule->getName(), 'object_id' => (!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0 )));
  968. else
  969. $aValues = $aInput['value'];
  970. $aValues = array_filter($aValues);
  971. foreach($aValues as $sValue) {
  972. if (!array_key_exists($sValue, $aValuesForSelect)){
  973. $aValuesForSelect[$sValue] = array('key' => $sValue, 'value' => $sValue);
  974. }
  975. }
  976. foreach($aValues as $sValue) {
  977. $sInput = $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, $sValue);
  978. $aSelectedItems[] = array('js_object' => $sJsObject, 'select_cat' => $sInput);
  979. }
  980. }
  981. else{
  982. $aSelectedItems = array(
  983. array('js_object' => $sJsObject, 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect))
  984. );
  985. }
  986. return $this->_oModule->_oTemplate->parseHtmlByName('form_categories.html', array(
  987. 'bx_repeat:items' => $aSelectedItems,
  988. 'js_object' => $sJsObject,
  989. 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, -1),
  990. 'input_cat' => $this->genCustomInputMulticatInput($aInput),
  991. 'js_object' => $sJsObject,
  992. 'btn_add' => $this->genCustomInputMulticatButton($aInput),
  993. 'btn_add_new' => $this->genCustomInputMulticatButtonNew($aInput)
  994. ));
  995. }
  996. protected function genCustomInputMulticatSelect($aInput, $aValues, $mixedValue = '')
  997. {
  998. foreach($aValues as $sKey => $aValue) {
  999. $aValues[$sKey]['value'] = _t($aValue['key']);
  1000. }
  1001. $aValues = array_merge(array('1' => array('key' => false, 'value' => _t('_sys_please_select'))), $aValues);
  1002. $aInput['type'] = 'select';
  1003. $aInput['name'] .= '[]';
  1004. $aInput['value'] = $mixedValue;
  1005. $aInput['values'] = $aValues;
  1006. return $this->genInput($aInput);
  1007. }
  1008. protected function genCustomInputMulticatInput($aInput)
  1009. {
  1010. $aInput['type'] = 'text';
  1011. $aInput['name'] .= '[]';
  1012. $aInput['value'] = '';
  1013. return $this->genInput($aInput);
  1014. }
  1015. protected function genCustomInputMulticatButton($aInput)
  1016. {
  1017. $sName = $aInput['name'];
  1018. $aInput['type'] = 'button';
  1019. $aInput['name'] .= '_add';
  1020. $aInput['value'] = _t('_sys_txt_categories_button_caption_add');
  1021. $aInput['attrs']['class'] = 'bx-def-margin-right bx-def-margin-sec-top';
  1022. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAdd(this, '" . $sName . "');";
  1023. return $this->genInputButton($aInput);
  1024. }
  1025. protected function genCustomInputMulticatButtonNew($aInput)
  1026. {
  1027. $sName = $aInput['name'];
  1028. $aInput['type'] = 'button';
  1029. $aInput['name'] .= '_add';
  1030. $aInput['value'] = _t('_sys_txt_categories_button_caption_addnew');
  1031. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  1032. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAddNew(this, '" . $sName . "');";
  1033. return $this->genInputButton($aInput);
  1034. }
  1035. protected function genCustomInputAuthor ($aInput)
  1036. {
  1037. if(empty($aInput['custom']) || !is_array($aInput['custom']))
  1038. $aInput['custom'] = array();
  1039. $aInput['custom']['only_once'] = 1;
  1040. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . "modules/?r=" . $this->_oModule->_oConfig->getUri() . "/ajax_get_profiles";
  1041. if (bx_is_api()){
  1042. $aInput['ajax_get_suggestions'] = $this->_oModule->_oConfig->getName() . "/get_profiles&params[]=";
  1043. $aProfile = BxDolProfile::getInstance($aInput['value'])->getUnitAPI(0, ['template' => 'unit_wo_info']);
  1044. $aInput['value_data'] = [$aProfile['author_data']];
  1045. }
  1046. return $this->genCustomInputUsernamesSuggestions($aInput);
  1047. }
  1048. protected function _isMulticatEnabled(){
  1049. $CNF = $this->_oModule->_oConfig->CNF;
  1050. return isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true && isset($CNF['FIELD_MULTICAT']);
  1051. }
  1052. protected function _replaceMarkersInControls($sKey = 'controls')
  1053. {
  1054. if(!isset($this->aInputs[$sKey]) || !is_array($this->aInputs[$sKey]))
  1055. return;
  1056. array_walk($this->aInputs[$sKey], function(&$aItem, $mKey) {
  1057. if(!is_int($mKey))
  1058. return;
  1059. if(!empty($aItem['attrs']) && is_array($aItem['attrs']))
  1060. $aItem['attrs'] = $this->_replaceMarkers($aItem['attrs']);
  1061. });
  1062. }
  1063. }
  1064. /** @} */