Created
October 16, 2008 17:56
-
-
Save delonnewman/17213 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'connection.inc.php'; | |
class Task { | |
protected $id; | |
protected $type; | |
protected $status; | |
protected $parameters; | |
public function __constuct($id, $type, $status='new', $parameters=null) | |
{ | |
$this->id = $id; | |
$this->type = $type; | |
$this->status = $status; | |
$this->parameters = $parameters; | |
} | |
public static function create($type, $status='new', $parameters=null) | |
{ | |
return new Task(null, $type, $status, $parameters); | |
} | |
public function save() { | |
$query = sprintf("INSERT INTO `tasks` (`type`, `status`) VALUES ('%s', '%s')", | |
mysql_real_escape_string($this->type), | |
mysql_real_escape_string($this->status)); | |
$result = mysql_query($query); | |
$this->id = mysql_insert_id(); | |
if ($this->parameters) $this->addParameters($this->parameters); | |
if (mysql_error()) { | |
echo $query . "\n"; | |
echo mysql_error(); | |
return false; | |
} | |
else { | |
return true; | |
} | |
} | |
public function addParameters($parameters) | |
{ | |
$this->parameters = $parameters; | |
foreach ($parameters as $name => $value) { | |
$query = sprintf("INSERT INTO `parameters` (`task_id`, `name`, `value`) VALUES ('%s', '%s', '%s')", | |
mysql_real_escape_string($this->id), | |
mysql_real_escape_string($name), | |
mysql_real_escape_string($value)); | |
mysql_query($query); | |
} | |
} | |
public static function find($task_id) | |
{ | |
$query = sprintf("SELECT id, type, status FROM tasks where id = %s", | |
mysql_real_escape_string($task_id)); | |
$result = mysql_query($query); | |
$task = mysql_fetch_assoc($result); | |
$query = sprintf("SELECT name, value FROM parameters where task_id = %s", | |
mysql_real_escape_string($task_id)); | |
$result = mysql_query($query); | |
$parameters = array(); | |
for ($i = mysql_num_rows($result) - 1; $i >= 0; $i--) { | |
if (!mysql_data_seek($result, $i)) { | |
echo "Cannot seek to row $i: " . mysql_error() . "\n"; | |
continue; | |
} | |
$row = mysql_fetch_assoc($result); | |
if (!$row) continue; | |
$parameters[$row['name']] = $row['value']; | |
} | |
return new Task($task['id'], $task['type'], $task['status'], $parameters); | |
} | |
public function getParameters() { return $this->parameters; } | |
public function getStatus() { return $this->status; } | |
public function getType() { return $this->type; } | |
public function getId() { return $this->id; } | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment