diff --git a/setup/cli/modules/user.php b/setup/cli/modules/user.php index 3e5602288bebae9876ed14975c398cdf3a65f779..aef84a2123e5ecea6b4d61841516b225d0fc2a60 100644 --- a/setup/cli/modules/user.php +++ b/setup/cli/modules/user.php @@ -19,6 +19,8 @@ class UserManager extends Module { var $options = array( 'file' => array('-f', '--file', 'metavar'=>'path', 'help' => 'File or stream to process'), + 'org' => array('-O', '--org', 'metavar'=>'ORGID', + 'help' => 'Set the organization ID on import'), ); var $stream; @@ -34,27 +36,19 @@ class UserManager extends Module { elseif (!($this->stream = fopen($options['file'], 'rb'))) $this->fail("Unable to open input file [{$options['file']}]"); - //Read the header (if any) - if (($data = fgetcsv($this->stream, 1000, ","))) { - if (Validator::is_email($data[1])) - fseek($this->stream, 0); // We don't have an header! - else; - // TODO: process the header here to figure out the columns - // for now we're assuming Name, Email + $extras = array(); + if ($options['org']) { + if (!($org = Organization::lookup($options['org']))) + $this->fail($options['org'].': Unknown organization ID'); + $extras['org_id'] = $options['org']; } - - while (($data = fgetcsv($this->stream, 1000, ",")) !== FALSE) { - if (!$data[0]) - $this->stderr->write('Invalid data or format: Name - required'); - elseif (!Validator::is_email($data[1])) - $this->stderr->write('Invalid data or format: Valid - email required'); - elseif (!User::fromVars(array('name' => $data[0], 'email' => $data[1]))) - $this->stderr->write('Unable to import user: '.print_r($data, true)); - } - + $status = User::importCsv($this->stream, $extras); + if (is_numeric($status)) + $this->stderr->write("Successfully imported $status clients\n"); + else + $this->fail($status); break; + case 'export': $stream = $options['file'] ?: 'php://stdout'; if (!($this->stream = fopen($stream, 'c')))