BxBasePrivacyFormGroupCustom.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. class BxBasePrivacyFormGroupCustom extends BxTemplFormView
  10. {
  11. protected $_iProfileId;
  12. protected $_iContentId;
  13. protected $_sObject;
  14. protected $_oObject;
  15. protected $_iGroupId;
  16. protected $_aGroupSettings;
  17. protected $_iGroupCustomId;
  18. protected $_aGroupCustomInfo;
  19. public function __construct($aInfo, $oTemplate)
  20. {
  21. parent::__construct($aInfo, $oTemplate);
  22. $this->_iProfileId = bx_get_logged_profile_id();
  23. $this->_iContentId = 0;
  24. $this->_sObject = '';
  25. $this->_oObject = null;
  26. $this->_iGroupId = 0;
  27. $this->_aGroupSettings = array();
  28. $this->_iGroupCustomId = 0;
  29. $this->_aGroupCustomInfo = array();
  30. switch($this->aParams['display']) {
  31. case 'sys_privacy_group_custom_members':
  32. $this->aInputs['action']['value'] = 'select_members';
  33. break;
  34. case 'sys_privacy_group_custom_memberships':
  35. $this->aInputs['action']['value'] = 'select_memberships';
  36. break;
  37. }
  38. }
  39. public function initChecker($aValues = array(), $aSpecificValues = array())
  40. {
  41. $this->initData($aValues);
  42. if(isset($this->aInputs['memberships']) && $this->_oObject->isAllowedMemberships($this->_iProfileId)) {
  43. $aSelected = array();
  44. if(!empty($this->_aGroupCustomInfo) && is_array($this->_aGroupCustomInfo))
  45. $this->aInputs['memberships']['value'] = $this->_aGroupCustomInfo['items'];
  46. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true, true, true);
  47. if(!empty($aLevels) && is_array($aLevels)) {
  48. $aLevelsValues = [];
  49. foreach($aLevels as $iId => $sTitle)
  50. $aLevelsValues[] = array('key' => $iId, 'value' => $sTitle);
  51. $this->aInputs['memberships']['values'] = $aLevelsValues;
  52. }
  53. }
  54. return parent::initChecker($aValues, $aSpecificValues);
  55. }
  56. public function getGroupId()
  57. {
  58. return $this->_iGroupId;
  59. }
  60. public function getGroupCustomId()
  61. {
  62. return $this->_iGroupCustomId;
  63. }
  64. public function getElementGroupCustom($aParams = array())
  65. {
  66. $this->initData($aParams);
  67. $sName = 'sys-pgc-' . str_replace('_', '-', $this->_sObject);
  68. $iValue = !empty($aParams['value']) ? (int)$aParams['value'] : $this->_iGroupCustomId;
  69. $sItemsName = $sName . '-items';
  70. $aItemsValue = array();
  71. if(!empty($aParams['value_items']) && is_array($aParams['value_items']))
  72. $aItemsValue = $aParams['value_items'];
  73. else if($this->_iGroupCustomId)
  74. $aItemsValue = $this->_aGroupCustomInfo['items'];
  75. $sMethodItemsValue = 'getElementGroupCustomValue' . bx_gen_method_name($this->_aGroupSettings['name']);
  76. if(!empty($aItemsValue) && method_exists($this, $sMethodItemsValue))
  77. $aItemsValue = $this->$sMethodItemsValue($sItemsName, $aItemsValue);
  78. $aInput = array(
  79. 'type' => 'hidden',
  80. 'name' => $sName . '-id',
  81. 'caption' => '',
  82. 'value' => $iValue,
  83. 'db' => array(
  84. 'pass' => 'Int'
  85. )
  86. );
  87. $aInputItems = array(
  88. 'type' => 'custom',
  89. 'name' => $sItemsName,
  90. 'caption' => '',
  91. 'value' => $aItemsValue,
  92. 'ajax_get_suggestions' => BX_DOL_URL_ROOT . bx_append_url_params('privacy.php', array(
  93. 'object' => $this->_sObject,
  94. 'action' => $this->_aGroupSettings['uri_get_items'],
  95. 'group' => $this->_iGroupId
  96. )),
  97. 'attrs' => array(
  98. 'disabled' => 'disabled'
  99. )
  100. );
  101. return $this->oTemplate->parseHtmlByName('privacy_group_custom.html', [
  102. 'js_object' => $this->_oObject->getJsObjectName(),
  103. 'html_id' => $sName,
  104. 'input_id' => $this->genInputStandard($aInput),
  105. 'input_items' => $this->genCustomInputUsernamesSuggestions($aInputItems)
  106. ]);
  107. }
  108. protected function getElementGroupCustomValueMembershipsSelected($sName, $aItems)
  109. {
  110. $oAcl = BxDolAcl::getInstance();
  111. $aTmplVarsItems = [];
  112. foreach($aItems as $iItem) {
  113. $aItemInfo = $oAcl->getMembershipInfo($iItem);
  114. if(empty($aItemInfo) || !is_array($aItemInfo))
  115. continue;
  116. $aTmplVarsItems[] = [
  117. 'name' => $sName,
  118. 'value' => $iItem,
  119. 'title' => _t($aItemInfo['name'])
  120. ];
  121. }
  122. return $this->oTemplate->parseHtmlByName('privacy_memberships_value.html', [
  123. 'bx_repeat:vals' => $aTmplVarsItems
  124. ]);
  125. }
  126. protected function genCustomInputSearch($aInput)
  127. {
  128. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . bx_append_url_params('privacy.php', array(
  129. 'object' => $this->_sObject,
  130. 'action' => 'users_list',
  131. 'group' => $this->_iGroupId
  132. ));
  133. return $this->genCustomInputUsernamesSuggestions($aInput);
  134. }
  135. protected function genCustomInputList($aInput)
  136. {
  137. $iProfileId = bx_get_logged_profile_id();
  138. $oConnection = BxDolConnection::getObjectInstance($this->_aGroupSettings['connection']);
  139. if(!$oConnection)
  140. return '';
  141. $aConnectedIds = $oConnection->getConnectedContent($iProfileId, true);
  142. $aSelected = array();
  143. if(!empty($this->_aGroupCustomInfo) && is_array($this->_aGroupCustomInfo))
  144. $aSelected = $this->_aGroupCustomInfo['items'];
  145. $aTmplVarsUsers = array();
  146. if(!empty($aConnectedIds) && is_array($aConnectedIds)) {
  147. $aCheckbox = $aInput;
  148. $aCheckbox['type'] = 'checkbox';
  149. $aCheckbox['name'] .= '[]';
  150. foreach($aConnectedIds as $iConnectedId) {
  151. $oProfile = BxDolProfile::getInstanceMagic($iConnectedId);
  152. if(!$oProfile)
  153. continue;
  154. $aCheckbox['value'] = $iConnectedId;
  155. $aCheckbox['checked'] = in_array($iConnectedId, $aSelected) ? 1 : 0;
  156. $aTmplVarsUsers[] = array(
  157. 'checkbox' => $this->genInput($aCheckbox),
  158. 'unit' => $oProfile->getUnit()
  159. );
  160. }
  161. }
  162. if(empty($aTmplVarsUsers))
  163. $aTmplVarsUsers = MsgBox(_t('_Empty'));
  164. return $this->oTemplate->parseHtmlByName('privacy_users_select_list.html', array(
  165. 'bx_repeat:users' => $aTmplVarsUsers
  166. ));
  167. }
  168. protected function initData($aData)
  169. {
  170. if(!empty($aData['profile_id']))
  171. $this->_iProfileId = (int)$aData['profile_id'];
  172. if(!empty($aData['content_id']))
  173. $this->_iContentId = (int)$aData['content_id'];
  174. if(!empty($aData['object'])) {
  175. $this->_sObject = $aData['object'];
  176. $this->_oObject = BxDolPrivacy::getObjectInstance($this->_sObject);
  177. }
  178. if(!empty($aData['group_id'])) {
  179. $this->_iGroupId = (int)$aData['group_id'];
  180. $this->_aGroupSettings = $this->_oObject ? $this->_oObject->getGroupSettings($this->_iGroupId) : array();
  181. }
  182. if(!empty($this->_iProfileId) && !empty($this->_sObject) && !empty($this->_iGroupId)) {
  183. $this->_aGroupCustomInfo = $this->_oObject->getGroupCustom(array(
  184. 'type' => 'pcog_ext',
  185. 'profile_id' => $this->_iProfileId,
  186. 'content_id' => $this->_iContentId,
  187. 'object' => $this->_sObject,
  188. 'group_id' => $this->_iGroupId,
  189. 'group_items_table' => $this->_aGroupSettings['db_table_items'],
  190. 'group_items_field' => $this->_aGroupSettings['db_field_item']
  191. ));
  192. if(!empty($this->_aGroupCustomInfo) && is_array($this->_aGroupCustomInfo))
  193. $this->_iGroupCustomId = (int)$this->_aGroupCustomInfo['id'];
  194. }
  195. }
  196. }
  197. /** @} */