Skip to content
28 changes: 28 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ This is the title of the administrative interface displayed to the user at the t

This is the directory location of your application's model config files. It's recommended to use a subdirectory of your application config called `administrator`, but you can put it anywhere you like.

You may also use an array of paths, in the event that any packages you're using provide model config files for administrator panels.

/**
* The path to your model config directory
*
* @type array
*/
'model_config_path' => [
app('path') . '/config/administrator',
base_path('vendor/vendorname/packagename/administrator')
],

Be careful of naming conflicts between model files! The array will be processed in order, and the first model file with the given name found will be used.

<a name="settings-config-path"></a>
### Settings Config Path

Expand All @@ -115,6 +129,20 @@ This is the directory location of your application's model config files. It's re

This is the directory location of your application's settings config files. If you want to create settings pages for your admins, you'll store each settings config file in the path above. As with the `model_config_path`, it's up to you how to organize this. The recommended approach is above, but you may just find a more sensible way to organize your config directories.

You may also use an array of paths, in the event that any packages you're using provide settings config files for administrator panels.

/**
* The path to your settings config directory
*
* @type array
*/
'settings_config_path' => [
app('path') . '/config/administrator/settings',
base_path('vendor/vendorname/packagename/administrator/settings')
],

Be careful of naming conflicts between settings files! The array will be processed in order, and the first settings file with the given name found will be used.

<a name="menu"></a>
### Menu

Expand Down
31 changes: 20 additions & 11 deletions src/Frozennode/Administrator/Config/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class Factory {
'title' => 'required|string',
'domain' => 'string',
'middleware' => 'array',
'model_config_path' => 'required|string|directory',
'settings_config_path' => 'required|string|directory',
'model_config_path' => 'required|dir_or_array_of_dirs',
'settings_config_path' => 'required|dir_or_array_of_dirs',
'menu' => 'required|array|not_empty',
'permission' => 'required|callable',
'use_dashboard' => 'required',
Expand Down Expand Up @@ -274,7 +274,7 @@ public function getPath()
{
$path = $this->type === 'settings' ? $this->options['settings_config_path'] : $this->options['model_config_path'];

return rtrim($path, '/') . '/';
return $path;
}

/**
Expand Down Expand Up @@ -314,18 +314,27 @@ public function getItemConfigObject(array $options)
public function fetchConfigFile($name)
{
$name = str_replace($this->getPrefix(), '', $name);
$path = $this->getPath() . $name . '.php';
$path = $this->getPath();

//check that this is a legitimate file
if (is_file($path))
if (is_string($path))
$path = [$path];

foreach ($path as $item)
{
//set the options var
$options = require $path;
$item = rtrim($item, '/') . '/';
$item = $item . $name . '.php';

//check that this is a legitimate file
if (is_file($item))
{
//set the options var
$options = require $item;

//add the name in
$options['name'] = $name;
//add the name in
$options['name'] = $name;

return $options;
return $options;
}
}

return false;
Expand Down
3 changes: 2 additions & 1 deletion src/Frozennode/Administrator/Config/Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Config extends ConfigBase implements ConfigInterface {
* @var array
*/
protected $defaults = array(
'edit_fields' => array(),
'filters' => array(),
'query_filter' => null,
'permission' => true,
Expand Down Expand Up @@ -60,7 +61,7 @@ class Config extends ConfigBase implements ConfigInterface {
'single' => 'required|string',
'model' => 'required|string|eloquent',
'columns' => 'required|array|not_empty',
'edit_fields' => 'required|array|not_empty',
'edit_fields' => 'array',
'filters' => 'array',
'query_filter' => 'callable',
'permission' => 'callable',
Expand Down
12 changes: 8 additions & 4 deletions src/Frozennode/Administrator/Fields/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,15 @@ public function getEditFields($loadRelationships = true, $override = false)
{
$this->editFields = array();

//iterate over each supplied edit field
foreach ($this->config->getOption('edit_fields') as $name => $options)
$fields = $this->config->getOption('edit_fields');

if (is_array($fields))
{
$fieldObject = $this->make($name, $options, $loadRelationships);
$this->editFields[$fieldObject->getOption('field_name')] = $fieldObject;
//iterate over each supplied edit field
foreach ($fields as $name => $options) {
$fieldObject = $this->make($name, $options, $loadRelationships);
$this->editFields[$fieldObject->getOption('field_name')] = $fieldObject;
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/Frozennode/Administrator/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Validator extends \Illuminate\Validation\Validator {
'not_empty' => "The :attribute option must not be empty",
'callable' => "The :attribute option must be a function",
'eloquent' => "The :attribute option must be the string name of a valid Eloquent model",
'dir_or_array_of_dirs' => "The :attribute option must either be a valid directory or an array of valid directories",
);

/**
Expand Down Expand Up @@ -139,6 +140,26 @@ public function validateArray($attribute, $value)
return is_array($value);
}

/**
* Validates that an item is a directory or an array of directories
*/
public function validateDirOrArrayOfDirs($attribute, $value, $parameters)
{
if (!is_array($value))
return is_dir($value);

if (count($value) == 0)
return false;

foreach ($value as $item)
{
if (!is_dir($item))
return false;
}

return true;
}

/**
* Validates that an item is an array
*/
Expand Down
4 changes: 2 additions & 2 deletions src/views/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
</div>

<script type="text/javascript">
var site_url = "<?php echo url() ?>",
var site_url = "<?php echo Request::url() ?>",
base_url = "<?php echo $baseUrl ?>/",
asset_url = "<?php echo $assetUrl ?>",
save_url = "<?php echo route('admin_settings_save', array($config->getOption('name'))) ?>",
Expand All @@ -26,4 +26,4 @@

<script id="settingsTemplate" type="text/html">
<?php echo view("administrator::templates.settings")?>
</script>
</script>
20 changes: 20 additions & 0 deletions tests/Config/ConfigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ public function testFetchConfigWorks()
unlink($filename);
}

public function testFetchConfigWithArrayWorks()
{
$name = 'some_model';
$filename = __DIR__ . '/' . $name . '.php';
file_put_contents($filename, "<?php return array();");
$factory = m::mock('Frozennode\Administrator\Config\Factory[getPath,getPrefix]', array($this->validator, $this->validator, array()));
$factory->shouldReceive('getPath')->once()->andReturn([__DIR__])
->shouldReceive('getPrefix')->once()->andReturn('');
$this->assertEquals($factory->fetchConfigFile($name), array('name' => $name));
unlink($filename);
}

public function testFetchConfigFails()
{
$name = 'some_model';
Expand All @@ -155,4 +167,12 @@ public function testFetchConfigFails()
$this->assertEquals($factory->fetchConfigFile($name), false);
}

public function testFetchConfigWithArrayFails()
{
$name = 'some_model';
$factory = m::mock('Frozennode\Administrator\Config\Factory[getPath,getPrefix]', array($this->validator, $this->validator, array()));
$factory->shouldReceive('getPath')->once()->andReturn([__DIR__])
->shouldReceive('getPrefix')->once()->andReturn('');
$this->assertEquals($factory->fetchConfigFile($name), false);
}
}
13 changes: 13 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@ public function testValidateDirectoryFails()
$this->assertFalse($this->validator->validateDirectory(null, null, null));
}

public function testValidateDirOrArrayOfDirsSucceeds()
{
$this->assertTrue($this->validator->validateDirOrArrayOfDirs(null, __DIR__, null));
$this->assertTrue($this->validator->validateDirOrArrayOfDirs(null, [__DIR__], null));
$this->assertTrue($this->validator->validateDirOrArrayOfDirs(null, [__DIR__,__DIR__], null));
}

public function testValidateDirOrArrayOfDirsFails()
{
$this->assertFalse($this->validator->validateDirOrArrayOfDirs(null, null, null));
$this->assertFalse($this->validator->validateDirOrArrayOfDirs(null, [], null));
}

public function testValidateArraySucceeds()
{
$this->assertTrue($this->validator->validateArray(null, array(), null));
Expand Down