2009-02-24 Esteban Sanchez <estebans@artica.es>

* include/functions_db.php: Fixed a bug in
	format_array_to_where_clause_sql() when only offset and limit where
	given. A new parameter was added to solve it, so the behaviour is a
	bit different now (take a look to the doc examples). Changes were also
	done in other functions to reflect this new situation.

	* include/functions_alerts.php, include/functions_modules.php: Updated
	to changes in format_array_to_where_clause_sql()

	* include/functions_ui.php: Added a class to each link of the
	paginator. Style correction.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1479 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
Esteban Sanchez 2009-02-24 16:24:02 +00:00
parent 876864faeb
commit cfcb9c24ef
5 changed files with 79 additions and 39 deletions

View File

@ -1,3 +1,17 @@
2009-02-24 Esteban Sanchez <estebans@artica.es>
* include/functions_db.php: Fixed a bug in
format_array_to_where_clause_sql() when only offset and limit where
given. A new parameter was added to solve it, so the behaviour is a
bit different now (take a look to the doc examples). Changes were also
done in other functions to reflect this new situation.
* include/functions_alerts.php, include/functions_modules.php: Updated
to changes in format_array_to_where_clause_sql()
* include/functions_ui.php: Added a class to each link of the
paginator. Style correction.
2009-02-24 Esteban Sanchez <estebans@artica.es>
* include/auth/mysql.php: Added get_user_id(). Removed field check on

View File

@ -617,7 +617,7 @@ function get_alerts_agent_module ($id_agent_module, $disabled = false, $filter =
$where .= ' AND disabled = 0 ';
if ($filter) {
$where .= ' AND '.format_array_to_where_clause_sql ($filter);
$where .= format_array_to_where_clause_sql ($filter, 'AND', ' AND ');
}
$sql = sprintf ('SELECT * FROM talert_template_modules

View File

@ -1577,9 +1577,13 @@ function get_db_row_filter ($table, $filter, $fields = false, $where_join = 'AND
}
if (is_array ($filter))
$filter = format_array_to_where_clause_sql ($filter, $where_join);
$filter = format_array_to_where_clause_sql ($filter, $where_join, ' WHERE ');
else if (is_string ($filter))
$filter = 'WHERE '.$filter;
else
$filter = '';
$sql = sprintf ('SELECT %s FROM %s WHERE %s',
$sql = sprintf ('SELECT %s FROM %s %s',
$fields, $table, $filter);
return get_db_row_sql ($sql);
@ -1652,9 +1656,13 @@ function get_db_all_rows_filter ($table, $filter, $fields = false, $where_join =
}
if (is_array ($filter))
$filter = format_array_to_where_clause_sql ($filter, $where_join);
$filter = format_array_to_where_clause_sql ($filter, $where_join, ' WHERE ');
else if (is_string ($filter))
$filter = 'WHERE '.$filter;
else
$filter = '';
$sql = sprintf ('SELECT %s FROM %s WHERE %s',
$sql = sprintf ('SELECT %s FROM %s %s',
$fields, $table, $filter);
return get_db_all_rows_sql ($sql);
@ -1843,8 +1851,7 @@ function format_array_to_update_sql ($values) {
*
* This function is useful to generate a WHERE clause for a SQL sentence from
* a list of values. Example code:
*
* <code>
<code>
$values = array ();
$values['name'] = "Name";
$values['description'] = "Long description";
@ -1861,11 +1868,33 @@ function format_array_to_update_sql ($values) {
* There are special parameters such as 'limit' and 'offset' that will be used
* as LIMIT and OFFSET clauses respectively.
* @param string Join operator. AND by default.
* @param string A prefix to be added to the string. It's useful when limit and
* offset could be given to avoid this cases:
<code>
$values = array ();
$values['limit'] = 10;
$values['offset'] = 20;
$sql = 'SELECT * FROM table WHERE '.format_array_to_where_clause_sql ($values);
// Wrong SQL: SELECT * FROM table WHERE LIMIT 10 OFFSET 20
$values = array ();
$values['limit'] = 10;
$values['offset'] = 20;
$sql = 'SELECT * FROM table WHERE '.format_array_to_where_clause_sql ($values, 'AND', 'WHERE');
// Good SQL: SELECT * FROM table LIMIT 10 OFFSET 20
$values = array ();
$values['value'] = 5;
$values['limit'] = 10;
$values['offset'] = 20;
$sql = 'SELECT * FROM table WHERE '.format_array_to_where_clause_sql ($values, 'AND', 'WHERE');
// Good SQL: SELECT * FROM table WHERE value = 5 LIMIT 10 OFFSET 20
</code>
*
* @return string Values joined into an SQL string that can fits into the WHERE
* clause of an SQL sentence.
*/
function format_array_to_where_clause_sql ($values, $join = 'AND') {
function format_array_to_where_clause_sql ($values, $join = 'AND', $prefix = false) {
$fields = array ();
if (! is_array ($values)) {
@ -1875,6 +1904,15 @@ function format_array_to_where_clause_sql ($values, $join = 'AND') {
$query = '';
$limit = '';
$offset = '';
if (isset ($values['limit'])) {
$limit = sprintf (' LIMIT %d', $values['limit']);
unset ($values['limit']);
}
if (isset ($values['offset'])) {
$offset = sprintf (' OFFSET %d', $values['offset']);
unset ($values['offset']);
}
$i = 1;
$max = count ($values);
foreach ($values as $field => $value) {
@ -1882,16 +1920,6 @@ function format_array_to_where_clause_sql ($values, $join = 'AND') {
/* Avoid numeric field names */
continue;
if ($field == 'limit') {
$limit = sprintf (' LIMIT %d', $value);
continue;
}
if ($field == 'offset') {
$offset = sprintf (' OFFSET %d', $value);
continue;
}
if ($field[0] != "`") {
$field = "`".$field."`";
}
@ -1912,7 +1940,7 @@ function format_array_to_where_clause_sql ($values, $join = 'AND') {
$i++;
}
return $query.$limit.$offset;
return (! empty ($query) ? $prefix: '').$query.$limit.$offset;
}
/**
@ -2729,12 +2757,11 @@ function process_sql_update ($table, $values, $where = false, $where_join = 'AND
}
if ($where) {
$query .= ' WHERE ';
if (is_string ($where)) {
/* FIXME: Should we clean the string for sanity? */
$query .= $where;
} else if (is_array ($where)) {
$query .= format_array_to_where_clause_sql ($where, $where_join);
$query .= format_array_to_where_clause_sql ($where, $where_join, ' WHERE ');
}
}

View File

@ -93,8 +93,7 @@ function get_network_components ($id_module, $filter = false) {
$where = '';
if (is_array ($filter)) {
$where = ' AND ';
$where .= format_array_to_where_clause_sql ($filter);
$where = format_array_to_where_clause_sql ($filter, 'AND', ' AND ');
}
$sql = sprintf ('SELECT * FROM tnetwork_component

View File

@ -719,13 +719,13 @@ function pagination ($count, $url, $offset = 0, $pagination = 0, $return = false
$output = '<div>';
// Show GOTO FIRST button
$output .= '<a href="'.$url.'&amp;offset=0">'.print_image ("images/control_start_blue.png", true, array ("class" => "bot")).'</a>&nbsp;';
$output .= '<a class="pagination go_first" href="'.$url.'&amp;offset=0">'.print_image ("images/control_start_blue.png", true, array ("class" => "bot")).'</a>&nbsp;';
// Show PREVIOUS button
if ($index_page > 0) {
$index_page_prev = ($index_page - (floor ($block_limit / 2))) * $pagination;
if ($index_page_prev < 0)
$index_page_prev = 0;
$output .= '<a href="'.$url.'&amp;offset='.$index_page_prev.'">'.print_image ("images/control_rewind_blue.png", true, array ("class" => "bot")).'</a>';
$output .= '<a class="pagination go_rewind" href="'.$url.'&amp;offset='.$index_page_prev.'">'.print_image ("images/control_rewind_blue.png", true, array ("class" => "bot")).'</a>';
}
$output .= "&nbsp;&nbsp;";
// Draw blocks markers
@ -741,7 +741,7 @@ function pagination ($count, $url, $offset = 0, $pagination = 0, $return = false
$inicio_bloque_fake = $inicio_bloque + 1;
// To Calculate last block (doesnt end with round data,
// it must be shown if not round to block limit)
$output .= '<a href="'.$url.'&amp;offset='.$inicio_bloque.'">';
$output .= '<a class="pagination" href="'.$url.'&amp;offset='.$inicio_bloque.'">';
if ($inicio_bloque == $offset) {
$output .= "<b>[ $i ]</b>";
} else {
@ -756,7 +756,7 @@ function pagination ($count, $url, $offset = 0, $pagination = 0, $return = false
$prox_bloque = ($i + ceil ($block_limit / 2)) * $pagination;
if ($prox_bloque > $count)
$prox_bloque = ($count -1) - $pagination;
$output .= '<a href="'.$url.'&amp;offset='.$prox_bloque.'">'.print_image ("images/control_fastforward_blue.png", true, array ("class" => "bot")).'</a>';
$output .= '<a class="pagination go_fastforward" href="'.$url.'&amp;offset='.$prox_bloque.'">'.print_image ("images/control_fastforward_blue.png", true, array ("class" => "bot")).'</a>';
$i = $index_counter;
}
// if exists more registers than i can put in a page (defined by $block_size config parameter)
@ -765,7 +765,7 @@ function pagination ($count, $url, $offset = 0, $pagination = 0, $return = false
// as painted in last block (last integer block).
if (($count - $pagination) > 0) {
$myoffset = floor (($count - 1) / $pagination) * $pagination;
$output .= '<a href="'.$url.'&amp;offset='.$myoffset.'">'.print_image ("images/control_end_blue.png", true, array ("class" => "bot")).'</a>';
$output .= '<a class="pagination go_last" href="'.$url.'&amp;offset='.$myoffset.'">'.print_image ("images/control_end_blue.png", true, array ("class" => "bot")).'</a>';
}
// End div and layout
$output .= "</div>";