/**
  * Query
  *
  * @param array $args
  * @return Doctrine_Query $q
  */
 protected function air_query($args = array())
 {
     $src_id = $this->parent_rec->src_id;
     $q = Doctrine_Query::create()->from('SrcActivity a');
     $q->andWhere('a.sact_src_id = ?', $src_id);
     $q->leftJoin('a.CreUser cu');
     $q->leftJoin('a.UpdUser uu');
     $q->leftJoin('a.ActivityMaster am');
     $q->leftJoin('a.Project p');
     SrcActivity::joinRelated($q, 'a');
     return $q;
 }
 /**
  * Post-insert hook to log an activity for "manual-entry" submissions.
  * (Can't use regular AIR2Logger, since we need the postInsert xid).
  *
  * @param DoctrineEvent $event
  */
 public function postInsert($event)
 {
     parent::postInsert($event);
     // only manual-entries, and only if requestion
     if ($this->srs_type != self::$TYPE_MANUAL_ENTRY) {
         return;
     }
     if (!self::$LOG_MANUAL_ENTRY) {
         return;
     }
     // figure out what type of entry this is
     $actm_id = false;
     foreach (Inquiry::$MANUAL_TYPES as $code => $config) {
         if ($this->SrcResponse[0]->sr_orig_value == $config['label']) {
             $actm_id = $config['actm'];
             break;
         }
     }
     // find project id
     $prj_id = false;
     if (count($this->Inquiry->ProjectInquiry) > 0) {
         $prj_id = $this->Inquiry->ProjectInquiry[0]->pinq_prj_id;
     }
     // only log if the type matched something
     if ($actm_id && $prj_id) {
         $sact = new SrcActivity();
         $sact->sact_src_id = $this->srs_src_id;
         $sact->sact_actm_id = $actm_id;
         $sact->sact_prj_id = $prj_id;
         $sact->sact_dtim = $this->srs_date;
         $sact->sact_desc = '{USER} entered {XID} for source {SRC}';
         $sact->sact_notes = null;
         $sact->sact_xid = $this->srs_id;
         $sact->sact_ref_type = SrcActivity::$REF_TYPE_RESPONSE;
         $sact->save();
     }
 }
 /**
  * Log a src_activity to indicate that we merged a source.
  *
  * @param array   $result_data
  * @param array   $response_data
  */
 protected function log_activity($result_data, $response_data)
 {
     $sa = new SrcActivity();
     $sa->sact_actm_id = ActivityMaster::SRCINFO_UPDATED;
     $sa->sact_src_id = $this->prime->src_id;
     $sa->sact_dtim = air2_date();
     $old_usr = $response_data['MergeSource']['src_username'];
     $sa->sact_desc = "{USER} merged source {SRC} with {$old_usr}";
     $sa->sact_notes = null;
     //TODO: something from result?
     $sa->save();
 }
 /**
  * Joins a SrcActivity to it's external reference in a Doctrine Query.
  *
  * @param AIR2_Query $q
  * @param string  $alias
  */
 public static function joinRelated($q, $alias)
 {
     $a = $alias ? "{$alias}." : "";
     SrcActivity::setupRelated();
     $q->leftJoin("{$a}SrcResponseSet WITH {$a}sact_ref_type = ?", self::$REF_TYPE_RESPONSE);
     $q->leftJoin("{$a}Tank WITH {$a}sact_ref_type = ?", self::$REF_TYPE_TANK);
     $q->leftJoin("{$a}Organization WITH {$a}sact_ref_type = ?", self::$REF_TYPE_ORG);
     $q->leftJoin("{$a}Inquiry WITH {$a}sact_ref_type = ?", self::$REF_TYPE_INQUIRY);
     $q->leftJoin("{$a}Email WITH {$a}sact_ref_type = ?", self::$REF_TYPE_EMAIL);
 }
 /**
  * Log a SrcActivity for SrcOutcome creation/deletion
  *
  * @param array   $params
  */
 protected static function log_srcout_activity($params)
 {
     $def = $params['def'];
     $rec = $params['rec'];
     // try really hard to get the outcome data (arrgg! Doctrine!!!)
     $outdata = $rec->Outcome->toArray();
     if (!$outdata || !isset($outdata['out_headline'])) {
         $rec->clearRelated();
         $outdata = $rec->Outcome->toArray();
     }
     if (!$outdata || !isset($outdata['out_headline'])) {
         $rec->Outcome->refresh();
         $outdata = $rec->Outcome->toArray();
     }
     $hdl = isset($outdata['out_headline']) ? $outdata['out_headline'] : '';
     $hdl = $hdl ? "\"{$hdl}\"" : '';
     // put operation/outcome-text into description
     $str_op = 'inserted';
     if ($params['op'] == 'oninsert') {
         $str_op = 'added';
     }
     if ($params['op'] == 'ondelete') {
         $str_op = 'removed';
     }
     $desc = preg_replace('/<OP>/', $str_op, $def['desc']);
     $desc = preg_replace('/<OUT>/', $hdl, $desc);
     $filter = array_flip($def['fields']);
     if ($outdata && is_array($outdata)) {
         $outdata = array_intersect_key($outdata, $filter);
     }
     // log the activity
     $sact = new SrcActivity();
     $sact->sact_actm_id = $def['actm_id'];
     $sact->sact_prj_id = null;
     //no project
     $sact->sact_dtim = air2_date();
     $sact->sact_desc = $desc;
     $sact->sact_notes = json_encode(array('outcome' => $outdata));
     // get the actual source and add an activity
     $source = $rec->hasRelation('Source') ? $rec->Source : $rec;
     if ($source->exists()) {
         $sact->sact_src_id = $source->src_id;
         $sact->save();
     } else {
         $source->SrcActivity[] = $sact;
         //saving will happen later
     }
 }