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
*/
2012-12-14 16:16:41 +00:00
use Drupal\contact\Plugin\Core\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 ;
2007-07-16 06:37:49 +00:00
/**
2012-12-14 16:16:41 +00:00
* Page callback : Presents the site - wide contact form .
*
* @ param Drupal\contact\Plugin\Core\Entity\Category $category
* ( 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
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' );
$default_category = config ( 'contact.settings' ) -> get ( 'default_category' );
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
}
}
2012-12-14 16:16:41 +00:00
$message = entity_create ( 'contact_message' , array (
'category' => $category -> id (),
));
return entity_get_form ( $message );
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
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-01-16 02:18:26 +00:00
drupal_set_title ( t ( 'Contact @username' , array ( '@username' => user_format_name ( $recipient ))), PASS_THROUGH );
2009-10-11 18:34:10 +00:00
2012-12-14 16:16:41 +00:00
$message = entity_create ( 'contact_message' , array (
'recipient' => $recipient ,
));
return entity_get_form ( $message );
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 () {
$config = config ( 'contact.settings' );
$limit = $config -> get ( 'flood.limit' );
$interval = $config -> get ( 'flood.interval' );
2013-03-09 04:15:31 +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
}
}