BxBaseModGeneralFormEntry.php 45 KB

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