diff options
author | Filipp Lepalaan <filipp@mac.com> | 2011-03-30 16:35:19 +0300 |
---|---|---|
committer | Filipp Lepalaan <filipp@mac.com> | 2011-03-30 16:35:19 +0300 |
commit | f5cb46d5ffa5c46d4b1aa176d9104e8dd67b24ae (patch) | |
tree | da5acaed70c97a00c2efbbc6bd617a840c685864 | |
parent | 914599c7c2a50c8f1439189214184a2a0f402fc8 (diff) | |
download | main-f5cb46d5ffa5c46d4b1aa176d9104e8dd67b24ae.tar.gz main-f5cb46d5ffa5c46d4b1aa176d9104e8dd67b24ae.tar.bz2 main-f5cb46d5ffa5c46d4b1aa176d9104e8dd67b24ae.zip |
better
-rw-r--r--[-rwxr-xr-x] | MainApp.php | 83 | ||||
-rw-r--r--[-rwxr-xr-x] | MainController.php | 13 | ||||
-rw-r--r--[-rwxr-xr-x] | MainDb.php | 9 | ||||
-rw-r--r--[-rwxr-xr-x] | MainView.php | 18 | ||||
-rw-r--r--[-rwxr-xr-x] | README.md (renamed from README) | 0 | ||||
-rw-r--r--[-rwxr-xr-x] | config.default.ini | 3 |
6 files changed, 76 insertions, 50 deletions
diff --git a/MainApp.php b/MainApp.php index e5f2811..0219a48 100755..100644 --- a/MainApp.php +++ b/MainApp.php @@ -3,13 +3,6 @@ // main/MainApp.php // @author Filipp Lepalaan <filipp@mekanisti.fi> // @copyright (c) 2009 Filipp Lepalaan - -/* This program is free software. It comes without any warranty, to - * the extent permitted by applicable law. You can redistribute it - * and/or modify it under the terms of the Do What The Fuck You Want - * To Public License, Version 2, as published by Sam Hocevar. See - * http://sam.zoy.org/wtfpl/COPYING for more details. */ - class MainApp { //// @@ -130,26 +123,33 @@ class MainApp } - static function ok($msg) + static function ok() { - $ok = array('ok' => $msg); - self::json($ok); + $args = func_get_args(); + $ok = array_shift($args); + self::json(array('ok' => $ok, 'data' => $args)); } static function error($msg) { $err = array('error' => $msg); + // send it to the browser self::json($err); trigger_error($msg, E_USER_NOTICE); - // And log it locally + // and log it locally self::log($msg); } - - static function json($msg) + + //// + // send JSON data back to browser + static function json() { - $json = json_encode($msg); + $out = array(); + $args = func_get_args(); + $out = (count($args == 1)) ? $args[0] : $args; + $json = json_encode($out); header('Content-Type: application/json'); - header('Content-Length: ' . mb_strlen($json)); + header('Content-Length: '.mb_strlen($json)); print $json; } @@ -163,26 +163,27 @@ class MainApp exit('Log file does not exist'); } - $fh = fopen($file, 'a+'); + $fh = fopen($file, 'a+') or die('Failed to open log file'); + $header = basename(__FILE__) . ' on line ' . __LINE__; foreach (func_get_args() as $arg) { if (is_array($arg) || is_object($arg)) { $arg = print_r($arg, true); } - fwrite($fh, date('r') . "\t" . trim($arg) . "\n"); + fwrite($fh, $header . "\t" . trim($arg) . "\n"); } fclose($fh); } //// // do a proper HTTP redirect - // @param string [$where] URL to redirect to + // @param string [$url] URL to redirect to // @return void static function redirect($url = null) { if (!$url) { - // Is it smart to redirect back to the page which redirected here? + // @fixme redirect back to the page which redirected here? $url = $_SERVER['HTTP_REFERER']; } header('HTTP/1.1 303 See Other'); @@ -201,7 +202,7 @@ class MainApp return sprintf('%s_%s', $loc, strtoupper($lang)); } - // Move this back to Controller once PHP 5.3 is out (get_called_class()) + // move this back to Controller once PHP 5.3 is out (get_called_class()) static function select($table, $where = 1, $what = '*', $order_by = '') { $out = array(); @@ -215,7 +216,7 @@ class MainApp $keys[] = "`$k` = :{$k}"; $values[":{$k}"] = $v; } - $query = implode(" AND ", $keys); + $query = implode(' AND ', $keys); } if (!empty($order_by)) { @@ -263,8 +264,7 @@ class MainApp // convert a "public" name to a class name static function classname($name) { - $name = str_replace('_', ' ', $name); - $name = ucwords($name); + $name = ucwords(str_replace('_', ' ', $name)); $class_name = str_replace(' ', '', $name); return $class_name; } @@ -297,6 +297,43 @@ class MainApp return urldecode(urldecode($string)); } + /** + * Load the proper language file and return the translated phrase + * The language file is JSON encoded and returns an associative array + * Language filename is determined by BCP 47 + RFC 4646 + * http://www.rfc-editor.org/rfc/bcp/bcp47.txt + * @param string $phrase The phrase that needs to be translated + * @return string + */ + static function localize($phrase) + { + /* Static keyword is used to ensure the file is loaded only once */ + static $translations = NULL; + + if (!defined('APP_LANGUAGE')) { + define('APP_LANGUAGE', self::conf('defaults.locale')); + } + + if (is_null($translations)) + { + $lang_file = '../system/lang/' . APP_LANGUAGE . '.txt'; + + if (!file_exists($lang_file)) { + return $phrase; + } + + $lang_file_content = file_get_contents($lang_file); + /* Load the language file as a JSON object and transform it into an associative array */ + $translations = json_decode($lang_file_content, TRUE); + } + + if (array_key_exists($phrase, $translations)) { + return $translations[$phrase]; + } else { + return $phrase; + } + + } } //// diff --git a/MainController.php b/MainController.php index 7b6012e..49a60b5 100755..100644 --- a/MainController.php +++ b/MainController.php @@ -2,13 +2,6 @@ //// // main/MainController.php // @TODO: transfer boeuf.php here - -/* This program is free software. It comes without any warranty, to - * the extent permitted by applicable law. You can redistribute it - * and/or modify it under the terms of the Do What The Fuck You Want - * To Public License, Version 2, as published by Sam Hocevar. See - * http://sam.zoy.org/wtfpl/COPYING for more details. */ - class MainController { public $view; // Where to store the data to be rendered @@ -224,10 +217,11 @@ class MainController `{$table}_{$this->table}`.`{$table}_id` = `$table`.id AND `{$this->table}`.id = ?"; } else if (@in_array($table, $ref_schema['belongsTo'])) { // 1/m - $sql = "SELECT * FROM `$ref` WHERE `$ref`.`{$table}_id` = ?"; + $sql = "SELECT * FROM `$ref` WHERE `$ref`.`{$table}_id` = ?"; } $stmt = MainDb::query($sql, array($id)); + if (!$stmt) { return MainApp::error('Error executing query ' . $sql); } @@ -330,7 +324,8 @@ class MainController //// // update this Thing // We keep this in the Controller since it might know - // more about the topmost class + // more about the topmost class + // return the updated Thing protected function update($data, $where = NULL) { if (!is_array($data)) { diff --git a/MainDb.php b/MainDb.php index b2ce171..b8c16ce 100755..100644 --- a/MainDb.php +++ b/MainDb.php @@ -3,13 +3,6 @@ // main/MainDb.php // @author Filipp Lepalaan <filipp@mekanisti.fi> // http://www.php.net/manual/en/language.oop5.patterns.php - -/* This program is free software. It comes without any warranty, to - * the extent permitted by applicable law. You can redistribute it - * and/or modify it under the terms of the Do What The Fuck You Want - * To Public License, Version 2, as published by Sam Hocevar. See - * http://sam.zoy.org/wtfpl/COPYING for more details. */ - class MainDb { private static $instance = NULL; @@ -110,7 +103,7 @@ class MainDb return MainApp::error($error); } - // DELETE statements should report number of rows + // DELETE statements should report number of rows deleted if (preg_match('/^DELETE/i', $sql)) { return $stmt->rowCount(); } diff --git a/MainView.php b/MainView.php index b475abb..b5bce86 100755..100644 --- a/MainView.php +++ b/MainView.php @@ -3,13 +3,6 @@ // main/MainView.php // @created 31.10.2009 // @author Filipp Lepalaan <filipp@mac.com> - -/* This program is free software. It comes without any warranty, to - * the extent permitted by applicable law. You can redistribute it - * and/or modify it under the terms of the Do What The Fuck You Want - * To Public License, Version 2, as published by Sam Hocevar. See - * http://sam.zoy.org/wtfpl/COPYING for more details. */ - class MainView { //// @@ -22,6 +15,13 @@ class MainView } //// + // render a table + function table($data, $cols = null) + { + + } + + //// // create HTML <select> options from array // @param array array // @param mixed select option with this value @@ -29,9 +29,9 @@ class MainView function select($array, $current = null) { $out = ''; - + foreach ($array as $k => $v) { - $sel = ($k == $current) ? ' selected="selected" ' : ''; + $sel = ($k == $current) ? ' selected="selected"' : ''; $out .= "<option value=\"{$k}\"{$sel}>{$v}</option>\n\t"; } diff --git a/config.default.ini b/config.default.ini index dca0c40..332bcff 100755..100644 --- a/config.default.ini +++ b/config.default.ini @@ -8,6 +8,7 @@ db.password = app.timezone = Europe/Helsinki app.error_log = error.log +defaults.locale = fi_FI defaults.controller = session defaults.action = check -defaults.title = MyWebApp
\ No newline at end of file +defaults.title = MyWebApp |