/**
  * Forces a source to be opted-into APMG
  *
  * @param int     $src_id
  * @return bool $inserted
  */
 public static function force_apmg($src_id)
 {
     $data = array('so_src_id' => $src_id, 'so_org_id' => Organization::$APMPIN_ORG_ID, 'so_uuid' => air2_generate_uuid(), 'so_effective_date' => air2_date(), 'so_home_flag' => 0, 'so_status' => self::$STATUS_OPTED_IN, 'so_cre_user' => 1, 'so_upd_user' => 1, 'so_cre_dtim' => air2_date(), 'so_upd_dtim' => air2_date());
     $flds = implode(',', array_keys($data));
     $vals = air2_sql_param_string($data);
     $stmt = "insert ignore into src_org ({$flds}) values ({$vals})";
     // execute
     $conn = AIR2_DBManager::get_master_connection();
     $n = $conn->exec($stmt, array_values($data));
     return $n;
 }
 /**
  * Tag an object using raw connections.  Returns true if the tag was added,
  * or false if the tag already existed.
  *
  * @param int     $xid
  * @param string  $type
  * @param string  $tag
  * @return boolean
  */
 public static function make_tag($xid, $type, $tag)
 {
     // sanity!
     if (!is_numeric($xid) || $xid < 1) {
         throw new Exception("Bad tag XID({$xid})");
     }
     if (!in_array($type, array('I', 'P', 'S', 'R'))) {
         throw new Exception("Bad tag TYPE({$type})");
     }
     if (!is_string($tag) || strlen($tag) < 1) {
         throw new Exception("Bad tag '{$tag}'");
     }
     // fetch/create tag master
     $tm_id = TagMaster::get_tm_id($tag);
     // params
     $usrid = defined('AIR2_REMOTE_USER_ID') ? AIR2_REMOTE_USER_ID : 1;
     $dtim = air2_date();
     $cols = array('tag_tm_id', 'tag_xid', 'tag_ref_type', 'tag_cre_user', 'tag_upd_user', 'tag_cre_dtim', 'tag_upd_dtim');
     $vals = array($tm_id, $xid, $type, $usrid, $usrid, $dtim, $dtim);
     // insert ignore
     $conn = AIR2_DBManager::get_master_connection();
     $colstr = implode(',', $cols);
     $params = air2_sql_param_string($cols);
     $n = $conn->exec("insert ignore into tag ({$colstr}) values ({$params})", $vals);
     // if tag existed, update userstamp
     $tag_was_new = true;
     if ($n == 0) {
         $tag_was_new = false;
         $set = "tag_upd_user={$usrid}, tag_upd_dtim='{$dtim}'";
         $where = "tag_tm_id={$tm_id} and tag_xid={$xid} and tag_ref_type='{$type}'";
         $conn->exec("update tag set {$set} where {$where}");
     }
     return $tag_was_new;
 }
 /**
  * Create or update a SrcOrg record for a Source.
  *
  * @param Source  $src
  */
 public function process_source(Source $src)
 {
     // run raw-sql for efficiency
     $conn = AIR2_DBManager::get_master_connection();
     $src_id = $src->src_id;
     $org_id = $this->to_org_id;
     $unset_home_flags = false;
     // check for existing (and total)
     $where = "where so_src_id = {$src_id} and so_org_id = {$org_id}";
     $status = "(select so_status from src_org {$where}) as status";
     $ishome = "(select so_home_flag from src_org {$where}) as ishome";
     $select = "select count(*) as total, {$status}, {$ishome}";
     $q = "{$select} from src_org where so_src_id = {$src_id}";
     $rs = $conn->fetchRow($q);
     // run operation
     $op = false;
     $data = array();
     if (is_null($rs['status'])) {
         // insert
         $data = array('so_src_id' => $src_id, 'so_org_id' => $org_id, 'so_uuid' => air2_generate_uuid(), 'so_effective_date' => air2_date(), 'so_home_flag' => $this->to_so_home_flag ? 1 : 0, 'so_status' => $this->to_so_status, 'so_cre_user' => $this->Tank->tank_user_id, 'so_upd_user' => $this->Tank->tank_user_id, 'so_cre_dtim' => $this->Tank->tank_cre_dtim, 'so_upd_dtim' => $this->Tank->tank_upd_dtim);
         // determine home flag
         if ($rs['total'] == 0) {
             $data['so_home_flag'] = true;
         } elseif ($this->to_so_home_flag && $data['total'] > 0) {
             $unset_home_flags = true;
         }
         // insert
         $flds = implode(',', array_keys($data));
         $vals = air2_sql_param_string($data);
         $q = "insert into src_org ({$flds}) values ({$vals})";
         $conn->exec($q, array_values($data));
     } else {
         // update
         $updates = array();
         // change to status
         if ($rs['status'] != $this->to_so_status) {
             $updates[] = "so_status='" . $this->to_so_status . "'";
         }
         // change to home flag (only allow setting, not unsetting)
         if ($this->to_so_home_flag && !$rs['ishome']) {
             $updates[] = "so_home_flag=1";
             //MUST be true
             if ($rs['total'] > 1) {
                 $unset_home_flags = true;
             }
         }
         // do we need to do anything?
         if (count($updates)) {
             $set = implode(', ', $updates);
             $where = "so_src_id={$src_id} and so_org_id={$org_id}";
             $q = "update src_org set {$set} where {$where}";
             $conn->exec($q);
         }
     }
     // optionally unset other home flags
     if ($unset_home_flags) {
         $set = 'so_home_flag=0';
         $where = "so_src_id={$src_id} and so_org_id!={$org_id}";
         $conn->exec("update src_org set {$set} where {$where}");
     }
 }
 /**
  * Create a SrcActivity record for a Source.
  *
  * @param Source  $src
  */
 public function process_source(Source $src)
 {
     if ($this->tact_type != self::$TYPE_SOURCE) {
         return;
     }
     // get data
     $data = array('sact_actm_id' => $this->tact_actm_id, 'sact_src_id' => $src->src_id, 'sact_prj_id' => $this->tact_prj_id, 'sact_dtim' => $this->tact_dtim, 'sact_desc' => $this->tact_desc, 'sact_notes' => $this->tact_notes, 'sact_cre_user' => $this->Tank->tank_user_id, 'sact_upd_user' => $this->Tank->tank_user_id, 'sact_cre_dtim' => $this->Tank->tank_cre_dtim, 'sact_upd_dtim' => $this->Tank->tank_upd_dtim);
     if ($this->tact_xid && $this->tact_ref_type) {
         $data['sact_xid'] = $this->tact_xid;
         $data['sact_ref_type'] = $this->tact_ref_type;
     }
     // run raw-sql for efficiency
     $conn = AIR2_DBManager::get_master_connection();
     $flds = implode(',', array_keys($data));
     $vals = air2_sql_param_string($data);
     $q = "insert into src_activity ({$flds}) values ({$vals})";
     $conn->exec($q, array_values($data));
 }