diff --git a/include/class.user.php b/include/class.user.php index b6d75dfe68e9993096c73637d6f814a710965e42..fc215f282b23bcba3819ecd5829b6bc53bf73ebe 100644 --- a/include/class.user.php +++ b/include/class.user.php @@ -15,6 +15,7 @@ vim: expandtab sw=4 ts=4 sts=4: **********************************************************************/ require_once(INCLUDE_DIR . 'class.orm.php'); +require_once(INCLUDE_DIR . 'class.organization.php'); class UserEmailModel extends VerySimpleModel { static $meta = array( diff --git a/setup/cli/modules/org.php b/setup/cli/modules/org.php new file mode 100644 index 0000000000000000000000000000000000000000..c47e5cd7d628d1638d922e1f28a09ac9cc7599a9 --- /dev/null +++ b/setup/cli/modules/org.php @@ -0,0 +1,72 @@ +<?php +require_once dirname(__file__) . "/class.module.php"; +require_once dirname(__file__) . "/../cli.inc.php"; + +class OrganizationManager extends Module { + var $prologue = 'CLI organization manager'; + + var $arguments = array( + 'action' => array( + 'help' => 'Action to be performed', + 'options' => array( + 'import' => 'Import organizations to the system', + 'export' => 'Export organizations from the system', + ), + ), + ); + + + var $options = array( + 'file' => array('-f', '--file', 'metavar'=>'path', + 'help' => 'File or stream to process'), + ); + + var $stream; + + function run($args, $options) { + + Bootstrap::connect(); + + switch ($args['action']) { + case 'import': + if (!$options['file']) + $this->fail('Import CSV file required!'); + 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 (strcasecmp($data[0], 'name')) + 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 one column of Name + } + + while (($data = fgetcsv($this->stream, 1000, ",")) !== FALSE) { + if (!$data[0]) + $this->stderr->write('Invalid data format: Name + required'); + elseif (!Organization::fromVars(array('name' => $data[0], 'email'))) + $this->stderr->write('Unable to import record: '.print_r($data, true)); + } + + break; + case 'export': + $stream = $options['file'] ?: 'php://stdout'; + if (!($this->stream = fopen($stream, 'c'))) + $this->fail("Unable to open output file [{$options['file']}]"); + + fputcsv($this->stream, array('Name')); + foreach (Organization::objects() as $org) + fputcsv($this->stream, + array((string) $org->getName())); + break; + default: + $this->stderr->write('Unknown action!'); + } + @fclose($this->stream); + } +} +Module::register('org', 'OrganizationManager'); +?>