BxBasePrivacy.php 8.7 KB

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