BxBaseModGeneralFormEntry.php 38 KB

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