Merge ajax select widget custom field + border & background color

This commit is contained in:
Nathan Gray 2010-01-20 23:39:39 +00:00
parent 230397b17c
commit a7164c6de0
4 changed files with 188 additions and 17 deletions

View File

@ -21,6 +21,7 @@
* This widget can get data from any function that can provide data to a nextmatch widget.
* This widget is generating html, so it does not work (without an extra implementation) in an other UI
*/
class ajax_select_widget
{
var $public_functions = array(
@ -30,6 +31,29 @@ class ajax_select_widget
);
var $human_name = 'AJAX Select'; // this is the name for the editor
// Accepted option keys if you're passing in an array to set up the widget
// Additional options will be passed to the search query
public static $known_options = array(
// These ones can be passed in from eTemplate editor in size
'get_rows',
'get_title',
'id_field',
'template',
'filter',
'filter2',
'link',
'icon',
// Pass by code only
'values',
);
// Flag used in id_field to indicate that the key of the record should be used as the value
const ARRAY_KEY = 'array_key';
// Array of static values to emulate a combo-box, with no DB lookup
protected static $static_values = array();
private $debug = false;
function ajax_select_widget($ui='')
@ -103,6 +127,13 @@ class ajax_select_widget
$options['template'] = 'etemplate.ajax_select_widget.row';
}
if(array_key_exists('values', $options)) {
if($options['values']) {
self::$static_values[$name] = $options['values'];
}
unset($options['values']);
}
$onchange = ($cell['onchange'] ? $cell['onchange'] : 'false');
// Set current value
@ -121,10 +152,15 @@ class ajax_select_widget
$title_obj =& CreateObject($title_app . '.' . $title_class);
}
}
if(!is_object($title_obj) || !method_exists($title_obj,$title_method)) {
echo "$entry_app.$entry_class.$entry_method is not a valid method for getting the title";
} elseif($current_value) {
$title = $title_obj->$title_method($current_value);
if($title_method == 'array_title' && $title_class == __CLASS__) {
$title = self::$title_method($current_value, $name);
} else {
$title = $title_obj->$title_method($current_value);
}
}
// Check get_rows method
@ -146,7 +182,8 @@ class ajax_select_widget
$cell['type'] = 'template';
$cell['size'] = $cell['name'];
$value = array('value' => $current_value, 'search' => $title);
$widget =& new etemplate('etemplate.ajax_select_widget');
$widget = new etemplate('etemplate.ajax_select_widget');
$widget->no_onclick = True;
// Link if readonly & link is set
@ -165,6 +202,9 @@ class ajax_select_widget
} else {
$search['type'] = 'text';
$search['size'] = '';
if($current_value == '' && $options['id_field'] == self::ARRAY_KEY) {
$search['blur'] = lang('Search...');
}
}
// Icon
@ -173,6 +213,11 @@ class ajax_select_widget
$cell['obj'] = &$widget;
// Save static values, if set
if(self::$static_values[$name]) {
$extension_data['values'] = self::$static_values[$name];
}
// Save options for post_processing
$extension_data['options'] = $options;
$extension_data['needed'] = $cell['needed'];
@ -181,6 +226,12 @@ class ajax_select_widget
$GLOBALS['egw_info']['flags']['include_xajax'] = True;
// JavaScript
// converter doesn't handle numeric well
foreach($options as $key => &$value) {
if(is_numeric($value)) {
$value = (string)$value;
}
}
$options = $GLOBALS['egw']->js->convert_phparray_jsarray("options['$name']", $options, true);
$GLOBALS['egw']->js->set_onload("if(!options) {
var options = new Object();
@ -250,12 +301,24 @@ class ajax_select_widget
}
return $return;
} else {
$value = $value_in['value'];
if (stripos($extension_data['options']['id_field'], ";")) {
$expected_fields = array_flip(explode(";", $extension_data['options']['id_field']));
$fields_n_values = explode(";", $value_in['value']);
foreach ($fields_n_values as $field_n_value) {
list($myfield, $myvalue) = explode(":", $field_n_value);
if (array_key_exists($myfield, $expected_fields)) {
$value_in[$myfield] = $myvalue;
}
}
$value = $value_in;
} else {
$value = $value_in['value'];
}
return true;
}
}
function ajax_search($id, $value, $set_id, $query) {
function ajax_search($id, $value, $set_id, $query, $etemplate_id) {
$base_id = substr($id, 0, strrpos($id, '['));
$result_id = ($set_id ? $set_id : $base_id . '[results]');
$response = new xajaxResponse();
@ -269,7 +332,7 @@ class ajax_select_widget
// Expand lists
foreach($query as $key => &$row) {
if(strpos($row, ',')) {
if($row && strpos($row, ',')) {
$query[$key] = explode(',', $row);
}
@ -281,6 +344,19 @@ class ajax_select_widget
}
}
$query['search'] = $value;
if($query['id_field'] == self::ARRAY_KEY) {
// Pass base_id so we can get the right values
$query['field_name'] = $base_id;
// Check for a provided list of values
if($request = etemplate_request::read($etemplate_id)) {
$extension_data = $request->extension_data[$base_id];
if(is_array($extension_data) && $extension_data['values']) {
self::$static_values[$base_id] = $extension_data['values'];
}
}
}
$result_list = array();
$readonlys = array();
@ -301,21 +377,53 @@ class ajax_select_widget
}
foreach($result_list as $key => &$row) {
if(!is_array($row)) {
continue;
}
if($query['id_field'] && $query['get_title']) {
if($row[$query['id_field']]) {
$row['title'] = ExecMethod($query['get_title'], $row[$query['id_field']]);
if($query['id_field'] == self::ARRAY_KEY) {
if(!is_array($row)) {
// Restructure $row to be an array
$row = array(
self::ARRAY_KEY => $key,
'id_field' => $key,
'title' => $row
);
}
} else {
continue;
}
}
//check for multiple id's
//this if control statement is to determine if there are multiple ids in the ID FIELD of the Ajax Widget
if(stristr($query['id_field'], ';') != FALSE) {
$id_field_keys = explode(';', $query['id_field']);
if($query['get_title']) {
//the title will always be created using the first ID FIELD
if($row[$id_field_keys[0]]) {
$row['title'] = ExecMethod($query['get_title'], $row[$id_field_keys[0]]);
}
}
foreach($id_field_keys as $value) {
$id_field_keys_values[] = $value.':'.$row[$value];
}
$row['id_field'] = implode(';',$id_field_keys_values);
unset($id_field_keys_values);
} else {
if($query['id_field'] && $query['get_title']) {
if($row[$query['id_field']] && $query['id_field'] != self::ARRAY_KEY) {
$row['title'] = ExecMethod($query['get_title'], $row[$query['id_field']]);
}
}
if($query['id_field'] != self::ARRAY_KEY) {
$row['id_field'] = $row[$query['id_field']];
}
}
// If we use htmlspecialchars, it causes issues with mixed quotes. addslashes() seems to handle it.
$row['id_field'] = addslashes($row['id_field']);
$data = ($query['nextmatch_template']) ? array(1=>$row) : $row;
$widget =& CreateObject('etemplate.etemplate', $query['template']);
$html = addslashes(str_replace("\n", '', $widget->show($data, '', $readonlys)));
// If we use htmlspecialchars, it causes issues with mixed quotes. addslashes() seems to handle it.
$row['id_field'] = addslashes($row[$query['id_field']]);
$row['title'] = addslashes($row['title']);
$response->addScript("add_ajax_result('$result_id', '${row['id_field']}', '" . $row['title'] . "', '$html');");
$count++;
if($count > $GLOBALS['egw_info']['user']['preferences']['common']['maxmatchs']) {
@ -328,4 +436,30 @@ class ajax_select_widget
}
return $response->getXML();
}
/**
* Use a simple array to get the title
* Values should be passed in to the widget as an array in $size['values']
*/
protected function array_title($id, $name) {
if(trim($id) == '') {
return lang('Search');
}
return self::$static_values[$name][$id];
}
/**
* Use a simple array to get the results
* Values should be passed in to the widget as an array in $size['values']
*/
protected function array_rows(&$query, &$result) {
foreach( self::$static_values[$query['field_name']] as $key => $value) {
if($query['search'] && stripos($value, $query['search']) === false) continue;
$result[$key] = $value;
}
$count = count($result);
$result = array_slice($result, $query['start'], $query['num_rows']);
return $count;
}
}

View File

@ -45,6 +45,7 @@ class customfields_widget
'text' => 'Text',
'label' => 'Label',
'select' => 'Selectbox',
'ajax_select' => 'Search',
'radio' => 'Radiobutton',
'checkbox' => 'Checkbox',
'date' => 'Date',
@ -235,6 +236,34 @@ class customfields_widget
*/
}
break;
case 'ajax_select' :
// Set some reasonable defaults for the widget
$options = array(
'get_title' => 'etemplate.ajax_select_widget.array_title',
'get_rows' => 'etemplate.ajax_select_widget.array_rows',
'id_field' => ajax_select_widget::ARRAY_KEY,
);
if($field['rows']) {
$options['num_rows'] = $field['rows'];
}
// If you specify an option known to the AJAX Select widget, it will be pulled from the list of values
// and used as such. All unknown values will be used for selection, not passed through to the query
if (isset($field['values']['@']))
{
$options['values'] = $this->_get_options_from_file($field['values']['@']);
unset($field['values']['@']);
} else {
$options['values'] = array_diff_key($field['values'], array_flip(ajax_select_widget::$known_options));
}
$options = array_merge($options, array_intersect_key($field['values'], array_flip(ajax_select_widget::$known_options)));
$input =& etemplate::empty_cell('ajax_select', $this->prefix.$lname, array(
'readonly' => $readonly,
'no_lang' => True,
'size' => $options
));
break;
case 'label' :
$row_class = 'th';
break;

View File

@ -253,7 +253,7 @@ function change(e, value) {
selects[i].style.visibility = 'hidden';
}
}
xajax_doXMLHTTP(current_app + ".ajax_select_widget.ajax_search.etemplate", id, value, set_id, query);
xajax_doXMLHTTP(current_app + ".ajax_select_widget.ajax_search.etemplate", id, value, set_id, query, document.getElementById('etemplate_exec_id').value);
}

View File

@ -2,7 +2,7 @@
/**
* eGroupWare - eTemplates for Application etemplate
* http://www.egroupware.org
* generated by soetemplate::dump4setup() 2009-02-23 14:22
* generated by soetemplate::dump4setup() 2010-01-13 11:32
*
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @package etemplate
@ -44,11 +44,19 @@ $templ_data[] = array('name' => 'etemplate.ajax_select_widget','template' => '',
}
td.select_search {
display: inline;
margin-left: -7px;
margin-left: -7px;
}
.resultBox {
clear: both;
background-color: white;
border-color: #D3DCE3;
border-width: 1px;
border-style: groove outset outset;
display: none;
}
.resultBox div {
background-color: #F1F1F1;
padding: 0em 1em;
}
.resultBox div:hover {
background-color: #D3DCE3;
@ -57,7 +65,7 @@ div.resultBoxSelected {
background-color: #D3DCE3;
}','modified' => '1176831076',);
$templ_data[] = array('name' => 'etemplate.ajax_select_widget.row','template' => '','lang' => '','group' => '0','version' => '','data' => 'a:1:{i:0;a:4:{s:4:"type";s:4:"grid";s:4:"data";a:2:{i:0;a:0:{}i:1;a:2:{s:1:"A";a:3:{s:4:"type";s:5:"label";s:7:"no_lang";s:1:"1";s:4:"name";s:8:"id_field";}s:1:"B";a:3:{s:4:"type";s:5:"label";s:7:"no_lang";s:1:"1";s:4:"name";s:5:"title";}}}s:4:"rows";i:1;s:4:"cols";i:2;}}','size' => '','style' => '','modified' => '1160761596',);
$templ_data[] = array('name' => 'etemplate.ajax_select_widget.row','template' => '','lang' => '','group' => '0','version' => '','data' => 'a:1:{i:0;a:4:{s:4:"type";s:4:"grid";s:4:"data";a:2:{i:0;a:0:{}i:1;a:1:{s:1:"A";a:3:{s:4:"type";s:5:"label";s:7:"no_lang";s:1:"1";s:4:"name";s:5:"title";}}}s:4:"rows";i:1;s:4:"cols";i:1;}}','size' => '','style' => '','modified' => '1160761596',);
$templ_data[] = array('name' => 'etemplate.date.test','template' => '','lang' => '','group' => '0','version' => '','data' => 'a:1:{i:0;a:4:{s:4:"type";s:4:"grid";s:4:"data";a:4:{i:0;a:1:{s:2:"c2";s:7:",bottom";}i:1;a:2:{s:1:"A";a:4:{s:4:"type";s:9:"date-time";s:4:"span";s:3:"all";s:5:"label";s:4:"Date";s:4:"name";s:4:"date";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}i:2;a:2:{s:1:"A";a:4:{s:4:"type";s:4:"date";s:4:"size";s:9:"d.m.Y H:i";s:5:"label";s:7:"2. Date";s:4:"name";s:5:"date2";}s:1:"B";a:3:{s:4:"type";s:13:"date-timeonly";s:4:"size";s:9:"d.m.Y H:i";s:4:"name";s:5:"date2";}}i:3;a:2:{s:1:"A";a:5:{s:4:"type";s:13:"date-timeonly";s:4:"size";s:3:"H:i";s:4:"span";s:3:"all";s:5:"label";s:4:"Time";s:4:"name";s:4:"time";}s:1:"B";a:1:{s:4:"type";s:5:"label";}}}s:4:"rows";i:3;s:4:"cols";i:2;}}','size' => '','style' => '','modified' => '1108209808',);