echo "<div class=\"content-small\">for " . $organisation->getTitle() . " &gt; " . ucwords($group) . " &gt; " . ucwords($opts["associated_role"]) . "</div><br />";
                    echo editMetaDataTable_Category($organisation_id, $group, $role, null, $category);
                } else {
                    //if there were any errors, return a 500 and display errors
                    header("HTTP/1.0 500 Internal Error");
                    echo display_status_messages(false);
                }
                break;
            case 'new_value':
                $cat_id = filter_input(INPUT_POST, "type", FILTER_SANITIZE_NUMBER_INT);
                $proxy_id = filter_input(INPUT_POST, "proxy_id", FILTER_SANITIZE_NUMBER_INT);
                $type = MetaDataType::get($cat_id);
                if ($type) {
                    $user = User::get($proxy_id);
                    $org_id = $user->getOrganisationId();
                    $group = $user->getGroup();
                    $role = $user->getRole();
                    $types = MetaDataTypes::get($org_id, $group, $role, $proxy_id);
                    $value_id = MetaDataValue::create($cat_id, $proxy_id);
                    $value = MetaDataValue::get($value_id);
                    $descendant_type_sets = getDescendentTypesArray($types, $type);
                    header("Content-Type: application/xml");
                    echo editMetaDataRow($value, $type, $descendant_type_sets);
                } else {
                    header("HTTP/1.0 500 Internal Error");
                    echo display_error("Invalid type. Please try again.");
                }
        }
    }
    exit;
}
function editMetaDataRow(MetaDataValue $value, MetaDataType $category, array $descendant_type_sets = array())
{
    $vid = $value->getID();
    ob_start();
    ?>
	<tr class="value_edit" id="value_edit_<?php 
    echo $vid;
    ?>
">
		<td class="control"><input type="checkbox" title="Delete record" class="delete_btn" id="delete_<?php 
    echo $vid;
    ?>
" name="value[<?php 
    echo $vid;
    ?>
][delete]" value="1" /></td>
		<td><?php 
    if ($descendant_type_sets) {
        ?>
			<select name="value[<?php 
        echo $vid;
        ?>
][type]">
				<?php 
        foreach ($descendant_type_sets as $type_set) {
            $type = end($type_set);
            $selected = $type === $value->getType();
            echo build_option($type->getID(), html_encode(implode(" > ", $type_set)), $selected);
        }
        ?>
			</select>
			<?php 
    } else {
        ?>
			<input type="hidden" name="value[<?php 
        echo $vid;
        ?>
][type]" value="<?php 
        echo $category->getID();
        ?>
" />
			<?php 
    }
    ?>
		</td>
		<td><input type="text" name="value[<?php 
    echo $vid;
    ?>
][value]" value="<?php 
    echo html_encode($value->getValue());
    ?>
" /></td>
		<td><input type="text" name="value[<?php 
    echo $vid;
    ?>
][notes]" value="<?php 
    echo nl2br(html_encode($value->getNotes()));
    ?>
" /></td>
		<td><input type="text" class="date" id="value_<?php 
    echo $vid;
    ?>
_effective_date" name="value[<?php 
    echo $vid;
    ?>
][effective_date]" value="<?php 
    echo ($eff_date = $value->getEffectiveDate()) ? date("Y-m-d", $eff_date) : "";
    ?>
" /></td>
		<td><input type="text" class="date" id="value_<?php 
    echo $vid;
    ?>
_expiry_date" name="value[<?php 
    echo $vid;
    ?>
][expiry_date]" value="<?php 
    echo ($exp_date = $value->getExpiryDate()) ? date("Y-m-d", $exp_date) : "";
    ?>
" /></td>
	</tr>
	<?php 
    return ob_get_clean();
}
 /**
  * Retrieves Multiple values based on provided criteria.  
  * @param $organisation Organisation ID
  * @param $group 
  * @param $role
  * @param $proxy_id User's ID 
  * @param MetaDataType $type
  * @param $options advanced selection criteroa
  * @return MetaDataValues
  */
 public static function get($organisation = null, $group = null, $role = null, $proxy_id = null, MetaDataType $type = null, $include_sub_types = true, $options = array())
 {
     global $db;
     $conditions = array();
     if (array_key_exists('order by', $options)) {
         $order = array();
         if (is_array($options['order by'])) {
             foreach ($options['order by'] as $orders) {
                 $order[] = "`" . $orders[0] . "` " . (isset($orders[1]) ? $orders[1] : "asc");
             }
         }
         $order_by = " ORDER BY " . implode(",", $order);
     } else {
         $order_by = " ORDER BY lastname, firstname";
     }
     if (isset($options['limit'])) {
         $limit = $options['limit'];
     } else {
         $limit = -1;
     }
     if (isset($options['offset'])) {
         $offset = $options['offset'];
     } else {
         $offset = -1;
     }
     if (isset($options['where'])) {
         $conditions[] = $options['where'];
     }
     $query = "SELECT a.*, b.`group`, b.`role`, c.* from `" . AUTH_DATABASE . "`.`user_data` a \n\t\t\t\t\tLEFT JOIN `" . AUTH_DATABASE . "`.`user_access` b on a.`id`=b.`user_id` and b.`app_id`=?\n\t\t\t\t\tLEFT JOIN `meta_values` c on c.`proxy_id`=a.`id`";
     $conditions[] = generateAccessConditions($organisation, $group, $role, $proxy_id, 'b');
     if ($type) {
         $type_id = $type->getID();
         if ($include_sub_types) {
             $types = MetaDataTypes::get($organisation, $group, $role, $proxy_id);
             $desc_type_ids = getUniqueDescendantTypeIDs($types, $type);
             if ($desc_type_ids) {
                 //includes values from the specified type as well as sub types
                 $desc_type_ids[] = $type_id;
                 $conditions[] = "`meta_type_id` IN (" . implode(",", $desc_type_ids) . ")";
             } else {
                 $conditions[] = "`meta_type_id`=" . $db->qstr($type_id);
             }
         } else {
             $conditions[] = "`meta_type_id`=" . $db->qstr($type_id);
         }
     }
     if ($conditions) {
         $query .= " WHERE " . implode(" AND ", $conditions);
     }
     $query .= $order_by;
     $results = $db->SelectLimit($query, $limit, $offset, array(AUTH_APP_ID));
     $values = array();
     if ($results) {
         foreach ($results as $result) {
             //print_r($result);
             //user caching
             //$tmp_user = User::fromArray($result);
             //print_r($tmp_user);
             $values[] = MetaDataValue::fromArray($result);
         }
     }
     return new self($values);
 }