Ejemplo n.º 1
0
 /**
  * @param \RainLoop\Model\Account $oAccount
  * @param string $sQuery
  * @param int $iLimit = 20
  *
  * @return array
  */
 public function Process($oAccount, $sQuery, $iLimit = 20)
 {
     $aResult = array();
     try {
         if (!$oAccount || !\RainLoop\Utils::IsOwnCloud() || !\class_exists('\\OCP\\Contacts') || !\OCP\Contacts::isEnabled() || !\class_exists('\\OCP\\User') || !\OCP\User::isLoggedIn()) {
             return $aResult;
         }
         $aSearchResult = \OCP\Contacts::search($sQuery, array('FN', 'EMAIL'));
         //$this->oLogger->WriteDump($aSearchResult);
         $aPreResult = array();
         if (\is_array($aSearchResult) && 0 < \count($aSearchResult)) {
             foreach ($aSearchResult as $aContact) {
                 if (0 >= $iLimit) {
                     break;
                 }
                 $sUid = empty($aContact['UID']) ? '' : $aContact['UID'];
                 if (!empty($sUid)) {
                     $sFullName = isset($aContact['FN']) ? \trim($aContact['FN']) : '';
                     $mEmails = isset($aContact['EMAIL']) ? $aContact['EMAIL'] : '';
                     if (!\is_array($mEmails)) {
                         $mEmails = array($mEmails);
                     }
                     if (!isset($aPreResult[$sUid])) {
                         $aPreResult[$sUid] = array();
                     }
                     foreach ($mEmails as $sEmail) {
                         $sEmail = \trim($sEmail);
                         if (!empty($sEmail)) {
                             $iLimit--;
                             $aPreResult[$sUid][] = array($sEmail, $sFullName);
                         }
                     }
                 }
             }
             $aPreResult = \array_values($aPreResult);
             //				$this->oLogger->WriteDump($aPreResult);
             foreach ($aPreResult as $aData) {
                 foreach ($aData as $aSubData) {
                     $aResult[] = $aSubData;
                 }
             }
         }
         unset($aSearchResult, $aPreResult);
     } catch (\Exception $oException) {
         if ($this->oLogger) {
             $this->oLogger->WriteException($oException);
         }
     }
     return $aResult;
 }
 /**
  * @param \RainLoop\Model\Account $oAccount
  * @param string $sQuery
  * @param int $iLimit = 20
  *
  * @return array
  */
 public function Process($oAccount, $sQuery, $iLimit = 20)
 {
     $iInputLimit = $iLimit;
     $aResult = array();
     $sQuery = \trim($sQuery);
     try {
         if ('' === $sQuery || !$oAccount || !\RainLoop\Utils::IsOwnCloudLoggedIn() || !\class_exists('OCP\\Contacts') || !\OCP\Contacts::isEnabled()) {
             return $aResult;
         }
         $aSearchResult = \OCP\Contacts::search($sQuery, array('FN', 'EMAIL'));
         //$this->oLogger->WriteDump($aSearchResult);
         $aHashes = array();
         if (\is_array($aSearchResult) && 0 < \count($aSearchResult)) {
             foreach ($aSearchResult as $aContact) {
                 if (0 >= $iLimit) {
                     break;
                 }
                 $sUid = empty($aContact['UID']) ? '' : $aContact['UID'];
                 if (!empty($sUid)) {
                     $sFullName = isset($aContact['FN']) ? \trim($aContact['FN']) : '';
                     $mEmails = isset($aContact['EMAIL']) ? $aContact['EMAIL'] : '';
                     if (!\is_array($mEmails)) {
                         $mEmails = array($mEmails);
                     }
                     foreach ($mEmails as $sEmail) {
                         $sHash = '"' . $sFullName . '" <' . $sEmail . '>';
                         if (!isset($aHashes[$sHash])) {
                             $aHashes[$sHash] = true;
                             $aResult[] = array($sEmail, $sFullName);
                             $iLimit--;
                         }
                     }
                 }
             }
             $aResult = \array_slice($aResult, 0, $iInputLimit);
         }
         unset($aSearchResult, $aHashes);
     } catch (\Exception $oException) {
         if ($this->oLogger) {
             $this->oLogger->WriteException($oException);
         }
     }
     return $aResult;
 }
Ejemplo n.º 3
0
 public function testEnabledAfterRegister()
 {
     // create mock for the addressbook
     $stub = $this->getMockForAbstractClass("OCP\\IAddressBook", array('getKey'));
     // we expect getKey to be called twice:
     // first time on register
     // second time on un-register
     $stub->expects($this->exactly(2))->method('getKey');
     // not enabled before register
     $this->assertFalse(\OCP\Contacts::isEnabled());
     // register the address book
     \OCP\Contacts::registerAddressBook($stub);
     // contacts api shall be enabled
     $this->assertTrue(\OCP\Contacts::isEnabled());
     // unregister the address book
     \OCP\Contacts::unregisterAddressBook($stub);
     // not enabled after register
     $this->assertFalse(\OCP\Contacts::isEnabled());
 }
Ejemplo n.º 4
0
 /**
  * Extracts all matching contacts with email address and name
  *
  * @param $term
  * @return array
  */
 public static function getMatchingRecipient($term)
 {
     if (!\OCP\Contacts::isEnabled()) {
         return array();
     }
     $result = \OCP\Contacts::search($term, array('FN', 'EMAIL'));
     $receivers = array();
     foreach ($result as $r) {
         $id = $r['id'];
         $fn = $r['FN'];
         $email = $r['EMAIL'];
         if (!is_array($email)) {
             $email = array($email);
         }
         // loop through all email addresses of this contact
         foreach ($email as $e) {
             $displayName = $fn . " <{$e}>";
             $receivers[] = array('id' => $id, 'label' => $displayName, 'value' => $displayName);
         }
     }
     return $receivers;
 }
Ejemplo n.º 5
0
 public static function search($str)
 {
     // The API is not active -> nothing to do
     if (!\OCP\Contacts::isEnabled() || strlen($str) < 3) {
         return array();
     }
     $result = \OCP\Contacts::search($str, array('FN', 'EMAIL'));
     $receivers = array();
     foreach ($result as $r) {
         $id = $r['id'];
         $fn = $r['FN'];
         $email = $r['EMAIL'];
         if (!is_array($email)) {
             $email = array($email);
         }
         // loop through all email addresses of this contact
         foreach ($email as $e) {
             $displayName = $fn . " <{$e}>";
             $receivers[] = array('id' => $id, 'label' => $displayName, 'value' => $displayName);
         }
     }
     return $receivers;
 }