Upgrader.php 2.2 KB
Newer Older
1 2 3
<?php

/**
4 5
 * Install and uninstall functions
 *
6 7 8 9
 */
class CRM_Petitionemail_Upgrader extends CRM_Petitionemail_Upgrader_Base {

  /**
10
   * Create civicrm_petition_email table during install 
11
   *
12
   *
13 14
  */
  public function install() {
15
    // Nothing to do. All *_install.sql files installed automatically.
16 17 18
  }

  /**
19 20
   * Delete civicrm_peition_email table during uninstall
   *
21
   */
22
  public function uninstall() {
23
    // Nothing to do. All *_uninstall.sql files installed automatically.
24 25
  }

26 27
  /**
   * Add new fields and table allowing for dynamic targets to be selected.
28
   * 
29 30
  */
  function upgrade_1001() {
31
    // First create the new table
32 33
    if(!$this->executeSqlFile('sql/Petitionemail_matching_field_install.sql')) return FALSE;

34 35
    // Now add the new recipient field if it doesn't exist.
    $sql = "DESC civicrm_petition_email";
36
    $dao = CRM_Core_DAO::executeQuery($sql);
37 38 39 40 41 42 43 44 45 46 47 48 49 50
    $needs_upgrade = TRUE;
    while($dao->fetch()) {
      if($dao->Field == 'recipients') {
        $needs_upgrade = FALSE;
      }
    }

    if($needs_upgrade) {
      $sql = "ALTER TABLE civicrm_petition_email ADD COLUMN `recipients` text ".
        "COMMENT 'The name and email address of additional targets that should ".
        "receive a copy of all petitions signed, separated by line breaks.'";
      $dao = CRM_Core_DAO::executeQuery($sql);
    }

51 52 53 54 55
    // Now transfer the data to the new tables
    $sql = "SELECT petition_id, recipient_email, recipient_name FROM civicrm_petition_email";
    $dao = CRM_Core_DAO::executeQuery($sql);
    while($dao->fetch()) {
      $target = '"' . $dao->recipient_name . '" <' . $dao->recipient_email . '>';
56
      $sql = "UPDATE civicrm_petition_email SET recipients = %0 WHERE petition_id = %1";
57
      $params = array(
58 59
        0 => array($target, 'String'),
        1 => array($dao->petition_id, 'Integer'),
60 61 62
      );
      CRM_Core_DAO::executeQuery($sql, $params);
    }
63
    // Now drop/add fields.
64 65 66
    if(!$this->executeSqlFile('sql/Petitionemail_1001_upgrade.sql')) return FALSE;
    return TRUE;
  }
67 68 69 70 71 72 73 74 75

  /**
   * Add field to allow user to specify custom subject line.
   */
  function upgrade_1002() {
    if(!$this->executeSqlFile('sql/Petitionemail_1002_upgrade.sql')) return FALSE;
    return TRUE;
  }

76
}