2007-07-16 06:37:49 +00:00
< ? php
// $Id$
/**
* @ file
* Admin page callbacks for the contact module .
*/
/**
* Categories / list tab .
*/
2009-10-09 02:34:07 +00:00
function contact_category_list () {
$header = array (
t ( 'Category' ),
t ( 'Recipients' ),
t ( 'Selected' ),
array ( 'data' => t ( 'Operations' ), 'colspan' => 2 ),
);
2007-07-16 06:37:49 +00:00
$rows = array ();
2009-02-05 19:26:21 +00:00
// Get all the contact categories from the database.
2010-02-26 18:35:35 +00:00
$categories = db_select ( 'contact' , 'c' )
-> addTag ( 'translatable' )
-> fields ( 'c' , array ( 'cid' , 'category' , 'recipients' , 'selected' ))
-> orderBy ( 'weight' )
-> orderBy ( 'category' )
-> execute ()
-> fetchAll ();
2009-02-05 19:26:21 +00:00
// Loop through the categories and add them to the table.
2009-10-09 02:34:07 +00:00
foreach ( $categories as $category ) {
2009-03-25 13:45:00 +00:00
$rows [] = array (
2009-12-28 11:53:49 +00:00
check_plain ( $category -> category ),
check_plain ( $category -> recipients ),
2009-10-09 02:34:07 +00:00
( $category -> selected ? t ( 'Yes' ) : t ( 'No' )),
l ( t ( 'Edit' ), 'admin/structure/contact/edit/' . $category -> cid ),
l ( t ( 'Delete' ), 'admin/structure/contact/delete/' . $category -> cid ),
2009-02-05 19:26:21 +00:00
);
}
2009-10-09 02:34:07 +00:00
if ( ! $rows ) {
$rows [] = array ( array (
'data' => t ( 'No categories available.' ),
'colspan' => 5 ,
));
2007-07-16 06:37:49 +00:00
}
2010-02-17 08:48:18 +00:00
$build [ 'category_table' ] = array (
'#theme' => 'table' ,
'#header' => $header ,
'#rows' => $rows ,
);
return $build ;
2007-07-16 06:37:49 +00:00
}
/**
* Category edit page .
*/
2009-10-09 02:34:07 +00:00
function contact_category_edit_form ( $form , & $form_state , array $category = array ()) {
// If this is a new category, add the default values.
$category += array (
'category' => '' ,
'recipients' => '' ,
'reply' => '' ,
'weight' => 0 ,
'selected' => 0 ,
'cid' => NULL ,
);
$form [ 'category' ] = array (
'#type' => 'textfield' ,
2007-07-16 06:37:49 +00:00
'#title' => t ( 'Category' ),
'#maxlength' => 255 ,
2009-10-09 02:34:07 +00:00
'#default_value' => $category [ 'category' ],
2007-07-16 06:37:49 +00:00
'#description' => t ( " Example: 'website feedback' or 'product information'. " ),
'#required' => TRUE ,
);
2009-10-09 02:34:07 +00:00
$form [ 'recipients' ] = array (
'#type' => 'textarea' ,
2007-07-16 06:37:49 +00:00
'#title' => t ( 'Recipients' ),
2009-10-09 02:34:07 +00:00
'#default_value' => $category [ 'recipients' ],
2008-04-14 17:48:46 +00:00
'#description' => t ( " Example: 'webmaster@example.com' or 'sales@example.com,support@example.com' . To specify multiple recipients, separate each e-mail address with a comma. " ),
2007-07-16 06:37:49 +00:00
'#required' => TRUE ,
);
2009-10-09 02:34:07 +00:00
$form [ 'reply' ] = array (
'#type' => 'textarea' ,
2007-07-16 06:37:49 +00:00
'#title' => t ( 'Auto-reply' ),
2009-10-09 02:34:07 +00:00
'#default_value' => $category [ 'reply' ],
2007-07-16 06:37:49 +00:00
'#description' => t ( 'Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.' ),
);
2009-10-09 02:34:07 +00:00
$form [ 'weight' ] = array (
'#type' => 'weight' ,
2007-07-16 06:37:49 +00:00
'#title' => t ( 'Weight' ),
2009-10-09 02:34:07 +00:00
'#default_value' => $category [ 'weight' ],
2007-07-16 06:37:49 +00:00
'#description' => t ( 'When listing categories, those with lighter (smaller) weights get listed before categories with heavier (larger) weights. Categories with equal weights are sorted alphabetically.' ),
);
2009-10-09 02:34:07 +00:00
$form [ 'selected' ] = array (
'#type' => 'select' ,
2007-07-16 06:37:49 +00:00
'#title' => t ( 'Selected' ),
2009-10-09 02:34:07 +00:00
'#options' => array (
0 => t ( 'No' ),
1 => t ( 'Yes' ),
),
'#default_value' => $category [ 'selected' ],
2007-07-16 06:37:49 +00:00
'#description' => t ( 'Set this to <em>Yes</em> if you would like this category to be selected by default.' ),
);
2009-10-09 02:34:07 +00:00
$form [ 'cid' ] = array (
'#type' => 'value' ,
'#value' => $category [ 'cid' ],
2007-07-16 06:37:49 +00:00
);
2010-04-24 14:49:14 +00:00
$form [ 'actions' ] = array ( '#type' => 'actions' );
2010-01-03 21:01:04 +00:00
$form [ 'actions' ][ 'submit' ] = array (
2009-10-09 02:34:07 +00:00
'#type' => 'submit' ,
2007-07-16 12:43:06 +00:00
'#value' => t ( 'Save' ),
2007-07-16 06:37:49 +00:00
);
return $form ;
}
/**
* Validate the contact category edit page form submission .
*/
2009-10-09 02:34:07 +00:00
function contact_category_edit_form_validate ( $form , & $form_state ) {
// Validate and each e-mail recipient.
2008-07-02 20:05:11 +00:00
$recipients = explode ( ',' , $form_state [ 'values' ][ 'recipients' ]);
2010-02-17 03:59:43 +00:00
// When creating a new contact form, or renaming the category on an existing
// contact form, make sure that the given category is unique.
$category = $form_state [ 'values' ][ 'category' ];
$query = db_select ( 'contact' , 'c' ) -> condition ( 'c.category' , $category , '=' );
if ( ! empty ( $form_state [ 'values' ][ 'cid' ])) {
$query -> condition ( 'c.cid' , $form_state [ 'values' ][ 'cid' ], '<>' );
}
if ( $query -> countQuery () -> execute () -> fetchField ()) {
form_set_error ( 'category' , t ( 'A contact form with category %category already exists.' , array ( '%category' => $category )));
}
2009-10-09 02:34:07 +00:00
foreach ( $recipients as & $recipient ) {
$recipient = trim ( $recipient );
if ( ! valid_email_address ( $recipient )) {
2008-07-02 20:05:11 +00:00
form_set_error ( 'recipients' , t ( '%recipient is an invalid e-mail address.' , array ( '%recipient' => $recipient )));
2007-07-16 06:37:49 +00:00
}
}
2009-10-09 02:34:07 +00:00
$form_state [ 'values' ][ 'recipients' ] = implode ( ',' , $recipients );
2007-07-16 06:37:49 +00:00
}
/**
* Process the contact category edit page form submission .
*/
2009-10-09 02:34:07 +00:00
function contact_category_edit_form_submit ( $form , & $form_state ) {
2007-07-16 06:37:49 +00:00
if ( $form_state [ 'values' ][ 'selected' ]) {
// Unselect all other contact categories.
2009-03-08 05:08:22 +00:00
db_update ( 'contact' )
-> fields ( array ( 'selected' => '0' ))
-> execute ();
2007-07-16 06:37:49 +00:00
}
2009-10-09 02:34:07 +00:00
if ( empty ( $form_state [ 'values' ][ 'cid' ])) {
drupal_write_record ( 'contact' , $form_state [ 'values' ]);
2007-07-16 06:37:49 +00:00
}
else {
2009-10-09 02:34:07 +00:00
drupal_write_record ( 'contact' , $form_state [ 'values' ], array ( 'cid' ));
2007-07-16 06:37:49 +00:00
}
2009-10-09 02:34:07 +00:00
drupal_set_message ( t ( 'Category %category has been saved.' , array ( '%category' => $form_state [ 'values' ][ 'category' ])));
watchdog ( 'contact' , 'Category %category has been saved.' , array ( '%category' => $form_state [ 'values' ][ 'category' ]), WATCHDOG_NOTICE , l ( t ( 'Edit' ), 'admin/structure/contact/edit/' . $form_state [ 'values' ][ 'cid' ]));
2009-07-20 18:51:36 +00:00
$form_state [ 'redirect' ] = 'admin/structure/contact' ;
2007-07-16 06:37:49 +00:00
}
/**
2009-10-09 02:34:07 +00:00
* Form builder for deleting a contact category .
*
* @ see contact_category_delete_form_submit ()
2007-07-16 06:37:49 +00:00
*/
2009-10-09 02:34:07 +00:00
function contact_category_delete_form ( $form , & $form_state , array $contact ) {
2007-07-16 06:37:49 +00:00
$form [ 'contact' ] = array (
'#type' => 'value' ,
'#value' => $contact ,
);
2009-10-09 02:34:07 +00:00
return confirm_form (
$form ,
t ( 'Are you sure you want to delete %category?' , array ( '%category' => $contact [ 'category' ])),
'admin/structure/contact' ,
t ( 'This action cannot be undone.' ),
t ( 'Delete' ),
t ( 'Cancel' )
);
2007-07-16 06:37:49 +00:00
}
/**
2009-10-09 02:34:07 +00:00
* Submit handler for the confirm delete category form .
*
* @ see contact_category_delete_form ()
2007-07-16 06:37:49 +00:00
*/
2009-10-09 02:34:07 +00:00
function contact_category_delete_form_submit ( $form , & $form_state ) {
$contact = $form [ 'contact' ][ '#value' ];
2009-03-08 05:08:22 +00:00
db_delete ( 'contact' )
-> condition ( 'cid' , $contact [ 'cid' ])
-> execute ();
2009-10-09 02:34:07 +00:00
2007-07-16 06:37:49 +00:00
drupal_set_message ( t ( 'Category %category has been deleted.' , array ( '%category' => $contact [ 'category' ])));
2009-10-09 02:34:07 +00:00
watchdog ( 'contact' , 'Category %category has been deleted.' , array ( '%category' => $contact [ 'category' ]), WATCHDOG_NOTICE );
2007-07-16 06:37:49 +00:00
2009-07-20 18:51:36 +00:00
$form_state [ 'redirect' ] = 'admin/structure/contact' ;
2007-07-16 06:37:49 +00:00
}