2007-07-16 06:37:49 +00:00
< ? php
/**
* @ file
2011-12-05 14:23:20 +00:00
* Page callbacks for the Contact module .
2007-07-16 06:37:49 +00:00
*/
2013-08-18 21:16:19 +00:00
use Drupal\contact\Entity\Category ;
2012-06-04 12:06:09 +00:00
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException ;
2012-06-02 19:41:40 +00:00
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException ;
2013-09-20 14:39:18 +00:00
use Drupal\Component\Utility\String ;
2012-06-02 19:41:40 +00:00
2007-07-16 06:37:49 +00:00
/**
2012-12-14 16:16:41 +00:00
* Page callback : Presents the site - wide contact form .
*
2013-10-03 11:26:25 +00:00
* @ param \Drupal\contact\Entity\Category $category
2012-12-14 16:16:41 +00:00
* ( optional ) The contact category to use .
2012-10-18 23:06:48 +00:00
*
* @ throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
* @ throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
2009-10-11 18:34:10 +00:00
*
2011-12-05 14:23:20 +00:00
* @ see contact_menu ()
2009-10-11 18:34:10 +00:00
* @ see contact_site_form_submit ()
2011-12-05 14:23:20 +00:00
* @ ingroup forms
2013-09-18 18:30:30 +00:00
*
* @ deprecated Use \Drupal\contact\Controller\ContactController :: contactSitePage ()
2007-07-16 06:37:49 +00:00
*/
2012-12-14 16:16:41 +00:00
function contact_site_page ( Category $category = NULL ) {
2009-10-11 01:06:27 +00:00
// Check if flood control has been activated for sending e-mails.
2012-12-14 16:16:41 +00:00
if ( ! user_access ( 'administer contact forms' )) {
contact_flood_control ();
2007-07-16 06:37:49 +00:00
}
2009-10-11 18:34:10 +00:00
2012-12-14 16:16:41 +00:00
if ( ! isset ( $category )) {
$categories = entity_load_multiple ( 'contact_category' );
2013-09-16 03:58:06 +00:00
$default_category = \Drupal :: config ( 'contact.settings' ) -> get ( 'default_category' );
2012-12-14 16:16:41 +00:00
if ( isset ( $categories [ $default_category ])) {
$category = $categories [ $default_category ];
2009-04-21 09:27:52 +00:00
}
2012-12-14 16:16:41 +00:00
// If there are no categories, do not display the form.
2009-04-21 09:27:52 +00:00
else {
2012-12-14 16:16:41 +00:00
if ( user_access ( 'administer contact forms' )) {
drupal_set_message ( t ( 'The contact form has not been configured. <a href="@add">Add one or more categories</a> to the form.' , array ( '@add' => url ( 'admin/structure/contact/add' ))), 'error' );
return array ();
}
else {
throw new NotFoundHttpException ();
}
2009-04-21 09:27:52 +00:00
}
}
2013-06-18 23:42:10 +00:00
else if ( $category -> id () == 'personal' ) {
throw new NotFoundHttpException ();
}
2012-12-14 16:16:41 +00:00
$message = entity_create ( 'contact_message' , array (
'category' => $category -> id (),
));
2013-09-20 14:39:18 +00:00
$form = \Drupal :: entityManager () -> getForm ( $message );
$form [ '#title' ] = String :: checkPlain ( $category -> label ());
return $form ;
2007-07-16 06:37:49 +00:00
}
/**
2012-10-18 23:06:48 +00:00
* Page callback : Form constructor for the personal contact form .
*
* @ param $recipient
* The account for which a personal contact form should be generated .
*
* @ throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
2011-12-05 14:23:20 +00:00
*
* @ see contact_menu ()
2009-10-11 18:34:10 +00:00
* @ see contact_personal_form_submit ()
2012-11-07 15:35:36 +00:00
*
2011-12-05 14:23:20 +00:00
* @ ingroup forms
2013-09-18 18:30:30 +00:00
*
* @ deprecated Use \Drupal\contact\Controller\ContactController :: contactPersonalPage ()
2007-07-16 06:37:49 +00:00
*/
2012-12-14 16:16:41 +00:00
function contact_personal_page ( $recipient ) {
2007-07-16 06:37:49 +00:00
global $user ;
2009-10-11 01:06:27 +00:00
// Check if flood control has been activated for sending e-mails.
2012-12-14 16:16:41 +00:00
if ( ! user_access ( 'administer contact forms' ) && ! user_access ( 'administer users' )) {
contact_flood_control ();
2007-07-16 06:37:49 +00:00
}
2009-10-11 18:34:10 +00:00
2012-12-14 16:16:41 +00:00
$message = entity_create ( 'contact_message' , array (
'recipient' => $recipient ,
2013-06-05 09:18:02 +00:00
'category' => 'personal' ,
2012-12-14 16:16:41 +00:00
));
2013-09-20 14:39:18 +00:00
$form = \Drupal :: entityManager () -> getForm ( $message );
$form [ '#title' ] = t ( 'Contact @username' , array ( '@username' => $recipient -> getUsername ()));
return $form ;
2007-07-16 06:37:49 +00:00
}
/**
2012-12-14 16:16:41 +00:00
* Throws an exception if the current user is not allowed to submit a contact form .
*
* @ throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*
* @ see contact_site_page ()
* @ see contact_personal_page ()
2007-07-16 06:37:49 +00:00
*/
2012-12-14 16:16:41 +00:00
function contact_flood_control () {
2013-09-16 03:58:06 +00:00
$config = \Drupal :: config ( 'contact.settings' );
2012-12-14 16:16:41 +00:00
$limit = $config -> get ( 'flood.limit' );
$interval = $config -> get ( 'flood.interval' );
2013-09-16 03:58:06 +00:00
if ( ! \Drupal :: service ( 'flood' ) -> isAllowed ( 'contact' , $limit , $interval )) {
2012-12-14 16:16:41 +00:00
drupal_set_message ( t ( " You cannot send more than %limit messages in @interval. Try again later. " , array (
'%limit' => $limit ,
'@interval' => format_interval ( $interval ),
)), 'error' );
throw new AccessDeniedHttpException ();
2007-07-16 06:37:49 +00:00
}
}