BxBasePrivacy.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * Privacy representation.
  11. * @see BxDolPrivacy
  12. */
  13. class BxBasePrivacy extends BxDolPrivacy
  14. {
  15. protected $_oTemplate;
  16. protected $_sJsObjClass;
  17. protected $_sJsObjName;
  18. public function __construct ($aOptions, $oTemplate)
  19. {
  20. parent::__construct ($aOptions);
  21. if ($oTemplate)
  22. $this->_oTemplate = $oTemplate;
  23. else
  24. $this->_oTemplate = BxDolTemplate::getInstance();
  25. $this->_sJsObjClass = 'BxDolPrivacy';
  26. $this->_sJsObjName = 'oBxDolPrivacy' . bx_gen_method_name($this->_sObject);
  27. $sHtmlId = str_replace(array('_' , ' '), array('-', '-'), $this->_sObject);
  28. $this->_aHtmlIds = array(
  29. 'group_element' => 'bx-form-element-' . $this->convertActionToField($this->_aObject['action']),
  30. 'group_custom_element' => 'sys-pgc-' . $sHtmlId,
  31. 'group_custom_select_popup' => 'sys-privacy-gcsp-' . $sHtmlId . '-'
  32. );
  33. }
  34. public function getJsObjectName()
  35. {
  36. return $this->_sJsObjName;
  37. }
  38. /**
  39. * Get initialization section of comments box
  40. *
  41. * @return string
  42. */
  43. public function getJsScript($sCodeAdd = '', $bDynamicMode = false)
  44. {
  45. $sCode = $this->_sJsObjName . " = new " . $this->_sJsObjClass . "(" . json_encode(array(
  46. 'sObject' => $this->_sObject,
  47. 'sObjName' => $this->_sJsObjName,
  48. 'sRootUrl' => BX_DOL_URL_ROOT,
  49. 'aGroupSettings' => $this->_aGroupsSettings,
  50. 'aHtmlIds' => $this->_aHtmlIds
  51. )) . ");" . $sCodeAdd;
  52. if($bDynamicMode) {
  53. $sCode = "var " . $this->_sJsObjName . " = null;
  54. if(typeof(jQuery.ui.position) == 'undefined')
  55. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('jquery-ui/jquery-ui.min.js'), BX_ESCAPE_STR_APOS) . "');
  56. if(window['" . $this->_sJsObjName . "'] === null)
  57. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('BxDolPrivacy.js'), BX_ESCAPE_STR_APOS) . "', function(data, textStatus, jqxhr) {
  58. " . $sCode . "
  59. });";
  60. }
  61. else
  62. $sCode = "if(window['" . $this->_sJsObjName . "'] == undefined) var " . $sCode;
  63. return $this->addCssJs($bDynamicMode) . $this->_oTemplate->_wrapInTagJsCode($sCode);
  64. }
  65. public function addCssJs($bDynamicMode = false)
  66. {
  67. $sInclude = '';
  68. if(!$bDynamicMode)
  69. $this->_oTemplate->addJs(array(
  70. 'jquery-ui/jquery-ui.min.js',
  71. 'BxDolPrivacy.js'
  72. ));
  73. $sInclude .= $this->_oTemplate->addCss(array(
  74. 'forms.css',
  75. 'privacy.css'
  76. ), $bDynamicMode);
  77. return $bDynamicMode ? $sInclude : '';
  78. }
  79. protected function getSelectGroup($aValues = array(), $aParams = array())
  80. {
  81. $sJsObject = $this->getJsObjectName();
  82. $oForm = BxDolForm::getObjectInstance($this->_sFormGroupCustom, $this->_sFormDisplayGcMembers);
  83. $oForm->initChecker($aValues);
  84. if($oForm->isSubmittedAndValid()) {
  85. $aMembers = array();
  86. if(($aMembersSearch = $oForm->getCleanValue('search')) !== false)
  87. $aMembers = array_merge($aMembers, $aMembersSearch);
  88. if(($aMembersList = $oForm->getCleanValue('list')) !== false)
  89. $aMembers = array_merge($aMembers, $aMembersList);
  90. $aMembers = array_unique($aMembers);
  91. $iGroupCustomId = $oForm->getGroupCustomId();
  92. if(!$iGroupCustomId) {
  93. if(empty($aMembers) || !is_array($aMembers))
  94. return array();
  95. $this->deleteGroupCustom(array(
  96. 'profile_id' => $oForm->getCleanValue('profile_id'),
  97. 'content_id' => $oForm->getCleanValue('content_id'),
  98. 'object' => $oForm->getCleanValue('object')
  99. ));
  100. $iGroupCustomId = $oForm->insert();
  101. foreach($aMembers as $iMemberId)
  102. $this->_oDb->insertGroupCustomMember(array('group_id' => $iGroupCustomId, 'member_id' => $iMemberId));
  103. }
  104. else {
  105. $this->_oDb->deleteGroupCustomMember(array('group_id' => $iGroupCustomId));
  106. foreach($aMembers as $iMemberId)
  107. $this->_oDb->insertGroupCustomMember(array('group_id' => $iGroupCustomId, 'member_id' => $iMemberId));
  108. }
  109. return array('eval' => $sJsObject . '.onSelectUsers(oData);', 'content' => $oForm->getElementGroupCustom(array(
  110. 'value' => $iGroupCustomId,
  111. 'value_items' => $aMembers
  112. )));
  113. }
  114. $iGroupId = $oForm->getGroupId();
  115. $sContent = BxTemplFunctions::getInstance()->transBox($this->_aHtmlIds['group_custom_select_popup'] . $iGroupId, $this->_oTemplate->parseHtmlByName('privacy_group_custom_select_popup.html', array(
  116. 'js_object' => $sJsObject,
  117. 'group' => $iGroupId,
  118. 'form' => $oForm->getCode(),
  119. 'form_id' => $oForm->getId()
  120. )));
  121. $aResult = array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false)));
  122. if(isset($aParams['popup_only']) && (bool)$aParams['popup_only'] === true)
  123. return $aResult;
  124. return array_merge($aResult, array(
  125. 'eval' => $sJsObject . '.onSelectGroup(oData);',
  126. 'content' => $oForm->getElementGroupCustom()
  127. ));
  128. }
  129. protected function getSelectMemberships($aValues = array(), $aParams = array())
  130. {
  131. $sJsObject = $this->getJsObjectName();
  132. $oForm = BxDolForm::getObjectInstance($this->_sFormGroupCustom, $this->_sFormDisplayGcMemberships);
  133. $oForm->initChecker($aValues);
  134. if($oForm->isSubmittedAndValid()) {
  135. $aMemberships = $oForm->getCleanValue('memberships');
  136. $iGroupCustomId = $oForm->getGroupCustomId();
  137. if(!$iGroupCustomId) {
  138. if(empty($aMemberships) || !is_array($aMemberships))
  139. return array();
  140. $this->deleteGroupCustom(array(
  141. 'profile_id' => $oForm->getCleanValue('profile_id'),
  142. 'content_id' => $oForm->getCleanValue('content_id'),
  143. 'object' => $oForm->getCleanValue('object')
  144. ));
  145. $iGroupCustomId = $oForm->insert();
  146. foreach($aMemberships as $iMembershipId)
  147. $this->_oDb->insertGroupCustomMembership(array('group_id' => $iGroupCustomId, 'membership_id' => $iMembershipId));
  148. }
  149. else {
  150. $this->_oDb->deleteGroupCustomMembership(array('group_id' => $iGroupCustomId));
  151. if(!empty($aMemberships) && is_array($aMemberships))
  152. foreach($aMemberships as $iMembershipId)
  153. $this->_oDb->insertGroupCustomMembership(array('group_id' => $iGroupCustomId, 'membership_id' => $iMembershipId));
  154. }
  155. return array('eval' => $sJsObject . '.onSelectMemberships(oData);', 'content' => $oForm->getElementGroupCustom(array(
  156. 'value' => $iGroupCustomId,
  157. 'value_items' => $aMemberships
  158. )));
  159. }
  160. $iGroupId = $oForm->getGroupId();
  161. $sContent = BxTemplFunctions::getInstance()->transBox($this->_aHtmlIds['group_custom_select_popup'] . $iGroupId, $this->_oTemplate->parseHtmlByName('privacy_group_custom_select_popup.html', array(
  162. 'js_object' => $sJsObject,
  163. 'group' => $iGroupId,
  164. 'form' => $oForm->getCode(),
  165. 'form_id' => $oForm->getId()
  166. )));
  167. $aResult = array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false)));
  168. if(isset($aParams['popup_only']) && (bool)$aParams['popup_only'] === true)
  169. return $aResult;
  170. return array_merge($aResult, array(
  171. 'eval' => $sJsObject . '.onSelectGroup(oData);',
  172. 'content' => $oForm->getElementGroupCustom()
  173. ));
  174. }
  175. protected function getLoadGroupCustom($iProfileId, $iContentId, $iGroupId, $aHtmlIds)
  176. {
  177. return $this->_sJsObjName . ".loadGroupCustom(" . json_encode(array(
  178. 'iProfileId' => $iProfileId,
  179. 'iContentId' => $iContentId,
  180. 'iGroupId' => $iGroupId,
  181. 'aHtmlIds' => $aHtmlIds
  182. )) . ");";
  183. }
  184. }
  185. /** @} */