/**
  * Load data for html printing
  *
  * @param type $uuid
  * @param type $base_rs
  */
 protected function show_print_html($uuid, $base_rs)
 {
     $bin = AIR2_Record::find('Bin', $uuid);
     $base_rs['sources'] = array();
     // authorized sources
     $authz_sources = array();
     $q = Doctrine_Query::create()->from('BinSource bs');
     $q->leftJoin('bs.Source s');
     $q->where('bs.bsrc_bin_id = ?', $bin->bin_id);
     $q->select('bs.bsrc_src_id, s.src_uuid');
     BinSource::query_may_read($q, $this->user, 'bs');
     $bsrcs = $q->fetchArray();
     foreach ($bsrcs as $bsrc) {
         $authz_sources[$bsrc['Source']['src_uuid']] = true;
     }
     // only keep fetching if there is stuff to get
     $authz_responses = array();
     if (count($authz_sources) > 0) {
         $q = Doctrine_Query::create()->from('BinSrcResponseSet bs');
         $q->leftJoin('bs.SrcResponseSet s');
         $q->where('bs.bsrs_bin_id = ?', $bin->bin_id);
         $q->select('bs.bsrs_srs_id, s.srs_uuid');
         BinSrcResponseSet::query_may_read($q, $this->user, 'bs');
         $bsrss = $q->fetchArray();
         foreach ($bsrss as $bsrs) {
             $authz_responses[$bsrs['SrcResponseSet']['srs_uuid']] = true;
         }
         // let perl do the heavy lifting
         $binsources = CallPerl::exec('AIR2::Bin->flatten', $bin->bin_id);
         foreach ($binsources as $src) {
             $src_uuid = $src['src_uuid'];
             if (isset($authz_sources[$src_uuid])) {
                 // apply authz to responses
                 if (is_array($src['response_sets'])) {
                     foreach ($src['response_sets'] as $idx => $srs) {
                         $srs_uuid = $srs['srs_uuid'];
                         if (!isset($authz_responses[$srs_uuid])) {
                             unset($src['response_sets'][$idx]);
                         }
                     }
                     $src['response_sets'] = array_values($src['response_sets']);
                 }
                 // add as value
                 $authz_sources[$src_uuid] = $src;
             }
         }
     }
     // reorganize for the print view
     $raw = array('bin' => $base_rs['radix'], 'sources' => array_values($authz_sources));
     $this->airoutput->view = 'print/bin';
     $this->response($raw);
 }
 /**
  * Set the environment the perl call will execute with
  *
  * @param type $new_env
  */
 public static function set_env($new_env)
 {
     self::$ENV = $new_env ? $new_env : false;
 }
 /**
  * Redirect to perl!
  *
  * @param  array $args
  * @return array $resp
  */
 public function rec_query($args)
 {
     $uid = $this->user->user_id;
     // get options
     $opts = array();
     if (isset($args['sources']) && $args['sources']) {
         $opts['sources'] = 1;
     }
     if (isset($args['org_uuid'])) {
         $org = AIR2_Record::find('Organization', $args['org_uuid']);
         if (!$org) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid org_uuid');
         }
         $opts['org_id'] = $org->org_id;
     }
     if (isset($args['prj_uuid'])) {
         $prj = AIR2_Record::find('Project', $args['prj_uuid']);
         if (!$prj) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid prj_uuid');
         }
         $opts['prj_id'] = $prj->prj_id;
     }
     if (isset($args['inq_uuid'])) {
         $inq = AIR2_Record::find('Inquiry', $args['inq_uuid']);
         if (!$inq) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid inq_uuid');
         }
         $opts['inq_id'] = $inq->inq_id;
     }
     if (isset($args['start_date'])) {
         if (strtotime($args['start_date']) === false) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid start_date');
         }
         $opts['start_date'] = $args['start_date'];
     }
     if (isset($args['end_date'])) {
         if (strtotime($args['end_date']) === false) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid end_date');
         }
         $opts['end_date'] = $args['end_date'];
     }
     # return just the count
     if (isset($args['count']) && $args['count']) {
         $opts['count'] = 1;
         $this->cached_total = CallPerl::exec('AIR2::OutcomeWriter->get_obj', $uid, $opts);
         return array();
     }
     // download results, or email
     $data = array();
     if (isset($args['email']) && $args['email']) {
         $opts['format'] = 'email';
         // build command
         $cmd = "PERL AIR2_ROOT/bin/outcome-export.pl --user_id={$uid}";
         foreach ($opts as $key => $val) {
             if ($key == 'sources') {
                 $cmd .= " --{$key}";
             } else {
                 $cmd .= " --{$key}={$val}";
             }
         }
         $job = new JobQueue();
         $job->jq_job = $cmd;
         $this->air_save($job);
         // success, but we want a non-200, so throw up!
         $msg = 'CSV export scheduled for background processing';
         throw new Rframe_Exception(Rframe::BGND_CREATE, $msg);
     } else {
         if (count($opts) == 0) {
             $opts = null;
         }
         //avoid hash vs array confusion
         $r = CallPerl::exec('AIR2::OutcomeWriter->get_obj', $uid, $opts);
         $this->fields = $r[0];
         $this->reset_fields();
         // unflatten ... yeah, I know this is inefficient
         // but it's backwards compatible
         for ($i = 1; $i < count($r); $i++) {
             $data[$i - 1] = array();
             foreach ($this->fields as $colnum => $name) {
                 $data[$i - 1][$name] = $r[$i][$colnum];
             }
         }
     }
     return $data;
 }
 /**
  * Send a preview email
  *
  * @param string  $addr
  */
 private function send_preview($addr)
 {
     try {
         $email_id = $this->parent_rec->email_id;
         CallPerl::exec('AIR2::Email->send_preview', $email_id, $addr, '[PROOF] ');
     } catch (PerlException $e) {
         throw new Rframe_Exception(Rframe::BAD_DATA, $e->getMessage());
     }
     throw new Rframe_Exception(Rframe::BGND_CREATE, 'Email preview sent');
 }
 /**
  * Save/send a single email, and throw a BGND-CREATE at the end
  *
  * @param Email   $eml
  * @param String  $addr
  * @param Integer $srs_id
  * @param Boolean $no_export
  */
 private function send_single($eml, $addr, $srs_id = null, $no_export = null)
 {
     $this->check_authz($eml, 'read');
     $this->air_save($eml);
     try {
         if (!$no_export) {
             CallPerl::exec('AIR2::Email->send_single', $eml->email_id, $addr, $srs_id);
         }
     } catch (PerlException $e) {
         throw new Rframe_Exception(Rframe::BAD_DATA, $e->getMessage());
     }
     throw new Rframe_Exception(Rframe::BGND_CREATE, $eml->email_uuid);
 }
 /**
  * Retrieve a preview of the published query
  *
  * @param unknown $format (optional)
  * @return unknown
  */
 public function get_preview($format = 'html')
 {
     return CallPerl::exec('AIR2::InquiryPublisher->get_preview', $this->inq_uuid, $format);
 }
 /**
  * Entirely redo discrimination on a tank_source
  *
  * @param TankSource $rec
  */
 private function _redo_discrimination($rec)
 {
     $valid = array(TankSource::$STATUS_ERROR, TankSource::$STATUS_CONFLICT);
     if (!in_array($rec->tsrc_status, $valid)) {
         $msg = 'Must be in ERROR or CONFLICT status to use redo';
         throw new Rframe_Exception(Rframe::BAD_DATA, $msg);
     }
     // reset tank_source entirely (BUT to error --- avoid NEW status)
     $rec->tsrc_status = TankSource::$STATUS_ERROR;
     $rec->tsrc_errors = null;
     $this->air_save($rec);
     // call perl discriminator
     try {
         CallPerl::set_env('REMOTE_USER='******'AIR2::TankSource->discriminate', $rec->tsrc_id);
     } catch (PerlException $e) {
         throw new Rframe_Exception(Rframe::BAD_DATA, $e->getMessage());
     }
 }
 /**
  * Redirect to perl!
  *
  * @param  array $args
  * @return array $resp
  */
 public function rec_query($args)
 {
     $bid = $this->parent_rec->bin_id;
     $uid = $this->user->user_id;
     // max csv  export size - #4458
     $conn = AIR2_DBManager::get_connection();
     $num = $conn->fetchOne("select count(*) from bin_source where bsrc_bin_id={$bid}", array(), 0);
     if ($num > Bin::$MAX_CSV_EXPORT && !$this->user->is_system()) {
         $msg = "Can only CSV export up to " . Bin::$MAX_CSV_EXPORT . " Sources";
         throw new Rframe_Exception(Rframe::BAD_DATA, $msg);
     }
     // get options
     $cpx = isset($args['allfacts']) && $args['allfacts'] ? 1 : 0;
     $log = 1;
     //default
     if (isset($args['logging']) && !$args['logging']) {
         $log = 0;
     }
     $notes = 0;
     if ($this->parent_rec->bin_status == Bin::$STATUS_ACTIVE_PROMPT_NOTES) {
         $notes = 1;
     }
     $opts = array('complex_facts' => $cpx, 'log_activity' => $log, 'bin_notes' => $notes);
     // download results, or email
     $data = array();
     if (isset($args['email']) && $args['email']) {
         // fix param names
         $extra = array();
         if ($opts['complex_facts']) {
             $extra[] = 'complex_facts';
         }
         if ($opts['log_activity']) {
             $extra[] = 'logging';
         }
         if ($opts['bin_notes']) {
             $extra[] = 'notes';
         }
         try {
             $this->parent_rec->queue_csv_export($this->user, $extra);
         } catch (Exception $e) {
             throw new Rframe_Exception(Rframe::BAD_DATA, $e->getMessage());
         }
         // success, but we want a non-200, so throw up!
         $msg = 'CSV export scheduled for background processing';
         throw new Rframe_Exception(Rframe::BGND_CREATE, $msg);
     } else {
         $r = CallPerl::exec('AIR2::CSVWriter->from_bin', $bid, $uid, $opts);
         $this->fields = $r[0];
         $this->reset_fields();
         // unflatten ... yeah, I know this is inefficient
         // but it's backwards compatible
         for ($i = 1; $i < count($r); $i++) {
             $data[$i - 1] = array();
             foreach ($this->fields as $colnum => $name) {
                 $data[$i - 1][$name] = $r[$i][$colnum];
             }
         }
     }
     return $data;
 }