BxBaseModGeneralFormEntry.php 46 KB

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