/** * Create * * @param array $data * @return ProjectOrg $rec */ protected function air_create($data) { $this->require_data($data, array('org_uuid', 'user_uuid')); // validate $o = AIR2_Record::find('Organization', $data['org_uuid']); if (!$o) { throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid org_uuid'); } $u = AIR2_Record::find('User', $data['user_uuid']); if (!$u) { throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid user_uuid'); } // unique org foreach ($this->parent_rec->ProjectOrg as $porg) { if ($porg->porg_org_id == $o->org_id) { throw new Rframe_Exception(Rframe::BAD_DATA, 'Organization already in Project'); } } // user in org, and has W/M role $org_and_parents = Organization::get_org_parents($o->org_id); $org_and_parents[] = $o->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'); } // create! $rec = new ProjectOrg(); $rec->porg_prj_id = $this->parent_rec->prj_id; $rec->porg_org_id = $o->org_id; $rec->porg_contact_user_id = $u->user_id; $rec->mapValue('org_uuid', $o->org_uuid); return $rec; }