Skip to content

Commit

Permalink
Added possibility to prefill a configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
doishub committed Jul 24, 2020
1 parent 3cb6cca commit 8c2c1e1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
]
},
"extra":{
"branch-alias": {
"dev-master": "1.0.x-dev"
},
"contao-manager-plugin": "Oveleon\\ContaoConfigDriverBundle\\ContaoManager\\Plugin"
}
}
40 changes: 36 additions & 4 deletions src/Resources/contao/drivers/DC_Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class DC_Config extends \DataContainer implements \listable, \editable

/**
* Database table
* @var boolean
* @var string
*/
protected $table;

/**
* Database column
* @var boolean
* @var string
*/
protected $column;

Expand All @@ -57,7 +57,7 @@ public function __construct($strTable)
// Build object from global configuration array
$this->strTable = $strTable;

// Set mode
// Set mode
$this->useDatabase = !empty($GLOBALS['TL_DCA'][$this->strTable]['config']['ptable']);

if($this->useDatabase)
Expand All @@ -81,7 +81,19 @@ public function __construct($strTable)
return;
}

// Call onload_callback (e.g. to check permissions)
// Prefill on empty (only database)
if($this->useDatabase)
{
$objDatabase = $this->Database->prepare("SELECT $this->column FROM $this->table WHERE id=?")
->execute($this->intId);

if (!!$GLOBALS['TL_DCA'][$this->strTable]['config']['fillOnEmpty'] && !$objDatabase->{$this->column})
{
$this->prefillConfig();
}
}

// Call onload_callback (e.g. to check permissions)
if (\is_array($GLOBALS['TL_DCA'][$this->strTable]['config']['onload_callback']))
{
foreach ($GLOBALS['TL_DCA'][$this->strTable]['config']['onload_callback'] as $callback)
Expand All @@ -99,6 +111,24 @@ public function __construct($strTable)
}
}

/**
* If the Config has never been saved before, all fields are prefilled
*/
private function prefillConfig()
{
$arrDefaults = [];

foreach ($GLOBALS['TL_DCA'][$this->strTable]['fields'] as $key => $field)
{
$arrDefaults[ $key ] = $field['default'];
}

$deserialize = serialize($arrDefaults);

$this->Database->prepare("UPDATE " . $this->table . " SET " . $this->column . "=? WHERE id=?")
->execute($deserialize, $this->intId);
}

/**
* Automatically switch to edit mode
*
Expand Down Expand Up @@ -708,6 +738,8 @@ protected function save($varValue)
\Config::set($this->strField, $deserialize);
}
}


}

/**
Expand Down

0 comments on commit 8c2c1e1

Please sign in to comment.