BxBaseModGeneralFormEntry.php 45 KB

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