BxBaseModGeneralFormEntry.php 57 KB

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