/**
  * get a list of all plugin instances of the given type ('import', 'export', 'matcher').
  *
  * If $enabled_only is set to true (default), only enabled plugins will be delivered.
  * 
  * @return an array of CRM_Banking_BAO_PluginInstances
  */
 static function listInstances($type_name, $enabled_only = TRUE)
 {
     // first, find the plugin type option group
     $plugin_types = civicrm_api('OptionGroup', 'get', array('version' => 3, 'name' => 'civicrm_banking.plugin_types'));
     if (isset($result['is_error']) && $result['is_error']) {
         CRM_Core_Error::fatal(sprintf(ts("Couldn't find group '%s'!"), 'civicrm_banking.plugin_types'));
         return array();
     }
     // then, find the correct plugin type
     $import_plugin_type = civicrm_api('OptionValue', 'get', array('version' => 3, 'name' => $type_name, 'group_id' => $plugin_types['id']));
     if (isset($result['is_error']) && $result['is_error'] || (!isset($import_plugin_type['id']) || !$import_plugin_type['id'])) {
         CRM_Core_Error::fatal(sprintf(ts("Couldn't find type '%s' in group %d!"), $type_name, $plugin_types['id']));
         return array();
     }
     // then, get the list of plugins matching this criteria
     $params = array('version' => 3, 'plugin_type_id' => $import_plugin_type['id']);
     if ($enabled_only) {
         $params['enabled'] = 1;
     }
     $instance_results = civicrm_api('BankingPluginInstance', 'get', $params);
     if (isset($result['is_error']) && $result['is_error']) {
         CRM_Core_Error::fatal(ts("Couldn't query plugin list from API!"));
         return array();
     }
     // create list of plugin instance BAOs
     $plugin_list = array();
     foreach ($instance_results['values'] as $plugin_info) {
         $plugin = new CRM_Banking_BAO_PluginInstance();
         $plugin->get('id', $plugin_info['id']);
         // insert with ascending weight
         // PD: I think this code is really complex - are you trying to sort on weight ? we can do that with a usort call
         //  BE: go ahead and replace it! But if you just comment it out, it stops working, and I get warnings (Undefined variable, below)
         for ($index = 0; $index < count($plugin_list); $index++) {
             if ($plugin->weight > $plugin_list[$index]->weight) {
                 array_splice($plugin_list, $index, 0, array($plugin));
                 $index = count($plugin_list) - 1;
                 // for the after-loop condition
                 break;
             }
         }
         if ($index == count($plugin_list)) {
             // i.e. it was not added during the loop
             array_push($plugin_list, $plugin);
         }
     }
     return $plugin_list;
 }
 /**
  * Initialize the list of plugins
  */
 private function initPlugins()
 {
     // perform a BAO query to select all active match plugins and insert instances for them into
     //    the matchers array by weight, then ksort descending
     $this->plugins = array();
     $matcher_type_id = banking_helper_optionvalueid_by_groupname_and_name('civicrm_banking.plugin_classes', 'match');
     $params = array('version' => 3, 'plugin_type_id' => $matcher_type_id, 'enabled' => 1);
     $result = civicrm_api('BankingPluginInstance', 'get', $params);
     if (isset($result['is_error']) && $result['is_error']) {
         CRM_Core_Session::setStatus(ts("Error while trying to query database for matcher plugins!"), ts('No processors'), 'alert');
     } else {
         foreach ($result['values'] as $instance) {
             $pi_bao = new CRM_Banking_BAO_PluginInstance();
             $pi_bao->get('id', $instance['id']);
             // add to array wrt the weight
             if (!isset($this->plugins[$pi_bao->weight])) {
                 $this->plugins[$pi_bao->weight] = array();
             }
             array_push($this->plugins[$pi_bao->weight], $pi_bao->getInstance());
         }
     }
     // sort array by weight
     ksort($this->plugins);
 }
 /**
  * Upon loading a BTX from database, restore suggestions as they were
  * stored in JSON format 
  * 
  * TODO: move the restore to an instance method of Suggestion, thus no longer
  * expising the structure of the Suggestion here
  */
 private function restoreSuggestions()
 {
     if ($this->suggestion_objects == null && $this->suggestions) {
         $sugs = $this->suggestions;
         if ($sugs != '') {
             $sugs = json_decode($sugs, true);
             foreach ($sugs as $sug) {
                 $pi_bao = new CRM_Banking_BAO_PluginInstance();
                 $pi_bao->get('id', $sug['plugin_id']);
                 $s = new CRM_Banking_Matcher_Suggestion($pi_bao->getInstance(), $this, $sug);
                 $this->addSuggestion($s);
             }
         }
     }
 }