first commit
This commit is contained in:
268
api/components/com_contact/src/Controller/ContactController.php
Normal file
268
api/components/com_contact/src/Controller/ContactController.php
Normal file
@ -0,0 +1,268 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.API
|
||||
* @subpackage com_contact
|
||||
*
|
||||
* @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Contact\Api\Controller;
|
||||
|
||||
use Joomla\CMS\Component\ComponentHelper;
|
||||
use Joomla\CMS\Event\Contact\SubmitContactEvent;
|
||||
use Joomla\CMS\Event\Contact\ValidateContactEvent;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Form\Form;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\Log\Log;
|
||||
use Joomla\CMS\Mail\Exception\MailDisabledException;
|
||||
use Joomla\CMS\Mail\MailTemplate;
|
||||
use Joomla\CMS\MVC\Controller\ApiController;
|
||||
use Joomla\CMS\MVC\Controller\Exception\SendEmail;
|
||||
use Joomla\CMS\Plugin\PluginHelper;
|
||||
use Joomla\CMS\Router\Exception\RouteNotFoundException;
|
||||
use Joomla\CMS\String\PunycodeHelper;
|
||||
use Joomla\CMS\Uri\Uri;
|
||||
use Joomla\CMS\User\UserFactoryAwareInterface;
|
||||
use Joomla\CMS\User\UserFactoryAwareTrait;
|
||||
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
|
||||
use Joomla\Registry\Registry;
|
||||
use Joomla\String\Inflector;
|
||||
use PHPMailer\PHPMailer\Exception as phpMailerException;
|
||||
use Tobscure\JsonApi\Exception\InvalidParameterException;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* The contact controller
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class ContactController extends ApiController implements UserFactoryAwareInterface
|
||||
{
|
||||
use UserFactoryAwareTrait;
|
||||
|
||||
/**
|
||||
* The content type of the item.
|
||||
*
|
||||
* @var string
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $contentType = 'contacts';
|
||||
|
||||
/**
|
||||
* The default view for the display method.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.0
|
||||
*/
|
||||
protected $default_view = 'contacts';
|
||||
|
||||
/**
|
||||
* Method to allow extended classes to manipulate the data to be saved for an extension.
|
||||
*
|
||||
* @param array $data An array of input data.
|
||||
*
|
||||
* @return array
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected function preprocessSaveData(array $data): array
|
||||
{
|
||||
foreach (FieldsHelper::getFields('com_contact.contact') as $field) {
|
||||
if (isset($data[$field->name])) {
|
||||
!isset($data['com_fields']) && $data['com_fields'] = [];
|
||||
|
||||
$data['com_fields'][$field->name] = $data[$field->name];
|
||||
unset($data[$field->name]);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit contact form
|
||||
*
|
||||
* @param integer $id Leave empty if you want to retrieve data from the request
|
||||
* @return static A \JControllerLegacy object to support chaining.
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function submitForm($id = null)
|
||||
{
|
||||
if ($id === null) {
|
||||
$id = $this->input->post->get('id', 0, 'int');
|
||||
}
|
||||
|
||||
$modelName = Inflector::singularize($this->contentType);
|
||||
|
||||
/** @var \Joomla\Component\Contact\Site\Model\ContactModel $model */
|
||||
$model = $this->getModel($modelName, 'Site');
|
||||
|
||||
if (!$model) {
|
||||
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_MODEL_CREATE'));
|
||||
}
|
||||
|
||||
$model->setState('filter.published', 1);
|
||||
|
||||
$data = $this->input->get('data', json_decode($this->input->json->getRaw(), true), 'array');
|
||||
$contact = $model->getItem($id);
|
||||
|
||||
if ($contact->id === null) {
|
||||
throw new RouteNotFoundException('Item does not exist');
|
||||
}
|
||||
|
||||
$contactParams = new Registry($contact->params);
|
||||
|
||||
if (!$contactParams->get('show_email_form')) {
|
||||
throw new \RuntimeException(Text::_('JLIB_APPLICATION_ERROR_DISPLAY_EMAIL_FORM'));
|
||||
}
|
||||
|
||||
// Contact plugins
|
||||
PluginHelper::importPlugin('contact');
|
||||
|
||||
Form::addFormPath(JPATH_COMPONENT_SITE . '/forms');
|
||||
|
||||
// Validate the posted data.
|
||||
$form = $model->getForm();
|
||||
|
||||
if (!$form) {
|
||||
throw new \RuntimeException($model->getError(), 500);
|
||||
}
|
||||
|
||||
if (!$model->validate($form, $data)) {
|
||||
$errors = $model->getErrors();
|
||||
$messages = [];
|
||||
|
||||
for ($i = 0, $n = \count($errors); $i < $n && $i < 3; $i++) {
|
||||
if ($errors[$i] instanceof \Exception) {
|
||||
$messages[] = "{$errors[$i]->getMessage()}";
|
||||
} else {
|
||||
$messages[] = "{$errors[$i]}";
|
||||
}
|
||||
}
|
||||
|
||||
throw new InvalidParameterException(implode("\n", $messages));
|
||||
}
|
||||
|
||||
// Validation succeeded, continue with custom handlers
|
||||
$results = $this->getDispatcher()->dispatch('onValidateContact', new ValidateContactEvent('onValidateContact', [
|
||||
'subject' => $contact,
|
||||
'data' => &$data, // @todo: Remove reference in Joomla 6, @deprecated: Data modification onValidateContact is not allowed, use onSubmitContact instead
|
||||
]))->getArgument('result', []);
|
||||
|
||||
foreach ($results as $result) {
|
||||
if ($result instanceof \Exception) {
|
||||
throw new InvalidParameterException($result->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// Passed Validation: Process the contact plugins to integrate with other applications
|
||||
$event = $this->getDispatcher()->dispatch('onSubmitContact', new SubmitContactEvent('onSubmitContact', [
|
||||
'subject' => $contact,
|
||||
'data' => &$data, // @todo: Remove reference in Joomla 6, see SubmitContactEvent::__constructor()
|
||||
]));
|
||||
// Get the final data
|
||||
$data = $event->getArgument('data', $data);
|
||||
|
||||
// Send the email
|
||||
$sent = false;
|
||||
|
||||
$params = ComponentHelper::getParams('com_contact');
|
||||
|
||||
if (!$params->get('custom_reply')) {
|
||||
$sent = $this->_sendEmail($data, $contact, $params->get('show_email_copy', 0));
|
||||
}
|
||||
|
||||
if (!$sent) {
|
||||
throw new SendEmail('Error sending message');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get a model object, loading it if required.
|
||||
*
|
||||
* @param array $data The data to send in the email.
|
||||
* @param \stdClass $contact The user information to send the email to
|
||||
* @param boolean $emailCopyToSender True to send a copy of the email to the user.
|
||||
*
|
||||
* @return boolean True on success sending the email, false on failure.
|
||||
*
|
||||
* @since 1.6.4
|
||||
*/
|
||||
private function _sendEmail($data, $contact, $emailCopyToSender)
|
||||
{
|
||||
$app = $this->app;
|
||||
|
||||
$app->getLanguage()->load('com_contact', JPATH_SITE, $app->getLanguage()->getTag(), true);
|
||||
|
||||
if ($contact->email_to == '' && $contact->user_id != 0) {
|
||||
$contact_user = $this->getUserFactory()->loadUserById($contact->user_id);
|
||||
$contact->email_to = $contact_user->get('email');
|
||||
}
|
||||
|
||||
$templateData = [
|
||||
'sitename' => $app->get('sitename'),
|
||||
'name' => $data['contact_name'],
|
||||
'contactname' => $contact->name,
|
||||
'email' => PunycodeHelper::emailToPunycode($data['contact_email']),
|
||||
'subject' => $data['contact_subject'],
|
||||
'body' => stripslashes($data['contact_message']),
|
||||
'url' => Uri::base(),
|
||||
'customfields' => '',
|
||||
];
|
||||
|
||||
// Load the custom fields
|
||||
if (!empty($data['com_fields']) && $fields = FieldsHelper::getFields('com_contact.mail', $contact, true, $data['com_fields'])) {
|
||||
$output = FieldsHelper::render(
|
||||
'com_contact.mail',
|
||||
'fields.render',
|
||||
[
|
||||
'context' => 'com_contact.mail',
|
||||
'item' => $contact,
|
||||
'fields' => $fields,
|
||||
]
|
||||
);
|
||||
|
||||
if ($output) {
|
||||
$templateData['customfields'] = $output;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$mailer = new MailTemplate('com_contact.mail', $app->getLanguage()->getTag());
|
||||
$mailer->addRecipient($contact->email_to);
|
||||
$mailer->setReplyTo($templateData['email'], $templateData['name']);
|
||||
$mailer->addTemplateData($templateData);
|
||||
$sent = $mailer->send();
|
||||
|
||||
// If we are supposed to copy the sender, do so.
|
||||
if ($emailCopyToSender == true && !empty($data['contact_email_copy'])) {
|
||||
$mailer = new MailTemplate('com_contact.mail.copy', $app->getLanguage()->getTag());
|
||||
$mailer->addRecipient($templateData['email']);
|
||||
$mailer->setReplyTo($templateData['email'], $templateData['name']);
|
||||
$mailer->addTemplateData($templateData);
|
||||
$sent = $mailer->send();
|
||||
}
|
||||
} catch (MailDisabledException | phpMailerException $exception) {
|
||||
try {
|
||||
Log::add(Text::_($exception->getMessage()), Log::WARNING, 'jerror');
|
||||
|
||||
$sent = false;
|
||||
} catch (\RuntimeException $exception) {
|
||||
Factory::getApplication()->enqueueMessage(Text::_($exception->errorMessage()), 'warning');
|
||||
|
||||
$sent = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $sent;
|
||||
}
|
||||
}
|
||||
134
api/components/com_contact/src/Serializer/ContactSerializer.php
Normal file
134
api/components/com_contact/src/Serializer/ContactSerializer.php
Normal file
@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Joomla! Content Management System
|
||||
*
|
||||
* @copyright (C) 2021 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Contact\Api\Serializer;
|
||||
|
||||
use Joomla\CMS\Router\Route;
|
||||
use Joomla\CMS\Serializer\JoomlaSerializer;
|
||||
use Joomla\CMS\Tag\TagApiSerializerTrait;
|
||||
use Joomla\CMS\Uri\Uri;
|
||||
use Tobscure\JsonApi\Collection;
|
||||
use Tobscure\JsonApi\Relationship;
|
||||
use Tobscure\JsonApi\Resource;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Temporary serializer
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class ContactSerializer extends JoomlaSerializer
|
||||
{
|
||||
use TagApiSerializerTrait;
|
||||
|
||||
/**
|
||||
* Build content relationships by associations
|
||||
*
|
||||
* @param \stdClass $model Item model
|
||||
*
|
||||
* @return Relationship
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function languageAssociations($model)
|
||||
{
|
||||
$resources = [];
|
||||
|
||||
// @todo: This can't be hardcoded in the future?
|
||||
$serializer = new JoomlaSerializer($this->type);
|
||||
|
||||
foreach ($model->associations as $association) {
|
||||
$resources[] = (new Resource($association, $serializer))
|
||||
->addLink('self', Route::link('site', Uri::root() . 'api/index.php/v1/contact/' . $association->id));
|
||||
}
|
||||
|
||||
$collection = new Collection($resources, $serializer);
|
||||
|
||||
return new Relationship($collection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build category relationship
|
||||
*
|
||||
* @param \stdClass $model Item model
|
||||
*
|
||||
* @return Relationship
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function category($model)
|
||||
{
|
||||
$serializer = new JoomlaSerializer('categories');
|
||||
|
||||
$resource = (new Resource($model->catid, $serializer))
|
||||
->addLink('self', Route::link('site', Uri::root() . 'api/index.php/v1/content/categories/' . $model->catid));
|
||||
|
||||
return new Relationship($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build category relationship
|
||||
*
|
||||
* @param \stdClass $model Item model
|
||||
*
|
||||
* @return Relationship
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function createdBy($model)
|
||||
{
|
||||
$serializer = new JoomlaSerializer('users');
|
||||
|
||||
$resource = (new Resource($model->created_by, $serializer))
|
||||
->addLink('self', Route::link('site', Uri::root() . 'api/index.php/v1/users/' . $model->created_by));
|
||||
|
||||
return new Relationship($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build editor relationship
|
||||
*
|
||||
* @param \stdClass $model Item model
|
||||
*
|
||||
* @return Relationship
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function modifiedBy($model)
|
||||
{
|
||||
$serializer = new JoomlaSerializer('users');
|
||||
|
||||
$resource = (new Resource($model->modified_by, $serializer))
|
||||
->addLink('self', Route::link('site', Uri::root() . 'api/index.php/v1/users/' . $model->modified_by));
|
||||
|
||||
return new Relationship($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build contact user relationship
|
||||
*
|
||||
* @param \stdClass $model Item model
|
||||
*
|
||||
* @return Relationship
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function userId($model)
|
||||
{
|
||||
$serializer = new JoomlaSerializer('users');
|
||||
|
||||
$resource = (new Resource($model->user_id, $serializer))
|
||||
->addLink('self', Route::link('site', Uri::root() . 'api/index.php/v1/users/' . $model->user_id));
|
||||
|
||||
return new Relationship($resource);
|
||||
}
|
||||
}
|
||||
214
api/components/com_contact/src/View/Contacts/JsonapiView.php
Normal file
214
api/components/com_contact/src/View/Contacts/JsonapiView.php
Normal file
@ -0,0 +1,214 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.API
|
||||
* @subpackage com_contact
|
||||
*
|
||||
* @copyright (C) 2019 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Component\Contact\Api\View\Contacts;
|
||||
|
||||
use Joomla\CMS\Language\Multilanguage;
|
||||
use Joomla\CMS\MVC\View\JsonApiView as BaseApiView;
|
||||
use Joomla\Component\Contact\Api\Serializer\ContactSerializer;
|
||||
use Joomla\Component\Content\Api\Helper\ContentHelper;
|
||||
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* The contacts view
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
class JsonapiView extends BaseApiView
|
||||
{
|
||||
/**
|
||||
* The fields to render item in the documents
|
||||
*
|
||||
* @var array
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $fieldsToRenderItem = [
|
||||
'id',
|
||||
'alias',
|
||||
'name',
|
||||
'category',
|
||||
'created',
|
||||
'created_by',
|
||||
'created_by_alias',
|
||||
'modified',
|
||||
'modified_by',
|
||||
'image',
|
||||
'tags',
|
||||
'featured',
|
||||
'publish_up',
|
||||
'publish_down',
|
||||
'version',
|
||||
'hits',
|
||||
'metakey',
|
||||
'metadesc',
|
||||
'metadata',
|
||||
'con_position',
|
||||
'address',
|
||||
'suburb',
|
||||
'state',
|
||||
'country',
|
||||
'postcode',
|
||||
'telephone',
|
||||
'fax',
|
||||
'misc',
|
||||
'email_to',
|
||||
'default_con',
|
||||
'user_id',
|
||||
'access',
|
||||
'mobile',
|
||||
'webpage',
|
||||
'sortname1',
|
||||
'sortname2',
|
||||
'sortname3',
|
||||
];
|
||||
|
||||
/**
|
||||
* The fields to render items in the documents
|
||||
*
|
||||
* @var array
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $fieldsToRenderList = [
|
||||
'id',
|
||||
'alias',
|
||||
'name',
|
||||
'category',
|
||||
'created',
|
||||
'created_by',
|
||||
'created_by_alias',
|
||||
'modified',
|
||||
'modified_by',
|
||||
'image',
|
||||
'tags',
|
||||
'user_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* The relationships the item has
|
||||
*
|
||||
* @var array
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected $relationship = [
|
||||
'category',
|
||||
'created_by',
|
||||
'modified_by',
|
||||
'user_id',
|
||||
'tags',
|
||||
];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param array $config A named configuration array for object construction.
|
||||
* contentType: the name (optional) of the content type to use for the serialization
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function __construct($config = [])
|
||||
{
|
||||
if (\array_key_exists('contentType', $config)) {
|
||||
$this->serializer = new ContactSerializer($config['contentType']);
|
||||
}
|
||||
|
||||
parent::__construct($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute and display a template script.
|
||||
*
|
||||
* @param array|null $items Array of items
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function displayList(array $items = null)
|
||||
{
|
||||
foreach (FieldsHelper::getFields('com_contact.contact') as $field) {
|
||||
$this->fieldsToRenderList[] = $field->name;
|
||||
}
|
||||
|
||||
return parent::displayList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute and display a template script.
|
||||
*
|
||||
* @param object $item Item
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public function displayItem($item = null)
|
||||
{
|
||||
foreach (FieldsHelper::getFields('com_contact.contact') as $field) {
|
||||
$this->fieldsToRenderItem[] = $field->name;
|
||||
}
|
||||
|
||||
if (Multilanguage::isEnabled()) {
|
||||
$this->fieldsToRenderItem[] = 'languageAssociations';
|
||||
$this->relationship[] = 'languageAssociations';
|
||||
}
|
||||
|
||||
return parent::displayItem();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare item before render.
|
||||
*
|
||||
* @param object $item The model item
|
||||
*
|
||||
* @return object
|
||||
*
|
||||
* @since 4.0.0
|
||||
*/
|
||||
protected function prepareItem($item)
|
||||
{
|
||||
foreach (FieldsHelper::getFields('com_contact.contact', $item, true) as $field) {
|
||||
$item->{$field->name} = $field->apivalue ?? $field->rawvalue;
|
||||
}
|
||||
|
||||
if (Multilanguage::isEnabled() && !empty($item->associations)) {
|
||||
$associations = [];
|
||||
|
||||
foreach ($item->associations as $language => $association) {
|
||||
$itemId = explode(':', $association)[0];
|
||||
|
||||
$associations[] = (object) [
|
||||
'id' => $itemId,
|
||||
'language' => $language,
|
||||
];
|
||||
}
|
||||
|
||||
$item->associations = $associations;
|
||||
}
|
||||
|
||||
if (!empty($item->tags->tags)) {
|
||||
$tagsIds = explode(',', $item->tags->tags);
|
||||
$tagsNames = $item->tagsHelper->getTagNames($tagsIds);
|
||||
|
||||
$item->tags = array_combine($tagsIds, $tagsNames);
|
||||
} else {
|
||||
$item->tags = [];
|
||||
}
|
||||
|
||||
if (isset($item->image)) {
|
||||
$item->image = ContentHelper::resolve($item->image);
|
||||
}
|
||||
|
||||
return parent::prepareItem($item);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user