protected function execute(InputInterface $input, OutputInterface $output) { $isVerbose = $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE; $sourceProject = $input->getOption('source-project-code'); $targetProject = $input->getOption('target-project-code'); $fromMemberResource = new MemberResource($this->sourceClient); $toMemberResource = new MemberResource($this->client); $projectResource = new ProjectResource($this->client); $userResource = new UserResource($this->client); $project = $projectResource->getOneBy(['code' => $targetProject]); if (!$project) { $output->writeln("<error>Target project {$targetProject} not found</error>"); die; } $page = 0; $perPage = 10; // $members = $fromMemberResource->getList($page, $perPage, [], [], [], ['project_code' => $sourceProject]); $maxPages = 1; while ($page < $maxPages) { $members = $fromMemberResource->getList(++$page, $perPage, [], [], [], ['project_code' => $sourceProject]); $maxPages = $members['pages']; // if no items, return if (!count($members['items']) || !$members['total']) { break; } foreach ($members['items'] as $key => $member) { if ($isVerbose) { $no = ($page - 1) * $perPage + $key + 1; $output->writeln("{$no} - Reading member #{$member['id']}"); } /* * try to find if member is already there * if yes - ignore. if not - import. * if current member doesn't have any of the $uniqueFields set * then ignore as we won't be able to find it later */ $uniqueFields = ['code', 'email_within_project', 'phone_within_project']; $findByField = false; $findByValue = false; foreach ($uniqueFields as $field) { if (isset($member[$field])) { $findByField = $field; $findByValue = $member[$field]; break; } } if ($findByField && $findByValue) { $findMember = $toMemberResource->getOneBy([$findByField => $findByValue, 'project_id' => $project['id']]); if ($findMember) { $output->writeln("<error>Member {$findByField}:{$findByValue} already exists for project {$project['code']}</error>"); continue; } } else { // ignore no-codes for now $output->writeln("<error>Member #{$member['id']} doesn't have any of the required fields: " . implode(',', $uniqueFields) . "</error>"); continue; } if (array_search($member['permission_type_code'], ['owner']) !== false) { // save admin only if user found // @todo - create user first $user = $userResource->getOneBy(['username' => $member['code']]); if (!$user) { $output->writeln("<error>Couldn't find user record for admin member {$member['code']} </error>"); continue; } $member['user_id'] = $user['id']; } $this->processMemberFields($member, $project); try { $res = $toMemberResource->save($member); if ($isVerbose) { $output->writeln("New member #{$res['id']} saved"); } } catch (\Exception $e) { $output->writeln("<error>Problem saving {$findByField}:{$findByValue}:{$e->getMessage()} </error>"); var_dump($member); } } } }
protected function execute(InputInterface $input, OutputInterface $output) { $filename = $input->getArgument('filename'); $serverCode = $input->getOption('server-code'); $timezoneId = $input->getOption('timezone-id'); $projectCode = $input->getOption('project-code'); $userResource = new UserResource($this->client); /** @type MemberResource $memberResource */ $memberResource = new MemberResource($this->client); $projectResource = new ProjectResource($this->client); /** @type ProjectResource $resource */ $resource = new ProjectResource($this->client); $reader = new \EasyCSV\Reader($filename, 'r', true); while ($row = $reader->getRow()) { //code,name,description,timezone_id $code = $row['code']; if ($projectCode && $code != $projectCode) { continue; } $params = []; if ($project = $resource->getOneBy(['code' => $code])) { $params['id'] = $project['id']; } try { /* $res = $resource->save( array_merge( $params, [ 'title' => $name = $row['name'], 'code' => $code, 'timezone_id' => $timezoneId, 'description' => $name." Project", 'background_server_code' => $serverCode, ] ) ); */ } catch (\Exception $e) { printf("Error saving project: %s\n", $e->getMessage()); printf("Project {$code} already exists\n"); } $project = $projectResource->getByCode($code); $res = $resource->addModule($code, 'turk'); $staffAdmins = ['tac', 'ho449']; try { foreach (array_merge([$code], $staffAdmins) as $idx => $username) { $user = $userResource->getOneBy(['username' => $username]); if (!$user) { print "user '{$username}' not found\n"; continue; } $params = ['code' => $username, 'project_id' => $project['id'], 'user_id' => $user['id'], 'permission_type_code' => 'owner', 'enrollment_status_code' => 'notenrolled']; $member = $memberResource->getOneBy(['code' => $username, 'project_id' => $project['id']]); if ($member) { $output->writeln("<error>Member '{$username}' already exists for project " . $project['code'] . "</error>"); continue; } print "Saving member '{$username}' for project " . $project['code'] . "\n"; $res = $memberResource->save($params); } } catch (Exception $e) { var_dump($params); $output->writeln("<error>Error importing member: {$e->getMessage()}</error>"); } } }