Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] ContactUs package #143

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions application/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
\Page\ConfigProvider::class,
\Meetup\ConfigProvider::class,
\Web\ConfigProvider::class,
\ContactUs\ConfigProvider::class,

new PhpFileProvider('config/autoload/{{,*.}global,{,*.}local}.php'),
]);
Expand Down
34 changes: 34 additions & 0 deletions application/data/phinx/migrations/20170830093221_contact_us.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Phinx\Migration\AbstractMigration;

/**
* Class ContactUs
*
* @package ContactUs
* @author Djordje Stojiljkovic <[email protected]>
*/
class ContactUs extends AbstractMigration
{
public function up()
{
$this
->table('contact_us', ['id' => false, 'primary_key' => 'contact_uuid'])
->addColumn('contact_uuid', 'binary', ['limit' => 16])
->addColumn('contact_id', 'text')
->addColumn('name', 'text')
->addColumn('email', 'text')
->addColumn('phone', 'text')
->addColumn('subject', 'text')
->addColumn('body', 'text')
->addColumn('created_at', 'timestamp', ['default' => 'CURRENT_TIMESTAMP'])
->create()
;
}


public function down()
{
$this->dropTable('contact_us');
}
}
34 changes: 34 additions & 0 deletions application/data/phinx/seeds/ContactUs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Phinx\Seed\AbstractSeed;

/**
* Class ContactUs
*
* @package ContactUs
* @author Djordje Stojiljkovic <[email protected]>
*/
class ContactUs extends AbstractSeed
{
public function run()
{
$faker = Faker\Factory::create();
$table = $this->table('contact_us');

for ($x = 0; $x <= 5; $x++) {
$id = $faker->uuid;
$mysqlUuid = (new MysqlUuid\Uuid($id))->toFormat(new MysqlUuid\Formats\Binary());
$table->insert(
[
'contact_uuid' => $mysqlUuid,
'contact_id' => $id,
'name' => $faker->name,
'email' => $faker->companyEmail,
'phone' => $faker->phoneNumber,
'subject' => 'We\'r contacting you because of...',
'body' => $faker->text,
]
)->save();
}
}
}
3 changes: 3 additions & 0 deletions application/packages/Admin/templates/layout/admin.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
<li>
<a href="<?= $this->url('admin.users') ?>"><i class="fa fa-user-secret"></i>Admin Users</a>
</li>
<li>
<a href="<?= $this->url('admin.contact-us') ?>"><i class="fa fa-envelope-o"></i>Contact Us</a>
</li>
</ul>
</div>
</div>
Expand Down
61 changes: 61 additions & 0 deletions application/packages/ContactUs/src/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace ContactUs;

use Zend\ServiceManager\Factory\InvokableFactory;

/**
* Class ConfigProvider
*
* @package ContactUs
* @author Djordje Stojiljkovic <[email protected]>
*/
class ConfigProvider
{
public function __invoke()
{
return [
'templates' => [
'map' => [
'contact-us/pagination' => __DIR__.'/../templates/partial/pagination.php',
'contact-us/errors' => __DIR__.'/../templates/partial/errors.php'
],
'paths' => [
'contact-us' => [__DIR__.'/../templates/contact-us'],
]
],

'dependencies' => [
'factories' => [
Controller\ContactUsController::class => Controller\ContactUsControllerFactory::class,
Service\ContactUsService::class => Service\ContactUsServiceFactory::class,
Mapper\ContactUsMapper::class => Mapper\ContactUsMapperFactory::class,
Filter\ContactUsFilter::class => InvokableFactory::class,
]
],

'routes' => [
[
'name' => 'admin.contact-us',
'path' => '/admin/contact-us/',
'middleware' => Controller\ContactUsController::class,
'allowed_methods' => ['GET'],
],
[
'name' => 'admin.contact-us.action',
'path' => '/admin/contact-us/{action}/{id}',
'middleware' => Controller\ContactUsController::class,
'allowed_methods' => ['GET', 'POST'],
]
],

'view_helpers' => [
'factories' => [

]
]
];
}
}
152 changes: 152 additions & 0 deletions application/packages/ContactUs/src/Controller/ContactUsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
<?php

namespace ContactUs\Controller;

use ContactUs\Entity\ContactUs;
use ContactUs\Service\ContactUsService;
use Std\FilterException;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Expressive\Router\RouterInterface as Router;
use Zend\Expressive\Template\TemplateRendererInterface as Template;
use Psr\Http\Message\ResponseInterface;
use Std\AbstractController;

/**
* Class ContactUsController
*
* @package ContactUs
* @author Djordje Stojiljkovic <[email protected]>
*/
class ContactUsController extends AbstractController
{
/**
* @var \Zend\Expressive\Router\RouterInterface $router
*/
protected $router;

/**
* @var \Zend\Expressive\Template\TemplateRendererInterface $template
*/
protected $template;

/**
* @var \ContactUs\Service\ContactUsService $contactUsService
*/
protected $contactUsService;

/**
* ContactUsController constructor.
*
* @param Template $template
* @param Router $router
* @param ContactUsService $contactUsService
*/
public function __construct(Template $template, Router $router, ContactUsService $contactUsService)
{
$this->template = $template;
$this->router = $router;
$this->contactUsService = $contactUsService;
}

/**
* @return HtmlResponse
*/
public function index(): HtmlResponse
{
$params = $this->request->getQueryParams();
$page = isset($params['page']) ? $params['page'] : 1;
$limit = isset($params['limit']) ? $params['limit'] : 15;
$pagination = $this->contactUsService->getPagination($page, $limit);

return new HtmlResponse(
$this->template->render(
'contact-us::index', [
'pagination' => $pagination,
'layout' => 'layout/admin',
]
)
);
}

/**
* @param array $errors
*
* @return HtmlResponse
*/
public function edit($errors = []): HtmlResponse
{
$id = $this->request->getAttribute('id');
$contactUs = $this->contactUsService->getById($id);

if ($this->request->getParsedBody()) {
$contactUs = new ContactUs();
$contactUs->exchangeArray(
$this->request->getParsedBody() + (array) $contactUs
);
$contactUs->setContactId($id);
}

return new HtmlResponse(
$this->template->render(
'contact-us::edit', [
'contact' => $contactUs,
'errors' => $errors,
'layout' => 'layout/admin',
]
)
);
}

/**
* @return ResponseInterface
*
* @throws \Exception
*/
public function save(): ResponseInterface
{
try
{
$id = $this->request->getAttribute('id');
$data = $this->request->getParsedBody();

if ($id) {
$this->contactUsService->update($data, $id);
} else {
$this->contactUsService->create($data);
}

return $this->response
->withStatus(302)
->withHeader('Location', $this->router->generateUri('admin.contact-us'))
;

} catch (FilterException $fe) {
return $this->edit($fe->getArrayMessages());
} catch (\Exception $e) {
throw $e;
}
}

/**
* @return ResponseInterface
*/
public function delete(): ResponseInterface
{
try
{
$id = $this->request->getAttribute('id');
$this->contactUsService->delete($id);

return $this->response
->withStatus(302)
->withHeader('Location', $this->router->generateUri('admin.contact-us'))
;

} catch (\Exception $e) {
return $this->response
->withStatus(302)
->withHeader('Location', $this->router->generateUri('admin.contact-us'))
;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace ContactUs\Controller;

use ContactUs\Service\ContactUsService;
use Interop\Container\ContainerInterface;
use Zend\Expressive\Router\RouterInterface;
use Zend\Expressive\Template\TemplateRendererInterface;

/**
* Class ContactUsControllerFactory
*
* @package ContactUs\Controller
* @author Djordje Stojiljkovic <[email protected]>
*/
class ContactUsControllerFactory
{
/**
* @param ContainerInterface $container
*
* @return ContactUsController
*/
public function __invoke(ContainerInterface $container): ContactUsController
{
return new ContactUsController(
$container->get(TemplateRendererInterface::class),
$container->get(RouterInterface::class),
$container->get(ContactUsService::class)
);
}
}
Loading