summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2008-12-25 23:43:44 +0000
committerBharat Mediratta <bharat@menalto.com>2008-12-25 23:43:44 +0000
commit2c91a7e9ced0d2134682450dc1713b8e1b239dae (patch)
tree25b7bd2cb67fba30e091ff8e250714250ae7ad4f
parent847d42682d02f2bd86ff0e5a247c38e1f55d86b9 (diff)
Rework log and message helpers to be parallel, but separate.
1) they now have their own matching severity constants 2) they both have convenience functions success(), info(), warning() and error() 3) they both have severity_class()
-rw-r--r--core/controllers/admin_modules.php4
-rw-r--r--core/controllers/albums.php12
-rw-r--r--core/controllers/photos.php4
-rw-r--r--core/controllers/welcome.php8
-rw-r--r--core/helpers/log.php71
-rw-r--r--core/helpers/message.php69
-rw-r--r--core/helpers/module.php4
-rw-r--r--core/views/admin_block_log_entries.html.php2
-rw-r--r--modules/user/controllers/admin_users.php10
-rw-r--r--modules/user/controllers/login.php6
-rw-r--r--modules/user/controllers/logout.php4
-rw-r--r--modules/watermark/controllers/admin_watermarks.php8
12 files changed, 160 insertions, 42 deletions
diff --git a/core/controllers/admin_modules.php b/core/controllers/admin_modules.php
index 152e49c3..c416a699 100644
--- a/core/controllers/admin_modules.php
+++ b/core/controllers/admin_modules.php
@@ -34,10 +34,10 @@ class Admin_Modules_Controller extends Admin_Controller {
$desired = $this->input->post($module_name) == 1;
if ($info->installed && !$desired) {
module::uninstall($module_name);
- message::add(sprintf(_("Uninstalled %s module"), $info->name));
+ message::success(sprintf(_("Uninstalled %s module"), $info->name));
} else if (!$info->installed && $desired) {
module::install($module_name);
- message::add(sprintf(_("Installed %s module"), $info->name));
+ message::success(sprintf(_("Installed %s module"), $info->name));
}
}
url::redirect("admin/modules");
diff --git a/core/controllers/albums.php b/core/controllers/albums.php
index 47412dfc..61d6ad22 100644
--- a/core/controllers/albums.php
+++ b/core/controllers/albums.php
@@ -79,9 +79,9 @@ class Albums_Controller extends Items_Controller {
$this->input->post("description"),
user::active()->id);
- log::add("content", "Created an album", log::INFO,
+ log::success("content", "Created an album",
html::anchor("albums/$new_album->id", "view album"));
- message::add(_("Successfully created album"));
+ message::success(sprintf(_("Created album %s"), $new_album->title));
print json_encode(
array("result" => "success",
@@ -107,9 +107,9 @@ class Albums_Controller extends Items_Controller {
$this->input->post("description"),
user::active()->id);
- log::add("content", "Added a photo", log::INFO,
+ log::success("content", "Added a photo",
html::anchor("photos/$photo->id", "view photo"));
- message::add(_("Successfully added photo"));
+ message::add(sprintf(_("Added photo %s"), $photo->title));
print json_encode(
array("result" => "success",
@@ -139,8 +139,8 @@ class Albums_Controller extends Items_Controller {
module::event("album_changed", $album);
- log::add("content", "Updated album", log::INFO, "<a href=\"albums/$album->id\">view</a>");
- message::add(_("Successfully saved album"));
+ log::success("content", "Updated album", "<a href=\"albums/$album->id\">view</a>");
+ message::success(sprintf(_("Saved album %s"), $album->title));
print json_encode(
array("result" => "success",
diff --git a/core/controllers/photos.php b/core/controllers/photos.php
index 04553bd1..13185283 100644
--- a/core/controllers/photos.php
+++ b/core/controllers/photos.php
@@ -56,8 +56,8 @@ class Photos_Controller extends Items_Controller {
module::event("photo_changed", $photo);
- log::add("content", "Updated photo", log::INFO, "<a href=\"photos/$photo->id\">view</a>");
- message::add(_("Successfully saved photo"));
+ log::success("content", "Updated photo", "<a href=\"photos/$photo->id\">view</a>");
+ message::success(sprintf(_("Saved photo %s"), $photo->title));
print json_encode(
array("result" => "success",
diff --git a/core/controllers/welcome.php b/core/controllers/welcome.php
index a5c8c9d6..637ab642 100644
--- a/core/controllers/welcome.php
+++ b/core/controllers/welcome.php
@@ -206,8 +206,8 @@ class Welcome_Controller extends Template_Controller {
}
if ($photo_count > 0) {
- log::add("content", "(scaffold) Added $photo_count photos", log::INFO,
- html::anchor("albums/$parent_id", "View album"));
+ log::success("content", "(scaffold) Added $photo_count photos"
+ html::anchor("albums/$parent_id", "View album"));
}
url::redirect("welcome");
@@ -245,11 +245,11 @@ class Welcome_Controller extends Template_Controller {
}
if ($photo_count > 0) {
- log::add("content", "(scaffold) Added $photo_count photos");
+ log::success("content", "(scaffold) Added $photo_count photos");
}
if ($album_count > 0) {
- log::add("content", "(scaffold) Added $album_count albums");
+ log::success("content", "(scaffold) Added $album_count albums");
}
url::redirect("welcome");
}
diff --git a/core/helpers/log.php b/core/helpers/log.php
index 43e2b8f8..4b135465 100644
--- a/core/helpers/log.php
+++ b/core/helpers/log.php
@@ -18,9 +18,50 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class log_Core {
- const INFO = 1;
- const WARNING = 2;
- const ERROR = 3;
+ const SUCCESS = 1;
+ const INFO = 2;
+ const WARNING = 3;
+ const ERROR = 4;
+
+ /**
+ * Report a successful event.
+ * @param string $category an arbitrary category we can use to filter log messages
+ * @param string $message a detailed log message
+ * @param string $html an html snippet presented alongside the log message to aid the admin
+ */
+ public static function success($category, $message, $html="") {
+ self::add($category, $message, $html, self::SUCCESS);
+ }
+
+ /**
+ * Report an informational event.
+ * @param string $category an arbitrary category we can use to filter log messages
+ * @param string $message a detailed log message
+ * @param string $html an html snippet presented alongside the log message to aid the admin
+ */
+ public static function info($category, $message, $html="") {
+ self::add($category, $message, $html, self::INFO);
+ }
+
+ /**
+ * Report that something went wrong, not fatal, but worth investigation.
+ * @param string $category an arbitrary category we can use to filter log messages
+ * @param string $message a detailed log message
+ * @param string $html an html snippet presented alongside the log message to aid the admin
+ */
+ public static function warning($category, $message, $html="") {
+ self::add($category, $message, $html, self::WARNING);
+ }
+
+ /**
+ * Report that something went wrong that should be fixed.
+ * @param string $category an arbitrary category we can use to filter log messages
+ * @param string $message a detailed log message
+ * @param string $html an html snippet presented alongside the log message to aid the admin
+ */
+ public static function error($category, $message, $html="") {
+ self::add($category, $message, $html, self::ERROR);
+ }
/**
* Add a log entry.
@@ -30,7 +71,7 @@ class log_Core {
* @param integer $severity INFO, WARNING or ERROR
* @param string $html an html snippet presented alongside the log message to aid the admin
*/
- function add($category, $message, $severity=log::INFO, $html="") {
+ private static function add($category, $message, $html, $severity) {
$log = ORM::factory("log");
$log->category = $category;
$log->message = $message;
@@ -44,4 +85,26 @@ class log_Core {
}
$log->save();
}
+
+
+ /**
+ * Convert a message severity to a CSS class
+ * @param integer $severity
+ * @return string
+ */
+ public function severity_class($severity) {
+ switch($severity) {
+ case self::SUCCESS:
+ return "gSuccess";
+
+ case self::INFO:
+ return "gInfo";
+
+ case self::WARNING:
+ return "gWarning";
+
+ case self::ERROR:
+ return "gError";
+ }
+ }
}
diff --git a/core/helpers/message.php b/core/helpers/message.php
index 1aa09e49..4c4d1e0a 100644
--- a/core/helpers/message.php
+++ b/core/helpers/message.php
@@ -18,33 +18,88 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class message_Core {
- public function add($msg, $severity=log::INFO) {
+ const SUCCESS = 1;
+ const INFO = 2;
+ const WARNING = 3;
+ const ERROR = 4;
+
+ /**
+ * Report a successful event.
+ * @param string $msg a detailed message
+ */
+ public static function success($msg) {
+ self::add($msg, self::SUCCESS);
+ }
+
+ /**
+ * Report an informational event.
+ * @param string $msg a detailed message
+ */
+ public static function info($msg) {
+ self::add($msg, self::INFO);
+ }
+
+ /**
+ * Report that something went wrong, not fatal, but worth investigation.
+ * @param string $msg a detailed message
+ */
+ public static function warning($msg) {
+ self::add($msg, self::WARNING);
+ }
+
+ /**
+ * Report that something went wrong that should be fixed.
+ * @param string $msg a detailed message
+ */
+ public static function error($msg) {
+ self::add($msg, self::ERROR);
+ }
+
+ /**
+ * Save a message in the session for our next page load.
+ * @param string $msg a detailed message
+ * @param integer $severity one of the severity constants
+ */
+ private function add($msg, $severity) {
$session = Session::instance();
$status = $session->get("messages");
$status[] = array($msg, $severity);
$session->set("messages", $status);
}
+ /**
+ * Get any pending messages. These must be displayed to the user since they can only be
+ * retrieved once.
+ * @return html text
+ */
public function get() {
- $messages = Session::instance()->get_once("messages", array());
- if ($messages) {
+ $msgs = Session::instance()->get_once("messages", array());
+ if ($msgs) {
$buf = "<ul id=\"gMessages\">";
- foreach ($messages as $msg) {
+ foreach ($msgs as $msg) {
$buf .= "<li class=\"" . self::severity_class($msg[1]) . "\">$msg[0]</li>";
}
return $buf .= "</ul>";
}
}
+ /**
+ * Convert a message severity to a CSS class
+ * @param integer $severity
+ * @return string
+ */
public function severity_class($severity) {
switch($severity) {
- case log::INFO:
+ case self::SUCCESS:
+ return "gSuccess";
+
+ case self::INFO:
return "gInfo";
- case log::WARNING:
+ case self::WARNING:
return "gWarning";
- case log::ERROR:
+ case self::ERROR:
return "gError";
}
}
diff --git a/core/helpers/module.php b/core/helpers/module.php
index 5dddfa61..415ecf4b 100644
--- a/core/helpers/module.php
+++ b/core/helpers/module.php
@@ -127,7 +127,7 @@ class module_Core {
}
self::load_modules();
- log::add("module", sprintf(_("Installed module %s"), $module_name));
+ log::success("module", sprintf(_("Installed module %s"), $module_name));
}
/**
@@ -137,7 +137,7 @@ class module_Core {
$installer_class = "{$module_name}_installer";
Kohana::log("debug", "$installer_class uninstall");
call_user_func(array($installer_class, "uninstall"));
- log::add("module", sprintf(_("Uninstalled module %s"), $module_name));
+ log::success("module", sprintf(_("Uninstalled module %s"), $module_name));
}
/**
diff --git a/core/views/admin_block_log_entries.html.php b/core/views/admin_block_log_entries.html.php
index 7b25d3fc..c0d9729d 100644
--- a/core/views/admin_block_log_entries.html.php
+++ b/core/views/admin_block_log_entries.html.php
@@ -1,7 +1,7 @@
<? defined("SYSPATH") or die("No direct script access."); ?>
<ul>
<? foreach ($entries as $entry): ?>
- <li class="<?= message::severity_class($entry->severity) ?>">
+ <li class="<?= log::severity_class($entry->severity) ?>">
<a href="<?= url::site("user/$entry->user_id") ?>"><?= $entry->user->name ?></a>
<?= date("Y-M-d H:i:s", $entry->timestamp) ?>
<?= $entry->message ?>
diff --git a/modules/user/controllers/admin_users.php b/modules/user/controllers/admin_users.php
index f7f475cf..8e2fec0a 100644
--- a/modules/user/controllers/admin_users.php
+++ b/modules/user/controllers/admin_users.php
@@ -34,8 +34,8 @@ class Admin_Users_Controller extends Controller {
$form->add_user->full_name->value, $form->add_user->password->value);
$user->email = $form->add_user->email->value;
$user->save();
- log::add("user", sprintf(_("Created user %s"), $user->name));
- message::add(sprintf(_("Created user %s"), $user->name));
+ log::success("user", sprintf(_("Created user %s"), $user->name));
+ message::success(sprintf(_("Created user %s"), $user->name));
$output = '<li>' . $user->name . ' <a href="#">edit</a><div>' .
user::get_edit_form_admin($user) . '</div><a href="#">delete</a><div>' .
user::get_delete_form_admin($user, "admin/users/delete/{$user->id}") .
@@ -62,8 +62,8 @@ class Admin_Users_Controller extends Controller {
$name = $user->name;
$user->delete();
- log::add("user", sprintf(_("Deleted user %s"), $name));
- message::add(sprintf(_("Deleted user %s"), $name));
+ log::success("user", sprintf(_("Deleted user %s"), $name));
+ message::success(sprintf(_("Deleted user %s"), $name));
url::redirect("admin/users");
}
@@ -83,7 +83,7 @@ class Admin_Users_Controller extends Controller {
$user->password = $form->edit_user->password->value;
$user->email = $form->edit_user->email->value;
$user->save();
- message::add(sprintf(_("Changed user %s"), $user->name));
+ message::success(sprintf(_("Changed user %s"), $user->name));
url::redirect("admin/users");
}
diff --git a/modules/user/controllers/login.php b/modules/user/controllers/login.php
index 48527a41..0abd021b 100644
--- a/modules/user/controllers/login.php
+++ b/modules/user/controllers/login.php
@@ -33,8 +33,8 @@ class Login_Controller extends Controller {
if ($valid) {
$user = ORM::factory("user")->where("name", $form->login->inputs["name"]->value)->find();
if (!$user->loaded || !user::is_correct_password($user, $form->login->password->value)) {
- log::add("user", sprintf(_("Failed login for %s"), $form->login->inputs["name"]->value),
- log::WARNING);
+ log::warning(
+ "user", sprintf(_("Failed login for %s"), $form->login->inputs["name"]->value));
$form->login->inputs["name"]->add_error("invalid_login", 1);
$valid = false;
}
@@ -42,7 +42,7 @@ class Login_Controller extends Controller {
if ($valid) {
user::login($user);
- log::add("user", "User $user->name logged in");
+ log::success("user", sprintf(_("User %s logged in"), $user->name));
print json_encode(
array("result" => "success"));
} else {
diff --git a/modules/user/controllers/logout.php b/modules/user/controllers/logout.php
index 5f4057af..e0da9a9f 100644
--- a/modules/user/controllers/logout.php
+++ b/modules/user/controllers/logout.php
@@ -21,8 +21,8 @@ class Logout_Controller extends Controller {
public function index() {
$user = user::active();
user::logout();
- log::add("user", "User $user->name logged out",
- log::INFO, html::anchor("user/$user->id", $user->name));
+ log::info("user", sprintf(_("User %s logged out"), $user->name),
+ html::anchor("user/$user->id", $user->name));
if ($this->input->get("continue")) {
url::redirect($this->input->get("continue"));
}
diff --git a/modules/watermark/controllers/admin_watermarks.php b/modules/watermark/controllers/admin_watermarks.php
index d1c9026d..fa17ba8c 100644
--- a/modules/watermark/controllers/admin_watermarks.php
+++ b/modules/watermark/controllers/admin_watermarks.php
@@ -25,15 +25,15 @@ class Admin_Watermarks_Controller extends Admin_Controller {
$pathinfo = pathinfo($file);
$name = preg_replace("/uploadfile-[^-]+-(.*)/", '$1', $pathinfo["basename"]);
if (ORM::factory("watermark")->where("name", $name)->count_all() > 0) {
- message::add(_("There is already a watermark with that name"), log::WARNING);
+ message::error(_("There is already a watermark with that name"));
} else if (!($image_info = getimagesize($file))) {
- message::add(_("An error occurred while saving this watermark"), log::WARNING);
+ message::warning(_("An error occurred while saving this watermark"));
} else {
if (empty($pathinfo["extension"])) {
$name .= "." . image_type_to_extension($image_info[2]);
}
if (!rename($file, VARPATH . "modules/watermark/$name")) {
- message::add(_("An error occurred while saving this watermark"), log::WARNING);
+ message::warning(_("An error occurred while saving this watermark"));
} else {
$watermark = ORM::factory("watermark");
$watermark->name = $name;
@@ -42,7 +42,7 @@ class Admin_Watermarks_Controller extends Admin_Controller {
$watermark->mime_type = $image_info["mime"];
$watermark->save();
- message::add(_("Watermark saved"));
+ message::success(_("Watermark saved"));
url::redirect("admin/watermarks");
}
}