You are here

function flag_install in Flag 5

Same name and namespace in other branches
  1. 6.2 flag.install \flag_install()
  2. 6 flag.install \flag_install()
  3. 7.2 flag.install \flag_install()

Implementation of hook_install().

File

./flag.install, line 11
Flag module install/update hooks.

Code

function flag_install() {
  if (_flag_flag_content_installed()) {

    // Prevent clash with the "Flag content" module. The flag_enable() hook will
    // print the error message.
    return;
  }

  // Load the flag API in case we want to use it when installing.
  include_once drupal_get_path('module', 'flag') . '/flag.module';

  // Explicitly loading 'flag.inc' is needed only for the D5 version. And this
  // isn't needed for code in hook_update_nnn because update.php calls our
  // module's hook_init() (indirectly, by doing a full bootstrap), which
  // already loads 'flag.inc'.
  include_once drupal_get_path('module', 'flag') . '/flag.inc';

  // If Views Bookmark is available, skip the install and do an upgrade instead.
  if (strpos($GLOBALS['db_type'], 'mysql') === 0) {
    include_once drupal_get_path('module', 'flag') . '/includes/flag.views_bookmark.inc';
    if (flag_views_bookmark_update_needed()) {
      flag_views_bookmark_update();
      return;
    }
  }
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {flags} (\n        fid smallint unsigned NOT NULL default '0',\n        content_type varchar(32) default '',\n        name varchar(32) default '',\n        title varchar(255) default '',\n        roles varchar(255) default '',\n        global tinyint default 0,\n        options text default NULL,\n        PRIMARY KEY (fid),\n        UNIQUE KEY (name)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {flag_content} (\n        fcid int unsigned NOT NULL auto_increment,\n        fid smallint unsigned NOT NULL default '0',\n        uid int(10) unsigned NOT NULL default '0',\n        content_type varchar(32) default '',\n        content_id int(10) unsigned NOT NULL default '0',\n        timestamp int(11) unsigned NOT NULL default '0',\n        PRIMARY KEY (fcid),\n        UNIQUE INDEX fid_content_type_content_id_uid (fid, content_type, content_id, uid),\n        INDEX content_type_content_id (content_type, content_id),\n        INDEX content_type_uid (content_type, uid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {flag_types} (\n        fid smallint unsigned NOT NULL default '0',\n        type varchar(32) NOT NULL default '',\n        INDEX (fid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {flag_counts} (\n        fid smallint unsigned NOT NULL default '0',\n        content_type varchar(32) default '',\n        content_id int(10) unsigned NOT NULL default '0',\n        count int(10) unsigned NOT NULL default '0',\n        PRIMARY KEY (fid, content_type, content_id),\n        INDEX fid_content_type (fid, content_type),\n        INDEX content_type_content_id (content_type, content_id)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      $success = TRUE;
      break;
    case 'pgsql':
      db_query("CREATE TABLE {flags} (\n        fid smallint NOT NULL default '0',\n        content_type varchar(32) default '',\n        name varchar(32) default '',\n        title varchar(255) default '',\n        roles varchar(255) default '',\n        global smallint default 0,\n        options text default NULL,\n        PRIMARY KEY (fid)\n      )");
      db_query("ALTER TABLE {flags} ADD CONSTRAINT {flags}_name_key UNIQUE (name)");
      db_query("CREATE SEQUENCE {flags}_fid_seq");
      db_query("CREATE TABLE {flag_content} (\n        fcid serial,\n        fid smallint NOT NULL default '0',\n        uid int NOT NULL default '0',\n        content_type varchar(32) default '',\n        content_id int NOT NULL default '0',\n        timestamp int NOT NULL default '0',\n        PRIMARY KEY (fcid)\n      )");
      db_query('ALTER TABLE {flag_content} ADD CONSTRAINT {flag_content}_fid_content_type_content_id_uid_key UNIQUE (fid, content_type, content_id, uid)');
      db_query('CREATE INDEX {flag_content}_content_type_content_id_idx ON {flag_content} (content_type, content_id)');
      db_query('CREATE INDEX {flag_content}_content_type_uid_idx ON {flag_content} (content_type, uid)');
      db_query("CREATE TABLE {flag_types} (\n        fid int NOT NULL default '0',\n        type varchar(32) NOT NULL default ''\n      )");
      db_query('CREATE INDEX {flag_types}_fid_idx ON {flag_types} (fid)');
      db_query("CREATE TABLE {flag_counts} (\n        fid smallint NOT NULL default '0',\n        content_type varchar(32) default '',\n        content_id int NOT NULL default '0',\n        count int NOT NULL default '0',\n        PRIMARY KEY (fid, content_type, content_id)\n      )");
      db_query('CREATE INDEX {flag_counts}_fid_content_type_idx ON {flag_content} (fid, content_type)');
      db_query('CREATE INDEX {flag_counts}_content_type_content_id_idx ON {flag_content} (content_type, content_id)');
      $success = TRUE;
      break;
  }
  if ($success) {

    // Install a demonstration flag.
    $flag = flag_flag::factory_by_content_type('node');
    $configuration = array(
      'name' => 'bookmarks',
      'global' => 0,
      'show_on_page' => 1,
      'show_on_teaser' => 1,
      'show_on_form' => 1,
      // The following UI labels aren't wrapped in t() because they are written
      // to the DB in English. They are passed to t() later, thus allowing for
      // multilingual sites.
      'title' => 'Bookmarks',
      'flag_short' => 'Bookmark this',
      'flag_long' => 'Add this post to your bookmarks',
      'flag_message' => 'This post has been added to your bookmarks',
      'unflag_short' => 'Unbookmark this',
      'unflag_long' => 'Remove this post from your bookmarks',
      'unflag_message' => 'This post has been removed from your bookmarks',
      'types' => array(
        'story',
        'forum',
        'blog',
      ),
    );
    $flag
      ->form_input($configuration);
    $flag
      ->save();
  }
  if ($success) {
    drupal_set_message(st('Flag module installed tables successfully.'));
  }
  else {
    drupal_set_message(st('The installation of Flag module failed.'), 'error');
  }
}