BxBaseModGeneralFormEntry.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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. $bMulticatEnabled = $this->_isMulticatEnabled();
  382. if ($bMulticatEnabled)
  383. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  384. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  385. if(!empty($iContentId)) {
  386. foreach($this->aInputs as $aInput) {
  387. if ($aInput['type'] == 'nested_form') {
  388. if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) {
  389. foreach ($aInput['ghost_template'] as $oFormNested) {
  390. $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId));
  391. if ($aInput['rateable']) {
  392. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId);
  393. }
  394. }
  395. }
  396. }
  397. }
  398. if ($bMulticatEnabled)
  399. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  400. }
  401. foreach($this->aInputs as $aInput) {
  402. if (isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form'){
  403. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName());
  404. }
  405. }
  406. return $iContentId;
  407. }
  408. function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  409. {
  410. $CNF = &$this->_oModule->_oConfig->CNF;
  411. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  412. $iAuthor = -1;
  413. if(isset($CNF['FIELD_AUTHOR']) && isset($aContentInfo[$CNF['FIELD_AUTHOR']])){
  414. $iAuthor = $aContentInfo[$CNF['FIELD_AUTHOR']];
  415. }
  416. if(isset($CNF['FIELD_AUTHOR']) && isset($CNF['FIELD_ANONYMOUS']) && isset($this->aInputs[$CNF['FIELD_ANONYMOUS']])){
  417. $aValsToAdd[$CNF['FIELD_AUTHOR']] = ($this->getCleanValue($CNF['FIELD_ANONYMOUS']) ? -1 : 1) * abs($aContentInfo[$CNF['FIELD_AUTHOR']]);
  418. $iAuthor = $aValsToAdd[$CNF['FIELD_AUTHOR']];
  419. }
  420. else{
  421. if(isset($this->aInputs[$CNF['FIELD_AUTHOR']]) && empty($this->getCleanValue($CNF['FIELD_AUTHOR']))){
  422. $aValsToAdd[$CNF['FIELD_AUTHOR']] = bx_get_logged_profile_id();
  423. $iAuthor = $aValsToAdd[$CNF['FIELD_AUTHOR']];
  424. }
  425. if(isset($this->aInputs[$CNF['FIELD_AUTHOR']]) && !empty($this->getCleanValue($CNF['FIELD_AUTHOR']))){
  426. $iAuthor = $this->getCleanValue($CNF['FIELD_AUTHOR']);
  427. }
  428. }
  429. if(isset($CNF['FIELD_CHANGED']) && empty($aValsToAdd[$CNF['FIELD_CHANGED']]) && empty($this->getCleanValue($CNF['FIELD_CHANGED'])))
  430. $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
  431. if(CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($iContentId) && isset($CNF['FIELD_THUMB'])) {
  432. $aThumb = bx_process_input (bx_get($CNF['FIELD_THUMB']), BX_DATA_INT);
  433. $aValsToAdd[$CNF['FIELD_THUMB']] = 0;
  434. if (!empty($aThumb) && is_array($aThumb) && ($iFileThumb = array_pop($aThumb)))
  435. $aValsToAdd[$CNF['FIELD_THUMB']] = $iFileThumb;
  436. }
  437. if(!empty($CNF['OBJECT_METATAGS']))
  438. $this->_processMetas($aValsToAdd);
  439. if(isset($CNF['FIELD_STATUS']) && isset($aContentInfo[$CNF['FIELD_STATUS']]) && $aContentInfo[$CNF['FIELD_STATUS']] == 'failed')
  440. $aValsToAdd[$CNF['FIELD_STATUS']] = 'active';
  441. $bMulticatEnabled = $this->_isMulticatEnabled();
  442. if ($bMulticatEnabled)
  443. $this->processMulticatBefore($CNF['FIELD_MULTICAT'], $aValsToAdd);
  444. $mixedResult = parent::update($iContentId, $aValsToAdd, $aTrackTextFieldsChanges);
  445. foreach($this->aInputs as $aInput) {
  446. if ($aInput['type'] == 'nested_form'){
  447. if (is_array($aInput['ghost_template']) && !isset($aInput['ghost_template']['inputs'])) {
  448. foreach ($aInput['ghost_template'] as $oFormNested) {
  449. $aSpecificValues = $oFormNested->getSpecificValues();
  450. $iNestedContentId = $oFormNested->getSubmittedValue($aInput['name'], BX_DOL_FORM_METHOD_SPECIFIC, $aSpecificValues);
  451. if (empty($iNestedContentId)){
  452. $iNestedContentId = $oFormNested->insert(array('content_id' => $iContentId));
  453. if ($aInput['rateable']){
  454. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName(), $iNestedContentId);
  455. }
  456. }
  457. else{
  458. $oFormNested->update($iNestedContentId, array('content_id' => $iContentId));
  459. }
  460. }
  461. }
  462. }
  463. }
  464. if ($bMulticatEnabled)
  465. $this->processMulticatAfter($CNF['FIELD_MULTICAT'], $iContentId);
  466. foreach($this->aInputs as $aInput) {
  467. if (isset($aInput['rateable']) && $aInput['rateable'] && $aInput['type'] != 'nested_form'){
  468. BxDolFormQuery::addFormField($this->id, $aInput['name'], $iContentId, $iAuthor, $this->_oModule->getName());
  469. }
  470. }
  471. if($mixedResult !== false)
  472. $this->_processTrackFields($iContentId);
  473. return $mixedResult;
  474. }
  475. function getHtmlEditorQueryParams($aInput)
  476. {
  477. $aQueryParams = parent::getHtmlEditorQueryParams($aInput);
  478. if (isset($this->MODULE)){
  479. $aQueryParams['m'] = $this->MODULE;
  480. }
  481. if (isset($this->_iContentId) && $this->_iContentId){
  482. $aQueryParams['cid'] = $this->_iContentId;
  483. }
  484. return $aQueryParams;
  485. }
  486. protected function _associalFileWithContent($oStorage, $iFileId, $iProfileId, $iContentId, $sPictureField = '')
  487. {
  488. $oStorage->updateGhostsContentId ($iFileId, $iProfileId, $iContentId, $this->_isAdmin($iContentId));
  489. }
  490. protected function _getPhotoGhostTmplVars($aContentInfo = array())
  491. {
  492. $CNF = &$this->_oModule->_oConfig->CNF;
  493. return array (
  494. 'name' => $this->aInputs[$CNF['FIELD_PHOTO']]['name'],
  495. 'content_id' => (int)$this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
  496. 'editor_id' => isset($CNF['FIELD_TEXT_ID']) ? $CNF['FIELD_TEXT_ID'] : '',
  497. 'thumb_id' => isset($CNF['FIELD_THUMB']) && isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
  498. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  499. 'bx_if:set_thumb' => array (
  500. 'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb($this->aInputs[$CNF['FIELD_PHOTO']]['content_id']),
  501. 'content' => array (
  502. 'name_thumb' => isset($CNF['FIELD_THUMB']) ? $CNF['FIELD_THUMB'] : '',
  503. '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')
  504. ),
  505. ),
  506. );
  507. }
  508. function delete ($iContentId, $aContentInfo = array())
  509. {
  510. $CNF = &$this->_oModule->_oConfig->CNF;
  511. // delete associated files
  512. $a = array('OBJECT_STORAGE', 'OBJECT_STORAGE_FILES', 'OBJECT_STORAGE_PHOTOS', 'OBJECT_STORAGE_VIDEOS', 'OBJECT_STORAGE_SOUNDS');
  513. foreach ($a as $k) {
  514. if (!empty($CNF[$k])) {
  515. $oStorage = BxDolStorage::getObjectInstance($CNF[$k]);
  516. if ($oStorage)
  517. $oStorage->queueFilesForDeletionFromGhosts($aContentInfo[$CNF['FIELD_AUTHOR']], $iContentId);
  518. }
  519. }
  520. // delete associated objects data
  521. if (!empty($CNF['OBJECT_VIEWS'])) {
  522. $o = BxDolView::getObjectInstance($CNF['OBJECT_VIEWS'], $iContentId);
  523. if ($o) $o->onObjectDelete();
  524. }
  525. if (!empty($CNF['OBJECT_VOTES'])) {
  526. $o = BxDolVote::getObjectInstance($CNF['OBJECT_VOTES'], $iContentId);
  527. if ($o) $o->onObjectDelete();
  528. }
  529. if (!empty($CNF['OBJECT_SCORES'])) {
  530. $o = BxDolScore::getObjectInstance($CNF['OBJECT_SCORES'], $iContentId);
  531. if ($o) $o->onObjectDelete();
  532. }
  533. if (!empty($CNF['OBJECT_COMMENTS'])) {
  534. $o = BxDolCmts::getObjectInstance($CNF['OBJECT_COMMENTS'], $iContentId);
  535. if ($o) $o->onObjectDelete();
  536. }
  537. if (!empty($CNF['OBJECT_METATAGS'])) {
  538. $o = BxDolMetatags::getObjectInstance($CNF['OBJECT_METATAGS']);
  539. if ($o) $o->onDeleteContent($iContentId);
  540. }
  541. // delete SEO links
  542. BxDolPage::deleteSeoLink ($this->_oModule->getName(), $this->_oModule->getName(), $iContentId);
  543. // delete db record
  544. BxDolFormQuery::removeFormField($this->id, $iContentId);
  545. return parent::delete($iContentId);
  546. }
  547. protected function _isAdmin ($iContentId = 0)
  548. {
  549. return $this->_oModule->_isModerator();
  550. }
  551. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  552. {
  553. $CNF = &$this->_oModule->_oConfig->CNF;
  554. if (!isset($this->aInputs[$sFieldFile]))
  555. return true;
  556. $mixedFileIds = $this->getCleanValue($sFieldFile);
  557. if(!$mixedFileIds)
  558. return true;
  559. $oStorage = BxDolStorage::getObjectInstance($this->aInputs[$sFieldFile]['storage_object']);
  560. if (!$oStorage)
  561. return false;
  562. $iProfileId = $this->getContentOwnerProfileId($iContentId);
  563. $aGhostFiles = $oStorage->getGhosts ($iProfileId, $isAssociateWithContent ? 0 : $iContentId, true, $this->_isAdmin($iContentId));
  564. if (!$aGhostFiles)
  565. return true;
  566. foreach ($aGhostFiles as $aFile) {
  567. if (is_array($mixedFileIds) && !in_array($aFile['id'], $mixedFileIds))
  568. continue;
  569. if ($aFile['private'])
  570. $oStorage->setFilePrivate ($aFile['id'], 1);
  571. if ($iContentId)
  572. $this->_associalFileWithContent($oStorage, $aFile['id'], $iProfileId, $iContentId, $sFieldFile);
  573. }
  574. return true;
  575. }
  576. function _deleteFile ($iFileId, $sStorage = '')
  577. {
  578. $CNF = &$this->_oModule->_oConfig->CNF;
  579. if (!$iFileId)
  580. return true;
  581. $sStorage = !empty($sStorage) ? $sStorage : $CNF['OBJECT_STORAGE'];
  582. $oStorage = BxDolStorage::getObjectInstance($sStorage);
  583. if (!$oStorage)
  584. return false;
  585. if (!$oStorage->getFile($iFileId))
  586. return true;
  587. $iProfileId = bx_get_logged_profile_id();
  588. return $oStorage->deleteFile($iFileId, $iProfileId);
  589. }
  590. function addCssJs ()
  591. {
  592. if ((!isset($this->aParams['view_mode']) || !$this->aParams['view_mode']) && !self::$_isCssJsGeneralModuleAdded) {
  593. $this->_oModule->_oTemplate->addCss('forms.css');
  594. $this->_oModule->_oTemplate->addJs('modules/base/general/js/|forms.js');
  595. self::$_isCssJsGeneralModuleAdded = true;
  596. }
  597. return parent::addCssJs ();
  598. }
  599. function genViewRowValue(&$aInput)
  600. {
  601. $s = parent::genViewRowValue($aInput);
  602. if ($this->_oMetatagsObject && in_array($aInput['name'], $this->_aMetatagsFieldsWithKeywords) && $s)
  603. $s = $this->_oMetatagsObject->metaParse($this->_oMetatagsContentId, $s);
  604. return $s;
  605. }
  606. function genViewRowWrapped(&$aInput)
  607. {
  608. $sResult = parent::genViewRowWrapped($aInput);
  609. if (!$aInput['rateable']){
  610. return $sResult;
  611. }
  612. $sValue = $this->genViewRowValue($aInput);
  613. if (null === $sValue)
  614. return '';
  615. // process rateable fields
  616. $iId = BxDolFormQuery::getFormField($this->id, $aInput['name'], $this->_iContentId);
  617. $sVote = '';
  618. $oVote = BxDolVote::getObjectInstance($aInput['rateable'], $iId, true, BxDolTemplate::getInstance());
  619. if ($oVote){
  620. $sVote = $oVote->getCounter(array('show_counter_empty' => true, 'show_counter' => true, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode));
  621. $sVoteBtn = $oVote->getElementInline(array('show_counter_empty' => true, 'show_counter' => false, 'show_counter_style' => 'simple', 'dynamic_mode' => $this->_bDynamicMode));
  622. return $this->oTemplate->parseHtmlByName('form_view_rateable_row.html', array(
  623. 'value' => $sResult,
  624. 'rate' => $sVote,
  625. 'rate_btn' => $sVoteBtn
  626. ));
  627. }
  628. return $sResult;
  629. }
  630. function setMetatagsKeywordsData($iId, $a, $o)
  631. {
  632. $this->_oMetatagsContentId = $iId;
  633. $this->_aMetatagsFieldsWithKeywords = $a;
  634. $this->_oMetatagsObject = $o;
  635. }
  636. function getContentOwnerProfileId ($iContentId)
  637. {
  638. return $this->_oModule->serviceGetContentOwnerProfileId($iContentId);
  639. }
  640. protected function _processTrackFields($mixedContent)
  641. {
  642. if(empty($mixedContent) || empty($this->_aTrackFieldsChanges))
  643. return;
  644. if(!is_array($mixedContent)) {
  645. $mixedContent = $this->_oModule->_oDb->getContentInfoById((int)$mixedContent);
  646. if(empty($mixedContent) || !is_array($mixedContent))
  647. return;
  648. }
  649. foreach($this->_aTrackFieldsChanges as $sField => $aValues)
  650. if($mixedContent[$sField] == $aValues['old'])
  651. $this->_aTrackFieldsChanges[$sField] = false;
  652. else
  653. $this->_aTrackFieldsChanges[$sField]['new'] = $mixedContent[$sField];
  654. }
  655. protected function _processMetas(&$aValsToAdd)
  656. {
  657. $CNF = &$this->_oModule->_oConfig->CNF;
  658. $sKey = 'FIELD_LABELS';
  659. if(isset($CNF[$sKey]) && isset($this->aInputs[$CNF[$sKey]]) && empty($aValsToAdd[$CNF[$sKey]])) {
  660. $aLabels = $this->getCleanValue($CNF[$sKey]);
  661. if(!empty($aLabels) && is_array($aLabels))
  662. $aValsToAdd[$CNF[$sKey]] = serialize($aLabels);
  663. else
  664. $aValsToAdd[$CNF[$sKey]] = '';
  665. }
  666. $sKey1 = 'FIELD_LOCATION';
  667. $sKey2 = 'FIELD_LOCATION_PREFIX';
  668. if(isset($CNF[$sKey1]) && isset($CNF[$sKey2]) && isset($this->aInputs[$CNF[$sKey1]]) && empty($aValsToAdd[$CNF[$sKey1]])) {
  669. $aLocation = BxDolMetatags::locationsRetrieveFromForm($CNF[$sKey2], $this);
  670. if(!empty($aLocation) && is_array($aLocation))
  671. $aValsToAdd[$CNF[$sKey1]] = serialize(BxDolMetatags::locationsParseComponents($aLocation));
  672. }
  673. }
  674. protected function _getPrivacyFields($aKeysF2O = array())
  675. {
  676. $CNF = &$this->_oModule->_oConfig->CNF;
  677. if(isset($CNF['PRIVACY_FIELD_TO_OBJECT']) && is_array($CNF['PRIVACY_FIELD_TO_OBJECT']))
  678. return $CNF['PRIVACY_FIELD_TO_OBJECT'];
  679. if(empty($aKeysF2O))
  680. $aKeysF2O = array('FIELD_ALLOW_VIEW_TO' => 'OBJECT_PRIVACY_VIEW');
  681. $aResult = array();
  682. foreach($aKeysF2O as $sKeyField => $sKeyObject) {
  683. if(!isset($CNF[$sKeyField]) || !isset($this->aInputs[$CNF[$sKeyField]]) || !isset($CNF[$sKeyObject]))
  684. continue;
  685. $aResult[$CNF[$sKeyField]] = $CNF[$sKeyObject];
  686. }
  687. return $aResult;
  688. }
  689. function _addCssJsPrivacyField($sField, $sPrivacyObject, $bDynamicMode = false)
  690. {
  691. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  692. if(!$oPrivacy)
  693. return;
  694. $this->aInputs[$sField]['content'] = $oPrivacy->addCssJs($bDynamicMode) . $this->aInputs[$sField]['content'];
  695. }
  696. protected function _preparePrivacyField($sField, $sPrivacyObject)
  697. {
  698. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  699. if(!$oPrivacy)
  700. return;
  701. $aSave = array('db' => array('pass' => 'Xss'));
  702. array_walk($this->aInputs[$sField], function ($a, $k, $aSave) {
  703. if (in_array($k, array('info', 'caption', 'value')))
  704. $aSave[0][$k] = $a;
  705. }, array(&$aSave));
  706. $aGroupChooser = $oPrivacy->getGroupChooser($sPrivacyObject, 0, array(
  707. 'object' => $this->aParams['object'],
  708. 'display' => $this->aParams['display']
  709. ));
  710. $this->aInputs[$sField] = array_merge($this->aInputs[$sField], $aGroupChooser, $aSave);
  711. }
  712. protected function _preloadPrivacyField($sField, $sPrivacyObject, $aValues)
  713. {
  714. $CNF = &$this->_oModule->_oConfig->CNF;
  715. $oPrivacy = BxDolPrivacy::getObjectInstance($sPrivacyObject);
  716. if(!$oPrivacy)
  717. return;
  718. $iContentId = !empty($aValues[$CNF['FIELD_ID']]) ? (int)$aValues[$CNF['FIELD_ID']] : 0;
  719. $iProfileId = !empty($iContentId) ? (int)$this->getContentOwnerProfileId($iContentId) : bx_get_logged_profile_id();
  720. $iGroupId = !empty($aValues[$sField]) ? $aValues[$sField] : 0;
  721. if(!isset($this->aInputs[$sField]['content']))
  722. $this->aInputs[$sField]['content'] = '';
  723. $this->aInputs[$sField]['content'] .= $oPrivacy->initGroupChooser($sPrivacyObject, $iProfileId, array(
  724. 'content_id' => $iContentId,
  725. 'group_id' => $iGroupId,
  726. 'html_ids' => array(
  727. 'form' => $this->getId()
  728. )
  729. ));
  730. }
  731. protected function _validatePrivacyField($sField, $sPrivacyObject, $aValues)
  732. {
  733. $mixedValue = $this->aInputs[$sField]['value'];
  734. $bValue = false;
  735. foreach($this->aInputs[$sField]['values'] as $aValue)
  736. if(isset($aValue['key']) && $aValue['key'] == $mixedValue) {
  737. $bValue = true;
  738. break;
  739. }
  740. if(!$bValue) {
  741. $sTitle = '';
  742. if(is_numeric($mixedValue) && (int)$mixedValue < 0 && ($oContext = BxDolProfile::getInstance(abs((int)$mixedValue))) !== false)
  743. $sTitle = $oContext->getDisplayName();
  744. else
  745. $sTitle = _t('_sys_ps_group_title_unknown');
  746. $this->aInputs[$sField]['values'] = array_merge(array(
  747. array('key' => $mixedValue, 'value' => $sTitle),
  748. array('key' => '', 'value' => '----', 'attrs' => array('disabled' => 'disabled'))
  749. ), $this->aInputs[$sField]['values']);
  750. }
  751. }
  752. /**
  753. *
  754. * MultiCategories related methods.
  755. *
  756. */
  757. protected function processMulticatBefore($sFieldName, &$aValsToAdd){
  758. if (isset($this->aInputs[$sFieldName])){
  759. $this->aInputs[$sFieldName]['value'] = array_unique(array_filter($this->aInputs[$sFieldName]['value'], function($sTmp){
  760. return trim($sTmp);
  761. }));
  762. $aValsToAdd[$sFieldName] = implode(',', $this->aInputs[$sFieldName]['value']);
  763. }
  764. }
  765. protected function processMulticatAfter($sFieldName, $iContentId){
  766. $CNF = &$this->_oModule->_oConfig->CNF;
  767. $bAutoActivation = (isset($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) && getParam($CNF['PARAM_MULTICAT_AUTO_ACTIVATION_FOR_CATEGORIES']) == 'on') ? true : false;
  768. $oCategories = BxDolCategories::getInstance();
  769. if (isset($this->aInputs[$sFieldName])){
  770. $oCategories->delete($this->_oModule->getName(), $iContentId);
  771. foreach($this->aInputs[$sFieldName]['value'] as $sValue) {
  772. $oCategories->add($this->_oModule->getName(), bx_get_logged_profile_id(), $sValue, $iContentId, $bAutoActivation);
  773. }
  774. }
  775. }
  776. protected function genCustomViewRowValueMulticat(&$aInput)
  777. {
  778. $oCategories = BxDolCategories::getInstance();
  779. $aValues = $oCategories->getData(array('type' => 'by_module_and_object', 'module' => $this->_oModule->getName(), 'object_id' => (!empty($aInput['content_id']) ? (int)$aInput['content_id'] : 0 )));
  780. if (count($aValues) > 0){
  781. $aVars = array('bx_repeat:cats' => array());
  782. foreach ($aValues as $sValue) {
  783. $aVars['bx_repeat:cats'][] = array(
  784. 'url' => $oCategories->getUrl($this->_oModule->getName(), $sValue),
  785. 'name' => _t($sValue),
  786. 'bx_if:more' => array(
  787. 'condition' => $sValue === end($aValues) ? false : true,
  788. 'content' => array('1')
  789. ),
  790. );
  791. }
  792. if (!$aVars['bx_repeat:cats'])
  793. return '';
  794. return $this->_oModule->_oTemplate->parseHtmlByName('category_list_inline.html', $aVars);
  795. }
  796. return '';
  797. }
  798. protected function genCustomInputMulticat(&$aInput)
  799. {
  800. $sJsObject = $this->_oModule->_oConfig->getJsObject('categories');
  801. $aValuesForSelect = BxDolCategories::getInstance()->getData(array('type' => 'by_module_and_author', 'module' => $this->_oModule->getName(), 'author' => bx_get_logged_profile_id()));
  802. $aSelectedItems = array();
  803. if (isset($aInput['value']) && is_array($aInput['value']))
  804. $aInput['value'] = array_filter($aInput['value']);
  805. if(!empty($aInput['value'])) {
  806. if (!is_array($aInput['value']))
  807. $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 )));
  808. else
  809. $aValues = $aInput['value'];
  810. $aValues = array_filter($aValues);
  811. foreach($aValues as $sValue) {
  812. if (!array_key_exists($sValue, $aValuesForSelect)){
  813. $aValuesForSelect[$sValue] = array('key' => $sValue, 'value' => $sValue);
  814. }
  815. }
  816. foreach($aValues as $sValue) {
  817. $sInput = $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, $sValue);
  818. $aSelectedItems[] = array('js_object' => $sJsObject, 'select_cat' => $sInput);
  819. }
  820. }
  821. else{
  822. $aSelectedItems = array(
  823. array('js_object' => $sJsObject, 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect))
  824. );
  825. }
  826. return $this->_oModule->_oTemplate->parseHtmlByName('form_categories.html', array(
  827. 'bx_repeat:items' => $aSelectedItems,
  828. 'js_object' => $sJsObject,
  829. 'select_cat' => $this->genCustomInputMulticatSelect($aInput, $aValuesForSelect, -1),
  830. 'input_cat' => $this->genCustomInputMulticatInput($aInput),
  831. 'js_object' => $sJsObject,
  832. 'btn_add' => $this->genCustomInputMulticatButton($aInput),
  833. 'btn_add_new' => $this->genCustomInputMulticatButtonNew($aInput)
  834. ));
  835. }
  836. protected function genCustomInputMulticatSelect($aInput, $aValues, $mixedValue = '')
  837. {
  838. foreach($aValues as $sKey => $aValue) {
  839. $aValues[$sKey]['value'] = _t($aValue['key']);
  840. }
  841. $aValues = array_merge(array('1' => array('key' => false, 'value' => _t('_sys_please_select'))), $aValues);
  842. $aInput['type'] = 'select';
  843. $aInput['name'] .= '[]';
  844. $aInput['value'] = $mixedValue;
  845. $aInput['values'] = $aValues;
  846. return $this->genInput($aInput);
  847. }
  848. protected function genCustomInputMulticatInput($aInput)
  849. {
  850. $aInput['type'] = 'text';
  851. $aInput['name'] .= '[]';
  852. $aInput['value'] = '';
  853. return $this->genInput($aInput);
  854. }
  855. protected function genCustomInputMulticatButton($aInput)
  856. {
  857. $sName = $aInput['name'];
  858. $aInput['type'] = 'button';
  859. $aInput['name'] .= '_add';
  860. $aInput['value'] = _t('_sys_txt_categories_button_caption_add');
  861. $aInput['attrs']['class'] = 'bx-def-margin-right bx-def-margin-sec-top';
  862. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAdd(this, '" . $sName . "');";
  863. return $this->genInputButton($aInput);
  864. }
  865. protected function genCustomInputMulticatButtonNew($aInput)
  866. {
  867. $sName = $aInput['name'];
  868. $aInput['type'] = 'button';
  869. $aInput['name'] .= '_add';
  870. $aInput['value'] = _t('_sys_txt_categories_button_caption_addnew');
  871. $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
  872. $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('categories') . ".categoryAddNew(this, '" . $sName . "');";
  873. return $this->genInputButton($aInput);
  874. }
  875. protected function genCustomInputAuthor ($aInput)
  876. {
  877. if(empty($aInput['custom']) || !is_array($aInput['custom']))
  878. $aInput['custom'] = array();
  879. $aInput['custom']['only_once'] = 1;
  880. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . "modules/?r=" . $this->_oModule->_oConfig->getUri() . "/ajax_get_profiles";
  881. return $this->genCustomInputUsernamesSuggestions($aInput);
  882. }
  883. protected function _isMulticatEnabled(){
  884. $CNF = $this->_oModule->_oConfig->CNF;
  885. return isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true && isset($CNF['FIELD_MULTICAT']);
  886. }
  887. }
  888. /** @} */