<?php
#-------------------------------------------------------------------------
# Module: Newsletter Made Simple
#        (c) to 2008 by Paul Lempkepf
#        (c) 2008 and on by Robert Campbell (calguy1000@cmsmadesimple.org)
#
#  An addon module for CMS Made Simple to provide a flexible
#  mailing list solution.
#
#-------------------------------------------------------------------------
# CMSMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# Visit the CMSMS Homepage at: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# However, as a special exception to the GPL, this software is distributed
# as an addon module to CMS Made Simple.  You may not use this software
# in any Non GPL version of CMS Made simple, or in any version of CMS
# Made simple that does not indicate clearly and obviously in its admin
# section that the site was built with CMS Made simple.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
#END_LICENSE
if (!isset($gCms)) exit;

$current_version = $oldversion;
$db =& $this->GetDb();

switch($current_version) {
  case '2.0':
  case '2.1.1':
    {
      $dict = NewDataDictionary($db);
      $sqlarray = $dict->AddColumnSQL(NMS_MESSAGES_TABLE, "archivable I1");
      $dict->ExecuteSQLArray($sqlarray);
      $sqlarray = $dict->AddColumnSQL(NMS_MESSAGES_TABLE, "text_only I1");
      $dict->ExecuteSQLArray($sqlarray);
      $query = 'UPDATE '.NMS_MESSAGES_TABLE.' SET archivable = 1';
      $db->Execute($query);
    } // notice, no break
  } // end switch

if( version_compare($current_version,'2.4') < 0 ) {
  $dict = NewDataDictionary($db);
  $sqlarray = $dict->AddColumnSQL(NMS_USERS_TABLE, "error_count I");
  $dict->ExecuteSQLArray($sqlarray);

  $this->CreateEvent('OnUnsubscribe');
}
if( version_compare($current_version,'2.5.1') < 0 ) {
  // should probably clear jobs here.

  $this->SetPreference('send_jobcomplete_groups','');
  $this->SetPreference('send_jobcomplete_subject',$this->Lang('jobcomplete_subject'));
  $this->SetTemplate('send_jobcomplete_template',
		     file_get_contents(dirname(__FILE__).'/templates/orig_jobcomplete_template.tpl'));

  $dict = NewDataDictionary($db);
  $sqlarray = $dict->AddColumnSQL(NMS_JOBS_TABLE,'extra X');
  $dict->ExecuteSQLArray($sqlarray);

  $taboptarray= array ('mysql' => 'TYPE=MyISAM');
  $flds = "jobid I KEY NOT NULL,
           messageid I KEY NOT NULL,
           email C(125) KEY NOT NULL,
           uniqueid C(255),
           username C(255),
           status   C(25),
           senddate I,
           extra    X";
  $sqlarray = $dict->CreateTableSQL(NMS_MAILS_TABLE,$flds,$taboptarray);
  $dict->ExecuteSQLArray($sqlarray);

  $this->AddEventHandler('CGJobMgr','JobDeleted',FALSE);
  $this->AddEventHandler('CGJobMgr','JobPaused',FALSE);
  $this->AddEventHandler('CGJobMgr','JobResumed',FALSE);
}
if( version_compare($current_version,'2.6.1') < 0 ) {
  $dict = NewDataDictionary($db);
  $sqlarray = $dict->AddColumnSQL(NMS_LIST_TABLE,'dynamic C(255)');
  $dict->ExecuteSQLArray($sqlarray);
}

if( version_compare($current_version,'2.8') < 0 ) {
  $dict = NewDataDictionary($db);
  $taboptarray= array ('mysql' => 'TYPE=MyISAM');

  $flds = "id I KEY AUTO,
           type C(20),
           name C(50) KEY NOT NULL,
           prompt C(50),
           iorder I,
           extra  X";
  $sqlarray= $dict->CreateTableSQL(NMS_FIELDDEFS_TABLE, $flds, $taboptarray);
  $dict->ExecuteSQLArray($sqlarray);

  $sqlarray = $dict->DropTableSQL( NMS_USER_DATA_TABLE );
  $dict->ExecuteSQLArray($sqlarray);

  $flds= "userid I KEY,
          fieldid I KEY,
	      value X";
  $sqlarray = $dict->CreateTableSQL(NMS_USER_DATA_TABLE, $flds, $taboptarray);
  $dict->ExecuteSQLArray($sqlarray);

  $sqlarray = $dict->AddColumnSQL(NMS_MAILS_TABLE,'userid I');
  $dict->ExecuteSQLArray($sqlarray);
}

if( version_compare($current_version,'2.9') < 0 ) {
    $this->AddEventHandler('CGJobMgr', 'JobError', FALSE);
    $this->AddEventHandler('CGJobMgr', 'JobFinished', FALSE);
}

if( version_compare($current_version,'2.11') < 0 ) {
    $dict = NewDataDictionary($db);
    $sqlarray = $dict->AddColumnSQL(NMS_MESSAGES_TABLE,'send_as C(255)');
    $dict->ExecuteSQLArray($sqlarray);
}

if( version_compare($current_version,'2.12') < 0 ) {
    $this->CreateEvent('OnUserConfirmed');
}
if( version_compare($current_version,'2.13') < 0 ) {
    $this->RemoveEventHandler('CGJobMgr','JobDeleted');
    $this->RemoveEventHandler('CGJobMgr','JobPaused');
    $this->RemoveEventHandler('CGJobMgr','JobResumed');
    $this->RemoveEventHandler('CGJobMgr','JobError');
    $this->RemoveEventHandler('CGJobMgr','JobFinished');
}

// let this fail if it fails.
$db->Execute('CREATE UNIQUE INDEX idx_nmsmails2 ON '.NMS_MAILS_TABLE.' (jobid,email)');
