This is the online demonstration of the I-Metrics CMS v1.11 (Stock Template 2 Columns SEO)

Database Records in Plugins

Friday 16 December, 2011

This article demonstrates how to use the database support functions to create and manage database tables for plugins. The extension system utilizes the core functions of the I-Metrics CMS and passes control to a plugin to backup or restore necessary plugin database entries. We will be using the banner system to better demonstrate the handshake mechanism between the framework and extensions.

The database responsible member functions

Starting with version 1.13 of the I-Metrics CMS the following functions and classes are introduced for database backup and restore purposes.

1. includes/classes/database_backup.php

A core class database_backup is available with functions to backup or restore individual tables directly or via a progressive jQuery/ajax interface. The later is suitable for plugins with huge database records where an interruption of the backup or restore operation is possible to take place.

2. create_references() in install.php of the plugin

Typically invoked with the install method the create_references() can create associated plugin database tables. If the plugin is language dependent the necessary tables for all languages should be created during installation. With the banner system this is what the create_references looks like:

function create_references() {
      extract(tep_load('database', 'languages'));

      $options_array = $this->load_options();
      //$lng->create_plugin_folders($this);

      $lng->create_table('TABLE_BANNERS');

      $result = true;
      $dir = trim($options_array['banners_path'], '/');
      $path = tep_front_physical_path(DIR_WS_CATALOG . $dir);

      if( !empty($dir) ) {
        $result = tep_mkdir($path);
      }

      $path = tep_front_physical_path(DIR_WS_CATALOG);
      $files_query_raw = "select filename from " . TABLE_BANNERS . " where filename != ''";
      $files_array = $db->query_to_array($files_query_raw, false, false);
      for($i=0, $j=count($files_array); $i<$j; $i++) {
        $src = $this->admin_path . 'front/images/' . basename($files_array[$i]['filename']);
        $dst = $path . $files_array[$i]['filename'];
        if( is_file($src) ) {
          copy($src, $dst);
        }
      }
      return $result;
    }

The importand part here is the language call to create_table having the TABLE_BANNERS as the argument. This signals the core framework to create a new multilingual table something the banner system requires for its operation.

3. delete_references() in install.php of the plugin

Typically called during plugin install and uninstall this function removes the database tables associated with the plugin.

function delete_references() {
      extract(tep_load('languages', 'database'));

      $tables_array = $db->get_tables();
      if( isset($tables_array[TABLE_BANNERS]) ) {
        $files_query_raw = "select filename from " . TABLE_BANNERS . " where filename != ''";
        $files_array = $db->query_to_array($files_query_raw, false, false);
        $path = tep_front_physical_path(DIR_WS_CATALOG);
        for( $i=0, $j=count($files_array); $i<$j; $i++) {
          if( is_file($path . $files_array[$i]['filename']) ) {
            @unlink($path . $files_array[$i]['filename']);
          }
        }
      }

      $lng->delete_table('TABLE_BANNERS');

      $db->query("drop table if exists " . TABLE_BANNERS_GROUP);
      $db->query("drop table if exists " . TABLE_BANNERS);
    }

The delete_table member method is the part that removes the language associated tables. Following the call the plugin may also remove other specific tables if required.

4. backup_database() in install.php of the plugin

The backup database should be invoked during plugin backup. This takes place when the administrator copies code and database records for a specific plugin using the revert function of the plugin manager. To support multiple languages and assuming the plugin is multilingual all language tables the plugin processes should be backed up at this stage. The banner system uses the following database backup function.

function backup_database() {
      extract(tep_load('languages', 'database', 'database_backup', 'message_stack'));

      $path = tep_front_physical_path(DIR_WS_CATALOG);

      $files_query_raw = "select filename from " . TABLE_BANNERS;
      $files_array = $db->query_to_array($files_query_raw, false, false);

      for($i=0, $j=count($files_array); $i<$j; $i++) {
        $src = $path . $files_array[$i]['filename'];
        $dst = $this->admin_path . 'front/images/' . basename($files_array[$i]['filename']);

        if( is_file($src) ) {
          copy($src, $dst);
        }
      }

      $tables_array = array(
        TABLE_BANNERS_GROUP
      );
      
      $tmp_array = $lng->get_language_tables(TABLE_BANNERS);
      $tables_array = array_merge($tables_array, $tmp_array);
      $database_backup->save_tables($this->admin_path . 'database.sql', $tables_array);
      $msg->add_session(sprintf(SUCCESS_PLUGIN_DATABASE_BACKUP, $db->prepare_input($this->title)), 'success');
      return true;
    }

The important part is the tables_array function and the interaction with the language class to so the language table includes the TABLE_BANNERS. The save_tables method accepts a path to the backup database file and an array of language aware tables.

Database tables that are multi-lingual, show with a different color under the languages -> assign/synchronize section with the administration tool. During plugin install and remove operations the database table definitions are automatically altered in the includes/database.php file on the web-front. It is important for a plugin to invoke the create_table method with the language table so the I-Metrics CMS framework is aware of the language table.

There are no comments on Database Records in Plugins

Leave a comment
Your Name:
Your E-Mail (will not be published):
Your Website:
Your Comment:
Rate it:
Bad
Good
Cast a Vote
Did you find Database Records in Plugins useful?
No Very