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

View File

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

View File

@ -27,31 +27,58 @@ class ComicPressAdminTest extends PHPUnit_Framework_TestCase {
function providerTestHandleUpdateComicPressOptions() { function providerTestHandleUpdateComicPressOptions() {
return array( return array(
array( array(
array('comic_dimensions' => '150x150'), array(
array('cp' => array( 'image_types' => array(
'comic_dimensions' => 'test' 'comic' => array('default' => true, 'dimensions' => '500x50')
)), )
array('comic_dimensions' => '150x150')
), ),
array( array(
array('comic_dimensions' => '150x150'), 'image_types' => array(
array('cp' => array( 'comic' => array('default' => 'yes', 'dimensions' => array('width' => '100', 'height' => '100'))
'comic_dimensions' => array(
'width' => '150',
'height' => ''
) )
)),
array('comic_dimensions' => '150x')
), ),
array( array(
array('comic_dimensions' => '150x150'), 'image_types' => array(
array('cp' => array( 'comic' => array('default' => true, 'dimensions' => '100x100')
'comic_dimensions' => array( )
'width' => '150.1', )
'height' => '' ),
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) { function testHandleUpdateComicPressOptions($original, $change, $new) {
$this->admin->comicpress = $this->getMock('ComicPress', array('save', 'init')); $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); $this->admin->comicpress->comicpress_options = array_merge($this->admin->comicpress->comicpress_options, $original);
add_category(2, (object)array('name' => 'test')); $this->admin->handle_update_comicpress_options($change);
$_POST = $change;
if (isset($_POST['cp'])) {
$this->admin->handle_update_comicpress_options($_POST['cp']);
}
foreach ($new as $key => $value) { foreach ($new as $key => $value) {
$this->assertEquals($value, $this->admin->comicpress->comicpress_options[$key]); $this->assertEquals($value, $this->admin->comicpress->comicpress_options[$key]);