Added: Sync contacts tags

Fixed: IMAP search
This commit is contained in:
RainLoop Team 2014-05-14 19:58:04 +04:00
parent e88c41c90a
commit 79e686f412
14 changed files with 28963 additions and 29048 deletions

View file

@ -8,58 +8,4 @@ interface AddressBookInterface
* @return bool
*/
public function IsSupported();
/**
* @param string $sEmail
* @param \RainLoop\Providers\AddressBook\Classes\Contact $oContact
*
* @return bool
*/
public function ContactSave($sEmail, &$oContact);
/**
* @param string $sEmail
* @param array $aContactIds
*
* @return bool
*/
public function DeleteContacts($sEmail, $aContactIds);
/**
* @param string $sEmail
* @param array $aTagsIds
*
* @return bool
*/
public function DeleteTags($sEmail, $aTagsIds);
/**
* @param string $sEmail
* @param int $iOffset = 0
* @param int $iLimit = 20
* @param string $sSearch = ''
* @param int $iResultCount = 0
*
* @return array
*/
public function GetContacts($sEmail, $iOffset = 0, $iLimit = 20, $sSearch = '', &$iResultCount = 0);
/**
* @param string $sEmail
* @param string $sSearch
* @param int $iLimit = 20
*
* @return array
*
* @throws \InvalidArgumentException
*/
public function GetSuggestions($sEmail, $sSearch, $iLimit = 20);
/**
* @param string $sEmail
* @param array $aEmails
*
* @return bool
*/
public function IncFrec($sEmail, $aEmails);
}

View file

@ -149,6 +149,8 @@ class Contact
{
$this->Properties[] = new \RainLoop\Providers\AddressBook\Classes\Property(PropertyType::FULLNAME, $this->Display);
}
$this->Tags = \array_map('trim', $this->Tags);
}
/**
@ -209,7 +211,7 @@ class Contact
$oVCard->VERSION = '3.0';
$oVCard->PRODID = '-//RainLoop//'.APP_VERSION.'//EN';
unset($oVCard->FN, $oVCard->EMAIL, $oVCard->TEL, $oVCard->URL);
unset($oVCard->FN, $oVCard->EMAIL, $oVCard->TEL, $oVCard->URL, $oVCard->{'X-RL-TAGS'});
$sFirstName = $sLastName = $sMiddleName = $sSuffix = $sPrefix = '';
foreach ($this->Properties as /* @var $oProperty \RainLoop\Providers\AddressBook\Classes\Property */ &$oProperty)
@ -270,6 +272,11 @@ class Contact
$oVCard->N = array($sLastName, $sFirstName, $sMiddleName, $sPrefix, $sSuffix);
$oVCard->REV = \gmdate('Ymd', $this->Changed).'T'.\gmdate('His', $this->Changed).'Z';
if (0 < \count($this->Tags))
{
$oVCard->{'X-RL-TAGS'} = \implode(';', $this->Tags);
}
return (string) $oVCard->serialize();
}
@ -582,6 +589,12 @@ class Contact
}
$this->Properties = $aProperties;
if (isset($oVCard->{'X-RL-TAGS'}) && 0 < \strlen($oVCard->{'X-RL-TAGS'}))
{
$this->Tags = \explode(';', $oVCard->{'X-RL-TAGS'});
$this->Tags = \array_map('trim', $this->Tags);
}
}
$this->UpdateDependentValues();

View file

@ -28,6 +28,11 @@ class PdoAddressBook
*/
private $sPassword;
/**
* @var array
*/
private $aTagsCache;
public function __construct($sDsn, $sUser = '', $sPassword = '', $sDsnType = 'mysql')
{
$this->sDsn = $sDsn;
@ -36,6 +41,7 @@ class PdoAddressBook
$this->sDsnType = $sDsnType;
$this->bExplain = false; // debug
$this->aTagsCache = array(); // debug
}
/**
@ -174,7 +180,7 @@ class PdoAddressBook
$oResponse = 'PUT' === $sCmd && null !== $mData ?
$oClient->request($sCmd, $sUrl, $mData) : $oClient->request($sCmd, $sUrl);
if ('GET' === $sCmd || false)
if ('GET' === $sCmd && false)
{
$this->oLogger->WriteDump($oResponse, \MailSo\Log\Enumerations\Type::INFO, 'DAV');
}
@ -523,11 +529,14 @@ class PdoAddressBook
);
}
$sSql = 'INSERT INTO rainloop_ab_properties '.
'( id_contact, id_user, prop_type, prop_type_str, prop_value, prop_value_custom, prop_frec) VALUES '.
'(:id_contact, :id_user, :prop_type, :prop_type_str, :prop_value, :prop_value_custom, :prop_frec)';
if (0 < \count($aParams))
{
$sSql = 'INSERT INTO rainloop_ab_properties '.
'( id_contact, id_user, prop_type, prop_type_str, prop_value, prop_value_custom, prop_frec) VALUES '.
'(:id_contact, :id_user, :prop_type, :prop_type_str, :prop_value, :prop_value_custom, :prop_frec)';
$this->prepareAndExecute($sSql, $aParams, true);
$this->prepareAndExecute($sSql, $aParams, true);
}
$aTags = array();
$aContactTags = $this->GetContactTags($sEmail);
@ -564,8 +573,11 @@ class PdoAddressBook
}
}
$sSql = 'INSERT INTO rainloop_ab_tags_contacts (id_tag, id_contact) VALUES (:id_tag, :id_contact)';
$this->prepareAndExecute($sSql, $aTags, true);
if (0 < \count($aTags))
{
$sSql = 'INSERT INTO rainloop_ab_tags_contacts (id_tag, id_contact) VALUES (:id_tag, :id_contact)';
$this->prepareAndExecute($sSql, $aTags, true);
}
}
}
catch (\Exception $oException)
@ -602,6 +614,8 @@ class PdoAddressBook
$iUserID = $this->getUserId($sEmail);
$this->aTagsCache = array();
$mResult = false;
try
{
@ -707,6 +721,8 @@ class PdoAddressBook
return false;
}
$this->aTagsCache = array();
$sIDs = \implode(',', $aTagsIds);
$aParams = array(':id_user' => array($iUserID, \PDO::PARAM_INT));
@ -716,6 +732,99 @@ class PdoAddressBook
return true;
}
/**
* @param string $sEmail
* @param array|\RainLoop\Providers\AddressBook\Classes\Contact $mContactOrContacts
*/
private function populateContactsByTags($sEmail, &$mContactOrContacts)
{
$aContacts = array();
if ($mContactOrContacts)
{
$aContacts = is_array($mContactOrContacts) ? $mContactOrContacts : array(&$mContactOrContacts);
}
if (\is_array($aContacts) && 0 < \count($aContacts))
{
$aIdContacts = array();
$aIdTagsContacts = array();
$aTags = $this->GetContactTags($sEmail);
if (\is_array($aTags) && 0 < \count($aTags))
{
foreach ($aContacts as $oItem)
{
$aIdContacts[$oItem->IdContact] = true;
}
if (0 < \count($aIdContacts))
{
$sSql = 'SELECT id_tag, id_contact FROM rainloop_ab_tags_contacts WHERE id_contact IN ('.\implode(',', \array_keys($aIdContacts)).')';
$oStmt = $this->prepareAndExecute($sSql);
if ($oStmt)
{
$aFetch = $oStmt->fetchAll(\PDO::FETCH_ASSOC);
if (\is_array($aFetch) && 0 < \count($aFetch))
{
foreach ($aFetch as $aItem)
{
if ($aItem && isset($aItem['id_tag'], $aItem['id_contact']))
{
$sID = (string) $aItem['id_contact'];
if (!isset($aIdTagsContacts[$sID]))
{
$aIdTagsContacts[$sID] = array();
}
$aIdTagsContacts[$sID][] = (string) $aItem['id_tag'];
}
}
}
unset($aFetch);
}
if (0 < \count($aIdTagsContacts))
{
$fFindFunc = function ($sID) use ($aTags) {
foreach ($aTags as $oItem)
{
if ($oItem && (string) $oItem->IdContactTag === $sID)
{
return\trim($oItem->Name);
}
}
return '';
};
foreach ($aContacts as &$oItem)
{
$sID = $oItem ? (string) $oItem->IdContact : '';
if (0 < \strlen($sID) && isset($aIdTagsContacts[$sID]) && \is_array($aIdTagsContacts[$sID]))
{
$aNames = array();
foreach ($aIdTagsContacts[$sID] as $sSubID)
{
$sName = $fFindFunc($sSubID);
if (0 < \strlen($sName))
{
$aNames[] = $sName;
}
}
$oItem->Tags = $aNames;
}
}
}
}
}
}
}
/**
* @param string $sEmail
* @param int $iOffset = 0
@ -908,95 +1017,23 @@ class PdoAddressBook
}
}
if (\is_array($aResult) && 0 < \count($aResult))
{
$aIdContacts = array();
$aIdTagsContacts = array();
$aTags = $this->GetContactTags($sEmail);
if (\is_array($aTags) && 0 < \count($aTags))
{
foreach ($aResult as $oItem)
{
$aIdContacts[$oItem->IdContact] = true;
}
if (0 < \count($aIdContacts))
{
$sSql = 'SELECT id_tag, id_contact FROM rainloop_ab_tags_contacts WHERE id_contact IN ('.\implode(',', \array_keys($aIdContacts)).')';
$oStmt = $this->prepareAndExecute($sSql);
if ($oStmt)
{
$aFetch = $oStmt->fetchAll(\PDO::FETCH_ASSOC);
if (\is_array($aFetch) && 0 < \count($aFetch))
{
foreach ($aFetch as $aItem)
{
if ($aItem && isset($aItem['id_tag'], $aItem['id_contact']))
{
$sID = (string) $aItem['id_contact'];
if (!isset($aIdTagsContacts[$sID]))
{
$aIdTagsContacts[$sID] = array();
}
$aIdTagsContacts[$sID][] = (string) $aItem['id_tag'];
}
}
}
unset($aFetch);
}
if (0 < \count($aIdTagsContacts))
{
$fFindFunc = function ($sID) use ($aTags) {
foreach ($aTags as $oItem)
{
if ($oItem && (string) $oItem->IdContactTag === $sID)
{
return\trim($oItem->Name);
}
}
return '';
};
foreach ($aResult as &$oItem)
{
$sID = $oItem ? (string) $oItem->IdContact : '';
if (0 < \strlen($sID) && isset($aIdTagsContacts[$sID]) && \is_array($aIdTagsContacts[$sID]))
{
$aNames = array();
foreach ($aIdTagsContacts[$sID] as $sSubID)
{
$sName = $fFindFunc($sSubID);
if (0 < \strlen($sName))
{
$aNames[] = $sName;
}
}
$oItem->Tags = $aNames;
}
}
}
}
}
}
$this->populateContactsByTags($sEmail, $aResult);
return $aResult;
}
/**
* @param string $sEmail
* @param bool $bCache = true
*
* @return array
*/
public function GetContactTags($sEmail)
public function GetContactTags($sEmail, $bCache = true)
{
if ($bCache && isset($this->aTagsCache[$sEmail]))
{
return $this->aTagsCache[$sEmail];
}
$this->SyncDatabase();
$iUserID = $this->getUserId($sEmail);
@ -1031,6 +1068,11 @@ class PdoAddressBook
}
}
if ($bCache)
{
$this->aTagsCache[$sEmail] = $aResult;
}
return $aResult;
}
@ -1133,6 +1175,11 @@ class PdoAddressBook
}
}
}
if ($oContact)
{
$this->populateContactsByTags($sEmail, $oContact);
}
return $oContact;
}