Ejemplo n.º 1
0
/**
* 	NOTE: PRIVATE FUNCTION.

	Will return the FROM and WHERE clauses for a selection from the item table.
	
	If $owner_id defined, will limit to only items owned by owner_id
	If $s_item_type defined, will limit to only items of that type.
	If $category defined, will limit to only items of that category.
	If $letter defined will limit to item.title starting with that letter.
	If $interest_level defined will limit to items with that interest level or higher.
	
	@param $HTTP_VARS['...'] variables supported: 
		owner_id, s_item_type, s_item_type[], s_item_type_group, title, title_match, category,
		rating, attribute_type, lookup_attribute_val, attribute_val, attr_match, 
		update_on, datetimemask, update_on_days, letter, start_item_id
		s_status_type[], status_comment, not_s_status_type[], interest_level
*/
function from_and_where_clause($HTTP_VARS, $column_display_config_rs = NULL, $query_type = 'LISTING')
{
    // For checking whether count (DISTINCT ...) is supported, and thus
    // whether we have to do any special processing!
    $from_r[] = 'item i';
    $from_r[] = 'item_instance ii';
    $where_r[] = 'ii.item_id = i.id';
    // only parent items should ever be listed.
    //
    // Owner restriction
    //
    if (strlen($HTTP_VARS['owner_id']) > 0) {
        $where_r[] = 'ii.owner_id = \'' . $HTTP_VARS['owner_id'] . '\'';
    } else {
        if (strlen($HTTP_VARS['not_owner_id']) > 0) {
            //For not showing current user items.
            $where_r[] = 'ii.owner_id <> \'' . $HTTP_VARS['not_owner_id'] . '\'';
        }
    }
    //
    // Item Type / Item Type group restriction
    //
    if (!is_array($HTTP_VARS['s_item_type']) && strlen($HTTP_VARS['s_item_type']) > 0) {
        $where_r[] = 'i.s_item_type = \'' . $HTTP_VARS['s_item_type'] . '\'';
    } else {
        if (strlen($HTTP_VARS['s_item_type_group']) > 0) {
            $from_r[] = 's_item_type_group_rltshp sitgr';
            $where_r[] = 'sitgr.s_item_type = i.s_item_type';
            $where_r[] = 'sitgr.s_item_type_group = \'' . $HTTP_VARS['s_item_type_group'] . '\'';
        } else {
            if (is_not_empty_array($HTTP_VARS['s_item_type'])) {
                $where_r[] = 'i.s_item_type IN(' . format_sql_in_clause($HTTP_VARS['s_item_type']) . ')';
            }
        }
    }
    $from_r[] = 's_status_type sst';
    $where_r[] = 'sst.s_status_type = ii.s_status_type';
    //
    // Status Type restriction
    //
    if (is_not_empty_array($HTTP_VARS['s_status_type'])) {
        $where_r[] = 'sst.s_status_type IN(' . format_sql_in_clause($HTTP_VARS['s_status_type']) . ')';
    } else {
        if ($HTTP_VARS['s_status_type'] != 'ALL' && strlen($HTTP_VARS['s_status_type']) > 0) {
            $where_r[] = 'sst.s_status_type = \'' . $HTTP_VARS['s_status_type'] . '\'';
        }
    }
    // no need for such a restriction if current user is item admin
    if (!is_user_granted_permission(PERM_ITEM_ADMIN)) {
        $where_r[] = "( sst.hidden_ind = 'N' OR ii.owner_id = '" . get_opendb_session_var('user_id') . "') ";
    }
    //
    // User and Status type restriction
    //
    if (strcmp($HTTP_VARS['owner_id'], get_opendb_session_var('user_id')) !== 0) {
        // not current user
        $from_r[] = 'user u';
        $where_r[] = 'u.user_id = ii.owner_id';
        $where_r[] = 'u.active_ind = \'Y\'';
    }
    //
    // Status Comment restriction
    //
    if (strlen($HTTP_VARS['status_comment']) > 0) {
        // Escape only the single quote!
        $HTTP_VARS['status_comment'] = str_replace("'", "\\'", $HTTP_VARS['status_comment']);
        if ($HTTP_VARS['status_comment_match'] != 'exact') {
            $parser = new BooleanParser();
            $statements = $parser->parseBooleanStatement($HTTP_VARS['status_comment']);
            if (is_array($statements)) {
                $where_r[] = build_boolean_clause($statements, 'ii.status_comment', $HTTP_VARS['status_comment_match'], 'AND', $HTTP_VARS['status_comment_case']);
            }
        } else {
            if (is_null($HTTP_VARS['status_comment_case'])) {
                $where_r[] = 'ii.status_comment = \'' . $HTTP_VARS['status_comment'] . '\'';
            } else {
                $where_r[] = 'BINARY ii.status_comment = \'' . $HTTP_VARS['status_comment'] . '\'';
            }
        }
    }
    //
    // Title restriction
    //
    if (strlen($HTTP_VARS['title']) > 0) {
        // Escape only the single quote!
        $HTTP_VARS['title'] = str_replace("'", "\\'", $HTTP_VARS['title']);
        if ($HTTP_VARS['title_match'] != 'exact') {
            $parser = new BooleanParser();
            $statements = $parser->parseBooleanStatement($HTTP_VARS['title']);
            if (is_array($statements)) {
                $where_r[] = build_boolean_clause($statements, 'i.title', $HTTP_VARS['title_match'], 'AND', $HTTP_VARS['title_case']);
            }
        } else {
            if (is_null($HTTP_VARS['title_case'])) {
                $where_r[] = 'i.title = \'' . $HTTP_VARS['title'] . '\'';
            } else {
                $where_r[] = 'BINARY i.title = \'' . $HTTP_VARS['title'] . '\'';
            }
        }
    } else {
        if (strlen($HTTP_VARS['letter']) > 0) {
            // Numeric match.
            if ($HTTP_VARS['letter'] == '#') {
                $where_r[] = 'ASCII(LEFT(title,1)) BETWEEN ASCII(\'0\') AND ASCII(\'9\')';
            } else {
                $where_r[] = 'UPPER(LEFT(i.title,1)) = \'' . strtoupper($HTTP_VARS['letter']) . '\'';
            }
        }
    }
    //
    // Last Updated support
    //
    if (strlen($HTTP_VARS['update_on']) > 0) {
        if (strlen($HTTP_VARS['datetimemask']) > 0) {
            $timestamp = get_timestamp_for_datetime($HTTP_VARS['update_on'], $HTTP_VARS['datetimemask']);
            if ($timestamp !== FALSE) {
                $where_r[] = 'ii.update_on >= FROM_UNIXTIME(' . $timestamp . ')';
            } else {
                // by default get items from 1 day ago, if update_on can not be parsed correctly.
                $where_r[] = 'TO_DAYS(ii.update_on) >= (TO_DAYS(now())-1)';
            }
        } else {
            $where_r[] = 'ii.update_on >= \'' . $HTTP_VARS['update_on'] . '\'';
        }
    } else {
        if (is_numeric($HTTP_VARS['update_on_days'])) {
            // GIve us all records updated in the last however many days.
            $where_r[] = 'TO_DAYS(ii.update_on) >= (TO_DAYS(now())-' . $HTTP_VARS['update_on_days'] . ')';
        }
    }
    //
    // Item Attribute listing/restriction
    //
    if (is_array($column_display_config_rs)) {
        for ($i = 0; $i < count($column_display_config_rs); $i++) {
            if ($column_display_config_rs[$i]['column_type'] == 's_attribute_type') {
                if ($column_display_config_rs[$i]['search_attribute_ind'] != 'y') {
                    // either LISTING or COUNT
                    if ($query_type != 'COUNT') {
                        $left_join = 'LEFT JOIN item_attribute ia' . $i . ' ON ' . 'ia' . $i . '.item_id = i.id AND (ia' . $i . '.instance_no = 0 OR ia' . $i . '.instance_no = ii.instance_no) AND ia' . $i . '.s_attribute_type = \'' . $column_display_config_rs[$i]['s_attribute_type'] . '\' AND ia' . $i . '.attribute_no = 1';
                        // So we can work out which search attribute types to display
                        if (is_numeric($column_display_config_rs[$i]['order_no'])) {
                            $left_join .= ' AND ia' . $i . '.order_no = ' . $column_display_config_rs[$i]['order_no'];
                        }
                        $left_join_from_r[] = $left_join;
                    }
                } else {
                    // search attribute
                    $from_r[] = 'item_attribute ia' . $i;
                    // now do the where clause.
                    $where_r[] = 'ia' . $i . '.item_id = i.id AND (ia' . $i . '.instance_no = 0 OR ia' . $i . '.instance_no = ii.instance_no) AND ia' . $i . '.s_attribute_type = \'' . $column_display_config_rs[$i]['s_attribute_type'] . '\'';
                    // AND ia'.$i.'.attribute_no = 1';
                    if (strlen($column_display_config_rs[$i]['attribute_val']) > 0 && $column_display_config_rs[$i]['attribute_val'] != '%' && $column_display_config_rs[$i]['attr_match'] != 'exact') {
                        $parser = new BooleanParser();
                        $statements = $parser->parseBooleanStatement(strtoupper(str_replace("'", "\\'", $column_display_config_rs[$i]['attribute_val'])));
                        if (is_array($statements)) {
                            if ($column_display_config_rs[$i]['lookup_attribute_ind'] == 'Y') {
                                $where_r[] = build_boolean_clause($statements, 'ia' . $i . '.lookup_attribute_val', 'plain', 'AND', $HTTP_VARS['attr_case']);
                            } else {
                                $where_r[] = build_boolean_clause($statements, 'ia' . $i . '.attribute_val', $column_display_config_rs[$i]['attr_match'], 'AND', $HTTP_VARS['attr_case']);
                            }
                        }
                    } else {
                        if (strlen($column_display_config_rs[$i]['lookup_attribute_val']) > 0 && $column_display_config_rs[$i]['lookup_attribute_val'] != '%' && $column_display_config_rs[$i]['lookup_attribute_ind'] == 'Y') {
                            $value = str_replace("'", "\\'", $column_display_config_rs[$i]['lookup_attribute_val']);
                            $where_r[] = 'ia' . $i . '.lookup_attribute_val = \'' . str_replace('\\_', '_', $value) . '\'';
                        } else {
                            if (strlen($column_display_config_rs[$i]['attribute_val']) > 0 && $column_display_config_rs[$i]['attribute_val'] != '%') {
                                if (starts_with($column_display_config_rs[$i]['attribute_val'], '"') && ends_with($column_display_config_rs[$i]['attribute_val'], '"')) {
                                    $column_display_config_rs[$i]['attribute_val'] = substr($column_display_config_rs[$i]['attribute_val'], 1, -1);
                                }
                                $value = strtoupper(str_replace("'", "\\'", $column_display_config_rs[$i]['attribute_val']));
                                $where_r[] = 'UPPER(ia' . $i . '.attribute_val) = \'' . str_replace('\\_', '_', $value) . '\'';
                            }
                        }
                    }
                    if (strlen($HTTP_VARS['attr_update_on']) > 0) {
                        if (strlen($HTTP_VARS['datetimemask']) > 0) {
                            $timestamp = get_timestamp_for_datetime($HTTP_VARS['attr_update_on'], $HTTP_VARS['datetimemask']);
                            if ($timestamp !== FALSE) {
                                $where_r[] = 'ia' . $i . '.update_on >= FROM_UNIXTIME(' . $timestamp . ')';
                            } else {
                                // by default get items from 1 day ago, if update_on can not be parsed correctly.
                                $where_r[] = 'TO_DAYS(ia' . $i . '.update_on) >= (TO_DAYS(now())-1)';
                            }
                        } else {
                            $where_r[] = 'ia' . $i . '.update_on >= \'' . $HTTP_VARS['attr_update_on'] . '\'';
                        }
                    } else {
                        if (is_numeric($HTTP_VARS['attr_update_on_days'])) {
                            // GIve us all records updated in the last however many days.
                            $where_r[] = 'TO_DAYS(ia' . $i . '.update_on) >= (TO_DAYS(now())-' . $HTTP_VARS['attr_update_on_days'] . ')';
                        }
                    }
                }
            } else {
                if ($column_display_config_rs[$i]['column_type'] == 's_field_type') {
                    if ($column_display_config_rs[$i]['s_field_type'] == 'CATEGORY') {
                        $from_r[] = 's_item_attribute_type catsiat';
                        $from_r[] = 's_attribute_type catsat';
                        $where_r[] = 'catsiat.s_item_type = i.s_item_type AND catsat.s_attribute_type = catsiat.s_attribute_type AND catsat.s_field_type = \'CATEGORY\'';
                        $left_join_clause = 'LEFT JOIN item_attribute catia ON ' . 'catia.item_id = i.id AND (catia.instance_no = 0 OR catia.instance_no = ii.instance_no) AND catia.s_attribute_type = catsiat.s_attribute_type AND catia.order_no = catsiat.order_no';
                        if (strlen($HTTP_VARS['category']) > 0 || strcasecmp($HTTP_VARS['attr_match'], 'category') === 0 && strlen($HTTP_VARS['attribute_val']) > 0) {
                            // Support specifying $attribute_val for $category where $attr_match=="category"!
                            // If item_type && item_type_group are not set!
                            if (strlen($HTTP_VARS['attribute_type']) > 0 && !is_array($HTTP_VARS['s_item_type']) && strlen($HTTP_VARS['s_item_type']) == 0 && strlen($HTTP_VARS['s_item_type_group']) == 0) {
                                $where_r[] = 'catsat.s_attribute_type = \'' . $HTTP_VARS['attribute_type'] . '\'';
                            }
                            // Escape single quotes only.
                            $value = strtoupper(str_replace("'", "\\'", ifempty($HTTP_VARS['category'], $HTTP_VARS['attribute_val'])));
                            $where_r[] = 'UPPER(catia.lookup_attribute_val) = \'' . str_replace('\\_', '_', $value) . '\'';
                        } else {
                            $left_join_clause .= ' AND catia.attribute_no = 1';
                        }
                        $left_join_from_r[] = $left_join_clause;
                    } else {
                        if ($column_display_config_rs[$i]['s_field_type'] == 'INTEREST') {
                            // can only restrict interest level if its displayed as a column
                            if (strlen($HTTP_VARS['interest_level']) > 0) {
                                $where_r[] = "it.item_id = ii.item_id AND it.instance_no = ii.instance_no AND it.user_id = '" . get_opendb_session_var('user_id') . "'" . " AND it.level >= " . $HTTP_VARS['interest_level'];
                                $from_r[] = "user_item_interest it";
                            } else {
                                $left_join_from_r[] = "LEFT JOIN user_item_interest it ON it.item_id = i.id AND it.instance_no = ii.instance_no AND it.user_id = '" . get_opendb_session_var('user_id') . "'";
                            }
                        }
                    }
                }
            }
        }
    }
    // If attribute_val specified without a attribute_type, then do a loose join to item_attribute table,
    // only on attribute_val column.
    if (strlen($HTTP_VARS['attribute_type']) == 0 && (strlen($HTTP_VARS['attribute_val']) > 0 || strlen($HTTP_VARS['attr_update_on']) > 0 || strlen($HTTP_VARS['attr_update_on_days']) > 0)) {
        $from_r[] = 'item_attribute ia';
        // now do the where clause.
        $where_r[] = 'ia.item_id = i.id ';
        //AND ia.attribute_no = 1';
        if ($HTTP_VARS['attr_match'] != 'exact') {
            $parser = new BooleanParser();
            $statements = $parser->parseBooleanStatement(strtoupper(str_replace("'", "\\'", $HTTP_VARS['attribute_val'])));
            if (is_array($statements)) {
                if (is_lookup_attribute_type($HTTP_VARS['attribute_type'])) {
                    $where_r[] = build_boolean_clause($statements, 'ia.lookup_attribute_val', 'plain', 'AND', $HTTP_VARS['attr_case']);
                } else {
                    $where_r[] = build_boolean_clause($statements, 'ia.attribute_val', $HTTP_VARS['attr_match'], 'AND', $HTTP_VARS['attr_case']);
                }
            }
        } else {
            // attr_match = 'exact'
            if (is_lookup_attribute_type($HTTP_VARS['attribute_type'])) {
                $value = str_replace("'", "\\'", $HTTP_VARS['attribute_val']);
                $where_r[] = 'ia.lookup_attribute_val = \'' . str_replace('\\_', '_', $value) . '\'';
            } else {
                $value = str_replace("'", "\\'", $HTTP_VARS['attribute_val']);
                if (is_null($HTTP_VARS['attr_case'])) {
                    $where_r[] = '( ia.attribute_val = \'' . str_replace('\\_', '_', $value) . '\' OR ' . 'ia.attribute_val LIKE \'% ' . $value . ' %\' OR ' . 'ia.attribute_val LIKE \'' . $value . ' %\' OR ' . 'ia.attribute_val LIKE \'% ' . $value . '\')';
                } else {
                    $where_r[] = '( BINARY ia.attribute_val = \'' . str_replace('\\_', '_', $value) . '\' OR ' . 'ia.attribute_val LIKE BINARY \'% ' . $value . ' %\' OR ' . 'ia.attribute_val LIKE BINARY \'' . $value . ' %\' OR ' . 'ia.attribute_val LIKE BINARY \'% ' . $value . '\')';
                }
            }
        }
        if (strlen($HTTP_VARS['attr_update_on']) > 0) {
            if (strlen($HTTP_VARS['datetimemask']) > 0) {
                $timestamp = get_timestamp_for_datetime($HTTP_VARS['attr_update_on'], $HTTP_VARS['datetimemask']);
                if ($timestamp !== FALSE) {
                    $where_r[] = 'ia.update_on >= FROM_UNIXTIME(' . $timestamp . ')';
                } else {
                    // by default get items from 1 day ago, if update_on can not be parsed correctly.
                    $where_r[] = 'TO_DAYS(ia.update_on) >= (TO_DAYS(now())-1)';
                }
            } else {
                $where_r[] = 'ia.update_on >= \'' . $HTTP_VARS['attr_update_on'] . '\'';
            }
        } else {
            if (is_numeric($HTTP_VARS['attr_update_on_days'])) {
                // GIve us all records updated in the last however many days.
                $where_r[] = 'TO_DAYS(ia.update_on) >= (TO_DAYS(now())-' . $HTTP_VARS['attr_update_on_days'] . ')';
            }
        }
    }
    //
    // Review restrictions
    //
    if (strlen($HTTP_VARS['rating']) > 0) {
        $where_r[] = 'r.item_id = i.id AND r.rating >= ' . $HTTP_VARS['rating'];
        $from_r[] = 'review r';
    }
    //
    // Item ID range restriction (Used by Import script)
    //
    if (strlen($HTTP_VARS['item_id_range']) > 0) {
        $where_r[] = 'i.id IN (' . expand_number_range($HTTP_VARS['item_id_range']) . ')';
    }
    //
    // Now build the SQL query
    //
    if (is_array($from_r)) {
        $from_clause = '';
        for ($i = 0; $i < count($from_r); $i++) {
            if (strlen($from_clause) > 0) {
                $from_clause .= ', ';
            }
            $from_clause .= $from_r[$i];
        }
        $query .= 'FROM (' . $from_clause . ') ';
    }
    if (is_array($left_join_from_r)) {
        $left_join_from_clause = '';
        for ($i = 0; $i < count($left_join_from_r); $i++) {
            if (strlen($left_join_from_clause) > 0) {
                $left_join_from_clause .= ' ';
            }
            $left_join_from_clause .= $left_join_from_r[$i];
        }
        $query .= $left_join_from_clause . ' ';
    }
    if (is_array($where_r)) {
        $where_clause = '';
        for ($i = 0; $i < count($where_r); $i++) {
            if (strlen($where_clause) > 0) {
                $where_clause .= ' AND ';
            }
            $where_clause .= $where_r[$i];
        }
        $query .= 'WHERE ' . $where_clause;
    }
    return $query;
}
 function item_attribute($s_attribute_type, $order_no, $attribute_val)
 {
     if ($s_attribute_type == 'IMDB_ID') {
         if ($this->imdbUrl != NULL) {
             $this->attribute_rs['URL'] = str_replace('{imdb_id}', $attribute_val, $this->imdbUrl);
         }
     } else {
         if ($s_attribute_type == 'MOVIEGENRE') {
             $this->attribute_rs['Genre'][] = $attribute_val;
         } else {
             if ($s_attribute_type == 'ACTORS') {
                 $this->attribute_rs['Actor'][] = $attribute_val;
             } else {
                 if ($s_attribute_type == 'DIRECTOR') {
                     $this->attribute_rs['Director'][] = $attribute_val;
                 } else {
                     if ($s_attribute_type == 'IMAGEURL') {
                         //  || $s_attribute_type == 'IMAGEURLB'
                         $this->attribute_rs['Cover'] = $attribute_val;
                     } else {
                         if ($s_attribute_type == 'PUR_DATE') {
                             $timestamp = get_timestamp_for_datetime($attribute_val, 'YYYYMMDDHH24MISS');
                             $this->attribute_rs['PurchaseDate'] = get_localised_timestamp('YYYYMMDDTT00:00:00', $timestamp);
                         } else {
                             if ($s_attribute_type == 'DVD_REGION') {
                                 // this is a giant hack!
                                 switch ($attribute_val) {
                                     case '2':
                                         $this->attribute_rs['Country'] = 'United Kingdom';
                                         break;
                                     case '4':
                                         $this->attribute_rs['Country'] = 'Australia';
                                         break;
                                     case '1':
                                     default:
                                         $this->attribute_rs['Country'] = 'United States';
                                 }
                             } else {
                                 if (isset($this->attribute_map[$s_attribute_type])) {
                                     $key = $this->attribute_map[$s_attribute_type];
                                     $this->attribute_rs[$key] = $attribute_val;
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     // return nothing yet as we will wrap it all up in end_item
     return NULL;
 }
Ejemplo n.º 3
0
function get_datetime_value($value, $format_mask = "", $auto_datetime = "")
{
    if (strlen($value) > 0) {
        // the timestamp is stored in the database with the format YYYYMMDDHH24MISS
        $timestamp = get_timestamp_for_datetime($value, 'YYYYMMDDHH24MISS');
        if ($timestamp !== FALSE) {
            if (strlen($format_mask) == 0) {
                $format_mask = 'DD/MM/YYYY';
            }
            $datetime = get_localised_timestamp($format_mask, $timestamp);
            if ($datetime === FALSE) {
                $datetime = $value;
                // as a last resort
            }
        } else {
            $datetime = $value;
            // as a last resort
        }
    } else {
        if ($value === NULL && strcasecmp($auto_datetime, 'Y') === 0) {
            $datetime = get_localised_timestamp($format_mask);
            // current date
        } else {
            $datetime = '';
        }
    }
    return $datetime;
}
Ejemplo n.º 4
0
function get_item_display_field($item_r, $item_attribute_type_r, $value = NULL, $dowrap = TRUE, $prompt_mask = NULL)
{
    if ($item_attribute_type_r['display_type'] == 'hidden') {
        return '';
    } else {
        if ($item_attribute_type_r['display_type'] == 'fileviewer') {
            $format_mask = ifempty($item_attribute_type_r['display_type_arg1'], '%value%');
            $width = ifempty($item_attribute_type_r['display_type_arg2'], '400');
            $height = ifempty($item_attribute_type_r['display_type_arg3'], '300');
            $target = ifempty($item_attribute_type_r['display_type_arg4'], '_blank');
            if (is_array($value)) {
                $values = $value;
            } else {
                $values[] = $value;
            }
            if (count($values) > 0) {
                $display_value_r = array();
                while (list(, $value) = each($values)) {
                    $value = trim($value);
                    $value_format_mask = $format_mask;
                    if (strpos($value_format_mask, '%img%') !== FALSE) {
                        $file_type_r = fetch_file_type_r(fetch_file_type_for_extension(get_file_ext($value)));
                        if (strlen($file_type_r['image']) > 0 && ($image_src = theme_image_src($file_type_r['image'])) !== FALSE) {
                            $img = '<img src="' . $image_src . '" title="' . $value . '">';
                        } else {
                            $img = '';
                        }
                        $value_format_mask = str_replace('%img%', $img, $value_format_mask);
                    }
                    if (strpos($value_format_mask, '%value%') !== FALSE) {
                        $value_format_mask = str_replace('%value%', $value, $value_format_mask);
                    }
                    $file_r = file_cache_get_image_r($value, 'display');
                    $url = $file_r['fullsize']['url'];
                    $display_value_r[] = "<a href=\"" . $value . "\" onclick=\"fileviewer('{$url}' ,'" . ($width + 20) . "', '" . ($height + 25) . "', '" . $target . "'); return false;\" title=\"" . $item_attribute_type_r['prompt'] . "\" class=\"popuplink\">{$value_format_mask}</a>";
                }
                $field = format_multivalue_block($display_value_r, 'fileviewer');
                if ($dowrap) {
                    return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                } else {
                    return $field;
                }
            } else {
                return '';
            }
        } else {
            if ($item_attribute_type_r['display_type'] == 'datetime') {
                if (is_array($value)) {
                    $values = $value;
                } else {
                    $values[] = $value;
                }
                if (count($values) > 0) {
                    $display_value_r = array();
                    while (list(, $value) = each($values)) {
                        $value = trim($value);
                        $timestamp = get_timestamp_for_datetime($value, 'YYYYMMDDHH24MISS');
                        if ($timestamp !== FALSE) {
                            if (strlen($item_attribute_type_r['display_type_arg1']) == 0) {
                                $item_attribute_type_r['display_type_arg1'] = 'DD/MM/YYYY';
                            }
                            $datetime = get_localised_timestamp($item_attribute_type_r['display_type_arg1'], $timestamp);
                            if ($datetime !== FALSE) {
                                $display_value_r[] = $datetime;
                            } else {
                                $display_value_r[] = $value;
                            }
                        } else {
                            $display_value_r[] = $value;
                        }
                    }
                    $field = format_multivalue_block($display_value_r, 'datetime');
                    if ($dowrap) {
                        return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                    } else {
                        return $field;
                    }
                } else {
                    return '';
                }
            } else {
                if ($item_attribute_type_r['display_type'] == 'format_mins') {
                    if (is_array($value)) {
                        $values = $value;
                    } else {
                        $values[] = $value;
                    }
                    if (count($values) > 0) {
                        $display_value_r = array();
                        while (list(, $value) = each($values)) {
                            $value = trim($value);
                            if (is_numeric($value)) {
                                // Ensure we have a mask to work with.
                                $display_mask = $item_attribute_type_r['display_type_arg1'];
                                if (strlen($display_mask) == 0) {
                                    $display_mask = '%h %H %m %M';
                                }
                                $hrs = floor($value / 60);
                                // hours
                                $mins = $value % 60;
                                // minutes
                                // Process display_mask and remove any bits that are not needed because the hour/minute is zero.
                                if ($mins == 0 && $hrs > 0) {
                                    // only get rid of minutes if $hrs is a value.
                                    $index = strpos($display_mask, '%H');
                                    if ($index !== FALSE) {
                                        $display_mask = substr($display_mask, 0, $index + 2);
                                    } else {
                                        $index = strpos($display_mask, '%m');
                                        if ($index != FALSE) {
                                            $display_mask = substr($display_mask, 0, $index);
                                        }
                                        //include the %H
                                    }
                                } else {
                                    if ($hrs == 0) {
                                        $index = strpos($display_mask, '%m');
                                        if ($index != FALSE) {
                                            $display_mask = substr($display_mask, $index);
                                        }
                                        //include the %H
                                    }
                                }
                                // Unfortunately we need to do $mins>0 and $hrs>0 if's twice, because otherwise once we
                                // replace the %h and %H the test for $mins>0 would not be able to cut the display_mask,
                                // based on the %h/%H...
                                if ($hrs > 0) {
                                    // Now do all replacements.
                                    $display_mask = str_replace('%h', $hrs, $display_mask);
                                    if ($hrs != 1) {
                                        $display_mask = str_replace('%H', get_opendb_lang_var('hours'), $display_mask);
                                    } else {
                                        $display_mask = str_replace('%H', get_opendb_lang_var('hour'), $display_mask);
                                    }
                                }
                                if ($mins >= 0 || $hrs === 0 && $mins === 0) {
                                    // Now do minute replacements only.
                                    $display_mask = str_replace('%m', $mins, $display_mask);
                                    if ($mins != 1) {
                                        $display_mask = str_replace('%M', get_opendb_lang_var('minutes'), $display_mask);
                                    } else {
                                        $display_mask = str_replace('%M', get_opendb_lang_var('minute'), $display_mask);
                                    }
                                }
                                $display_value_r[] = $display_mask;
                            } else {
                                // what else can we do here?!
                                $display_value_r[] = $value;
                            }
                        }
                        $field = format_multivalue_block($display_value_r, 'format_mins');
                        if ($dowrap) {
                            return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                        } else {
                            return $field;
                        }
                    } else {
                        return '';
                    }
                } else {
                    if ($item_attribute_type_r['display_type'] == 'star_rating') {
                        // arg[0] = rating range
                        if (is_array($value)) {
                            $values = $value;
                        } else {
                            $values[] = $value;
                        }
                        if (count($values) > 0) {
                            $display_value_r = array();
                            while (list(, $value) = each($values)) {
                                $value = trim($value);
                                // no point unless numeric
                                if (is_numeric($value)) {
                                    $total_count = $item_attribute_type_r['display_type_arg1'];
                                    if (is_numeric($total_count)) {
                                        $display_value = '';
                                        $j = $value;
                                        for ($i = 0; $i < $total_count; ++$i) {
                                            if ($j >= 0.75) {
                                                $display_value .= theme_image('rs.gif');
                                            } else {
                                                if ($j >= 0.25) {
                                                    $display_value .= theme_image('rgs.gif');
                                                } else {
                                                    $display_value .= theme_image('gs.gif');
                                                }
                                            }
                                            $j = $j - 1;
                                        }
                                        $ratingmask = $item_attribute_type_r['display_type_arg2'];
                                        if (strlen($ratingmask) > 0) {
                                            $ratingmask = str_replace('%value%', $value, $ratingmask);
                                            $ratingmask = str_replace('%maxrange%', $total_count, $ratingmask);
                                            $display_value = str_replace('%starrating%', $display_value, $ratingmask);
                                        }
                                        if ($item_attribute_type_r['listing_link_ind'] == 'Y') {
                                            $display_value = format_listing_link($value, $display_value, $item_attribute_type_r, NULL);
                                        }
                                    }
                                    $display_value_r[] = $display_value;
                                }
                            }
                            $field = format_multivalue_block($display_value_r, 'starrating');
                            if ($dowrap) {
                                return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                            } else {
                                return $field;
                            }
                        } else {
                            return '';
                            // nothing to do!
                        }
                    } else {
                        if (!is_array($value) && $item_attribute_type_r['display_type'] == 'display' && ifempty($item_attribute_type_r['display_type_arg1'], '%value%') == '%value%') {
                            // Support newline formatting by default.
                            $value = nl2br(trim($value));
                            if ($item_attribute_type_r['listing_link_ind'] == 'Y') {
                                $field = format_listing_links($value, $item_attribute_type_r, 'exact');
                            } else {
                                $field = $value;
                            }
                            if ($dowrap) {
                                return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                            } else {
                                return $field;
                            }
                        } else {
                            if ($item_attribute_type_r['display_type'] == 'list') {
                                //list(list_type [,delimiter])
                                if (is_array($value)) {
                                    $values = $value;
                                    $attr_match = 'exact';
                                } else {
                                    $value = trim($value);
                                    if (strlen($item_attribute_type_r['display_type_arg2']) == 0) {
                                        // Use newline!
                                        $values = explode_lines($value);
                                        $attr_match = 'partial';
                                    } else {
                                        $values = explode($item_attribute_type_r['display_type_arg2'], $value);
                                        if (strlen(trim($item_attribute_type_r['display_type_arg2'])) === 0) {
                                            $attr_match = 'word';
                                        } else {
                                            $attr_match = 'partial';
                                        }
                                    }
                                }
                                $field = format_list_from_array($values, $item_attribute_type_r, $item_attribute_type_r['listing_link_ind'] == 'Y' ? $attr_match : FALSE);
                                if ($dowrap) {
                                    return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                                } else {
                                    return $field;
                                }
                            } else {
                                if ($item_attribute_type_r['display_type'] == 'category' || $item_attribute_type_r['display_type'] == 'display') {
                                    $field = '';
                                    if (is_array($value)) {
                                        $value_array = $value;
                                    } else {
                                        $value_array[] = $value;
                                    }
                                    $attribute_value_rs = array();
                                    if ($item_attribute_type_r['lookup_attribute_ind'] == 'Y') {
                                        $results = fetch_value_match_attribute_type_lookup_rs($item_attribute_type_r['s_attribute_type'], $value_array, get_lookup_order_by($item_attribute_type_r['display_type_arg1']), 'asc');
                                        if ($results) {
                                            while ($lookup_r = db_fetch_assoc($results)) {
                                                $lookup_key = array_search2($lookup_r['value'], $value_array, TRUE);
                                                if ($lookup_key !== FALSE) {
                                                    // Remove the matched element
                                                    array_splice($value_array, $lookup_key, 1);
                                                    $attribute_value_rs[] = array(value => $lookup_r['value'], display => $lookup_r['display'], img => $lookup_r['img']);
                                                }
                                            }
                                            db_free_result($results);
                                        }
                                    }
                                    // where extra items that do not have a matching lookup value.
                                    if (is_not_empty_array($value_array)) {
                                        reset($value_array);
                                        while (list(, $value) = each($value_array)) {
                                            if (strlen(trim($value)) > 0) {
                                                // In case there are extra spaces
                                                $attribute_value_rs[] = array(value => $value, display => $value);
                                            }
                                        }
                                    }
                                    if (is_not_empty_array($attribute_value_rs)) {
                                        $field = format_lookup_display_block($item_attribute_type_r, $attribute_value_rs);
                                        if (strlen($field) > 0) {
                                            if ($dowrap) {
                                                return format_field($item_attribute_type_r['prompt'], $field, $prompt_mask);
                                            } else {
                                                return $field;
                                            }
                                        } else {
                                            return NULL;
                                        }
                                    }
                                } else {
                                    if ($item_attribute_type_r['display_type'] == 'review') {
                                        $total_count = fetch_attribute_type_cnt('S_RATING');
                                        if (is_numeric($total_count)) {
                                            $value = trim($value);
                                            if (!is_numeric($value)) {
                                                $value = 0;
                                            }
                                            $field = '';
                                            $j = $value;
                                            for ($i = 0; $i < $total_count; ++$i) {
                                                if ($j >= 0.75) {
                                                    $field .= theme_image('rs.gif');
                                                } else {
                                                    if ($j >= 0.25) {
                                                        $field .= theme_image('rgs.gif');
                                                    } else {
                                                        $field .= theme_image('gs.gif');
                                                    }
                                                }
                                                $j = $j - 1;
                                            }
                                            // If a mask is defined, format the display value.
                                            if (strlen($item_attribute_type_r['display_type_arg1']) > 0) {
                                                $lookup_r = fetch_attribute_type_lookup_r('S_RATING', $value);
                                                if (is_not_empty_array($lookup_r)) {
                                                    $field .= format_display_value($item_attribute_type_r['display_type_arg1'], $lookup_r['img'], $lookup_r['value'], $lookup_r['display']);
                                                }
                                            }
                                            return $field;
                                            // this is only used in a few places.
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    //else -- no display type match.
    if ($dowrap) {
        return format_field($item_attribute_type_r['prompt'], nl2br($value), $prompt_mask);
    } else {
        return nl2br($value);
    }
}