BxBaseModGeneralFormEntry.php 50 KB

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