aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFilipp Lepalaan <filipp@mac.com>2010-07-20 07:22:23 +0300
committerFilipp Lepalaan <filipp@mac.com>2010-07-20 07:22:23 +0300
commitdead03e955367d4635764251f62256365c01b221 (patch)
tree24696b6896871521b12e36d778f2219f9987ac28
parentad4a149c32bd42bf374b56c88cdf43b5ee801874 (diff)
downloadmain-dead03e955367d4635764251f62256365c01b221.tar.gz
main-dead03e955367d4635764251f62256365c01b221.tar.bz2
main-dead03e955367d4635764251f62256365c01b221.zip
ordering children, PHP 5.3 changes
-rw-r--r--MainApp.php54
-rw-r--r--MainController.php98
-rw-r--r--MainDb.php40
3 files changed, 98 insertions, 94 deletions
diff --git a/MainApp.php b/MainApp.php
index 5a149a8..3ad37a0 100644
--- a/MainApp.php
+++ b/MainApp.php
@@ -10,7 +10,6 @@ class MainApp
static public function init()
{
$url = self::url();
-
@list($controller, $param, $action) = $url;
// no controller given, read default one
@@ -193,45 +192,6 @@ class MainApp
return sprintf('%s_%s', $loc, strtoupper($lang));
}
- public function delete($table, $where)
- {
- if (empty($where)) {
- exit(self::error('Delete without parameters'));
- }
-
- list($key, $value) = each($where);
- $sql = "DELETE FROM `$table` WHERE $key = :{$key}";
-
- self::log($sql);
- self::query($sql, $where);
-
- }
-
- ////
- // insert something in the database
- static function insert($table, $data)
- {
- if (empty($data)) {
- exit(self::error('Empty insert'));
- }
-
- $cols = array();
- $vals = array();
-
- foreach ($data as $k => $v) {
- $cols[] = "`{$k}`";
- $vals[] = ":{$k}";
- }
-
- $cols = implode(',', $cols);
- $vals = implode(',', $vals);
- $sql = "INSERT INTO `$table` ($cols) VALUES ($vals)";
-
- self::log($sql);
- self::query($sql, $data);
-
- }
-
// Move this back to Controller once PHP 5.3 is out (get_called_class())
static function select($table, $where = 1, $what = '*', $order_by = '')
{
@@ -292,13 +252,25 @@ class MainApp
////
// output a JavaScript fragment
- public function js($string)
+ static function js($string)
{
header('Content-Type: text/javascript');
header('Content-Length: '.strlen($string));
echo '<script type="text/javascript" charset="utf-8">'.$string.'</script>';
}
+ ////
+ // urlencode() string twice to work with mod_rewrite
+ static function urlenc($string)
+ {
+ return urlencode(urlencode($string));
+ }
+
+ static function urldec($string)
+ {
+ return urldecode(urldecode($string));
+ }
+
}
////
diff --git a/MainController.php b/MainController.php
index 4eea176..f77f1bb 100644
--- a/MainController.php
+++ b/MainController.php
@@ -16,14 +16,16 @@ class MainController
const ForeignKey = '';
const TableSelect = ''; // extra fields to select
+ public $data;
+ private $table;
+
////
// create controller object
- function __construct($id = null)
+ function __construct($where = null)
{
// child classes typically have the same name as their tables
// but not always
$this->class = get_class($this);
-// $this->table = eval("return {$this->class}::TableName;");
$this->table = static::TableName;
// table name not defined, default to class name
@@ -33,24 +35,49 @@ class MainController
$this->mainView = new MainView();
- if ($id) {
- return $this->get($id);
+ if ($where) {
+ return $this->get($where);
}
return $this;
}
+ ////
+ // Get One Thing
+ public function get($where)
+ {
+ if (!is_array($where)) {
+ $where = array('id' => $where);
+ }
+
+ $this->find($where);
+
+ if (!is_array($this->data)) {
+ return false; // found nothing
+ }
+
+ if (count($this->data) == 1) {
+ return current($this->data);
+ }
+
+ return $this->data;
+
+ }
+
+ ////
+ // Initialize the fields of a class
+ // with the table's default values
public function init()
{
// populate indices
if ($_SESSION['config']['db.driver'] == 'mysql') {
- $schema = MainDb::fetch("DESCRIBE `{$this->table}`");
+ $schema = MainDb::fetch('DESCRIBE `'.$this->table.'`');
foreach ($schema as $s) {
$this->data[$s['Field']] = $s['Default'];
}
- return $schema;
}
+
if ($_SESSION['config']['db.driver'] == 'sqlite') {
$sql = 'PRAGMA TABLE_INFO('.$this->table.')';
$schema = MainDb::fetch($sql);
@@ -61,29 +88,11 @@ class MainController
}
////
- // Get One Thing
- public function get($where)
- {
- if (!is_array($where)) {
- $where = array('id' => $where);
- }
-
- $this->find($where);
-
- if (!is_array($this->data)) {
- return false; // found nothing
- }
-
- return current($this->data);
-
- }
-
- ////
// the New Find
public function find($where = null, $sort = false, $limit = false)
{
+ $q = '';
$select = '*';
- $q = '';
$this->data = array();
// allow custom queries
@@ -112,24 +121,22 @@ class MainController
}
} else {
$q = "WHERE `{$this->table}`.`id` = ?";
- $values = array($where);
+ $values = $where;
}
if ($where == NULL) {
$q = 'WHERE ?';
- $values = array(1);
+ $values = 1;
}
// $schema = App::conf('tables');
// $this->schema = $schema[$this->table];
// Ugly hack until PHP 5.3
- $i_sort = eval("return {$this->class}::OrderBy;");
- $i_fk = eval("return {$this->class}::ForeignKey;");
- $i_mtm = eval("return {$this->class}::ManyToMany;");
- $i_select = eval("return {$this->class}::TableSelect;");
-
-// $orderBy = ($sort) ? $sort :
+ $i_sort = static::OrderBy;
+ $i_fk = static::ForeignKey;
+ $i_mtm = static::ManyToMany;
+ $i_select = static::TableSelect;
if ($sort) {
list($col, $dir) = explode(' ', $sort);
@@ -154,8 +161,7 @@ class MainController
$result = MainDb::fetch($sql, $values);
if (empty($result)) {
- $this->data = false;
- return;
+ return $this->data = array();
}
for ($i=0; $i < count($result); $i++)
@@ -180,18 +186,24 @@ class MainController
private function find_children($row, $i)
{
$id = $row['id']; // ID of the parent
- $fk = explode(',', eval("return $this->class::HasMany;"));
+ $fk = explode(',', static::HasMany);
if (empty($fk[0])) {
- return false;
+ return array();
}
foreach ($fk as $child)
{
+ $order_by = '';
+
+ if ($child::OrderBy) {
+ $order_by = sprintf('ORDER BY `%s`.%s', $child, $child::OrderBy);
+ }
+
// determine nature of relationship
- $sql = "SELECT * FROM `$child` WHERE `{$this->table}_id` = ?";
$one_to_many = explode(',', eval("return $child::ForeignKey;"));
$many_to_many = explode(',', eval("return $child::ManyToMany;"));
+ $sql = "SELECT * FROM `$child` WHERE `{$this->table}_id` = ? $order_by";
if (in_array($this->table, $many_to_many)) // m/n
{
@@ -202,11 +214,15 @@ class MainController
WHERE `{$child}_{$this->table}`.`{$this->table}_id` = `$this->table`.id AND
`{$child}_{$this->table}`.`{$child}_id` = `$child`.id AND
`{$this->table}`.id = ?";
- } else if (@in_array ($table, $ref_schema['belongsTo'])) { // 1/m
+ } else if (@in_array($table, $ref_schema['belongsTo'])) { // 1/m
$sql = "SELECT * FROM `$ref` WHERE `$ref`.`{$table}_id` = ?";
}
$stmt = MainDb::query($sql, array($id));
+ if (!$stmt) {
+ return MainApp::error('Error executing query ' . $sql);
+ }
+
$this->data[$i][$child] = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
@@ -405,10 +421,10 @@ class MainController
$sql .= "`{$col}`,";
}
- $sql = rtrim($sql, ",");
+ $sql = rtrim($sql, ',');
$sql = "SELECT * FROM `{$this->table}` WHERE MATCH($sql) AGAINST('{$match}')";
- return MainApp::db()->query($sql);
+ return MainDb::fetch($sql);
}
diff --git a/MainDb.php b/MainDb.php
index 6417c19..f26527d 100644
--- a/MainDb.php
+++ b/MainDb.php
@@ -30,7 +30,6 @@ class MainDb
// always use UTF-8?
self::$instance->query('SET NAMES utf8');
break;
-
case 'sqlite':
self::$instance = new PDO('sqlite:'.$c['db.path']);
break;
@@ -56,15 +55,27 @@ class MainDb
////
// execute an SQL query
// @return mixed
- public static function query($sql, $data = null)
+ public static function query($sql, $data = NULL)
{
- if (!$data) {
- $data = array();
+ $args = func_get_args();
+ $sql = array_shift($args);
+
+ if (!is_string($sql)) {
+ return false;
}
- // might be just a string
if (!is_array($data)) {
+ $data = $args;
+ }
+
+ // might just be a string
+ if (!is_array($data)) {
$data = array($data);
+ }
+
+ // might be just an empty array
+ if (empty($data)) {
+ $data = array();
}
try {
@@ -74,7 +85,7 @@ class MainDb
if (!$stmt) {
list($ec, $dec, $emsg) = $pdo->errorInfo();
- $error = $emsg ."\n" . print_r(debug_backtrace(), true);
+ $error = $emsg ."\n" . print_r(debug_backtrace(), TRUE);
return MainApp::error($error);
}
@@ -82,13 +93,13 @@ class MainDb
if (!$result) {
list($ec, $dec, $emsg) = $pdo->errorInfo();
- $error = $emsg ."\n" . print_r(debug_backtrace(), true);
+ $error = $emsg ."\n" . print_r(debug_backtrace(), TRUE);
return MainApp::error($error);
}
} catch (PDOException $e) {
$error = $e->getMessage() . $sql;
- $error .= "\n" . print_r(debug_backtrace(), true);
+ $error .= "\n" . print_r(debug_backtrace(), TRUE);
return MainApp::error($error);
}
@@ -102,7 +113,7 @@ class MainDb
return $stmt;
}
- // describe statements need the query results
+ // pragma statements need the query results (SQLite)
if (preg_match('/^PRAGMA/i', $sql)) {
return $stmt;
}
@@ -125,10 +136,15 @@ class MainDb
////
// fetch something from DB
- public static function fetch($sql, $data = null)
+ public static function fetch($sql, $data = NULL)
{
- $stmt = self::query($sql, $data)
- or exit(MainApp::error('Error executing query '.$sql));
+ $args = func_get_args();
+ $sql = array_shift($args);
+
+ if (is_array($data)) {
+ $args = $data;
+ }
+ $stmt = self::query($sql, $args) or exit(MainApp::error('Error executing query '.$sql));
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}