/**
  * Format the value returned from rec_query() into an array radix.
  *
  * @param Doctrine_Query $q
  * @return array $radix
  */
 protected function format_query_radix(Doctrine_Query $q)
 {
     $q2 = $q->copy()->select('org_id');
     $rs = $q2->fetchArray();
     // also select all parent orgs
     $ids = array();
     foreach ($rs as $org) {
         $parent_ids = Organization::get_org_parents($org['org_id']);
         $ids = array_merge($ids, $parent_ids);
         $ids[] = $org['org_id'];
     }
     $q->orWhereIn('o.org_id', $ids);
     $q->removeDqlQueryPart('limit');
     // now get the radix
     $this->_fields['org_parent_id'] = 1;
     $this->_fields['org_id'] = 1;
     $radix = parent::format_query_radix($q);
     // display organizations as a tree
     return $this->get_tree_data($radix);
 }
 /**
  * Get authorization object for this User.
  *   array(org_id => bitmask-role)
  *
  * This represents the Organization Asset authz.  That is, explicit roles
  * cascade both up and down the Org tree.  Explicit roles will always
  * override implicit ones.
  *
  * @return array
  */
 public function get_authz()
 {
     if ($this->authz) {
         return $this->authz;
     }
     // reset
     $this->authz = array();
     $this->authz_up = array();
     // sort orgs, and calculate authz
     $sorted = Organization::sort_by_depth($this->UserOrg, 'uo_org_id');
     foreach ($sorted as $uo) {
         if ($uo->uo_status == UserOrg::$STATUS_ACTIVE) {
             $children = Organization::get_org_children($uo->uo_org_id);
             $bitmask = $uo->AdminRole->get_bitmask();
             foreach ($children as $org_id) {
                 $this->authz[$org_id] = $bitmask;
                 $this->authz_up[$org_id] = $bitmask;
             }
             // get upwards-authz
             $parents = Organization::get_org_parents($uo->uo_org_id);
             foreach ($parents as $org_id) {
                 $curr = isset($this->authz_up[$org_id]) ? $this->authz_up[$org_id] : 0;
                 $this->authz_up[$org_id] = max(array($bitmask, $curr));
             }
         }
     }
     return $this->authz;
 }
 /**
  * Update
  *
  * @param ProjectOrg $rec
  * @param array      $data
  */
 protected function air_update($rec, $data)
 {
     if (isset($data['user_uuid'])) {
         $u = AIR2_Record::find('User', $data['user_uuid']);
         if (!$u) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid user_uuid');
         }
         // user in org, and has W/M role
         $org_and_parents = Organization::get_org_parents($rec->porg_org_id);
         $org_and_parents[] = $rec->porg_org_id;
         $in_org = false;
         foreach ($u->UserOrg as $uo) {
             $r = $uo->AdminRole->ar_code;
             if (in_array($uo->uo_org_id, $org_and_parents)) {
                 if ($r == 'W' || $r == 'M') {
                     $in_org = true;
                     break;
                 }
             }
         }
         if (!$in_org) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'ContactUser not Writer/Manager for organization');
         }
         // set
         $rec->porg_contact_user_id = $u->user_id;
     }
 }
 /**
  * Query
  *
  * @param array $args
  * @return Doctrine_Query $q
  */
 protected function air_query($args = array())
 {
     $q = Doctrine_Query::create()->from('User u');
     $q->leftJoin('u.UserOrg uo WITH uo.uo_home_flag = true');
     $q->leftJoin('uo.Organization o');
     $q->leftJoin('u.UserEmailAddress e with e.uem_primary_flag = true');
     $q->leftJoin('u.UserPhoneNumber p with p.uph_primary_flag = true');
     $q->leftJoin("u.Avatar av WITH av.img_ref_type = ?", 'A');
     $q->leftJoin('u.CreUser cu');
     $q->leftJoin('u.UpdUser uu');
     // flatten
     $q->addSelect('e.uem_address as uem_address');
     $q->addSelect('p.uph_number as uph_number');
     $q->addSelect('p.uph_ext as uph_ext');
     $q->addSelect('uo.uo_user_title as uo_user_title');
     $q->addSelect('o.org_uuid as org_uuid');
     $q->addSelect('o.org_name as org_name');
     $q->addSelect('o.org_display_name as org_display_name');
     $q->addSelect('o.org_html_color as org_html_color');
     // sort by some home_org first
     if (isset($args['sort_home'])) {
         $q->addSelect("(o.org_name = '{$args['sort_home']}') as myhome");
         $q->addOrderBy('myhome desc');
     }
     // restrict to some home_org
     if (isset($args['home_org'])) {
         $q->addWhere("o.org_name = '{$args['home_org']}'");
     }
     // status and type
     if (isset($args['status'])) {
         air2_query_in($q, $args['status'], 'u.user_status');
     }
     if (isset($args['type'])) {
         air2_query_in($q, $args['type'], 'u.user_type');
     }
     // text filter
     $str = isset($args['filter']) ? $args['filter'] : false;
     if ($str && strlen($str) > 0) {
         $usrs = "u.user_username LIKE '{$str}%' OR u.user_first_name " . "LIKE '{$str}%' OR u.user_last_name LIKE '{$str}%'";
         $orgs = "o.org_display_name LIKE '{$str}%' OR o.org_name LIKE '{$str}%'";
         $titles = "uo.uo_user_title LIKE '{$str}%'";
         $emails = "e.uem_address LIKE '{$str}%'";
         $q->addWhere("(({$usrs}) OR ({$orgs}) OR ({$titles}) OR ({$emails}))");
     }
     // exclude users belonging to an organization
     if (isset($args['excl_org'])) {
         $conn = AIR2_DBManager::get_connection();
         $orgq = "select z.org_id from organization z where z.org_uuid = ?";
         $excl = "select uo_user_id from user_org where uo_org_id = ({$orgq})";
         $exclude = $conn->fetchColumn($excl, array($args['excl_org']), 0);
         if (count($exclude) > 0) {
             $q->whereNotIn('u.user_id', $exclude);
         }
     }
     // users that are eligible contacts for an organization
     if (isset($args['incl_contact_org'])) {
         $org = AIR2_Record::find('Organization', $args['incl_contact_org']);
         if (!$org) {
             throw new Rframe_Exception(Rframe::BAD_DATA, 'invalid incl_contact_org');
         }
         $orgids = Organization::get_org_parents($org->org_id);
         $orgids[] = $org->org_id;
         // assemble query
         $orgids = implode(',', $orgids);
         $orgids = "uo_org_id in ({$orgids})";
         $arids = "select ar_id from admin_role where ar_code in ('M','W')";
         $arids = "uo_ar_id in ({$arids})";
         $uids = "select uo_user_id from user_org where {$orgids} and {$arids}";
         $q->addWhere("u.user_id in ({$uids})");
     }
     return $q;
 }