/**
  * @param int $iIdUser
  * @param null $iGroupId = null
  * @param bool $bWatchShare = false
  * @param bool $bWatchShareAndUser = true
  * @param string $sTablePrefix = ''
  * @param bool $bAll = false
  *
  * @return string
  */
 protected function sharedItemsSqlHelper($iIdUser, $iGroupId = null, $bWatchShare = false, $bWatchShareAndUser = false, $sTablePrefix = '', $bAll = false)
 {
     if ($bAll) {
         $bWatchShare = true;
         $bWatchShareAndUser = true;
     }
     $aTypes[] = sprintf('%stype = %d', $sTablePrefix, EContactType::Personal);
     if ($iGroupId && 0 < $iGroupId) {
         $aTypes[] = sprintf('%stype = %d', $sTablePrefix, EContactType::Global_);
     }
     if ($bWatchShare && null !== $iIdUser) {
         $oAccount = null;
         if (isset($this->aAccountsCache[$iIdUser])) {
             $oAccount = $this->aAccountsCache[$iIdUser];
         }
         if (!$oAccount) {
             if (null === $this->oUsersManager) {
                 $this->oUsersManager = CApi::Manager('users');
             }
             $oAccount = $this->oUsersManager ? $this->oUsersManager->getDefaultAccount($iIdUser) : null;
             if ($oAccount) {
                 $this->aAccountsCache[$iIdUser] = $oAccount;
             }
         }
         if ($oAccount) {
             $sEnd = '1 = 0';
             if (EContactsGABVisibility::Off !== $oAccount->GlobalAddressBook) {
                 if (0 <= $oAccount->IdDomain && $oAccount->Domain) {
                     if (EContactsGABVisibility::DomainWide === $oAccount->GlobalAddressBook) {
                         $sEnd = sprintf('%sid_domain = %d', $sTablePrefix, $oAccount->IdDomain);
                     } else {
                         if (EContactsGABVisibility::TenantWide === $oAccount->GlobalAddressBook) {
                             $sEnd = sprintf('%sid_tenant = %d', $sTablePrefix, $oAccount->Domain->IdTenant);
                         } else {
                             if (EContactsGABVisibility::SystemWide === $oAccount->GlobalAddressBook) {
                                 $sEnd = '1 = 1';
                             }
                         }
                     }
                 }
             }
             if ($bAll) {
                 $aTypes[] = sprintf('%stype = %d', $sTablePrefix, EContactType::GlobalAccounts);
                 $aTypes[] = sprintf('%stype = %d', $sTablePrefix, EContactType::GlobalMailingList);
             }
             return '(' . '(' . implode(' OR ', $aTypes) . ') AND (' . sprintf('(%sshared_to_all = 1 AND %s)', $sTablePrefix, $sEnd) . ($bAll ? sprintf(' OR ((%stype = %d OR %stype = %d) AND %s)', $sTablePrefix, EContactType::GlobalAccounts, $sTablePrefix, EContactType::GlobalMailingList, $sEnd) : '') . ($bWatchShareAndUser ? sprintf(' OR (%sid_user = %d AND %sshared_to_all = 0)', $sTablePrefix, $iIdUser, $sTablePrefix) : '') . '))';
         }
     }
     return null !== $iIdUser ? sprintf('(%sid_user = %d AND %sshared_to_all = 0 AND (%s))', $sTablePrefix, $iIdUser, $sTablePrefix, implode(' OR ', $aTypes)) : '';
 }