]> git.andy128k.dev Git - ipf.git/commitdiff
rework admin log
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 24 Aug 2014 17:50:53 +0000 (20:50 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 24 Aug 2014 17:50:53 +0000 (20:50 +0300)
ipf/admin/log.php [new file with mode: 0644]
ipf/admin/migrations/20140103000000_create_admin_log.php [new file with mode: 0644]
ipf/admin/model.php
ipf/admin/models.yml [deleted file]
ipf/admin/models/AdminLog.php [deleted file]
ipf/admin/models/_generated/BaseAdminLog.php [deleted file]
ipf/admin/templates/admin/index.html
ipf/admin/views.php

diff --git a/ipf/admin/log.php b/ipf/admin/log.php
new file mode 100644 (file)
index 0000000..d6c487b
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+
+class IPF_Admin_Log
+{
+    public static function log($who, $action, $repr, $class, $url=null)
+    {
+        $data = array(
+            'username'      => $who->username,
+            'user_id'       => $who->id,
+            'action'        => $action,
+            'object_class'  => $class,
+            'object_repr'   => $repr,
+            'object_url'    => $url,
+        );
+        \PFF\Container::databaseQuery()
+            ->insertInto('admin_log', $data)
+            ->execute();
+    }
+
+    public static function logObject($component, $action, $object, $object_id=null)
+    {
+        $url = $object_id ? IPF_HTTP_URL::urlForView('IPF_Admin_Views_EditItem', array($component->app->slug(), $component->slug(), $object_id)) : '';
+        self::log($component->request->user, $action, (string)$object, $component->verbose_name(), $url);
+    }
+}
+
diff --git a/ipf/admin/migrations/20140103000000_create_admin_log.php b/ipf/admin/migrations/20140103000000_create_admin_log.php
new file mode 100644 (file)
index 0000000..015a9f5
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+class Migration_20140103000000 extends \PFF\Migrations\Migration
+{
+    function migrate()
+    {
+        $this->connection->exec('CREATE TABLE admin_log (' .
+            'id BIGINT AUTO_INCREMENT,' .
+            'username     VARCHAR(32),' .
+            'user_id      BIGINT,' .
+            'action       VARCHAR(32) NOT NULL,' .
+            'object_class VARCHAR(200),' .
+            'object_repr  VARCHAR(200),' .
+            'object_url   VARCHAR(200),' .
+            'created_at   TIMESTAMP,' .
+            'INDEX idx_created_at_idx (created_at),' .
+            'PRIMARY KEY(id)) '.
+            'DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = INNODB');
+    }
+}
+
index 9a9fba388b8bd2d942b36f6ead448d12bd89a7ad..679ecad42df4ce42406d4ff8a4896b26cce91280 100644 (file)
@@ -593,7 +593,7 @@ abstract class IPF_Admin_Component
             if ($form->isValid()) {
                 list($id, $object) = $this->saveObject($form, null);
 
-                AdminLog::logAction($request->user, AdminLog::ADDITION, (string)$object, IPF_HTTP_URL::urlForView('IPF_Admin_Views_EditItem', array($this->app->slug(), $this->slug(), $id)));
+                IPF_Admin_Log::logObject($this, 'add', $object, $id);
 
                 $url = @$request->POST['ipf_referrer'];
                 if (!$url)
@@ -630,7 +630,7 @@ abstract class IPF_Admin_Component
             throw new IPF_HTTP_Error404;
 
         if ($request->method == 'POST') {
-            AdminLog::logAction($request->user, AdminLog::DELETION, (string)$object);
+            IPF_Admin_Log::logObject($this, 'delete', $object);
 
             $this->deleteObject($object);
 
@@ -664,7 +664,7 @@ abstract class IPF_Admin_Component
             if ($form->isValid()) {
                 list($id, $object) = $this->saveObject($form, $object);
 
-                AdminLog::logAction($request->user, AdminLog::CHANGE, (string)$object, IPF_HTTP_URL::urlForView('IPF_Admin_Views_EditItem', array($this->app->slug(), $this->slug(), $id)));
+                IPF_Admin_Log::logObject($this, 'change', $object, $id);
 
                 $url = @$request->POST['ipf_referrer'];
                 if (!$url)
diff --git a/ipf/admin/models.yml b/ipf/admin/models.yml
deleted file mode 100644 (file)
index b98918c..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-AdminLog:
-    tableName: admin_log
-    actAs: 
-        Timestampable:
-            updated:
-                disabled: true
-    columns:
-        username: string(32)
-        user_id: integer
-        object_id: integer
-        object_class: string(200)
-        object_repr: string(200)
-        object_url: string(200)
-        action_flag: integer
-        change_message: string(200)
-
-    indexes:
-        idx_created_at: 
-            fields: created_at
-
-    options:
-        type: INNODB
-        collate: utf8_unicode_ci
-        charset: utf8
-
diff --git a/ipf/admin/models/AdminLog.php b/ipf/admin/models/AdminLog.php
deleted file mode 100644 (file)
index 3f8fa00..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-<?php
-
-class AdminLog extends BaseAdminLog
-{
-    const ADDITION  = 1;
-    const CHANGE    = 2;
-    const DELETION  = 3;
-
-    public static function logAction($who, $action, $repr, $url=null)
-    {
-        $log = new AdminLog;
-        $log->username    = $who->username;
-        $log->user_id     = $who->id;
-        $log->object_repr = $repr;
-        $log->object_url  = $url;
-        $log->action_flag = $action;
-        $log->save();
-    }
-
-    public function is_addition()
-    {
-        return $this->action_flag == AdminLog::ADDITION;
-    }
-
-    public function is_change()
-    {
-        return $this->action_flag == AdminLog::CHANGE;
-    }
-
-    public function is_deletion()
-    {
-        return $this->action_flag == AdminLog::DELETION;
-    }
-}
-
diff --git a/ipf/admin/models/_generated/BaseAdminLog.php b/ipf/admin/models/_generated/BaseAdminLog.php
deleted file mode 100644 (file)
index b49ba81..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-<?php
-
-/**
- * This class has been auto-generated by the IPF_ORM Framework.
- * Changes to this file may cause incorrect behavior
- * and will be lost if the code is regenerated.
- */
-
-abstract class BaseAdminLog extends IPF_ORM_Record
-{
-  public static function setTableDefinition(IPF_ORM_Table $table)
-  {
-    $table->setTableName('admin_log');
-    $table->setColumn('username', 'string', 32, array('type' => 'string', 'length' => '32'));
-    $table->setColumn('user_id', 'integer', null, array('type' => 'integer'));
-    $table->setColumn('object_id', 'integer', null, array('type' => 'integer'));
-    $table->setColumn('object_class', 'string', 200, array('type' => 'string', 'length' => '200'));
-    $table->setColumn('object_repr', 'string', 200, array('type' => 'string', 'length' => '200'));
-    $table->setColumn('object_url', 'string', 200, array('type' => 'string', 'length' => '200'));
-    $table->setColumn('action_flag', 'integer', null, array('type' => 'integer'));
-    $table->setColumn('change_message', 'string', 200, array('type' => 'string', 'length' => '200'));
-    $table->addIndex('idx_created_at', array('fields' => 'created_at'));
-    $table->setOption('type', 'INNODB');
-    $table->setOption('collate', 'utf8_unicode_ci');
-    $table->setOption('charset', 'utf8');
-
-  }
-
-  public static function setUp(IPF_ORM_Table $table)
-  {
-    $table->addTemplate(new IPF_ORM_Template_Timestampable(array('updated' => array('disabled' => true))));
-  }
-
-  public static function table()
-  {
-    return IPF_ORM::getTable('AdminLog');
-  }
-
-  public static function query($alias='')
-  {
-    return IPF_ORM::getTable('AdminLog')->createQuery($alias);
-  }
-}
\ No newline at end of file
index 9236fe3c20aa861d75c50007fd87894e82ad1b1a..34f7272e5b4b4732eee9acfef1dee51b817b1b70 100644 (file)
             <h3>{trans 'My Actions'}</h3>
             <ul class="actionlist">
             {foreach $admin_log as $log}
-            <li class="{if $log.is_addition()}addlink{/if}{if $log.is_change()}changelink{/if}{if $log.is_deletion()}deletelink{/if}">{if !$log.is_deletion()}<a href="{$log.object_url}">{/if}{$log.object_repr}{if !$log.is_deletion()}</a>{/if}<br /><span class="mini quiet">{$log.object_class} at {$log.created_at|date} by {$log.username}</span></li>
+            <li class="{$log.action}link">
+              {if $log.object_url}
+                <a href="{$log.object_url}">{$log.object_repr}</a>
+              {else}
+                {$log.object_repr}
+              {/if}
+              <br><span class="mini quiet">{$log.object_class} at {$log.created_at|date} by {$log.username}</span>
+            </li>
             {/foreach}
             </ul>
         </div>
index 8a42707cddd559983dc90577f4e4bb5b704587ee..e65d44b92decf771ac754c85a41137901202c4d3 100644 (file)
@@ -25,12 +25,12 @@ function IPF_Admin_Views_Index($request, $match)
         }
     }
 
-    $admin_log = IPF_ORM_Query::create()
-        ->select("*")
-        ->from('AdminLog')
-        ->orderby('created_at desc')
+    $admin_log = \PFF\Container::databaseQuery()
+        ->from('admin_log')
+        ->orderBy('created_at DESC')
         ->limit(10)
-        ->execute();
+        ->asObject(true)
+        ->fetchAll();
 
     $context = array(
         'page_title' => __('Site Administration'),