Example #1
0
 public function import()
 {
     if (is_null($this->userid)) {
         throw new \Exception('No user id set');
     }
     if (!$this->isValid()) {
         return false;
     }
     $this->numofcomponents = count($this->vcardobject);
     if ($this->overwrite) {
         foreach (VCard::all($this->id) as $obj) {
             VCard::delete($obj['id']);
         }
     }
     foreach ($this->vcardobject as $object) {
         if (!$object instanceof \Sabre\VObject\Component\VCard) {
             //continue;
         }
         $vcard = \Sabre\VObject\Reader::read($object, \Sabre\VObject\Reader::OPTION_IGNORE_INVALID_LINES);
         $importVCard = VCard::structureContact($vcard);
         if ($importVCard !== false) {
             $newVcard = $this->parseVCard($importVCard, $vcard);
             $insertid = VCard::add($this->id, $newVcard);
             $this->abscount++;
         } else {
             $this->errorCount++;
         }
         if ($this->isDuplicate($insertid)) {
             VCard::delete($insertid);
             $this->abscount--;
         }
         $this->updateProgress(intval($this->abscount / $this->numofcomponents * 100));
     }
     \OC::$server->getCache()->remove($this->progresskey);
     return true;
 }
    /**
     * @param string $pattern
     * @param string[] $searchProperties
     * @param $options
     * @return array|false
     */
    public function search($pattern, $searchProperties, $options)
    {
        $propTable = '*PREFIX*conplus_cards_properties';
        $contTable = '*PREFIX*conplus_cards';
        $addrTable = '*PREFIX*conplus_addressbooks';
        $results = array();
        /**
         * This query will fetch all contacts which match the $searchProperties
         * It will look up the addressbookid of the contact and the user id of the owner of the contact app
         */
        $query = <<<SQL
\t\t\tSELECT
\t\t\t\tDISTINCT
\t\t\t\t`{$propTable}`.`contactid`,
\t\t\t\t`{$contTable}`.`addressbookid`,
\t\t\t\t`{$addrTable}`.`userid`

\t\t\tFROM
\t\t\t\t`{$propTable}`
\t\t\tINNER JOIN
\t\t\t\t`{$contTable}`
\t\t\tON `{$contTable}`.`id` = `{$propTable}`.`contactid`
  \t\t\t\tINNER JOIN `{$addrTable}`
\t\t\tON `{$addrTable}`.id = `{$contTable}`.addressbookid
\t\t\tWHERE
\t\t\t\t(`{$contTable}`.addressbookid = ?) AND
\t\t\t\t(
SQL;
        $params = array();
        $params[] = $this->getKey();
        foreach ($searchProperties as $property) {
            $params[] = $property;
            $params[] = '%' . $pattern . '%';
            $query .= '(`name` = ? AND `value` ILIKE ?) OR ';
        }
        $query = substr($query, 0, strlen($query) - 4);
        $query .= ')';
        $stmt = \OCP\DB::prepare($query);
        $result = $stmt->execute($params);
        if (\OCP\DB::isError($result)) {
            \OCP\Util::writeLog('contactsplus', __METHOD__ . 'DB error: ' . \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
            return false;
        }
        $j = [];
        while ($row = $result->fetchRow()) {
            $id = $row['contactid'];
            //$addressbookKey = $row['addressbookid'];
            $vcard = App::getContactVCard($id);
            $contact = VCard::structureContact($vcard);
            $j['data'] = $contact;
            $j['data']['id'] = $id;
            $j['data']['metadata'] = $row;
            $j['data']['photo'] = false;
            if (isset($vcard->BDAY)) {
                $j['data']['birthday'] = $vcard->BDAY;
            }
            if (isset($vcard->PHOTO) || isset($vcard->LOGO)) {
                $j['data']['photo'] = true;
                $url = \OC::$server->getURLGenerator()->linkToRoute('contactsplus.contacts.getContactPhoto', array('id' => $id));
                $url = \OC::$server->getURLGenerator()->getAbsoluteURL($url);
                $j['data']['PHOTO'] = "uri:{$url}";
            }
            $results[] = $this->convertToSearchResult($j);
        }
        return $results;
    }
 /**
  * @param $properties
  * @return mixed
  */
 public function createOrUpdate($properties)
 {
     $id = null;
     $vcard = null;
     if (array_key_exists('id', $properties)) {
         // TODO: test if $id belongs to this addressbook
         $id = $properties['id'];
         // TODO: Test $vcard
         $vcard = App::getContactVCard($properties['id']);
         foreach (array_keys($properties) as $name) {
             if (isset($vcard->{$name})) {
                 unset($vcard->{$name});
             }
         }
     } else {
         $vcard = \Sabre\VObject\Component::create('VCARD');
         $uid = substr(md5(rand() . time()), 0, 10);
         $vcard->add('UID', $uid);
         try {
             $id = VCard::add($this->id, $vcard, null, true);
         } catch (Exception $e) {
             \OC_Log::write('contacts', __METHOD__ . ' ' . $e->getMessage(), \OCP\Util::ERROR);
             return false;
         }
     }
     foreach ($properties as $name => $value) {
         switch ($name) {
             case 'ADR':
             case 'N':
                 if (is_array($value)) {
                     $property = \Sabre\VObject\Property::create($name);
                     $property->setParts($value);
                     $vcard->add($property);
                 } else {
                     $vcard->{$name} = $value;
                 }
                 break;
             case 'BDAY':
                 // TODO: try/catch
                 $date = new \DateTime($value);
                 $vcard->BDAY = $date->format('Y-m-d');
                 $vcard->BDAY->VALUE = 'DATE';
                 break;
             case 'EMAIL':
             case 'TEL':
             case 'IMPP':
                 // NOTE: We don't know if it's GTalk, Jabber etc. only the protocol
             // NOTE: We don't know if it's GTalk, Jabber etc. only the protocol
             case 'URL':
                 if (is_array($value)) {
                     foreach ($value as $val) {
                         $vcard->add($name, strip_tags($val));
                     }
                 } else {
                     $vcard->add($name, strip_tags($value));
                 }
             default:
                 $vcard->{$name} = $value;
                 break;
         }
     }
     try {
         VCard::edit($id, $vcard);
     } catch (Exception $e) {
         \OC_Log::write('contacts', __METHOD__ . ' ' . $e->getMessage(), \OCP\Util::ERROR);
         return false;
     }
     $asarray = VCard::structureContact($vcard);
     $asarray['id'] = $id;
     return $asarray;
 }