BxBasePrivacy.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.core) == 'undefined')
  55. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('jquery-ui/jquery.ui.core.min.js'), BX_ESCAPE_STR_APOS) . "');
  56. if(typeof(jQuery.ui.widget) == 'undefined')
  57. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('jquery-ui/jquery.ui.widget.min.js'), BX_ESCAPE_STR_APOS) . "');
  58. if(typeof(jQuery.ui.menu) == 'undefined')
  59. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('jquery-ui/jquery.ui.menu.min.js'), BX_ESCAPE_STR_APOS) . "');
  60. if(typeof(jQuery.ui.autocomplete) == 'undefined')
  61. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('jquery-ui/jquery.ui.autocomplete.min.js'), BX_ESCAPE_STR_APOS) . "');
  62. if(window['" . $this->_sJsObjName . "'] === null)
  63. $.getScript('" . bx_js_string($this->_oTemplate->getJsUrl('BxDolPrivacy.js'), BX_ESCAPE_STR_APOS) . "', function(data, textStatus, jqxhr) {
  64. " . $sCode . "
  65. });";
  66. }
  67. else
  68. $sCode = "if(window['" . $this->_sJsObjName . "'] == undefined) var " . $sCode;
  69. return $this->addCssJs($bDynamicMode) . $this->_oTemplate->_wrapInTagJsCode($sCode);
  70. }
  71. public function addCssJs($bDynamicMode = false)
  72. {
  73. $sInclude = '';
  74. if(!$bDynamicMode)
  75. $this->_oTemplate->addJs(array(
  76. 'jquery-ui/jquery.ui.core.min.js',
  77. 'jquery-ui/jquery.ui.widget.min.js',
  78. 'jquery-ui/jquery.ui.menu.min.js',
  79. 'jquery-ui/jquery.ui.autocomplete.min.js',
  80. 'BxDolPrivacy.js'
  81. ));
  82. $sInclude .= $this->_oTemplate->addCss(array(
  83. 'forms.css',
  84. 'privacy.css'
  85. ), $bDynamicMode);
  86. return $bDynamicMode ? $sInclude : '';
  87. }
  88. protected function getSelectGroup($aValues = array(), $aParams = array())
  89. {
  90. $sJsObject = $this->getJsObjectName();
  91. $oForm = BxDolForm::getObjectInstance($this->_sFormGroupCustom, $this->_sFormDisplayGcMembers);
  92. $oForm->initChecker($aValues);
  93. if($oForm->isSubmittedAndValid()) {
  94. $aMembers = array();
  95. if(($aMembersSearch = $oForm->getCleanValue('search')) !== false)
  96. $aMembers = array_merge($aMembers, $aMembersSearch);
  97. if(($aMembersList = $oForm->getCleanValue('list')) !== false)
  98. $aMembers = array_merge($aMembers, $aMembersList);
  99. $aMembers = array_unique($aMembers);
  100. $iGroupCustomId = $oForm->getGroupCustomId();
  101. if(!$iGroupCustomId) {
  102. if(empty($aMembers) || !is_array($aMembers))
  103. return array();
  104. $this->deleteGroupCustom(array(
  105. 'profile_id' => $oForm->getCleanValue('profile_id'),
  106. 'content_id' => $oForm->getCleanValue('content_id'),
  107. 'object' => $oForm->getCleanValue('object')
  108. ));
  109. $iGroupCustomId = $oForm->insert();
  110. foreach($aMembers as $iMemberId)
  111. $this->_oDb->insertGroupCustomMember(array('group_id' => $iGroupCustomId, 'member_id' => $iMemberId));
  112. }
  113. else {
  114. $this->_oDb->deleteGroupCustomMember(array('group_id' => $iGroupCustomId));
  115. foreach($aMembers as $iMemberId)
  116. $this->_oDb->insertGroupCustomMember(array('group_id' => $iGroupCustomId, 'member_id' => $iMemberId));
  117. }
  118. return array('eval' => $sJsObject . '.onSelectUsers(oData);', 'content' => $oForm->getElementGroupCustom(array(
  119. 'value' => $iGroupCustomId,
  120. 'value_items' => $aMembers
  121. )));
  122. }
  123. $iGroupId = $oForm->getGroupId();
  124. $sContent = BxTemplFunctions::getInstance()->transBox($this->_aHtmlIds['group_custom_select_popup'] . $iGroupId, $this->_oTemplate->parseHtmlByName('privacy_group_custom_select_popup.html', array(
  125. 'js_object' => $sJsObject,
  126. 'group' => $iGroupId,
  127. 'form' => $oForm->getCode(),
  128. 'form_id' => $oForm->getId()
  129. )));
  130. $aResult = array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false)));
  131. if(isset($aParams['popup_only']) && (bool)$aParams['popup_only'] === true)
  132. return $aResult;
  133. return array_merge($aResult, array(
  134. 'eval' => $sJsObject . '.onSelectGroup(oData);',
  135. 'content' => $oForm->getElementGroupCustom()
  136. ));
  137. }
  138. protected function getSelectMemberships($aValues = array(), $aParams = array())
  139. {
  140. $sJsObject = $this->getJsObjectName();
  141. $oForm = BxDolForm::getObjectInstance($this->_sFormGroupCustom, $this->_sFormDisplayGcMemberships);
  142. $oForm->initChecker($aValues);
  143. if($oForm->isSubmittedAndValid()) {
  144. $aMemberships = $oForm->getCleanValue('memberships');
  145. $iGroupCustomId = $oForm->getGroupCustomId();
  146. if(!$iGroupCustomId) {
  147. if(empty($aMemberships) || !is_array($aMemberships))
  148. return array();
  149. $this->deleteGroupCustom(array(
  150. 'profile_id' => $oForm->getCleanValue('profile_id'),
  151. 'content_id' => $oForm->getCleanValue('content_id'),
  152. 'object' => $oForm->getCleanValue('object')
  153. ));
  154. $iGroupCustomId = $oForm->insert();
  155. foreach($aMemberships as $iMembershipId)
  156. $this->_oDb->insertGroupCustomMembership(array('group_id' => $iGroupCustomId, 'membership_id' => $iMembershipId));
  157. }
  158. else {
  159. $this->_oDb->deleteGroupCustomMembership(array('group_id' => $iGroupCustomId));
  160. foreach($aMemberships as $iMembershipId)
  161. $this->_oDb->insertGroupCustomMembership(array('group_id' => $iGroupCustomId, 'membership_id' => $iMembershipId));
  162. }
  163. return array('eval' => $sJsObject . '.onSelectMemberships(oData);', 'content' => $oForm->getElementGroupCustom(array(
  164. 'value' => $iGroupCustomId,
  165. 'value_items' => $aMemberships
  166. )));
  167. }
  168. $iGroupId = $oForm->getGroupId();
  169. $sContent = BxTemplFunctions::getInstance()->transBox($this->_aHtmlIds['group_custom_select_popup'] . $iGroupId, $this->_oTemplate->parseHtmlByName('privacy_group_custom_select_popup.html', array(
  170. 'js_object' => $sJsObject,
  171. 'group' => $iGroupId,
  172. 'form' => $oForm->getCode(),
  173. 'form_id' => $oForm->getId()
  174. )));
  175. $aResult = array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false)));
  176. if(isset($aParams['popup_only']) && (bool)$aParams['popup_only'] === true)
  177. return $aResult;
  178. return array_merge($aResult, array(
  179. 'eval' => $sJsObject . '.onSelectGroup(oData);',
  180. 'content' => $oForm->getElementGroupCustom()
  181. ));
  182. }
  183. protected function getLoadGroupCustom($iProfileId, $iContentId, $iGroupId, $aHtmlIds)
  184. {
  185. return $this->_sJsObjName . ".loadGroupCustom(" . json_encode(array(
  186. 'iProfileId' => $iProfileId,
  187. 'iContentId' => $iContentId,
  188. 'iGroupId' => $iGroupId,
  189. 'aHtmlIds' => $aHtmlIds
  190. )) . ");";
  191. }
  192. }
  193. /** @} */