示例#1
0
 /**
  * Upgrade to 1.4:
  *  - update to new, prefixed, settings names
  *  - (on 4.6) if no profile exists, create default (from legacy indivudual values)
  *  - (on 4.6) migrate all profiles into a "settings bag"
  *
  * REMARK: using the CRM_Core_BAO_Setting::getItem in order to evaluate the group_name
  *
  * @return TRUE on success
  * @throws Exception
  */
 public function upgrade_0140()
 {
     $OLD_SETTINGS_GROUP = 'Donation Receipt Profiles';
     // STEP 1: Migrate old general settings to prefixed ones
     $settings_migration = array('default_profile' => 'donrec_default_profile', 'packet_size' => 'donrec_packet_size', 'pdfinfo_path' => 'donrec_pdfinfo_path');
     $migrated_values = array();
     foreach ($settings_migration as $old_key => $new_key) {
         $new_value = CRM_Core_BAO_Setting::getItem($OLD_SETTINGS_GROUP, $new_key);
         if ($new_value === NULL) {
             $old_value = CRM_Core_BAO_Setting::getItem($OLD_SETTINGS_GROUP, $old_key);
             if ($old_value !== NULL) {
                 $migrated_values[$new_key] = $old_value;
             }
         }
     }
     if (!empty($migrated_values)) {
         civicrm_api3('Setting', 'create', $migrated_values);
     }
     // Migrate profiles
     //  (only works on 4.6. With 4.7 the group_name was dropped, and we cannot find the profiles any more)
     $existing_profiles = civicrm_api3('Setting', 'getvalue', array('name' => 'donrec_profiles'));
     if (empty($existing_profiles) && version_compare(CRM_Utils_System::version(), '4.6', '<=')) {
         // FIXME: is there a better way than a SQL query?
         $profiles = array();
         $query = CRM_Core_DAO::executeQuery("SELECT name FROM civicrm_setting WHERE group_name = '{$OLD_SETTINGS_GROUP}'");
         while ($query->fetch()) {
             $profile_data = CRM_Core_BAO_Setting::getItem($OLD_SETTINGS_GROUP, $query->name);
             if (is_array($profile_data)) {
                 $profiles[$query->name] = $profile_data;
             } else {
                 $this->ctx->log->warn('Profile "{$query->name}" seems to be broken and is lost.');
             }
         }
         // if there is no default profile, create one and copy legacy (pre 1.3) values
         if (empty($profiles['Default'])) {
             $default_profile = new CRM_Donrec_Logic_Profile('Default');
             $profile_data = $default_profile->getData();
             foreach (array_keys($profile_data) as $field_name) {
                 $legacy_value = CRM_Core_BAO_Setting::getItem(CRM_Donrec_Logic_Settings::$SETTINGS_GROUP, $field_name);
                 if ($legacy_value !== NULL) {
                     $profile_data[$field_name] = $legacy_value;
                 }
             }
             $legacy_contribution_types = CRM_Core_BAO_Setting::getItem(CRM_Donrec_Logic_Settings::$SETTINGS_GROUP, 'contribution_types');
             if ($legacy_contribution_types !== NULL && $legacy_contribution_types != 'all') {
                 $profile_data['financial_types'] = explode(',', $legacy_contribution_types);
             }
             $profiles['Default'] = $profile_data;
             $this->ctx->log->warn('Created default profile.');
         }
         CRM_Donrec_Logic_Profile::setAllData($profiles);
         $profiles_migrated = count($profiles);
         $this->ctx->log->info('Migrated {$profiles_migrated} profiles.');
     }
     return TRUE;
 }
 function postProcess()
 {
     // process all form values and save valid settings
     $values = $this->exportValues();
     // save generic settings
     CRM_Donrec_Logic_Settings::set('donrec_packet_size', $values['packet_size']);
     if ($values['pdfinfo_path']) {
         CRM_Donrec_Logic_Settings::set('donrec_pdfinfo_path', $values['pdfinfo_path']);
     }
     // first, update current values into slected profile
     if (!empty($values['selected_profile'])) {
         $profile = $values['selected_profile'];
         $profile_data = json_decode($values['profile_data'], 1);
         $profile_defaults = CRM_Donrec_Logic_Profile::defaultProfileData();
         foreach (array_keys($profile_defaults) as $field_name) {
             $value = CRM_Utils_Array::value($field_name, $values, NULL);
             if ($value != NULL) {
                 $profile_data[$profile][$field_name] = $value;
             }
         }
         // verify some stuff
         foreach ($profile_data as $profile_name => $profile) {
             // test the ID pattern
             try {
                 $generator = new CRM_Donrec_Logic_IDGenerator($profile['id_pattern'], false);
             } catch (Exception $e) {
                 $session = CRM_Core_Session::singleton();
                 $session->setStatus(ts("One of the Receipt ID patterns are invalid! Changes NOT saved!", array('domain' => 'de.systopia.donrec')), ts('Error', array('domain' => 'de.systopia.donrec')), 'error');
                 return;
             }
         }
         // then store the profiles
         CRM_Donrec_Logic_Profile::setAllData($profile_data);
     }
     $session = CRM_Core_Session::singleton();
     $session->setStatus(ts("Settings successfully saved", array('domain' => 'de.systopia.donrec')), ts('Settings', array('domain' => 'de.systopia.donrec')), 'success');
     $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/setting/donrec'));
 }