/** * @NoAdminRequired */ public function importVcards() { $pProgresskey = $this->params('progresskey'); $pGetprogress = $this->params('getprogress'); $pPath = $this->params('path'); $pFile = $this->params('file'); $pMethod = $this->params('method'); $pAddressbook = $this->params('addressbookname'); $pIsDragged = $this->params('isDragged'); $pId = $this->params('id'); $pOverwrite = $this->params('overwrite'); \OC::$server->getSession()->close(); if (isset($pProgresskey) && isset($pGetprogress)) { $aCurrent = \OC::$server->getCache()->get($pProgresskey); $aCurrent = json_decode($aCurrent); $numVC = isset($aCurrent->{'all'}) ? $aCurrent->{'all'} : 0; $currentVCCount = isset($aCurrent->{'current'}) ? $aCurrent->{'current'} : 0; $currentFn = isset($aCurrent->{'currentFn'}) ? $aCurrent->{'currentFn'} : ''; $percent = isset($aCurrent->{'percent'}) ? $aCurrent->{'percent'} : ''; if ($percent == '') { $percent = 0; } $params = ['status' => 'success', 'percent' => $percent, 'currentmsg' => $currentFn . ' ' . $percent . '% (' . $currentVCCount . '/' . $numVC . ')']; $response = new JSONResponse($params); return $response; } if ($pIsDragged === 'true') { //OCP\JSON::error(array('error'=>'404')); $file = explode(',', $pFile); $file = end($file); $file = base64_decode($file); } else { $file = \OC\Files\Filesystem::file_get_contents($pPath . '/' . $pFile); } if (!$file) { $params = ['status' => 'error', 'error' => '404']; $response = new JSONResponse($params); return $response; } $file = \Sabre\VObject\StringUtil::convertToUTF8($file); $import = new Import($file); $import->setUserID($this->userId); $import->setProgresskey($pProgresskey); $import->enableProgressCache(); \OCP\Util::writeLog($this->appName, ' PROG: ' . $pProgresskey, \OCP\Util::DEBUG); if (!$import->isValid()) { $params = ['status' => 'error', 'error' => 'notvalid']; $response = new JSONResponse($params); return $response; } $newAddressbook = false; if ($pMethod == 'new') { $id = Addressbook::add($this->userId, $pAddressbook); if ($id) { Addressbook::setActive($id, 1); $newAddressbook = true; } } else { $id = $pId; Addressbook::find($id); $import->setOverwrite($pOverwrite); } //\OCP\Util::writeLog($this->appName,' METHOD: '.$pMethod.'ID:'.$id, \OCP\Util::DEBUG); $import->setAddressbookId($id); try { $import->import(); } catch (\Exception $e) { $params = ['status' => 'error', 'message' => $this->l10n->t('Import failed')]; $response = new JSONResponse($params); return $response; } $count = $import->getCount(); $errorCount = $import->getErrorCount(); if ($count == 0) { if ($newAddressbook) { Addressbook::delete($id); } $params = ['status' => 'error', 'message' => $this->l10n->t('The file contained either no vcard or all vcards are already saved in your addessbook.')]; $response = new JSONResponse($params); return $response; } else { if ($newAddressbook) { $params = ['status' => 'success', 'message' => $count . ' ' . $this->l10n->t('vcards has been saved in the new addressbook') . ' ' . strip_tags($pAddressbook)]; $response = new JSONResponse($params); return $response; } else { $params = ['status' => 'success', 'message' => $errorCount . ' Error - ' . $count . ' ' . $this->l10n->t('vcards has been saved in your addressbook')]; $response = new JSONResponse($params); return $response; } } }
/** * @NoAdminRequired */ public function add() { $pName = $this->params('name'); $pDescription = $this->params('description'); $pName = isset($pName) ? trim(strip_tags($pName)) : null; $description = isset($pDescription) ? trim(strip_tags($pDescription)) : null; if (is_null($pName)) { $msg = 'Cannot add addressbook with an empty name.'; } $bookid = Addressbook::add($this->userId, $pName, $pDescription); if (!$bookid) { $msg = 'Error adding addressbook: ' . $pName; } if (!Addressbook::setActive($bookid, 1)) { $msg = 'Error activating addressbook.'; } $addressbook = Addressbook::find($bookid); $params = ['status' => 'success', 'data' => ['addressbook' => $addressbook]]; $response = new JSONResponse($params); return $response; }