-
Notifications
You must be signed in to change notification settings - Fork 23
Add import and export endpoints #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -258,4 +258,52 @@ public function account_list($user_id, $query = array()) | |
| $filter['created_by'] = $user_id; | ||
| return Account::search($filter); | ||
| } | ||
|
|
||
| /** | ||
| * Import new users from into DB | ||
| * | ||
| * @url GET /users/csv | ||
| */ | ||
| public function import_csv($data = array(), $mime = 'text/csv') | ||
| { | ||
| $this->_authorize('user_create'); | ||
| $csv_data = explode(PHP_EOL, $data); | ||
| $temp = array_pop($csv_data); | ||
|
|
||
| // skip header row | ||
| $header_csv = str_getcsv($csv_data[0], ","); | ||
| foreach(array_slice($csv_data, 1) as $row) { | ||
| $row_csv = str_getcsv($row, ","); | ||
| $user_data = array_combine($header_csv, $row_csv); | ||
|
|
||
| $oUser = new User(); | ||
| $this->set($oUser, $user_data); | ||
| $oUser->__set('active', intval($user_data['active'])); | ||
| if (!$oUser->save()) { | ||
| throw new CoreException(417, 'Could not save User'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Export all users form DB | ||
| * | ||
| * @url POST /users/csv | ||
|
||
| */ | ||
| public function export_csv() | ||
| { | ||
| $fields = array('username', 'passwd', 'first_name', 'last_name', 'email', 'address', 'company', 'active'); | ||
| $query = 'SELECT ' . implode(", ", $fields) . ' FROM usr'; | ||
| $result = DB::query('usr', $query); | ||
| $filepath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'users.csv'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use random file name,
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure what you mean by this. Could you please elaborate? thanks |
||
| $handle = fopen($filepath, 'w'); | ||
| $head = implode(",", $fields) . "\n"; | ||
| fwrite($handle, $head); | ||
| while ($row = mysql_fetch_array($result, MYSQL_NUM)) { | ||
| $csv = implode(",",$row) . "\n"; | ||
| fwrite($handle, $csv); | ||
| } | ||
| fclose($handle); | ||
| return new SplFileInfo($filepath); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it not supposed to be a POST API?
@url POST /users/csv
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Update on latest commit.