contact.php 3.67 KB
Newer Older
Richard Mansfield's avatar
Richard Mansfield committed
1 2 3 4 5
<?php
/**
 *
 * @package    mahara
 * @subpackage core
6
 * @author     Catalyst IT Ltd
7 8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
Richard Mansfield's avatar
Richard Mansfield committed
9 10 11 12
 *
 */

define('INTERNAL', 1);
13
define('PUBLIC', 1);
14 15 16
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'site');
define('SECTION_PAGE', 'contact');
Richard Mansfield's avatar
Richard Mansfield committed
17
require('init.php');
18
require_once('lib/antispam.php');
19
define('TITLE', get_string('contactus'));
Richard Mansfield's avatar
Richard Mansfield committed
20

Richard Mansfield's avatar
Richard Mansfield committed
21
if ($USER->is_logged_in()) {
22 23
    $userid = $USER->get('id');
    $name = display_name($userid);
Richard Mansfield's avatar
Richard Mansfield committed
24
    $email = $USER->get('email');
Richard Mansfield's avatar
Richard Mansfield committed
25 26
}
else {
27
    $userid = 0;
Richard Mansfield's avatar
Richard Mansfield committed
28 29 30 31
    $name = '';
    $email = '';
}

32 33 34 35 36 37 38 39 40 41 42 43 44 45
$elements = array(
    'name' => array(
        'type'  => 'text',
        'title' => get_string('name'),
        'defaultvalue' => $name,
        'rules' => array(
            'required'    => true
        ),
    ),
    'email' => array(
        'type'  => 'text',
        'title' => get_string('email'),
        'defaultvalue' => $email,
        'rules' => array(
46 47
            'required'    => true,
            'email' => true,
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
        ),
    ),
    'subject' => array(
        'type'  => 'text',
        'title' => get_string('subject'),
        'defaultvalue' => '',
    ),
    'message' => array(
        'type'  => 'textarea',
        'rows'  => 10,
        'cols'  => 60,
        'title' => get_string('message'),
        'defaultvalue' => '',
        'rules' => array(
            'required'    => true
        ),
    )
);
66 67 68 69 70
if (!$USER->is_logged_in()) {
    $elements['captcha'] = array(
        'type' => 'captcha',
    );
}
71 72 73 74 75 76
$elements['userid'] = array(
    'type'  => 'hidden',
    'value' => $userid,
);
$elements['submit'] = array(
    'type'  => 'submit',
77
    'value' => get_string('sendmessage'),
78
    'class' => 'btn-primary submit'
79 80
);

81
$contactform = pieform(array(
82 83 84 85 86 87
    'name'       => 'contactus',
    'method'     => 'post',
    'action'     => '',
    'elements'   => $elements,
    'spam' => array(
        'secret'       => get_config('formsecret'),
88
        'class' => 'hidden',
89 90 91 92
        'mintime'      => 5,
        'hash'         => array('name', 'email', 'subject', 'message', 'userid', 'submit'),
        'reorder'      => array('name', 'email'),
    ),
Richard Mansfield's avatar
Richard Mansfield committed
93
));
Richard Mansfield's avatar
Richard Mansfield committed
94

95 96
function contactus_validate(Pieform $form, $values) {
    global $SESSION;
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    $spamtrap = new_spam_trap(array(
        array(
            'type' => 'name',
            'value' => $values['name'],
        ),
        array(
            'type' => 'email',
            'value' => $values['email'],
        ),
        array(
            'type' => 'subject',
            'value' => $values['subject'],
        ),
        array(
            'type' => 'body',
            'value' => $values['message'],
        ),
    ));
    if ($form->spam_error() || $spamtrap->is_spam()) {
116 117 118 119 120
        $msg = get_string('formerror');
        $emailcontact = get_config('emailcontact');
        if (!empty($emailcontact)) {
            $msg .= ' ' . get_string('formerroremail', 'mahara', $emailcontact, $emailcontact);
        }
121
        $form->set_error(null, $msg);
122 123 124
    }
}

125
function contactus_submit(Pieform $form, $values) {
126
    global $SESSION;
127
    $data = new StdClass;
128 129 130 131 132 133
    $data->fromname    = $values['name'];
    $data->fromemail   = $values['email'];
    $data->subject     = $values['subject'];
    $data->message     = $values['message'];
    if ($values['userid']) {
        $data->fromuser = $values['userid'];
134
    }
135 136
    require_once('activity.php');
    activity_occurred('contactus', $data);
137 138
    $SESSION->add_ok_msg(get_string('messagesent'));
    redirect();
139 140
}

Richard Mansfield's avatar
Richard Mansfield committed
141
$smarty = smarty();
142 143
$smarty->assign('form', $contactform);
$smarty->display('form.tpl');