-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Creates a downloadable vcard from all company data using the following insert tag {company::vcard_url}} @Xirdion
- Loading branch information
Showing
10 changed files
with
286 additions
and
3 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
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
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
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Oveleon\ContaoCompanyBundle\Controller; | ||
|
||
use Contao\PageModel; | ||
use Oveleon\ContaoCompanyBundle\Generator\vCardGenerator; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
/** | ||
* @Route(defaults={"_scope" = "frontend"}) | ||
*/ | ||
class vCardController extends AbstractController | ||
{ | ||
private vCardGenerator $generator; | ||
|
||
/** | ||
* @param vCardGenerator $generator | ||
*/ | ||
public function __construct(vCardGenerator $generator) | ||
{ | ||
$this->generator = $generator; | ||
} | ||
|
||
/** | ||
* @Route("/company/vcard/download", name="contao_company_vcard_download") | ||
* | ||
* @param Request $request | ||
* | ||
* @return Response | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function download(Request $request): Response | ||
{ | ||
// Try to load the current contao page | ||
$pageId = (int) $request->query->get('page'); | ||
$pageModel = PageModel::findById($pageId); | ||
if (null === $pageModel) { | ||
throw new \Exception('Could not load correct redirect page'); | ||
} | ||
|
||
try { | ||
// Load all details of the page to have access to rootId | ||
$pageModel->loadDetails(); | ||
|
||
// Generate the vcard | ||
$vcf = $this->generator | ||
->createCard($pageModel) | ||
->getContent(); | ||
|
||
$headers = $this->generator->getHeaders(); | ||
} catch (\Exception $e) { | ||
$vcf = ''; | ||
$headers = []; | ||
} | ||
|
||
return new Response($vcf, 200, $headers); | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,166 @@ | ||
<?php | ||
|
||
namespace Oveleon\ContaoCompanyBundle\Generator; | ||
|
||
use Contao\FilesModel; | ||
use Contao\PageModel; | ||
use Contao\System; | ||
use JeroenDesloovere\VCard\VCard; | ||
use Oveleon\ContaoCompanyBundle\Company; | ||
use Symfony\Component\Filesystem\Path; | ||
|
||
class vCardGenerator | ||
{ | ||
private ?VCard $vCard; | ||
private Company $company; | ||
|
||
public function __construct() | ||
{ | ||
$this->vCard = null; | ||
} | ||
|
||
/** | ||
* Generate a vcard with the given company values. | ||
* | ||
* @param PageModel $pageModel | ||
* | ||
* @return $this | ||
*/ | ||
public function createCard(PageModel $pageModel): self | ||
{ | ||
// Init company with current page model | ||
$this->company = new Company(); | ||
$this->company->initialize($pageModel, null, null); | ||
|
||
// Generate the vcard | ||
$this->vCard = new VCard(); | ||
$this | ||
->addName() | ||
->addCompany() | ||
->addAddress() | ||
->addEmail() | ||
->addPhone() | ||
->addFax() | ||
->addLogo() | ||
; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the content of the vcard as string. | ||
* | ||
* @return string | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function getContent(): string | ||
{ | ||
if (null === $this->vCard) { | ||
throw new \Exception('You must create a v-card first!'); | ||
} | ||
|
||
return $this->vCard->getOutput(); | ||
} | ||
|
||
/** | ||
* Get the correct headers to be able to download the vcard. | ||
* | ||
* @return array | ||
*/ | ||
public function getHeaders(): array | ||
{ | ||
return $this->vCard->getHeaders(true); | ||
} | ||
|
||
private function addName(): self | ||
{ | ||
$this->vCard->addName($this->company::get('name')); | ||
|
||
return $this; | ||
} | ||
|
||
private function addCompany(): self | ||
{ | ||
$this->vCard->addCompany($this->company::get('name')); | ||
|
||
return $this; | ||
} | ||
|
||
private function addAddress(): self | ||
{ | ||
$street = $this->company::get('street') ?: null; | ||
$city = $this->company::get('city') ?: null; | ||
$state = $this->company::get('state') ?: null; | ||
$zip = $this->company::get('postal') ?: null; | ||
$country = $this->company::get('country') ?: null; | ||
$type = 'WORK;POSTAL'; | ||
if (null === $street && null === $city && null === $state && null === $zip && null === $country) { | ||
return $this; | ||
} | ||
|
||
$this->vCard->addAddress(null, null, $street, $city, $state, $zip, $country, $type); | ||
|
||
return $this; | ||
} | ||
|
||
private function addEmail(): self | ||
{ | ||
$mail = $this->company::get('email') ?: null; | ||
if (null === $mail) { | ||
$mail = $this->company::get('email2') ?: null; | ||
} | ||
if (null === $mail) { | ||
return $this; | ||
} | ||
|
||
$this->vCard->addEmail($mail, 'WORK'); | ||
|
||
return $this; | ||
} | ||
|
||
private function addPhone(): self | ||
{ | ||
$phone = $this->company::get('phone') ?: null; | ||
if (null === $phone) { | ||
$phone = $this->company::get('phone2') ?: null; | ||
} | ||
if (null === $phone) { | ||
return $this; | ||
} | ||
|
||
$this->vCard->addPhoneNumber($phone, 'WORK'); | ||
|
||
return $this; | ||
} | ||
|
||
private function addFax(): self | ||
{ | ||
$fax = $this->company::get('fax') ?: null; | ||
if (null === $fax) { | ||
return $this; | ||
} | ||
|
||
$this->vCard->addPhoneNumber($fax, 'FAX'); | ||
|
||
return $this; | ||
} | ||
|
||
private function addLogo(): self | ||
{ | ||
$uuid = $this->company::get('logo') ?: null; | ||
if (null === $uuid) { | ||
return $this; | ||
} | ||
|
||
$file = FilesModel::findByUuid($uuid); | ||
if (null === $file) { | ||
return $this; | ||
} | ||
|
||
$projectDir = System::getContainer()->getParameter('kernel.project_dir'); | ||
$this->vCard->addLogo(Path::makeAbsolute($file->path, $projectDir)); | ||
|
||
return $this; | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
company: | ||
resource: '../../Controller' | ||
type: annotation |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
services: | ||
_defaults: | ||
autowire: true | ||
|
||
contao_company.vcard.generator: | ||
class: Oveleon\ContaoCompanyBundle\Generator\vCardGenerator | ||
|
||
Oveleon\ContaoCompanyBundle\Controller\vCardController: | ||
arguments: | ||
- '@contao_company.vcard.generator' | ||
tags: | ||
- controller.service_arguments |