forked from jimmylorunning/FormBuilder-2-Craft-CMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormBuilder2Plugin.php
executable file
·174 lines (147 loc) · 6.08 KB
/
FormBuilder2Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/*
Plugin Name: FormBuilder 2
Plugin Url: https://github.com/roundhouse/FormBuilder-2
Author: Vadim Goncharov (https://github.com/owldesign)
Author URI: http://roundhouseagency.com
Description: FormBuilder 2 is a Craft CMS plugin that lets you create forms for your front-end.
Version: 2.0.11
*/
namespace Craft;
class FormBuilder2Plugin extends BasePlugin
{
public function init()
{
// Getting date for releases.json
// Craft::dd(DateTimeHelper::toIso8601(DateTimeHelper::currentTimeStamp()));
if (craft()->request->isCpRequest()) {
craft()->templates->hook('formBuilder2.prepCpTemplate', array($this, 'prepCpTemplate'));
}
craft()->on('fields.saveFieldLayout', function(Event $e) {
$layout = $e->params['layout'];
$customfield = craft()->request->getPost('customfield');
if($customfield) {
$transaction = craft()->db->getCurrentTransaction() ? false : craft()->db->beginTransaction();
try {
foreach($customfield as $fieldId => $labelInfo) {
$label = new FormBuilder2_FieldModel();
$label->fieldId = $fieldId;
$label->fieldLayoutId = $layout->id;
if(array_key_exists('template', $labelInfo)) {
$label->template = $labelInfo['template'];
}
craft()->formBuilder2_field->saveLabel($label);
}
if($transaction) {
$transaction->commit();
}
} catch(\Exception $e) {
if($transaction) {
$transaction->rollback();
}
throw $e;
}
unset($_POST['customfield']);
}
});
}
public function getReleaseFeedUrl()
{
return 'https://raw.githubusercontent.com/roundhouse/FormBuilder-2-Craft-CMS/master/releases.json';
}
public function getName()
{
$settings = $this->getSettings();
if ($settings->pluginName) {
return $settings->pluginName;
}
return 'FormBuilder 2';
}
public function getSchemaVersion()
{
return '2.0.0';
}
public function getVersion()
{
return '2.0.11';
}
public function getDeveloper()
{
return 'Roundhouse Agency';
}
public function getDeveloperUrl()
{
return 'https://github.com/roundhouse';
}
public function getDocumentationUrl()
{
return 'https://github.com/roundhouse/FormBuilder-2-Craft-CMS';
}
public function getDescription()
{
return Craft::t('Simply Forms Manager');;
}
public function hasCpSection()
{
return true;
}
public function getSettingsUrl()
{
return 'formbuilder2/tools/configuration';
}
/**
* Plugin settings.
*
* @return array
*/
protected function defineSettings()
{
return array(
'pluginName' => array(AttributeType::String),
'canDoActions' => array(AttributeType::Bool, 'default' => false)
);
}
public function prepCpTemplate(&$context)
{
$pluginSettings = $this->getSettings();
$context['subnav'] = array();
$context['subnav']['dashboard'] = array('label' => Craft::t('Dashboard'), 'url' => 'formbuilder2/dashboard');
if (craft()->userSession->isAdmin() || $pluginSettings->canDoActions) {
$context['subnav']['forms'] = array('label' => Craft::t('Forms'), 'url' => 'formbuilder2/forms');
}
$context['subnav']['entries'] = array('label' => Craft::t('Entries'), 'url' => 'formbuilder2/entries');
if (craft()->userSession->isAdmin() || $pluginSettings->canDoActions) {
$context['subnav']['templates'] = array('icon' => 'settings', 'label' => Craft::t('Templates'), 'url' => 'formbuilder2/templates');
$context['subnav']['configuration'] = array('icon' => 'settings', 'label' => Craft::t('Configuration'), 'url' => 'formbuilder2/tools/configuration');
}
}
public function addTwigExtension()
{
Craft::import('plugins.formbuilder2.twigextensions.FormBuilder2TwigExtension');
return new FormBuilder2TwigExtension();
}
public function registerCpRoutes()
{
return array(
'formbuilder2' => array('action' => 'formBuilder2_Dashboard/dashboard'),
'formbuilder2/dashboard' => array('action' => 'formBuilder2_Dashboard/dashboard'),
'formbuilder2/tools/configuration' => array('action' => 'formBuilder2/configurationIndex'),
'formbuilder2/tools/backup-restore' => array('action' => 'formBuilder2/backupRestoreIndex'),
'formbuilder2/tools/export' => array('action' => 'formBuilder2/exportIndex'),
'formbuilder2/forms' => array('action' => 'formBuilder2_Form/formsIndex'),
'formbuilder2/forms/new' => array('action' => 'formBuilder2_Form/editForm'),
'formbuilder2/forms/(?P<formId>\d+)' => array('action' => 'formBuilder2_Form/editForm'),
'formbuilder2/forms/(?P<formId>\d+)/edit' => array('action' => 'formBuilder2_Form/editForm'),
'formbuilder2/entries' => array('action' => 'formBuilder2_Entry/entriesIndex'),
'formbuilder2/entries/(?P<entryId>\d+)/edit' => array('action' => 'formBuilder2_Entry/viewEntry'),
'formbuilder2/templates' => array('action' => 'formBuilder2_Template/index'),
'formbuilder2/templates/new' => array('action' => 'formBuilder2_Template/editTemplate'),
'formbuilder2/templates/(?P<templateId>\d+)' => array('action' => 'formBuilder2_Template/editTemplate'),
'formbuilder2/templates/(?P<templateId>\d+)/edit' => array('action' => 'formBuilder2_Template/editTemplate'),
'formbuilder2/templates/layouts' => array('action' => 'formBuilder2_Layout/index'),
'formbuilder2/templates/layouts/new' => array('action' => 'formBuilder2_Layout/editLayout'),
'formbuilder2/templates/layouts/(?P<layoutId>\d+)' => array('action' => 'formBuilder2_Layout/editLayout'),
'formbuilder2/templates/layouts/(?P<layoutId>\d+)/edit' => array('action' => 'formBuilder2_Layout/editLayout'),
);
}
}