Skip to content

Commit

Permalink
Merge pull request #5 from itmundi/global-settings-additions
Browse files Browse the repository at this point in the history
Global settings additions
  • Loading branch information
hubertprein committed Oct 13, 2015
2 parents e6c3fae + 168d3f7 commit 0658318
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 3 deletions.
85 changes: 83 additions & 2 deletions AmFormsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

class AmFormsPlugin extends BasePlugin
{
/**
* @return null|string
*/
public function getName()
{
if (craft()->plugins->getPlugin('amforms')) {
Expand All @@ -20,21 +23,96 @@ public function getName()
return Craft::t('a&m forms');
}

/**
* @return string
*/
public function getVersion()
{
return '1.1.2';
return '1.1.3';
}

/**
* @return string
*/
public function getDeveloper()
{
return 'a&m impact';
}

/**
* @return string
*/
public function getDeveloperUrl()
{
return 'http://www.am-impact.nl';
}

/**
* @return string
*/
public function getSettingsUrl()
{
return 'amforms/settings';
}

/**
* @return Model
*/
public function getSettings()
{
$settings = craft()->amForms_settings->getAllSettings();
$settings = array_map(function (AmForms_SettingModel $settings) {
return $settings->value;
}, $settings);

$settingsModel = new Model($settings);
$settingsModel->setAttributes($settings);

return $settingsModel;
}

/**
* @inheritDoc
*
* @param array|BaseModel $values
*
* @return null
*/
public function setSettings($values)
{
if ($values)
{
if ($values instanceof BaseModel)
{
$values = $values->attributes;
}

// Get all available settings for this type
$availableSettings = craft()->amForms_settings->getAllSettings();

// Save each available setting
foreach ($availableSettings as $setting) {
// Find new settings
if (array_key_exists($setting->handle, $values)) {
$setting->value = $values[$setting->handle];
craft()->amForms_settings->saveSettings($setting);
}
}
}
}

/**
* @inheritdoc
* @param array $values
* @return Model
*/
public function prepSettings($values)
{
$settingsModel = new Model($values);
$settingsModel->setAttributes($values);
return $settingsModel;
}

/**
* Plugin has control panel section.
*
Expand Down Expand Up @@ -105,6 +183,9 @@ public function registerCpRoutes()
),
'amforms/settings/recaptcha' => array(
'action' => 'amForms/settings/recaptcha'
),
'amforms/settings/templates' => array(
'action' => 'amForms/settings/templates'
)
);
}
Expand All @@ -117,7 +198,7 @@ public function registerCpRoutes()
public function registerUserPermissions()
{
return array(
'accessAmFormsExports' => array(
'accessAmFormsExports' => array(
'label' => Craft::t('Access to exports')
),
'accessAmFormsFields' => array(
Expand Down
16 changes: 16 additions & 0 deletions controllers/AmForms_SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ public function actionRecaptcha()
$this->renderTemplate('amForms/settings/recaptcha', $variables);
}

/**
* Show reCAPTCHA settings.
*/
public function actionTemplates()
{
$variables = array(
'type' => AmFormsModel::SettingsTemplatePaths,
'templates' => craft()->amForms_settings->getAllSettingsByType(AmFormsModel::SettingsTemplatePaths)
);
$this->renderTemplate('amForms/settings/templates', $variables);
}

/**
* Saves settings.
*/
Expand Down Expand Up @@ -111,6 +123,10 @@ private function _saveSettings($type)
}
}

/** @var AmFormsPlugin $plugin */
$plugin = craft()->plugins->getPlugin('amForms');
craft()->plugins->savePluginSettings($plugin, $plugin->getSettings());

if ($success) {
craft()->userSession->setNotice(Craft::t('Settings saved.'));
}
Expand Down
36 changes: 36 additions & 0 deletions migrations/m151009_122433_amforms_addGlobalTemplateSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Craft;

/**
* The class name is the UTC timestamp in the format of mYYMMDD_HHMMSS_pluginHandle_migrationName
*/
class m151009_122433_amforms_addGlobalTemplateSettings extends BaseMigration
{
/**
* Any migration code in here is wrapped inside of a transaction.
*
* @return bool
*/
public function safeUp()
{
$settings = array(
array(
'name' => 'Form template',
'value' => ''
),
array(
'name' => 'Tab template',
'value' => ''
),
array(
'name' => 'Field template',
'value' => ''
),
array(
'name' => 'Notification template',
'value' => ''
),
);
return craft()->amForms_install->installSettings($settings, AmFormsModel::SettingsTemplatePaths);
}
}
1 change: 1 addition & 0 deletions models/AmFormsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ class AmFormsModel extends BaseModel
const SettingExport = 'export';
const SettingAntispam = 'antispam';
const SettingRecaptcha = 'recaptcha';
const SettingsTemplatePaths = 'templates';
}
5 changes: 5 additions & 0 deletions services/AmFormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function getDisplayTemplateInfo($defaultTemplate, $overrideTemplate)
{
// Plugin's default template path
$templatePath = craft()->path->getPluginsPath() . 'amforms/templates/_display/templates/';
$templateSetting = craft()->amForms_settings->getSettingsByHandleAndType($defaultTemplate . 'Template', AmFormsModel::SettingsTemplatePaths);

if (empty($overrideTemplate) && $templateSetting) {
$overrideTemplate = $templateSetting->value;
}

// Is the override template set?
if ($overrideTemplate) {
Expand Down
30 changes: 30 additions & 0 deletions services/AmForms_InstallService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function install()
$this->_installExport();
$this->_installAntiSpam();
$this->_installRecaptcha();
$this->_installTemplates();
$this->_installFields();
}

Expand Down Expand Up @@ -47,13 +48,15 @@ public function installSettings(array $settings, $settingType)
$settingRecord->value = $setting['value'];
$settingRecord->save();
}
return true;
}

/**
* Remove a set of settings.
*
* @param array $settings
* @param string $settingType
* @return bool
*/
public function removeSettings(array $settings, $settingType)
{
Expand All @@ -69,6 +72,7 @@ public function removeSettings(array $settings, $settingType)
craft()->amForms_settings->deleteSettingById($setting->id);
}
}
return true;
}

/**
Expand Down Expand Up @@ -191,6 +195,32 @@ private function _installRecaptcha()
$this->installSettings($settings, AmFormsModel::SettingRecaptcha);
}

/**
* Install gloval templates settings
*/
private function _installTemplates()
{
$settings = array(
array(
'name' => 'Form template',
'value' => ''
),
array(
'name' => 'Tab template',
'value' => ''
),
array(
'name' => 'Field template',
'value' => ''
),
array(
'name' => 'Notification template',
'value' => ''
),
);
$this->installSettings($settings, AmFormsModel::SettingsTemplatePaths);
}

/**
* Install Craft fields.
*/
Expand Down
9 changes: 9 additions & 0 deletions services/AmForms_SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public function getAllSettingsByType($type, $enabled = '*')
return null;
}

/**
* @return array
*/
public function getAllSettings()
{
$settingRecords = AmForms_SettingRecord::model()->ordered()->findAll();
return AmForms_SettingModel::populateModels($settingRecords, 'handle');
}

/**
* Get a setting by their handle and type.
*
Expand Down
3 changes: 2 additions & 1 deletion templates/_layouts/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
general: { label: 'General'|t, url: url('amforms/settings') },
exports: { label: 'Exports'|t, url: url('amforms/settings/exports') },
antispam: { label: 'AntiSpam'|t, url: url('amforms/settings/antispam') },
recaptcha: { label: 'reCAPTCHA'|t, url: url('amforms/settings/recaptcha') }
recaptcha: { label: 'reCAPTCHA'|t, url: url('amforms/settings/recaptcha') },
templates: { label: 'Templates'|t, url: url('amforms/settings/templates') }
} %}

{% set content %}
Expand Down
42 changes: 42 additions & 0 deletions templates/settings/templates.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% extends 'amforms/_layouts/settings' %}

{% set title = 'Templates'|t %}
{% set selectedTab = 'templates' %}

{% block fields %}
{{ forms.textField({
label: templates.formTemplate.name|t,
id: templates.formTemplate.handle,
name: templates.formTemplate.handle,
value: templates.formTemplate.value,
placeholder: '_amforms/form',
instructions: 'Override default template with your own template. Enter the folder (with template), e.g.: {template}'|t({'template': '_amforms'})
}) }}

{{ forms.textField({
label: templates.tabTemplate.name|t,
id: templates.tabTemplate.handle,
name: templates.tabTemplate.handle,
value: templates.tabTemplate.value,
placeholder: '_amforms/tab',
instructions: 'Override default template with your own template. Enter the folder (with template), e.g.: {template}'|t({'template': '_amforms'})
}) }}

{{ forms.textField({
label: templates.fieldTemplate.name|t,
id: templates.fieldTemplate.handle,
name: templates.fieldTemplate.handle,
value: templates.fieldTemplate.value,
placeholder: '_amforms/field',
instructions: 'Override default template with your own template. Enter the folder (with template), e.g.: {template}'|t({'template': '_amforms'})
}) }}

{{ forms.textField({
label: templates.notificationTemplate.name|t,
id: templates.notificationTemplate.handle,
name: templates.notificationTemplate.handle,
value: templates.notificationTemplate.value,
placeholder: '_amforms/email',
instructions: 'Override default template with your own template. Enter the folder (with template), e.g.: {template}'|t({'template': '_amforms'})
}) }}
{% endblock %}

0 comments on commit 0658318

Please sign in to comment.