BxBaseModGeneralFormEntry.php 44 KB

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