Skip to content
This repository was archived by the owner on May 15, 2018. It is now read-only.

Commit 783fdb8

Browse files
committed
initial entry saving code
0 parents  commit 783fdb8

5 files changed

+117
-0
lines changed

.DS_Store

6 KB
Binary file not shown.

ContactFormEntriesPlugin.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
namespace Craft;
3+
4+
class ContactFormEntriesPlugin extends BasePlugin
5+
{
6+
function getName()
7+
{
8+
return Craft::t('Contact Form Entries');
9+
}
10+
11+
function getVersion()
12+
{
13+
return '0.1';
14+
}
15+
16+
function getDeveloper()
17+
{
18+
return 'Josh Angell';
19+
}
20+
21+
function getDeveloperUrl()
22+
{
23+
return 'http://joshangell.co.uk';
24+
}
25+
26+
protected function defineSettings()
27+
{
28+
return array(
29+
'section' => array(AttributeType::Number, 'required' => true)
30+
);
31+
}
32+
33+
public function getSettingsHtml()
34+
{
35+
return craft()->templates->render('contactformentries/_settings', array(
36+
'settings' => $this->getSettings()
37+
));
38+
}
39+
40+
public function init()
41+
{
42+
craft()->on('contactForm.beforeSend', function(ContactFormEvent $event) {
43+
44+
$message = $event->params['message'];
45+
46+
$messageEntry = new ContactFormEntriesModel();
47+
48+
$messageEntry->fromEmail = $message->fromEmail;
49+
$messageEntry->fromName = $message->fromName;
50+
$messageEntry->subject = $message->subject;
51+
$messageEntry->message = $message->message;
52+
53+
craft()->contactFormEntries->saveMessageEntry($messageEntry);
54+
});
55+
}
56+
}

models/ContactFormEntriesModel.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Craft;
3+
4+
class ContactFormEntriesModel extends BaseModel
5+
{
6+
protected function defineAttributes()
7+
{
8+
return array(
9+
'fromName' => array(AttributeType::String, 'label' => 'From Name'),
10+
'fromEmail' => array(AttributeType::Email, 'required' => true, 'label' => 'From Email'),
11+
'message' => array(AttributeType::String, 'required' => true, 'label' => 'Message'),
12+
'subject' => array(AttributeType::String, 'label' => 'Subject')
13+
);
14+
}
15+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace Craft;
3+
4+
/**
5+
* Contact Form Entries service
6+
*/
7+
class ContactFormEntriesService extends BaseApplicationComponent
8+
{
9+
/**
10+
* Saves an email to the channel specified in the settings.
11+
*
12+
* @paraam ContactFormModel $message
13+
* @return bool
14+
*/
15+
public function saveMessageEntry(ContactFormEntriesModel $message)
16+
{
17+
$plugin = craft()->plugins->getPlugin('contactFormEntries');
18+
$settings = $plugin->getSettings();
19+
20+
$entry = new EntryModel();
21+
$entry->sectionId = $settings['section'];
22+
$entry->enabled = true;
23+
24+
// populate fields
25+
$entry->getContent()->setAttributes(array(
26+
'title' => $message->subject,
27+
'body' => $message->message,
28+
'fromName' => $message->fromName,
29+
'fromEmail' => $message->fromEmail
30+
));
31+
32+
// save entry to db
33+
$success = craft()->entries->saveEntry($entry);
34+
}
35+
}

templates/_settings.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{% import "_includes/forms" as forms %}
2+
3+
{{ forms.textField({
4+
first: true,
5+
label: "Section ID"|t,
6+
id: 'section',
7+
name: 'section',
8+
value: settings.section,
9+
autofocus: true,
10+
errors: settings.getErrors('section')
11+
}) }}

0 commit comments

Comments
 (0)