/**
  * Returns all MetaDataTypes irrespective of permissions
  * <code>
  * $types = MetaDataTypes::getAll();
  * foreach ($types as $type) { ... } 
  * @return MetaDataTypes
  */
 public static function getAll()
 {
     if (self::$has_init) {
         return self::$types;
     }
     global $db;
     $query = "SELECT * from `meta_types`";
     $results = $db->getAll($query);
     $types = array();
     if ($results) {
         foreach ($results as $result) {
             $type = MetaDataType::fromArray($result);
             $types[] = $type;
         }
     }
     self::$types = new self($types);
     self::$has_init = true;
 }
function display_category_select($organisation_id = null, $group = null, $role = null, $proxy_id = null, $cat_id = null)
{
    $types = MetaDataTypes::get($organisation_id, $group, $role, $proxy_id);
    $categories = getCategories($types);
    if (count($categories) == 0) {
        return "None";
    }
    ob_start();
    ?>
		<select id="associated_cat_id" name="associated_cat_id">
			<?php 
    foreach ($categories as $category) {
        echo build_option($category->getID(), $category->getLabel(), $cat_id == $category->getID());
    }
    ?>
		</select>
	<?php 
    return ob_get_clean();
}
                    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;
}
                }
                if (!has_error()) {
                    echo editMetaDataTable_User($user);
                } 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);
                $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;
}
 /**
  * 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);
 }