/**
  * Set the correct sv_type based on which fields are populated.
  *
  * @param array   $data
  * @param TankSource $tsrc
  * @param int     $op
  */
 public function discriminate($data, &$tsrc, $op = null)
 {
     if (isset($data['sv_value']) || isset($data['sv_basis'])) {
         $this->sv_type = SrcVita::$TYPE_EXPERIENCE;
     } elseif (isset($data['sv_notes'])) {
         $this->sv_type = SrcVita::$TYPE_INTEREST;
     }
     parent::discriminate($data, $tsrc, $op);
 }
 /**
  * By default, discriminate throws conflicts on existing, non-matching
  * values.  Here, we'll allow overwriting the src_status.
  *
  * @param array   $data
  * @param TankSource $tsrc
  * @param int     $op
  */
 public function discriminate($data, &$tsrc, $op = null)
 {
     if (isset($data['src_status']) && strlen($data['src_status']) == 1) {
         $this->src_status = $data['src_status'];
     }
     parent::discriminate($data, $tsrc, $op);
 }
 /**
  * Alias will overwrite existing values without conflict.  For now, only
  * allow a source to have one SrcAlias record.
  *
  * @param array   $data
  * @param TankSource $tsrc
  * @param int     $op
  */
 public function discriminate($data, &$tsrc, $op = null)
 {
     // search for existing
     $q = $this->getTable()->createQuery();
     $q->where('sa_src_id = ?', $this->sa_src_id);
     $existing_rec = $q->fetchOne(array(), Doctrine::HYDRATE_ARRAY);
     if ($existing_rec) {
         $this->assignIdentifier($existing_rec['sa_id']);
         $this->hydrate($existing_rec);
     }
     // call parent
     parent::discriminate($data, $tsrc, $op);
 }
 /**
  * Make sure the source doesn't get duplicate addresses
  *
  * @param array   $data
  * @param TankSource $tsrc
  * @param unknown $op   (optional)
  */
 public function discriminate($data, &$tsrc, $op = null)
 {
     // search for existing
     if ($op != AIR2_DISCRIM_ADD) {
         $q = $this->getTable()->createQuery();
         $q->where('smadd_src_id = ?', $this->smadd_src_id);
         // ID existing by line_1, city, or zip
         $subqry = array();
         $params = array();
         if (isset($data['smadd_line_1'])) {
             $subqry[] = 'smadd_line_1 = ?';
             $params[] = $data['smadd_line_1'];
         }
         if (isset($data['smadd_city'])) {
             $subqry[] = 'smadd_city = ?';
             $params[] = $data['smadd_city'];
         }
         if (isset($data['smadd_zip'])) {
             $subqry[] = 'smadd_zip = ?';
             $params[] = $data['smadd_zip'];
         }
         $subqry = implode(' OR ', $subqry);
         if ($subqry) {
             $q->andWhere("({$subqry})", $params);
             $existing_rec = $q->fetchOne(array(), Doctrine::HYDRATE_ARRAY);
             if ($existing_rec) {
                 $this->assignIdentifier($existing_rec['smadd_id']);
                 $this->hydrate($existing_rec);
             }
         }
         $q->free();
         //cleanup
     }
     // just set primary --- actual logic in postsave hook
     if (isset($data['smadd_primary_flag'])) {
         $this->smadd_primary_flag = $data['smadd_primary_flag'];
         unset($data['smadd_primary_flag']);
     }
     // set remaining data with parent
     parent::discriminate($data, $tsrc, $op);
 }
 /**
  * Check for existing facts
  *
  * @param array   $data
  * @param TankSource $tsrc
  * @param unknown $op   (optional)
  */
 public function discriminate($data, &$tsrc, $op = null)
 {
     $overwrite_facts = array('household_income', 'education_level', 'political_affiliation', 'religion', 'source_website', 'lifecycle', 'timezone');
     $overwrite = false;
     // determine if this SrcFact exists yet
     $id = array('sf_src_id' => $this->sf_src_id, 'sf_fact_id' => $this->sf_fact_id);
     $q = Doctrine_Query::create()->from('SrcFact sf');
     $q->leftJoin('sf.Fact f');
     $q->where('sf_fact_id = ?', $this->sf_fact_id);
     $q->andWhere('sf_src_id = ?', $this->sf_src_id);
     $q->select('sf.*, f.fact_identifier as fident');
     $existing_rec = $q->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY);
     $q->free();
     if ($existing_rec) {
         $this->_disc_fact_ident = $existing_rec['fident'];
         unset($existing_rec['fident']);
         $this->assignIdentifier($id);
         $this->hydrate($existing_rec);
     }
     // update the record
     parent::discriminate($data, $tsrc, $op);
 }
 /**
  * Make sure the source doesn't get duplicate sem_emails
  *
  * @param array   $data
  * @param TankSource $tsrc
  * @param unknown $op   (optional)
  */
 public function discriminate($data, &$tsrc, $op = null)
 {
     // ignore if sem_email isn't set
     if (isset($data['sem_email'])) {
         // search for existing
         if ($op != AIR2_DISCRIM_ADD) {
             $q = $this->getTable()->createQuery();
             $q->where('sem_src_id = ?', $this->sem_src_id);
             $q->andWhere('sem_email = ?', $data['sem_email']);
             $existing_rec = $q->fetchOne(array(), Doctrine::HYDRATE_ARRAY);
             if ($existing_rec) {
                 $this->assignIdentifier($existing_rec['sem_id']);
                 $this->hydrate($existing_rec);
             }
             $q->free();
             //cleanup
         }
         // just set primary --- actual logic in postsave hook
         if (isset($data['sem_primary_flag'])) {
             $this->sem_primary_flag = $data['sem_primary_flag'];
             unset($data['sem_primary_flag']);
         }
         // email addresses should ALWAYS be lowercased
         $data['sem_email'] = strtolower($data['sem_email']);
         if ($this->sem_email) {
             $this->sem_email = strtolower($this->sem_email);
         }
         // set remaining data with parent
         parent::discriminate($data, $tsrc, $op);
     }
 }
 /**
  * Attempts to discriminate data being saved to a record, tracking any
  * conflicts.  Note that the AIR2_Record->discriminate() method should
  * save the record, so call it from a try-catch.
  *
  * @param AIR2_Record $rec
  * @param array   $data
  * @param TankSource $tsrc
  * @param int     $op
  */
 protected function resolve_conflicts($rec, $data, $tsrc, $op = null)
 {
     // ignore this piece of the tank_source
     if ($op == AIR2_DISCRIM_IGNORE) {
         return;
     }
     // unset any null data vals
     foreach ($data as $key => $val) {
         if (is_null($val)) {
             unset($data[$key]);
         } elseif (is_string($val) && strlen($val) == 0) {
             unset($data[$key]);
         }
     }
     // discriminate, if we have data
     if (count($data) > 0) {
         $rec->discriminate($data, $tsrc, $op);
         // try to save the record
         try {
             $rec->save();
         } catch (Doctrine_Validator_Exception $e) {
             $cls = get_class($rec);
             $stack = $rec->getErrorStack()->toArray();
             foreach ($stack as $col => $problem) {
                 $tsrc->add_conflict($cls, $col, $problem);
             }
         } catch (Exception $e) {
             $cls = get_class($rec);
             $msg = $e->getMessage();
             $tsrc->add_error("FATAL ERROR on {$cls} - {$msg}");
         }
     }
 }