function run()
 {
     CRM_Utils_System::setTitle(ts('Bank Transaction Exporter'));
     // get the plugins
     $plugin_list = CRM_Banking_BAO_PluginInstance::listInstances('export');
     $this->assign('plugin_list', $plugin_list);
     // get the IDs
     $txbatch2ids = CRM_Banking_PluginModel_Exporter::getIdLists($_REQUEST);
     $txcount = 0;
     foreach ($txbatch2ids as $txbatchid => $txbatchcontent) {
         $txcount += count($txbatchcontent);
     }
     $this->assign('txbatch_count', count($txbatch2ids));
     $this->assign('tx_count', $txcount);
     if (!empty($_REQUEST['list'])) {
         $this->assign('list', $_REQUEST['list']);
     }
     if (!empty($_REQUEST['s_list'])) {
         $this->assign('s_list', $_REQUEST['s_list']);
     }
     // check for the page mode
     if (isset($_REQUEST['exporter-plugin'])) {
         // EXECUTE
         // get the plugin instance
         $plugin_id = $_REQUEST['exporter-plugin'];
         foreach ($plugin_list as $plugin) {
             if ($plugin->id == $plugin_id) {
                 break;
             }
         }
         $plugin_instance = $plugin->getInstance();
         // TODO: select WHICH mode (this is only file mode)
         // start exporting
         $file_data = $plugin_instance->export_file($txbatch2ids, $_REQUEST);
         // process result (redirect, ...)
         if (empty($file_data['is_error'])) {
             $mime_type = mime_content_type($file_data['path']);
             $buffer = file_get_contents($file_data['path']);
             CRM_Utils_System::download($file_data['file_name'], $file_data['mime_type'], $buffer, $file_data['file_extension']);
         }
     } else {
         // CONFIGURATION MODE:
         $plugin_capabilities = array();
         foreach ($plugin_list as $plugin) {
             $capability = '';
             $instance = $plugin->getInstance();
             if ($instance->does_export_files()) {
                 $capability .= 'F';
             }
             if ($instance->does_export_stream()) {
                 $capability .= 'S';
             }
             $plugin_capabilities[$plugin->id] = $capability;
         }
         $this->assign('plugin_capabilities', $plugin_capabilities);
     }
     // URLs
     $this->assign('url_action', CRM_Utils_System::url('civicrm/banking/export'));
     parent::run();
 }
 /**
  * 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);
             }
         }
     }
 }
 function run()
 {
     // Example: Set the page-title dynamically; alternatively, declare a static title in xml/Menu/*.xml
     CRM_Utils_System::setTitle(ts('Bank Transaction Importer'));
     // get the plugins
     $plugin_list = CRM_Banking_BAO_PluginInstance::listInstances('import');
     // check for the page mode
     if (isset($_REQUEST['importer-plugin'])) {
         // RUN MODE
         $this->assign('page_mode', 'run');
         $plugin_id = $_REQUEST['importer-plugin'];
         $this->assign('plugin_id', $plugin_id);
         // assign values
         $this->assign('dry_run', isset($_REQUEST['dry_run']) ? $_REQUEST['dry_run'] : "off");
         $this->assign('process', isset($_REQUEST['process']) ? $_REQUEST['process'] : "off");
         foreach ($plugin_list as $plugin) {
             if ($plugin->id == $plugin_id) {
                 $this->assign('plugin_list', array($plugin));
                 break;
             }
         }
         // RUN the importer
         $file_info = isset($_FILES['uploadFile']) ? $_FILES['uploadFile'] : null;
         $this->assign('file_info', $file_info);
         $plugin_instance = $plugin->getInstance();
         $import_parameters = array('dry_run' => isset($_REQUEST['dry_run']) ? $_REQUEST['dry_run'] : "off", 'source' => isset($file_info['name']) ? $file_info['name'] : 'stream');
         if ($file_info != null && $plugin_instance::does_import_files()) {
             // run file import
             $file = $file_info['tmp_name'];
             if ($plugin_instance->probe_file($file, $import_parameters)) {
                 $plugin_instance->import_file($file, $import_parameters);
             } else {
                 CRM_Core_Session::setStatus(ts('File rejected by importer!'), ts('Bad input file'), 'alert');
             }
         } else {
             if ($plugin_instance::does_import_stream()) {
                 // run stream import
                 if ($plugin_instance->probe_stream($import_parameters)) {
                     $plugin_instance->import_stream($import_parameters);
                 } else {
                     CRM_Core_Session::setStatus(ts('Import stream rejected by importer, maybe not ready!'), ts('Bad input stream'), 'alert');
                 }
             } else {
                 CRM_Core_Session::setStatus(ts('Importer needs a file to proceed.'), ts('No input file'), 'alert');
             }
         }
         // TODO: RUN the processor
         if (isset($_REQUEST['process']) && $_REQUEST['process'] == "on") {
             CRM_Core_Session::setStatus(ts('Automated running not yet implemented'), ts('Not implemented'), 'alert');
         }
         // add the resulting log
         $this->assign('log', $plugin_instance->getLog());
     } else {
         // CONFIGURATION MODE:
         $this->assign('page_mode', 'config');
         $this->assign('plugin_list', $plugin_list);
         // extract the sources for the plugins
         $has_file_source = array();
         foreach ($plugin_list as $plugin) {
             $class = $plugin->getClass();
             if ($class::does_import_files()) {
                 $has_file_source[$plugin->id] = 'true';
             } else {
                 $has_file_source[$plugin->id] = 'false';
             }
         }
         $this->assign('has_file_source', $has_file_source);
     }
     // URLs
     $this->assign('url_action', CRM_Utils_System::url('civicrm/banking/import'));
     $this->assign('url_payments', CRM_Utils_System::url('civicrm/banking/payments', 'show=statements'));
     parent::run();
 }