Im trying to create a customized version of admin_double_list where each
option tag get a style attribute setting the background image to a
thumbnail. Ive got it displaying the thumbnails just fine, but for some
reason it will not save the addition to the database. Ive essentially copied
2 methods, object_admin_double_list and options_for_select to a new helper
which is called in a customized theme. I havent tweaked anything about those
functions other than adding some paramters and an extra block of logic. I
haavent removed any logic or adjusted anything beyond adding the extra
params and code block. Furthermore the code on the rendered page is
identical to that of a generic admin_double_list (at least as far as I can
see). Also note I havent added/changed any class or id attributes on any of
the elements.
Im sure it must be something simple im overlooking but ill be damned if I
find it.
[code]
<?php
use_helper('ObjectAdmin', 'Form', 'Object');
function object_gallery_admin_double_list($object, $method, $options =
array(), $callback = null)
{
/*
* Identitcal to options object_admin_double_list() except
* that it calls gallery_options_for_select instead of
* options_for_select()
*/
$options = _parse_attributes($options);
$options['multiple'] = true;
$options['class'] = 'sf_admin_multiple';
if (!isset($options['size']))
{
$options['size'] = 10;
}
$label_all = __(isset($options['unassociated_label']) ?
$options['unassociated_label'] : 'Unassociated');
$label_assoc = __(isset($options['associated_label']) ?
$options['associated_label'] : 'Associated');
// get the lists of objects
list($all_objects, $objects_associated, $associated_ids) =
_get_object_list($object, $method, $options, $callback);
$objects_unassociated = array();
foreach ($all_objects as $object)
{
if (!in_array($object->getPrimaryKey(), $associated_ids))
{
$objects_unassociated[] = $object;
}
}
// override field name
unset($options['control_name']);
$name = _convert_method_to_name($method, $options);
$name1 = 'unassociated_'.$name;
$name2 = 'associated_'.$name;
// Here we call our functions instead of the sf functions
$select1 = select_tag($name1,
gallery_options_for_select(_get_options_from_objects($objects_unassociated),
_get_thumbnails_from_objects($objects_unassociated), '', $options),
$options);
$options['class'] = 'sf_admin_multiple-selected';
$select2 = select_tag($name2,
gallery_options_for_select(_get_options_from_objects($objects_associated),
_get_thumbnails_from_objects($objects_unassociated), '', $options),
$options);
$html =
'<div>
<div style="float: left">
<div style="font-weight: bold; padding-bottom: 0.5em">%s</div>
%s
</div>
<div style="float: left">
%s<br />
%s
</div>
<div style="float: left">
<div style="font-weight: bold; padding-bottom: 0.5em">%s</div>
%s
</div>
<br style="clear: both" />
</div>
';
$response = sfContext::getInstance()->getResponse();
$response->addJavascript(sfConfig::get('sf_prototype_web_dir').'/js/prototyp
e');
$response->addJavascript(sfConfig::get('sf_admin_web_dir').'/js/double_list'
);
return sprintf($html,
$label_all,
$select1,
submit_image_tag(sfConfig::get('sf_admin_web_dir').'/images/next.png',
"style=\"border: 0\" onclick=\"double_list_move(\$('{$name1}'),
\$('{$name2}')); return false;\""),
submit_image_tag(sfConfig::get('sf_admin_web_dir').'/images/previous.png',
"style=\"border: 0\" onclick=\"double_list_move(\$('{$name2}'),
\$('{$name1}')); return false;\""),
$label_assoc,
$select2
);
}
function gallery_options_for_select($options, $thumbnails, $selected = '',
$html_options = array())
{
/*
* Identitcal to options_for_select except that
* it also adds the proper settings for the style
* attribute of the <option> to display the thumbnail of
* or the image.
*/
$html_options = _parse_attributes($html_options);
if (is_array($selected))
{
$selected = array_map('strval', array_values($selected));
}
$html = '';
if ($value = _get_option($html_options, 'include_custom'))
{
$html .= content_tag('option', $value, array('value' => ''))."\n";
}
else if (_get_option($html_options, 'include_blank'))
{
$html .= content_tag('option', '', array('value' => ''))."\n";
}
foreach ($options as $key => $value)
{
if (is_array($value))
{
$html .= content_tag('optgroup', options_for_select($value,
$selected, $html_options), array('label' => $key))."\n";
}
else
{
$option_options = array('value' => $key);
// perfrom check for thumbnails and add inline styles
if(array_key_exists($key, $thumbnails))
{
$option_options['style'] = 'background-image:
url('.$thumbnails[$key].'); background-repeat: no-repeat; height: 36px;
padding-left: 39px; margin-bottom: 3px;';
} else {
$option_options['style'] = 'height: 36px; padding-left: 39px;
margin-bottom: 3px;';
}
if (
(is_array($selected) && in_array(strval($key), $selected, true))
||
(strval($key) == strval($selected))
)
{
$option_options['selected'] = 'selected';
}
$html .= content_tag('option', $value, $option_options)."\n";
}
}
return $html;
}
function _get_thumbnails_from_objects($objects, $text_method =
'getThumbnailUri', $args = 'sm')
{
/*
* Retrieves the thumbnail uris from the Media objects
* and returns them in an array assigned the same keys as
* the array returned by _get_options_from_objects()
*
*/
$select_options = array();
if ($objects)
{
// multi primary keys handling
$multi_primary_keys = is_array($objects[0]->getPrimaryKey()) ? true :
false;
// which method to call?
$methodToCall = '';
foreach (array($text_method, '__toString', 'toString', 'getPrimaryKey')
as $method)
{
if (is_callable(array($objects[0], $method)))
{
$methodToCall = $method;
break;
}
}
// construct select option list
foreach ($objects as $tmp_object)
{
$key = $multi_primary_keys ? implode('/',
$tmp_object->getPrimaryKey()) : $tmp_object->getPrimaryKey();
$value = $tmp_object->$methodToCall($args);
$select_options[$key] = $value;
}
}
return $select_options;
}
?>
[/code]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---