BxBaseModGeneralFormEntry.php 44 KB

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