escape($id); $object = $this->getOneBySql("WHERE `id` = '$id'"); return $object; } function getOneByField($field, $value) { if (empty($field) || empty($value)) return FALSE; $field = $this->escape($field); $value = $this->escape($value); $object = $this->getOneBySql("WHERE `$field` = '$value'"); return $object; } function getOneBySql($sql = '') { $objects = $this->getManyBySql("$sql LIMIT 1"); if (is_array($objects)) { return $objects[0]; } return FALSE; } function getManyBySql($sql = '') { $query = "SELECT `$this->table_name`.`id`, ".$this->getFieldsForSql()." FROM $this->table_name $sql"; $qres = mysql_query($query) or die("Fehler:". mysql_error()); if (mysql_num_rows($qres) > 0) { while(FALSE != ($user_array = mysql_fetch_array($qres, MYSQL_ASSOC))) { $this_class = get_class($this); $object = new $this_class; foreach ($user_array as $key => $value) { $object->$key = stripslashes($value); } $objects[] = $object; } mysql_free_result($qres); //mysql_set_charset("utf8"); return $objects; } mysql_free_result($qres); return false; } function save() { return $this->abstract_save(); } function abstract_save() { foreach ($this->field_names as $field) { if (isset($this->$field)) { $sql_set .= ", `$field` = '".$this->escape($this->$field)."'"; } } $sql_set = substr($sql_set, 2); if (is_numeric($this->id)) { mysql_query("UPDATE $this->table_name SET $sql_set WHERE id = $this->id"); } else { mysql_query("INSERT $this->table_name SET $sql_set"); $this->id = mysql_insert_id(); } return true; } function delete() { $this->abstract_delete(); } function abstract_delete() { if (!isset($this->id)) return false; mysql_query("DELETE FROM $this->table_name WHERE `id` = '$this->id'"); if (mysql_affected_rows() > 0) { return true; } else { return false; } } function getFieldsForSql() { $tmp = array_map(create_function('$a','return "`'.$this->table_name.'`.`$a`";'), $this->field_names); return implode(', ', $tmp); } function saveValue($field, $value, $sql_function = '') { if (!is_numeric($this->id)) return false; $value = $this->escape($value); $value = "'$value'"; if ($sql_function == 'PASSWORD') { $value = "PASSWORD($value)"; } mysql_query("UPDATE $this->table_name SET `$field` = $value WHERE `id` = '$this->id'"); return true; } function setBooleanValue($field, $value = false) { if ($value === true || $value === '1' || $value === 1 || $value === 'on') { $this->setValue($field, 1); } else { $this->setValue($field, 0); } return true; } function setValue($field, $value, $stripslashes = false) { if (!in_array($field, $this->field_names)) return false; if ($stripslashes && get_magic_quotes_gpc()) { $value = stripslashes($value); } $this->$field = $value; return true; } function setValues($values, $stripslashes = false) { $this->abstract_setValues($values, $stripslashes); } function setId($id) { $this->id = $id; } function abstract_setValues($values, $stripslashes = false) { if (!is_array($values)) return false; foreach ($values as $field => $value) { $this->setValue($field, $value, $stripslashes); } return true; } function escape($string) { if(get_magic_quotes_gpc()) { $string = stripslashes($string); } $string = mysql_real_escape_string($string); return $string; } function asArray() { return $this->abstract_asArray(); } function abstract_asArray() { if (!is_numeric($this->id)) return false; $array = array(); $array['id'] = $this->id; foreach ($this->field_names as $field) { $array[$field] = $this->$field; } return $array; } function getInputForm($name, $value, $label, $error = false) { $result .= "\n"; return $result; } // Kann von der Kindklasse überschrieben werden. // Wenn der Rückgabewert false ist, wird getInputForm aufgerufen, // ansonsten wird der Rückgaberwert ausgegeben function modifyInputForm($field, $value, $label) { return $this->getInputForm("input[$field]", $value, $label); } function getInputForms($labels = false) { foreach ($this->field_names as $field) { if (empty($labels[$field])) $labels[$field] = $field; $result .= $this->modifyInputForm($field, $this->$field, $labels[$field]); } return $result; } function isSaved() { return is_numeric($this->id); } } ?>