Plugin altering 'account' schema

By jenolan - October 7, 2016

Hi I am tinkering again, I need to create (if not present) an extra filed in the website membership table (if plugin installed).

Is the 'correct' method to simply loadSchema() twiddle the array saveSchema() and then createMissingSchemaTablesAndFields()?

Thanks,
Larry

---
<?= "Jenolan(Larry) :: Coding Since 1973" ?>
Peace and Long Life

By Damon - October 17, 2016

Hi Larry,

I haven't done this before but if you are using a custom accounts table, you can add the extra field into the $schema = array( ) to have it auto created.

You could also add the field into the /data/accounts.ini.php schema file to have it auto created. 

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Dave - October 19, 2016

Hey Larry, 

If you've haven't already done this.  Here's some sample plugin code from "Simple Forum" that adds fields to a schema when it's not already there:

// plugin actions
addAction('section_init', '_sforum_pluginSetup_updateAccountsSchema', null, 1);

// make sure that all required accounts schema changes are made - runs when user visits plugins menu
function _sforum_pluginSetup_updateAccountsSchema($tableName) {
  $isPluginMenu = $tableName == 'admin' && @$_REQUEST['action'] == 'plugins';
  if ($tableName && !$isPluginMenu) { return; } // only run on plugin menu

  // ensure membership account schema has appropriate fields
  $order = time();
  $fieldsToEnsure = array(
    '__sforum_header__' => array(
      'order'           => ++$order,
      'label'           => '',
      'type'            => 'separator',
      'separatorType'   => 'header bar',
      'separatorHeader' => 'Forum Settings',
      'separatorHTML'   => '',
      'isSystemField'     => '1',
    ),
    '_sforum_signature' => array(
      'order'             => ++$order,
      'label'             => 'Signature',
      'type'              => 'textbox',
      'autoFormat'        => '0',
      'isSystemField'     => '1',
    ),
    '_sforum_adminForums' => array(
      'order'             => ++$order,
      'label'             => 'Admin for Forums',
      'type'              => 'list',
      'indexed'           => '0',
      'defaultValue'      => '',
      'fieldPrefix'       => '',
      'description'       => '',
      'isRequired'        => '0',
      'isUnique'          => '0',
      'listType'          => 'checkboxes',
      'optionsType'       => 'table',
      'optionsTablename'  => '_frm_forums',
      'optionsValueField' => 'num',
      'optionsLabelField' => 'name',
      'isSystemField'     => '1',
    ),
  );

  // add fields to accounts table if necessary
  $accountsTable = coalesce( @$GLOBALS['WSM_ACCOUNTS_TABLE'], 'accounts');
  $accountSchema = loadSchema($accountsTable);
  $updated = false;
  foreach ($fieldsToEnsure as $newFieldName => $newFieldSchema) {
    if (!@$accountSchema[$newFieldName]) {
      $accountSchema[$newFieldName] = $newFieldSchema;
      $updated = true;
    }
  }

  // save schema if necessary
  if ($updated) {
    saveSchema( $accountsTable, $accountSchema );
    createMissingSchemaTablesAndFields();
  }
  
  // add email templates
  // SFORUM-NOTIFY-POST
  emailTemplate_addToDB(array(
    'template_id'  => "SFORUM-NOTIFY-POST",
    'description'  => "Sent to users when their forum posts are replied to",
    'placeholders' => array('user.email','post.username','post.read_link','post.reply_link','post.unsubscribe_link','post.message_snippet','topic.subject','forum'), // array of placeholder names
    'from'         => "#settings.adminEmail#",
    'to'           => "#user.email#",
    'subject'      => "New comment on: #topic.subject#",
    'html'         => <<<__HTML__
<p><strong>#post.username#</strong> has commented on: <strong><a href="#post.read_link#">#topic.subject#</a></strong></p>
<p style="padding-left: 30px;"><em>#post.message_snippet#... <a href="#post.read_link#">more</a></em></p>
<p style="padding-left: 30px;"><a href="#post.read_link#"><strong>See Comment</strong></a>  |  <strong><a href="#post.reply_link#">Reply to comment</a></strong><em><strong><a href="#post.reply_link#"><br /><br /></a></strong></em></p>
<hr />
<p><span style="color: #808080;">This update was sent to #user.email# because you commented on this forum topic, are subscribed to this thread, or are subscribed to this forum.</span><br /><span style="color: #808080;">If you don't want to receive updates on this forum topic, please <span style="color: #3366ff;"><a href="#post.unsubscribe_link#"><span style="color: #3366ff;">unsubscribe</span></a></span>.</span></p>
__HTML__
,
  'placeholders' => <<<__PLACEH__
#user.email#
#post.username#
#post.read_link#
#post.reply_link#
#post.unsubscribe_link#
#post.message_snippet#
#topic.subject#
#forum#

#server.http_host#
#server.remote_addr#
#settings.adminEmail#
#settings.adminUrl#
#settings.programName#
#settings.programName#
__PLACEH__
  ));
}

You can remove the bit about adding a default email template if you don't need that.

Hope that helps!

Dave Edis - Senior Developer

interactivetools.com

By jenolan - October 19, 2016

Thanks I will have a gander, have had to stop work on /bin/plex due to a couple of clients having site issues....

Larry

---
<?= "Jenolan(Larry) :: Coding Since 1973" ?>
Peace and Long Life