Upgrader.php 2.88 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
    $needs_upgrade = TRUE;
38
    $needs_transfer = FALSE;
39 40 41 42
    while($dao->fetch()) {
      if($dao->Field == 'recipients') {
        $needs_upgrade = FALSE;
      }
43 44 45 46
      if($dao->Field == 'recipient_email') {
        $needs_transfer = TRUE;

      }
47 48 49 50 51 52 53 54 55
    }

    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);
    }

56
    // Now transfer the data to the new tables
57 58 59 60 61 62 63 64 65 66 67 68
    if($needs_transfer) {
      $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 . '>';
        $sql = "UPDATE civicrm_petition_email SET recipients = %0 WHERE petition_id = %1";
        $params = array(
          0 => array($target, 'String'),
          1 => array($dao->petition_id, 'Integer'),
        );
        CRM_Core_DAO::executeQuery($sql, $params);
      }
69
    }
70
    // Now drop/add fields.
71 72 73
    if(!$this->executeSqlFile('sql/Petitionemail_1001_upgrade.sql')) return FALSE;
    return TRUE;
  }
74 75 76 77 78 79 80 81 82

  /**
   * 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;
  }

83 84 85 86 87
  /**
   * Ensure all profiles and custom fields are created.
   */
  function upgrade_1003() {
    // These are indempotent.
88 89 90 91
    if(FALSE === petitionemail_create_custom_fields()) return FALSE;
    if(FALSE === petitionemail_get_profile_id('petitionemail_profile_matching_fields')) return FALSE;
    if(FALSE === petitionemail_get_profile_id('petitionemail_profile_default_contact')) return FALSE;
    if(FALSE === petitionemail_get_profile_id('petitionemail_profile_default_activity')) return FALSE;
92
    return TRUE;
93 94
  }

95
}