-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Hamza Mahjoubi <[email protected]>
- Loading branch information
Showing
1 changed file
with
50 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,12 @@ | |
namespace OCA\Contacts\Controller; | ||
|
||
use OCA\Contacts\AppInfo\Application; | ||
use OCP\App\IAppManager; | ||
use OCP\AppFramework\ApiController; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\JSONResponse; | ||
use OCP\App\IAppManager; | ||
use OCP\Files\NotFoundException; | ||
use OCP\Files\IAppData; | ||
use OCP\Files\NotFoundException; | ||
use OCP\IConfig; | ||
use OCP\IRequest; | ||
|
||
|
@@ -21,8 +21,8 @@ class DefaultContactController extends ApiController { | |
public function __construct( | ||
IRequest $request, | ||
private IConfig $config, | ||
private IAppData $appData, | ||
private IAppManager $appManager | ||
private IAppData $appData, | ||
private IAppManager $appManager, | ||
) { | ||
parent::__construct(Application::APP_ID, $request); | ||
} | ||
|
@@ -36,58 +36,56 @@ public function __construct( | |
* @return JSONResponse an empty JSONResponse with respective http status code | ||
*/ | ||
public function setAppConfig($allow) { | ||
$key ='enableDefaultContact'; | ||
if($allow ==='yes' && !$this->defaultContactExists()){ | ||
$this->setInitialDefaultContact(); | ||
} | ||
$key = 'enableDefaultContact'; | ||
if ($allow === 'yes' && !$this->defaultContactExists()) { | ||
$this->setInitialDefaultContact(); | ||
} | ||
$this->config->setAppValue(Application::APP_ID, $key, $allow); | ||
return new JSONResponse([], Http::STATUS_OK); | ||
} | ||
|
||
public function setDefaultContact($contactData){ | ||
if(!$this->config->getAppValue(Application::APP_ID, 'enableDefaultContact', 'yes')){ | ||
return new JSONResponse([], Http::STATUS_FORBIDDEN); | ||
} | ||
try{ | ||
$folder = $this->appData->getFolder('defaultContact'); | ||
} | ||
catch(NotFoundException $e){ | ||
$folder = $this->appData->newFolder('defaultContact'); | ||
} | ||
$file = (!$folder->fileExists('defaultContact.vcf')) ? $folder->newFile('defaultContact.vcf') : $folder->getFile('defaultContact.vcf'); | ||
$file->putContent($contactData); | ||
return new JSONResponse([], Http::STATUS_OK); | ||
} | ||
public function setDefaultContact($contactData) { | ||
if (!$this->config->getAppValue(Application::APP_ID, 'enableDefaultContact', 'yes')) { | ||
return new JSONResponse([], Http::STATUS_FORBIDDEN); | ||
} | ||
try { | ||
$folder = $this->appData->getFolder('defaultContact'); | ||
} catch (NotFoundException $e) { | ||
$folder = $this->appData->newFolder('defaultContact'); | ||
} | ||
$file = (!$folder->fileExists('defaultContact.vcf')) ? $folder->newFile('defaultContact.vcf') : $folder->getFile('defaultContact.vcf'); | ||
$file->putContent($contactData); | ||
return new JSONResponse([], Http::STATUS_OK); | ||
} | ||
|
||
private function setInitialDefaultContact(){ | ||
$cardData = 'BEGIN:VCARD' . PHP_EOL . | ||
'VERSION:3.0' . PHP_EOL . | ||
'PRODID:-//Nextcloud Contacts v' . $this->appManager->getAppVersion('contacts') . PHP_EOL . | ||
'UID: janeDoe' . PHP_EOL . | ||
'FN:Jane Doe' . PHP_EOL . | ||
'ADR;TYPE=HOME:;;123 Street Street;City;State;;Country' . PHP_EOL . | ||
'EMAIL;TYPE=WORK:[email protected]' . PHP_EOL . | ||
'TEL;TYPE=HOME,VOICE:+999999999999' . PHP_EOL . | ||
'TITLE:Manager' . PHP_EOL . | ||
'ORG:Company' . PHP_EOL . | ||
'BDAY;VALUE=DATE:20000101' . PHP_EOL . | ||
'URL;VALUE=URI:https://example.com/' . PHP_EOL . | ||
'REV;VALUE=DATE-AND-OR-TIME:20241227T144820Z' . PHP_EOL . | ||
'END:VCARD'; | ||
$folder = $this->appData->getFolder('defaultContact'); | ||
$file = $folder->newFile('defaultContact.vcf'); | ||
$file->putContent($cardData); | ||
} | ||
private function setInitialDefaultContact() { | ||
$cardData = 'BEGIN:VCARD' . PHP_EOL . | ||
'VERSION:3.0' . PHP_EOL . | ||
'PRODID:-//Nextcloud Contacts v' . $this->appManager->getAppVersion('contacts') . PHP_EOL . | ||
'UID: janeDoe' . PHP_EOL . | ||
'FN:Jane Doe' . PHP_EOL . | ||
'ADR;TYPE=HOME:;;123 Street Street;City;State;;Country' . PHP_EOL . | ||
'EMAIL;TYPE=WORK:[email protected]' . PHP_EOL . | ||
'TEL;TYPE=HOME,VOICE:+999999999999' . PHP_EOL . | ||
'TITLE:Manager' . PHP_EOL . | ||
'ORG:Company' . PHP_EOL . | ||
'BDAY;VALUE=DATE:20000101' . PHP_EOL . | ||
'URL;VALUE=URI:https://example.com/' . PHP_EOL . | ||
'REV;VALUE=DATE-AND-OR-TIME:20241227T144820Z' . PHP_EOL . | ||
'END:VCARD'; | ||
$folder = $this->appData->getFolder('defaultContact'); | ||
$file = $folder->newFile('defaultContact.vcf'); | ||
$file->putContent($cardData); | ||
} | ||
|
||
private function defaultContactExists(): bool { | ||
try{ | ||
$folder = $this->appData->getFolder('defaultContact'); | ||
} | ||
catch(NotFoundException $e){ | ||
$this->appData->newFolder('defaultContact'); | ||
return false; | ||
} | ||
return $folder->fileExists('defaultContact.vcf'); | ||
} | ||
private function defaultContactExists(): bool { | ||
try { | ||
$folder = $this->appData->getFolder('defaultContact'); | ||
} catch (NotFoundException $e) { | ||
$this->appData->newFolder('defaultContact'); | ||
return false; | ||
} | ||
return $folder->fileExists('defaultContact.vcf'); | ||
} | ||
|
||
} | ||
} |