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) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • How to manage sequence installation in extension?
Pages: [1]

Author Topic: How to manage sequence installation in extension?  (Read 1145 times)

rubofvil

  • I’m new here
  • *
  • Posts: 23
  • Karma: 1
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 7
  • MySQL version: mysql 5.1
  • PHP version: php 5.2
How to manage sequence installation in extension?
February 27, 2013, 03:47:41 am
How can i manage sequence installation in extension?

Example:
  • Create a custom report template in a extension.
  • Create instance of this custom report template.


I need execute instance of custom report after the ReportTemplate(file.mgd.php ) insert in database record.
Where i can put the code of this instance in extension?

Thanks.

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: How to manage sequence installation in extension?
February 27, 2013, 09:56:59 pm
That is a good question.

The mgd.php stuff (aka hook_civicrm_managed) runs pretty late during the installation process -- after hook_civicrm_install or hook_civicrm_enable.

If you're in a pinch and need to implement now for Civi 4.2, then you can bypass the mgd.php stuff and define your own install/upgrade sequence. The basic steps would be:

1) Run "civix generate:upgrader"
2) In the newly created Upgrader.php, create an "install()" function.
3) Copy the details from your mgd.php file and put them in the install function, e.g.

Code: [Select]
  function install() {
    $result = civicrm_api('ReportTemplate', array(...params from mgd.php...));
  }

4) Add the code to insert your report-instance. I'm not sure the easiest way to do this -- it might need to execute a SQL query b/c there's no public API for this.
5) Delete the mgd.php file. It's now redundant.

That's a bit cumbersome, though, and doing it right would require a lot more time to handle corner cases (like cleaning up the report during uninstallation).

For a better solution, I would appreciate your feedback on what makes sense... Off-the-cuff, these changes would be needed in core:

1) Add a public API for CRUD'ing ReportInstances. The public API would be used in *.mgd.php files. (There's an API for ReportTemplate but not ReportInstance.)
2) Allow *.mgd.php files to specify a 'weight' that indicates the order of insertion. (So the ReportTemplate would be inserted before the ReportInstance.) This might not be strictly necessary -- I think (as a matter of happenstance/dumb-luck) that out-of-order insertion will work for this, but it seems prudent.
3) Patch "civix generate:report". In new reports, we should automatically declare the ReportInstance as well as the ReportTemplate.

rubofvil

  • I’m new here
  • *
  • Posts: 23
  • Karma: 1
  • CiviCRM version: 4.2.7
  • CMS version: Drupal 7
  • MySQL version: mysql 5.1
  • PHP version: php 5.2
Re: How to manage sequence installation in extension?
March 01, 2013, 04:18:21 am
Thank for your steps!

Finally I do it the second option.

API to InstanceReport
Code: [Select]
function civicrm_api3_instance_report_create($params) {
  // CRM_Core_Error::debug($params);
  // die;
  if(isset($params["report_id"]) && isset($params["title"]) && isset($params["permission"])) {
 
    $dao_membership = new CRM_Report_DAO_Instance();
    $dao_membership->report_id = $params["report_id"];   
    $dao_membership->title = $params["title"];   
    $dao_membership->description = $params["description"];
    $dao_membership->permission = $params["permission"];
    $dao_membership->grouprole = $params["grouprole"];
    $dao_membership->domain_id = $params["domain_id"];
    $dao_membership->email_subject = $params["email_subject"];
    $dao_membership->email_to = $params["email_to"];
    $dao_membership->email_cc = $params["email_cc"];
    $dao_membership->header = $params["header"];
    $dao_membership->footer = $params["footer"];
    $dao_membership->form_values = $params["params"];
    $result = $dao_membership->save();



    $returnValues = array( // OK, return several data rows
      //1 => array('id' => $result['id'], 'title' => $result['title']),
    );
    // ALTERNATIVE: $returnValues = array(); // OK, success
    // ALTERNATIVE: $returnValues = array("Some value"); // OK, return a single value

    // Spec: civicrm_api3_create_success($values = 1, $params = array(), $entity = NULL, $action = NULL)
    return civicrm_api3_create_success($returnValues, $params, 'InstanceReport', 'Create');
  } else {
    throw new API_Exception(/*errorMessage*/ 'Required fields Title, report_id, permission', /*errorCode*/ 1234);
  }
}

And the code of file mgd
Code: [Select]
$params = array( 'fields' => array(
    'sort_name' => '1',
    'first_name' => '1',
    'last_name' => '1',
    'membership_type_id' => '1',
    'membership_start_date' => '1',
    'membership_end_date' => '1',
    'name' => '1',
    'custom_1' => '1',
    'custom_2' => '1',
    'custom_3' => '1',
    'custom_4' => '1',
    'custom_6' => '1',
    'custom_5' => '1',
    'custom_7' => '1',
    'custom_8' => '1', ),
    'sort_name_op' => 'has',
    'id_op' => 'lte',
    'join_date_relative' => '0',
    'membership_start_date_relative' => '0',
    'membership_end_date_relative' => '0',
    'owner_membership_id_op' => 'lte',
    'tid_op' => 'in',
    'tid_value' => array ( 0 => '2', ),
    'sid_op' => 'in',
    'sid_value' => array ( 0 => '8', ),
    'receive_date_relative' => '0',
    'contribution_type_id_op' => 'in',
    'payment_instrument_id_op' => 'in',
    'contribution_status_id_op' => 'in',
    'total_amount_op' => 'lte',
    'tagid_op' => 'in',
    'gid_op' => 'in',
    'custom_1_op' => 'lte',
    'custom_2_op' => 'in',
    'custom_2_value' => array ( 0 => 'x', ),
    'custom_3_relative' => 'earlier.day', 
    'custom_4_op' => 'in',
    'custom_6_op' => 'lte',
    'custom_5_op' => 'has',
    'custom_7_op' => 'lte',
    'custom_8_op' => 'lte',
    'description' => '',
    'domain_id' => 1,
    'is_reserved' => false, );

$params_serialize = serialize($params);
$header = "";
$footer = "";


return array (
  0 =>
  array (
....
    'entity' => 'ReportTemplate',
....
  ),
  1 =>
  array (
    'name' => 'Instace Report',
    'entity' => 'InstanceReport',
    'params' =>
    array (
      'version' => 3,
      'label' => '',
      'sequential' => '1',
      'report_id' => ,
      'title' => 'D export',
      'description' => 'D export',
      'permission' => 'access CiviReport',
      'grouprole' => null,
      'domain_id' => 1,
      'email_subject' => null,
      'email_to' => null,
      'email_cc' => null,
      'email_subject' => null,
      'header' => $header,
      'footer' => $footer,
      'params' => $params_serialize

    ),
  ),
);

And it's work

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • How to manage sequence installation in extension?

This forum was archived on 2017-11-26.