CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Changing global settings via the API
Pages: [1]

Author Topic: Changing global settings via the API  (Read 1367 times)

jamie

  • I post occasionally
  • **
  • Posts: 95
  • Karma: 6
Changing global settings via the API
August 23, 2010, 08:48:52 am
I'm working on a project to help us deploy CiviCRM easily and quickly.

There are a number of global configuration changes that we need to make on all installations, so I'm working on extending the CiviCRM drush functions (used to interact with a Drupal site via the command line) to make these global configuration changes.

However - I'm stumped. The first configuration change I'm working on is to set Street Parsing to 1 for Address options.

So far, I can pull out the existing setting:

$address_options = CRM_Core_BAO_Preferences::valueOptions( 'address_options', true, null, true );

Set it to the right value:

$address_options['street_address_parsing'] = 1;

But, I'm not sure how to save it.

I can create a new Preferences object:

$config = new CRM_Core_DAO_Preferences( );

And, this object has a save() method. However, the object is not populated with the current settings.


jamie

  • I post occasionally
  • **
  • Posts: 95
  • Karma: 6
Re: Changing global settings via the API
August 23, 2010, 09:05:50 am
On lobo's suggestion I've opened the following feature request:

http://issues.civicrm.org/jira/browse/CRM-6712

jamie

jamie

  • I post occasionally
  • **
  • Posts: 95
  • Karma: 6
Re: Changing global settings via the API
August 23, 2010, 09:43:24 am
For the time being, I've put together the following hack:

Code: [Select]
  civicrm_initialize();
  include_once 'CRM/Core/DAO/Preferences.php';
  require_once 'CRM/Core/BAO/CustomOption.php';
  $config = new CRM_Core_DAO_Preferences( );
  $config->domain_id = CRM_Core_Config::domainID( );
  $config->is_domain = 1;
  $config->contact_id = null;
  $config->find( true );

  // $address_options = CRM_Core_BAO_Preferences::valueOptions( 'address_options', true, null, true );
  // $address_options['street_address_parsing'] = 1;
  // print_r($address_options);
  $address_options = array(
    1,2,3,4,5,6,8,9,10,11,13
  );
  $sep = CRM_Core_BAO_CustomOption::VALUE_SEPERATOR;

  $config->address_options = $sep . implode($sep,$address_options) . $sep;
  $config->save( );

Normally there should be security checking to ensure the user has access - left out because this should only be executable via drush.

I got stuck on the address_options.  CRM_Core_BAO_Preferences::valueOptions( 'address_options', true, null, true ) gives me a nice array with indexes set to human readable names.

However, CRM_Core_DAO_Preferences( ) wants to save the value as a VALUE_SEPARATOR separated string of numeric digits. Wasn't sure how to convert.

jamie

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Changing global settings via the API
August 23, 2010, 12:07:08 pm
Hi,

You didn't succeed putting that into an api ? It isn't be complicated to add a checking of the premission (I'm assuming "administer civicrm" is the one we should request).

It would be quite useful IMO to have that in the api, so:
1) it can be tested easily with a unit test
2) Can be used in other tests (eg. so you can run all the address api tests with your flag set, and be sure it also works for your config)
3) Probably makes it easier to script a configuration, but I'd like to hear more about how you do it with drush. Could you share your config scripts ?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

jamie

  • I post occasionally
  • **
  • Posts: 95
  • Karma: 6
Re: Changing global settings via the API
August 25, 2010, 10:11:45 am
Thanks for the quick resolution to CRM-6712. It looks like lobo committed a new
function to handle setting option values and I just tested and it works like a
charm :).

Attached is my drush script - it now checks for the existence of the new
setValue function and uses it if it's there. It also shows the big picture of
what we working on.

The relevant code using the new feature, with two examples:
Code: [Select]
include_once 'CRM/Core/BAO/Preferences.php';

$address_options = CRM_Core_BAO_Preferences::valueOptions( 'address_options', true, null, true );
$address_options['street_address_parsing'] = 1;
CRM_Core_BAO_Preferences::setValue( 'address_options',$address_options);
 
$contact_autocomplete_options = CRM_Core_BAO_Preferences::valueOptions( 'contact_autocomplete_options', true, null, true );
$contact_autocomplete_options['phone'] = 1;
$contact_autocomplete_options['email'] = 0;
CRM_Core_BAO_Preferences::setValue( 'contact_autocomplete_options',$contact_autocomplete_options);

Some more code showing me trying to set other options that aren't handled the same way (and suggestions on doing this better would be appreciated!

Code: [Select]
require_once "CRM/Core/BAO/Domain.php";
$domain = CRM_Core_BAO_Domain::getDomain();
$params = (array) $domain;

$prompts = array(
'name' => 'Friendly name of Organization',
'description' => 'Description',
'email_name' => 'Email from name (e.g. name of organization)',
'email_address' => 'Email from address',
);

$user_input = opb_prompt_user($prompts,$params);
$params = array_merge($params, $user_input);

// save name and description
$domain = CRM_Core_BAO_Domain::edit($params, $domain->id);

// save email_name and email_address
$emailName = '"' . $params['email_name'] . '"<' . $params['email_address'] . '>';

$emailParams = array( 'label'       => $emailName,
'description' => $params['description'],
'is_active'   => 1,
'is_default'  => 1 );

$groupParams = array( 'name' => 'from_email_address' );

require_once 'CRM/Core/Action.php';
$action = CRM_Core_Action::ADD;
require_once 'CRM/Utils/Weight.php';
$grpId = CRM_Core_DAO::getFieldValue( 'CRM_Core_DAO_OptionGroup', 'from_email_address', 'id', 'name' );
$fieldValues = array('option_group_id' => $grpId );
$emailParams['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues );

require_once 'CRM/Core/OptionValue.php';

//reset default within domain.
$emailParams['reset_default_for'] = array( 'domain_id' => CRM_Core_Config::domainID( ) );

$from_email_id = null;
CRM_Core_OptionValue::addOptionValue( $emailParams, $groupParams, $action, $from_email_id );

xavier: since this is a drush script - I'm not sure setting permissions is meaningful. The default when running drush is to run as UID 1. It's possible to purposefully run as a lower priv user - and perhaps it would be useful to check permissions in that case. But I'm not sure if that would be a common use case.

jamie


Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Post-installation Setup and Configuration (Moderator: Dave Greenberg) »
  • Changing global settings via the API

This forum was archived on 2017-11-26.