protected function execute(InputInterface $input, OutputInterface $output)
 {
     $userResource = new UserResource($this->sourceClient);
     $result = $userResource->getList(1, 1000);
     foreach ($result['items'] as $user) {
         $data[] = ['username' => $user['username'], 'email' => isset($user['email']) ? $user['email'] : '', 'roles' => join(',', $user['roles'])];
     }
     $table = new Table($output);
     $table->setHeaders(['name', 'email', 'roles'])->setRows($data);
     $table->render();
 }
Example #2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $isVerbose = $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
     $sourceUsername = $input->getOption('source-user');
     if (!$sourceUsername) {
         $output->writeln("<error>--source-user is required</error>");
         die;
     }
     $targetUsername = $input->getOption('target-user') ?: $sourceUsername;
     $fromLocationResource = new LocationResource($this->sourceClient);
     $toObserveResource = new ObserveResource($this->client);
     $fromUserResource = new UserResource($this->sourceClient);
     $toUserResource = new UserResource($this->client);
     $sourceUser = $fromUserResource->getOneBy(['username' => $sourceUsername]);
     if (!$sourceUser) {
         $output->writeln("<error>Source user {$sourceUsername} not found</error>");
         die;
     }
     $targetUser = $toUserResource->getOneBy(['username' => $targetUsername]);
     if (!$targetUser) {
         $output->writeln("<error>Target user {$targetUsername} not found</error>");
         die;
     }
     die('Not yet implemented');
     // @todo
     $page = 0;
     $perPage = 10;
     $maxPages = 1;
     while ($page < $maxPages) {
         $result = $fromLocationResource->getList(++$page, $perPage, ['user_id' => $sourceUser['id']], [], [], ['details' => true]);
         var_dump($result['items']);
         die;
         $maxPages = $result['pages'];
         // if no items, return
         if (!count($result['items']) || !$result['total']) {
             break;
         }
         var_dump($result['items']);
         die;
         $locationsToPost = [];
         foreach ($result['items'] as $key => $location) {
             if ($isVerbose) {
                 $no = ($page - 1) * $perPage + $key + 1;
                 $output->writeln("{$no} - Reading location #{$location['id']}");
             }
             $locationsToPost[] = [];
         }
     }
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $isVerbose = $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
     $username = $input->getOption('user');
     $date = $input->getOption('date');
     $output->writeln("Getting locations for {$date}");
     $userResource = new UserResource($this->sourceClient);
     $criteria = [];
     if ($username) {
         $user = $userResource->getOneBy(['username' => $username]);
         if (!$user) {
             throw new \Exception("User {$username} not found");
         }
         $criteria['user_id'] = $user['id'];
     }
     $locationResource = new LocationResource($this->sourceClient);
     $result = $locationResource->getList(1, 100, $criteria, null, null, ['start_date' => $date]);
     var_dump($result);
 }
Example #4
0
 protected function processMessage($inData, $message)
 {
     if ($this->output->isVerbose()) {
         print json_encode($inData, JSON_PRETTY_PRINT) . "\n";
     }
     $username = $inData->id;
     $userResource = new UserResource($this->client);
     $user = $userResource->getOneBy(['username' => $username]);
     if (!$user) {
         $this->output->writeln("<error>No such user: {$username}</error>");
         return false;
     }
     $timestamp = gmdate('Y-m-d\\TH:i:s\\Z', $inData->timestamp);
     $userAgent = $inData->user_agent;
     $app = ['name' => 'Traccar'];
     if (preg_match('{TraccarClient/([\\d.]+)}', $userAgent, $m)) {
         $app['version'] = $m[1];
     }
     $device = ['uuid' => md5($username . '-traccar')];
     // not sure what to use
     if (preg_match('{Android ([\\d.]+); (\\S+) Build}', $userAgent, $m)) {
         $device['platform'] = 'Android';
         $device['version'] = $m[1];
         $device['model'] = $m[2];
     } elseif (preg_match('{ Darwin/}', $userAgent)) {
         $device['platform'] = 'iOS';
     }
     $outData = ['tz' => $user['timezone_name'], 'app' => $app, 'device' => $device, 'location' => ['uuid' => md5($username . $timestamp), 'coords' => ['latitude' => $inData->lat, 'longitude' => $inData->lon, 'speed' => $inData->speed, 'heading' => $inData->bearing, 'altitude' => $inData->altitude], 'battery' => ['level' => $inData->batt / 100], 'timestamp' => $timestamp]];
     if ($this->output->isVerbose()) {
         print json_encode($outData, JSON_PRETTY_PRINT) . "\n";
     }
     $observeResource = new ObserveResource($this->client);
     $response = $observeResource->postLocation($outData, $user['id']);
     if (isset($response['locations']) && $response['locations'] == 1) {
         $this->output->writeln('Point added');
         return true;
     }
     $this->output->writeln('<error>Error: ' . json_encode($response) . '</error>');
     return false;
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $isVerbose = $output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE;
     $username = $input->getOption('user');
     $date = $input->getOption('date');
     $output->writeln("Getting locations for user {$username} and {$date}");
     $userResource = new UserResource($this->sourceClient);
     $criteria = [];
     if ($date) {
         $criteria['date'] = $date;
     }
     if ($username) {
         $user = $userResource->getOneBy(['username' => $username]);
         if (!$user) {
             throw new \Exception("User {$username} not found");
         }
         $criteria['user_id'] = $user['id'];
     }
     $locationResource = new DailySummaryResource($this->sourceClient);
     $result = $locationResource->getList(1, 100, $criteria);
     $this->printResponse($input->getOption('format'), $result['items'], $output);
 }
Example #6
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $name = $input->getArgument('filename');
     $projectResource = new ProjectResource($this->client);
     $userResource = new UserResource($this->client);
     $memberResource = new MemberResource($this->client);
     $reader = new \EasyCSV\Reader('new_members.csv');
     while ($row = $reader->getRow()) {
         $project = $projectResource->getByCode($row['project_code']);
         // we ned that user for admins, maybe it should be separate field
         $user = $userResource->getOneBy(['username' => $row['username']]);
         if (!$user) {
             print "user '{$row['username']}' not found\n";
         }
         try {
             $res = $memberResource->save(['code' => $row['code'], 'project_id' => $project['id'], 'user_id' => $user['id'], 'permission_type_code' => $row['permission_type_code']]);
         } catch (Exception $e) {
             print "Error importing member {$row['code']}:" . $e->getMessage() . "\n";
         }
     }
     $output->writeln($text);
 }
 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>");
         }
     }
 }
Example #9
0
<?php

require __DIR__ . '/../vendor/autoload.php';
use Survos\Client\SurvosClient;
use Survos\Client\Resource\UserResource;
$config = json_decode(file_get_contents(__DIR__ . '/config.json'), true);
$client = new SurvosClient($config['endpoint']);
if (!$client->authorize($config['username'], $config['password'])) {
    throw new \Exception('Wrong credentials!');
}
//user save
$data = ['username' => 'john85210101', 'email' => '*****@*****.**', 'name' => 'Nick'];
$resource = new UserResource($client);
$item = $resource->save($data);
$id = $item['id'];
//user getById
$item = $resource->getById($id);
//user getList
$page = 1;
$maxPerPage = 10;
$criteria = ['id' => $id];
$response = $resource->getList($page, $maxPerPage, $criteria);
//user delete
$result = $resource->deleteById($id);