Exemple #1
0
 /** Upgrade the installation, if necessary. */
 function upgrade_database()
 {
     global $ec3, $wpdb;
     // Check version - return if no upgrade required.
     $installed_version = get_option('ec3_version');
     if ($installed_version == $ec3->version) {
         return;
     }
     $v0 = new ec3_Version($installed_version);
     $v1 = new ec3_Version($ec3->version);
     if ($v0->cmp($v1) > 0) {
         return;
     }
     // Installed version later than this one ?!?!
     // Upgrade.
     $message = sprintf(__('Upgraded database to %1$s Version %2$s', 'ec3'), 'Event-Calendar', $ec3->version) . '.';
     $tables = $wpdb->get_results('SHOW TABLES', ARRAY_N);
     if (!$tables) {
         die(sprintf(__('Error upgrading database for %s plugin.', 'ec3'), 'Event-Calendar'));
     }
     $table_exists = false;
     foreach ($tables as $t) {
         if (preg_match("/{$ec3->schedule}/", $t[0])) {
             $table_exists = true;
         }
     }
     if ($table_exists) {
         $message .= '<br />' . __('Table already existed', 'ec3') . '.';
     } else {
         $message .= '<br />' . sprintf(__('Created table %s', 'ec3'), $ec3->schedule) . '.';
         $wpdb->query("CREATE TABLE {$ec3->schedule} (\n           sched_id BIGINT(20) AUTO_INCREMENT,\n           post_id  BIGINT(20),\n           sequence BIGINT(20),\n           start    DATETIME,\n           end      DATETIME,\n           allday   BOOL,\n           rpt      VARCHAR(64),\n           PRIMARY KEY(sched_id)\n         )");
         // Force the special upgrade page if we are coming from v3.0
         if ($ec3->event_category && (empty($v0) || $v0[0] < 3 || $v0[0] == 3 && $v0[1] == 0)) {
             update_option('ec3_upgrade_posts', 1);
         }
     }
     // end if(!$table_exists)
     // Sequence column is new in v3.2.dev-01
     $v32dev01 = new ec3_Version('3.2.dev-01');
     if ($v0->cmp($v32dev01) < 0) {
         $message .= '<br />' . sprintf(__('Added SEQUENCE column to table %s', 'ec3'), $ec3->schedule) . '.';
         $wpdb->query("ALTER TABLE {$ec3->schedule} ADD COLUMN sequence BIGINT(20) DEFAULT 1");
     }
     // Option ec3_show_event_box is new in v3.2.dev-02
     $hide_event_box = get_option('ec3_hide_event_box');
     if ($hide_event_box !== false) {
         if (intval($hide_event_box)) {
             $ec3->set_show_event_box(2);
         } else {
             $ec3->set_show_event_box(0);
         }
         update_option('ec3_hide_event_box', false);
     }
     // Record the new version number
     update_option('ec3_version', $ec3->version);
     // Display an informative message.
     echo '<div id="message" class="updated fade"><p><strong>';
     echo $message;
     echo "</strong></p></div>\n";
 }
function create_plugin_database_table()
{
    global $wpdb;
    // Check version - return if no upgrade required.
    $installed_version = get_option('ec3_version');
    $v0 = new ec3_Version($installed_version);
    $v1 = new ec3_Version('3.4.1');
    $miseAJour = 'ok';
    if ($v0->cmp($v1) < 0) {
        $table_schedule = $wpdb->prefix . 'ec3_schedule';
        $table_lieux = $wpdb->prefix . 'ec3_lieux';
        $table_opt = $wpdb->prefix . 'ec3_add_opt';
        $table_geo = $wpdb->prefix . 'geopress';
        $table_meta = $wpdb->prefix . 'postmeta';
        $table_oa_event = $wpdb->prefix . 'ec3_oa_event';
        $table_oa_agenda = $wpdb->prefix . 'ec3_oa_agenda';
        $table_exists = false;
        $table_geo_exists = false;
        $tables = $wpdb->get_results('SHOW TABLES', ARRAY_N);
        foreach ($tables as $t) {
            if (preg_match("/{$table_schedule}/", $t[0])) {
                $table_exists = true;
            }
            if (preg_match("/{$table_geo}/", $t[0])) {
                $table_geo_exists = true;
            }
        }
        $sql1 = "CREATE TABLE {$table_lieux} (\n                 lieux_id     INT(10) AUTO_INCREMENT,\n                 departement  VARCHAR(3),\n                 nom_ville    VARCHAR(85),\n                 nom_lieux    VARCHAR(255),\n                 adresse      VARCHAR(255),\n                 longitude    FLOAT(18,14),\n                 latitude     FLOAT(18,14),\n                 lieux_uid    BIGINT(20),\n                 PRIMARY KEY(lieux_id)\n               )";
        $sql2 = "CREATE TABLE {$table_opt} (\n                 option_id        INT(10) AUTO_INCREMENT,\n                 nom              VARCHAR(85),\n                 message          VARCHAR(255),\n                 post_type_expo   ENUM('true', 'false') DEFAULT 'false',\n                 PRIMARY KEY(option_id)\n               )";
        $sql3 = "CREATE TABLE {$table_oa_agenda} (\n                 agenda_uid   BIGINT(20),\n                 title        VARCHAR(255),\n                 slugName     VARCHAR(255),\n                 PRIMARY KEY(agenda_uid)\n               )";
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        dbDelta($sql1);
        dbDelta($sql2);
        dbDelta($sql3);
        if ($table_exists) {
            // envoye des status dans la table ec3_add_opt
            $toutes_les_options = $wpdb->get_results("SELECT DISTINCT status FROM {$table_schedule}");
            foreach ($toutes_les_options as $the_option) {
                $wpdb->insert($ec3->table_opt, array('nom' => $the_option->status));
            }
            // ajout de colones dans la table schedule
            /*$wpdb->query("IF COL_LENGTH('$table_schedule', 'sequence') IS NULL BEGIN (ALTER TABLE $table_schedule ADD sequence INT(10) AFTER `rpt` ) END");
              $wpdb->query("IF COL_LENGTH('$table_schedule', 'lieux_id') IS NULL BEGIN (ALTER TABLE $table_schedule ADD lieux_id INT(10) AFTER `sequence` ) END");
              $wpdb->query("IF COL_LENGTH('$table_schedule', 'option_id') IS NULL BEGIN (ALTER TABLE $table_schedule ADD option_id INT(10) AFTER `lieux_id` ) END");
              $wpdb->query("IF COL_LENGTH('$table_schedule', 'sync') IS NULL BEGIN (ALTER TABLE $table_schedule ADD sync INT(10) AFTER `option_id` ) END");
              $wpdb->query("IF COL_LENGTH('$table_schedule', 'event_uid') IS NULL BEGIN (ALTER TABLE $table_schedule ADD event_uid INT(10) AFTER `sync` ) END");
              */
            $wpdb->query("ALTER TABLE {$table_schedule} ADD lieux_id INT(10) AFTER `sequence` ");
            $wpdb->query("ALTER TABLE {$table_schedule} ADD option_id INT(10) AFTER `lieux_id` ");
            $wpdb->query("ALTER TABLE {$table_schedule} ADD sync tinyint(1) DEFAULT 0 AFTER `option_id` ");
            $wpdb->query("ALTER TABLE {$table_schedule} ADD event_uid BIGINT(20) DEFAULT 0 AFTER `sync` ");
            // réinport des options dans schedule
            $status = $wpdb->get_results("SELECT * FROM {$table_opt}");
            foreach ($status as $key => $value) {
                $wpdb->update($table_schedule, array('option_id' => $value->option_id), array('status' => $value->nom));
            }
            // supression de la colone status
            //$wpdb->query("IF COL_LENGTH('$table_schedule', 'status') IS NOT NULL BEGIN ( ALTER TABLE $table_schedule DROP COLUMN status ) END");
            $wpdb->query("ALTER TABLE {$table_schedule} DROP COLUMN status");
        } else {
            $sql5 = "CREATE TABLE {$table_schedule} (\n               sched_id     BIGINT(20) AUTO_INCREMENT,\n               post_id      BIGINT(20),\n               start        DATETIME,\n               end          DATETIME,\n               allday       BOOL,\n               rpt          VARCHAR(64),\n               sequence     BIGINT(20),\n               lieux_id     INT(10),\n               option_id    INT(10),\n               sync         tinyint(1) DEFAULT 0,\n               event_uid    BIGINT(20) DEFAULT 0,\n               infos        TEXT,\n               PRIMARY KEY(sched_id),\n               FOREIGN KEY (lieux_id) REFERENCES {$table_lieux}(lieux_id),\n               FOREIGN KEY (option_id) REFERENCES {$table_opt}(option_id)\n             )";
        }
        if (isset($sql5)) {
            dbDelta($sql5);
        }
        // Remise à zero des options.
        /*$wpdb->query('DELETE FROM '.$table_opt.'');
          $wpdb->insert( $table_opt, array('option_id' => 1, 'nom' => 'RAS', 'message' => 'Rien à signaler' ), array( '%s', '%s' ) );*/
        // Update de la time zone.
        $time_zone = $wpdb->get_row('SELECT * FROM ' . $wpdb->prefix . 'options WHERE option_name = "ec3_tz" LIMITE 1');
        if (null == $event_uid) {
            update_option('ec3_tz', 'Europe/Paris');
        }
        if ($table_geo_exists) {
            $toutes_les_geo = $wpdb->get_results("SELECT geopress_id, name, loc, coord FROM {$table_geo}");
            foreach ($toutes_les_geo as $lieu) {
                $lieux = $lieu->geopress_id;
                preg_match('/\\((((?>[^()]+)|(?R))*)\\)/U', $lieu->name, $matches);
                $departement = $matches[1];
                $nom_ville = substr($lieu->name, 0, strpos($lieu->name, "(") - 1);
                $nom_lieux = substr($lieu->name, strpos($lieu->name, "|") + 1);
                $adresse = $lieu->loc;
                $log_lat = explode(" ", $lieu->coord);
                $longitude = $log_lat['0'];
                $latitude = $log_lat['1'];
                $wpdb->insert($table_lieux, array('lieux_id' => $lieux, 'departement' => $departement, 'nom_ville' => $nom_ville, 'nom_lieux' => $nom_lieux, 'adresse' => $adresse, 'longitude' => $longitude, 'latitude' => $latitude));
            }
            $lien_post_lieu = $wpdb->get_results("SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key = '_geopress_id' ");
            foreach ($lien_post_lieu as $value) {
                $lieux = $value->meta_value;
                $post_id = $value->post_id;
                $wpdb->update($table_schedule, array('lieux_id' => $lieux), array('post_id' => $post_id));
            }
        }
    }
    if ($installed_version == '3.4') {
    }
    if ($installed_version == '3.4.1') {
        return;
    }
    if ($v0->cmp($v1) > 0) {
        return;
        // Installed version later than this one ?!?!
    }
}