save complex image options

This commit is contained in:
John Bintz 2009-11-12 20:25:46 -05:00
parent 7cc91aa787
commit cc609bc79f
3 changed files with 90 additions and 66 deletions

View File

@ -234,34 +234,42 @@ class ComicPressAdmin {
* Update ComicPress options.
*/
function handle_update_comicpress_options($info) {
foreach (array('helpers', 'options') as $type) {
foreach (array('helpers') as $type) {
$this->comicpress->comicpress_options[$type] = array();
}
foreach ($this->comicpress->comicpress_options as $option => $value) {
if (isset($info[$option])) {
switch ($option) {
case 'comic_dimensions':
case 'rss_dimensions':
case 'archive_dimensions':
case 'mini_dimensions':
if (is_array($info[$option])) {
$dim_parts = array();
$is_valid = true;
foreach (array('width', 'height') as $field) {
$requested_dim = trim($info[$option][$field]);
if ($requested_dim == "") {
$dim_parts[] = $requested_dim;
} else {
if ((int)$requested_dim == $requested_dim) {
$dim_parts[] = $requested_dim;
} else {
$is_valid = false; break;
case 'image_types':
if (is_array($info['image_types'])) {
$this->comicpress->comicpress_options['image_types'] = array();
$defined_default = null;
foreach ($info['image_types'] as $type => $image_info) {
if (is_array($image_info)) {
foreach ($image_info as $field => $field_value) {
$new_value = array();
switch ($field) {
case "default": $defined_default = $type; break;
case "dimensions":
if (is_array($field_value)) {
if (count(array_intersect(array('width', 'height'), array_keys($field_value))) == 2) {
$new_value['dimensions'] = "{$field_value['width']}x{$field_value['height']}";
}
}
break;
}
$this->comicpress->comicpress_options['image_types'][$type] = $new_value;
}
}
}
if ($is_valid) {
$this->comicpress->comicpress_options[$option] = implode("x", $dim_parts);
}
if (is_null($defined_default)) {
$defined_default = array_keys($this->comicpress->comicpress_options['image_types']);
if (!empty($defined_default)) { $defined_default = reset($defined_default); }
}
if (!is_null($defined_default)) {
if (isset($this->comicpress->comicpress_options['image_types'][$defined_default])) {
$this->comicpress->comicpress_options['image_types'][$defined_default]['default'] = true;
}
}
break;

View File

@ -22,7 +22,7 @@ class ComicPressBackend {
function generate_from_id($id) {
$comicpress = ComicPress::get_instance();
foreach ($comicpress->backends as $backend) {
$result = $backend->generate_from_id($id);
$result = call_user_func(array($backend, 'generate_from_id'), $id);
if ($result !== false) {
return $result;
}

View File

@ -27,31 +27,58 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase {
function providerTestHandleUpdateComicPressOptions() {
return array(
array(
array('comic_dimensions' => '150x150'),
array('cp' => array(
'comic_dimensions' => 'test'
)),
array('comic_dimensions' => '150x150')
array(
'image_types' => array(
'comic' => array('default' => true, 'dimensions' => '500x50')
)
),
array(
array('comic_dimensions' => '150x150'),
array('cp' => array(
'comic_dimensions' => array(
'width' => '150',
'height' => ''
'image_types' => array(
'comic' => array('default' => 'yes', 'dimensions' => array('width' => '100', 'height' => '100'))
)
)),
array('comic_dimensions' => '150x')
),
array(
array('comic_dimensions' => '150x150'),
array('cp' => array(
'comic_dimensions' => array(
'width' => '150.1',
'height' => ''
'image_types' => array(
'comic' => array('default' => true, 'dimensions' => '100x100')
)
)
),
array(
array(
'image_types' => array(
'comic' => array('default' => true, 'dimensions' => '500x50')
)
),
array(
'image_types' => array(
'comic' => array('dimensions' => array('width' => '500', 'height' => '50')),
'archive' => array('default' => 'yes', 'dimensions' => array('width' => '100', 'height' => '100')),
)
),
array(
'image_types' => array(
'comic' => array('dimensions' => '500x50'),
'archive' => array('default' => true, 'dimensions' => '100x100'),
)
)
),
array(
array(
'image_types' => array(
'comic' => array('default' => true, 'dimensions' => '500x50'),
'archive' => array('dimensions' => '100x100'),
)
),
array(
'image_types' => array(
'archive' => array('default' => 'yes', 'dimensions' => array('width' => '100', 'height' => '100')),
)
),
array(
'image_types' => array(
'archive' => array('default' => true, 'dimensions' => '100x100'),
)
)
)),
array('comic_dimensions' => '150x150')
),
);
}
@ -61,20 +88,9 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase {
*/
function testHandleUpdateComicPressOptions($original, $change, $new) {
$this->admin->comicpress = $this->getMock('ComicPress', array('save', 'init'));
$this->admin->comicpress->comicpress_options = array(
'comic_dimensions' => '760x',
'rss_dimensions' => '350x',
'archive_dimensions' => '125x'
);
$this->admin->comicpress->comicpress_options = array_merge($this->admin->comicpress->comicpress_options, $original);
add_category(2, (object)array('name' => 'test'));
$_POST = $change;
if (isset($_POST['cp'])) {
$this->admin->handle_update_comicpress_options($_POST['cp']);
}
$this->admin->handle_update_comicpress_options($change);
foreach ($new as $key => $value) {
$this->assertEquals($value, $this->admin->comicpress->comicpress_options[$key]);