Skip to content

Commit

Permalink
Added support for forms, closes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed May 28, 2015
1 parent 99cf216 commit ee42e6c
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 19 deletions.
3 changes: 3 additions & 0 deletions config/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
'MadeYourDay\\Contao\\CustomElementsConvert' => 'system/modules/rocksolid-custom-elements/src/MadeYourDay/Contao/CustomElementsConvert.php',
'MadeYourDay\\Contao\\Module\\CustomModule' => 'system/modules/rocksolid-custom-elements/src/MadeYourDay/Contao/Module/CustomModule.php',
'MadeYourDay\\Contao\\Element\\CustomElement' => 'system/modules/rocksolid-custom-elements/src/MadeYourDay/Contao/Element/CustomElement.php',
'MadeYourDay\\Contao\\Form\\CustomWidget' => 'system/modules/rocksolid-custom-elements/src/MadeYourDay/Contao/Form/CustomWidget.php',
'MadeYourDay\\Contao\\Model\\DummyModel' => 'system/modules/rocksolid-custom-elements/src/MadeYourDay/Contao/Model/DummyModel.php',
'MadeYourDay\\Contao\\Template\\CustomTemplate' => 'system/modules/rocksolid-custom-elements/src/MadeYourDay/Contao/Template/CustomTemplate.php',
'MadeYourDay\\Contao\\Widget\\ListStart' => 'system/modules/rocksolid-custom-elements/src/MadeYourDay/Contao/Widget/ListStart.php',
'MadeYourDay\\Contao\\Widget\\ListStop' => 'system/modules/rocksolid-custom-elements/src/MadeYourDay/Contao/Widget/ListStop.php',
Expand All @@ -27,6 +29,7 @@
));

TemplateLoader::addFiles(array(
'form_rsce_plain' => 'system/modules/rocksolid-custom-elements/templates',
'be_rsce_list' => 'system/modules/rocksolid-custom-elements/templates',
'be_rsce_group' => 'system/modules/rocksolid-custom-elements/templates',
'be_rsce_hidden' => 'system/modules/rocksolid-custom-elements/templates',
Expand Down
25 changes: 25 additions & 0 deletions dca/tl_form_field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/*
* Copyright MADE/YOUR/DAY OG <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* RockSolid Custom Elements DCA
*
* @author Martin Auswöger <[email protected]>
*/

$GLOBALS['TL_DCA']['tl_form_field']['config']['onload_callback'][] = array('MadeYourDay\Contao\CustomElements', 'onloadCallback');
$GLOBALS['TL_DCA']['tl_form_field']['config']['onsubmit_callback'][] = array('MadeYourDay\Contao\CustomElements', 'onsubmitCallback');
$GLOBALS['TL_DCA']['tl_form_field']['fields']['rsce_data'] = array(
'label' => &$GLOBALS['TL_LANG']['tl_form_field']['rsce_data'],
'exclude' => true,
'inputType' => 'rsce_list_hidden',
'sql' => "mediumblob NULL",
'save_callback' => array(
array('MadeYourDay\\Contao\\CustomElements', 'saveDataCallback'),
),
);
15 changes: 15 additions & 0 deletions languages/de/tl_form_field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/*
* Copyright MADE/YOUR/DAY OG <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* RockSolid Custom Elements tl_form_field translations
*
* @author Martin Auswöger <[email protected]>
*/

$GLOBALS['TL_LANG']['tl_form_field']['rsce_data'][0] = 'Custom Elements Daten';
15 changes: 15 additions & 0 deletions languages/en/tl_form_field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/*
* Copyright MADE/YOUR/DAY OG <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* RockSolid Custom Elements tl_form_field translations
*
* @author Martin Auswöger <[email protected]>
*/

$GLOBALS['TL_LANG']['tl_form_field']['rsce_data'][0] = 'Custom Elements Data';
60 changes: 41 additions & 19 deletions src/MadeYourDay/Contao/CustomElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use MadeYourDay\Contao\Template\CustomTemplate;

/**
* RockSolid Custom Elements DCA (tl_content and tl_module)
* RockSolid Custom Elements DCA (tl_content, tl_module and tl_form_field)
*
* Provide miscellaneous methods that are used by the data configuration arrays.
*
Expand All @@ -37,7 +37,7 @@ class CustomElements
protected $fieldsConfig = array();

/**
* tl_content and tl_module DCA onload callback
* tl_content, tl_module and tl_form_field DCA onload callback
*
* Reloads config and creates the DCA fields
*
Expand Down Expand Up @@ -85,7 +85,7 @@ public function onloadCallback($dc)
}

/**
* tl_content and tl_module DCA onsubmit callback
* tl_content, tl_module and tl_form_field DCA onsubmit callback
*
* Creates empty arrays for empty lists if no data is available
* (e.g. for new elements)
Expand Down Expand Up @@ -417,7 +417,7 @@ protected function createDca($dc, $type, $createFromPost = false, $tmpField = nu
$standardFields
);

$GLOBALS['TL_LANG'][$dc->table]['rsce_legend'] = $GLOBALS['TL_LANG'][$dc->table === 'tl_content' ? 'CTE' : 'FMD'][$type][0];
$GLOBALS['TL_LANG'][$dc->table]['rsce_legend'] = $GLOBALS['TL_LANG'][$dc->table === 'tl_content' ? 'CTE' : ($dc->table === 'tl_module' ? 'FMD' : 'FFL')][$type][0];
}

/**
Expand Down Expand Up @@ -820,7 +820,7 @@ public static function getConfigByType($type)
/**
* Generates the palette definition
*
* @param string $table "tl_content" or "tl_module"
* @param string $table "tl_content", "tl_module" or "tl_form_field"
* @param array $paletteFields Palette fields
* @param array $standardFields Standard fields
* @return string Palette definition
Expand All @@ -838,7 +838,7 @@ protected static function generatePalette($table, array $paletteFields = array()
}
else {
$palette .= '{type_legend},type';
if (in_array('headline', $standardFields)) {
if ($table === 'tl_content' && in_array('headline', $standardFields)) {
$palette .= ',headline';
}
if (in_array('columns', $standardFields)) {
Expand All @@ -865,13 +865,20 @@ protected static function generatePalette($table, array $paletteFields = array()
$palette .= ';{image_legend},addImage';
}

$palette .= ';{protected_legend:hide},protected;{expert_legend:hide},guests';

if (in_array('cssID', $standardFields)) {
$palette .= ',cssID';
if ($table === 'tl_form_field') {
$palette .= ';{expert_legend:hide},class';
}
if (in_array('space', $standardFields)) {
$palette .= ',space';
else {

$palette .= ';{protected_legend:hide},protected;{expert_legend:hide},guests';

if (in_array('cssID', $standardFields)) {
$palette .= ',cssID';
}
if (in_array('space', $standardFields)) {
$palette .= ',space';
}

}

if ($table === 'tl_content') {
Expand Down Expand Up @@ -934,7 +941,7 @@ public static function purgeCache()
}

/**
* Load the TL_CTE and FE_MOD configuration and use caching if possible
* Load the TL_CTE, FE_MOD and TL_FFL configuration and use caching if possible
*
* @param bool $bypassCache
* @return void
Expand Down Expand Up @@ -1058,7 +1065,7 @@ function ($count) {
'config' => $config,
'label' => isset($config['label']) ? $config['label'] : array(implode(' ', array_map('ucfirst', explode('_', substr($template, 5)))), ''),
'labelPrefix' => '',
'types' => isset($config['types']) ? $config['types'] : array('content', 'module'),
'types' => isset($config['types']) ? $config['types'] : array('content', 'module', 'form'),
'contentCategory' => isset($config['contentCategory']) ? $config['contentCategory'] : 'custom_elements',
'moduleCategory' => isset($config['moduleCategory']) ? $config['moduleCategory'] : 'custom_elements',
'template' => $template,
Expand Down Expand Up @@ -1110,11 +1117,6 @@ function ($count) {
$contents[] = '$GLOBALS[\'TL_LANG\'][\'CTE\'][\'' . $element['template'] . '\'][0] = ' . var_export($element['labelPrefix'], true) . ' . $GLOBALS[\'TL_LANG\'][\'CTE\'][\'' . $element['template'] . '\'][0];';
}

if (!empty($element['config']['wrapper']['type'])) {
$GLOBALS['TL_WRAPPERS'][$element['config']['wrapper']['type']][] = $element['template'];
$contents[] = '$GLOBALS[\'TL_WRAPPERS\'][' . var_export($element['config']['wrapper']['type'], true) . '][] = ' . var_export($element['template'], true) . ';';
}

}

if (in_array('module', $element['types'])) {
Expand All @@ -1132,6 +1134,26 @@ function ($count) {

}

if (in_array('form', $element['types'])) {

$GLOBALS['TL_FFL'][$element['template']] = 'MadeYourDay\\Contao\\Form\\CustomWidget';
$contents[] = '$GLOBALS[\'TL_FFL\'][\'' . $element['template'] . '\'] = \'MadeYourDay\\\\Contao\\\\Form\\\\CustomWidget\';';

$GLOBALS['TL_LANG']['FFL'][$element['template']] = static::getLabelTranslated($element['label']);
$contents[] = '$GLOBALS[\'TL_LANG\'][\'FFL\'][\'' . $element['template'] . '\'] = \\MadeYourDay\\Contao\\CustomElements::getLabelTranslated(' . var_export($element['label'], true) . ');';

if ($addLabelPrefix && $element['labelPrefix']) {
$GLOBALS['TL_LANG']['FFL'][$element['template']][0] = $element['labelPrefix'] . $GLOBALS['TL_LANG']['FFL'][$element['template']][0];
$contents[] = '$GLOBALS[\'TL_LANG\'][\'FFL\'][\'' . $element['template'] . '\'][0] = ' . var_export($element['labelPrefix'], true) . ' . $GLOBALS[\'TL_LANG\'][\'FFL\'][\'' . $element['template'] . '\'][0];';
}

}

if (!empty($element['config']['wrapper']['type'])) {
$GLOBALS['TL_WRAPPERS'][$element['config']['wrapper']['type']][] = $element['template'];
$contents[] = '$GLOBALS[\'TL_WRAPPERS\'][' . var_export($element['config']['wrapper']['type'], true) . '][] = ' . var_export($element['template'], true) . ';';
}

}

$file = new \File($filePath, true);
Expand Down
25 changes: 25 additions & 0 deletions src/MadeYourDay/Contao/CustomElementsConvert.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,31 @@ public function run()

}

$formElements = \FormFieldModel::findBy(array(\FormFieldModel::getTable() . '.type LIKE ?'), 'rsce_%');

while ($formElements && $formElements->next()) {

$html = $this->getHtmlFromElement($formElements);

if (!$html) {
$failedElements[] = array('form', $formElements->id, $formElements->type);
}
else {

$this->createInitialVersion(\FormFieldModel::getTable(), $formElements->id);

$this->Database
->prepare('UPDATE ' . \FormFieldModel::getTable() . ' SET tstamp = ?, type = \'html\', html = ? WHERE id = ?')
->executeUncached(time(), $html, $formElements->id);
$elementsCount++;

$this->createNewVersion(\FormFieldModel::getTable(), $formElements->id);
$this->log('A new version of record "' . \FormFieldModel::getTable() . '.id=' . $formElements->id . '" has been created', __METHOD__, TL_GENERAL);

}

}

foreach ($failedElements as $element) {
$this->log('Failed to convert ' . $element[0] . ' element ID ' . $element[1] . ' (' . $element[2] . ') to a standard HTML element', __METHOD__, TL_ERROR);
}
Expand Down
60 changes: 60 additions & 0 deletions src/MadeYourDay/Contao/Form/CustomWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/*
* Copyright MADE/YOUR/DAY OG <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace MadeYourDay\Contao\Form;

use MadeYourDay\Contao\Element\CustomElement;
use MadeYourDay\Contao\Model\DummyModel;

/**
* Custom form widget
*
* @author Martin Auswöger <[email protected]>
*/
class CustomWidget extends \Widget
{
/**
* @var string Template
*/
protected $strTemplate = 'form_rsce_plain';

/**
* @var CustomElement
*/
private $customElement;

/**
* {@inheritdoc}
*/
public function __construct($data = null)
{
if (!empty($data['class'])) {
$data['cssID'] = serialize(array('', $data['class']));
}

$this->customElement = new CustomElement(new DummyModel(null, $data));

parent::__construct($data);
}

/**
* {@inheritdoc}
*/
public function generate()
{
return $this->customElement->generate();
}

/**
* Do not validate
*/
public function validate()
{
return;
}
}
26 changes: 26 additions & 0 deletions src/MadeYourDay/Contao/Model/DummyModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/*
* Copyright MADE/YOUR/DAY OG <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace MadeYourDay\Contao\Model;

/**
* Dummy model
*
* @author Martin Auswöger <[email protected]>
*/
class DummyModel extends \Model
{
/**
* {@inheritdoc}
*/
public function __construct(\Database\Result $objResult = null, $data = array())
{
$this->arrModified = array();
$this->setRow($data);
}
}
2 changes: 2 additions & 0 deletions templates/form_rsce_plain.html5
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
echo $this->generate();

0 comments on commit ee42e6c

Please sign in to comment.