From f5cb46d5ffa5c46d4b1aa176d9104e8dd67b24ae Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Wed, 30 Mar 2011 16:35:19 +0300 Subject: better --- MainApp.php | 83 +++++++++++++++++++++++++++++++++++++++--------------- MainController.php | 13 +++------ MainDb.php | 9 +----- MainView.php | 18 ++++++------ README | 59 -------------------------------------- README.md | 59 ++++++++++++++++++++++++++++++++++++++ config.default.ini | 3 +- 7 files changed, 135 insertions(+), 109 deletions(-) mode change 100755 => 100644 MainApp.php mode change 100755 => 100644 MainController.php mode change 100755 => 100644 MainDb.php mode change 100755 => 100644 MainView.php delete mode 100755 README create mode 100644 README.md mode change 100755 => 100644 config.default.ini diff --git a/MainApp.php b/MainApp.php old mode 100755 new mode 100644 index e5f2811..0219a48 --- a/MainApp.php +++ b/MainApp.php @@ -3,13 +3,6 @@ // main/MainApp.php // @author Filipp Lepalaan // @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 old mode 100755 new mode 100644 index 7b6012e..49a60b5 --- 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 old mode 100755 new mode 100644 index b2ce171..b8c16ce --- a/MainDb.php +++ b/MainDb.php @@ -3,13 +3,6 @@ // main/MainDb.php // @author Filipp Lepalaan // 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 old mode 100755 new mode 100644 index b475abb..b5bce86 --- a/MainView.php +++ b/MainView.php @@ -3,13 +3,6 @@ // main/MainView.php // @created 31.10.2009 // @author 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 MainView { //// @@ -21,6 +14,13 @@ class MainView include $base.'/'.$path; } + //// + // render a table + function table($data, $cols = null) + { + + } + //// // create HTML