-
Notifications
You must be signed in to change notification settings - Fork 23
Import Users CSV API #10
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 all commits
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 |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| <?php | ||
|
|
||
| namespace ICT\Core\Api; | ||
|
|
||
| /* * *************************************************************** | ||
| * Copyright © 2016 ICT Innovations Pakistan All Rights Reserved * | ||
| * Developed By: Nasir Iqbal * | ||
| * Website : http://www.ictinnovations.com/ * | ||
| * Mail : [email protected] * | ||
| * *************************************************************** */ | ||
|
|
||
| use ICT\Core\Account; | ||
| use ICT\Core\Api; | ||
| use ICT\Core\CoreException; | ||
| use ICT\Core\User; | ||
| use ICT\Core\User\Permission; | ||
| use SplFileInfo; | ||
|
|
||
| class ImportCsvApi extends Api | ||
| { | ||
|
|
||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Import Users | ||
| * | ||
| * @url POST /import/users | ||
| * | ||
| */ | ||
| public function import_csv($data = array(), $mime = 'text/csv') | ||
|
Member
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. Move this function into UserApi.php and drop this file.
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. Moved to UserApi.php |
||
| { | ||
| global $path_root, $path_cache; | ||
| $newUsers=$errors=array(); | ||
| $allowedTypes = array('csv' => 'text/csv', 'txt' => 'text/plain'); | ||
| if (in_array($mime, $allowedTypes)) { | ||
| if (!empty($data)) { | ||
| $file_path = $path_cache . DIRECTORY_SEPARATOR . 'users.csv'; | ||
| file_put_contents($file_path, $data); | ||
| if (file_exists($file_path)) { | ||
| $csvFile = fopen($file_path, 'r'); | ||
|
|
||
| // Skip the first line | ||
| fgetcsv($csvFile); | ||
| $line_no=0; | ||
| while(($line = fgetcsv($csvFile)) !== FALSE){ | ||
| if($line[4]!=""){ | ||
| $line_no++; | ||
| // Get row data | ||
|
|
||
| $data=array( | ||
| 'first_name'=>$line[1], | ||
| 'last_name'=>$line[2], | ||
| 'phone'=>$line[3], | ||
| 'email'=>$line[4], | ||
| 'address'=>$line[5], | ||
| 'company'=>(int)$line[6], | ||
| 'country_id'=>(int)$line[7], | ||
| 'language_id'=>(int)$line[8], | ||
| 'timezone_id'=>(int)$line[9] | ||
| ); | ||
|
|
||
| $oUser = new User(); | ||
| $this->set($oUser, $data); | ||
|
|
||
| if ($oUser->save()) { | ||
| array_push($newUsers,$oUser->user_id); | ||
| } else { | ||
| array_push($errors, $line_no); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
| fclose($csvFile); | ||
|
|
||
| if(empty($errors)){ | ||
| return "User Ids: ".json_encode($newUsers); | ||
|
Member
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. Instead of array of IDs, returning the count is sufficient .
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. Done |
||
| } | ||
| else{ | ||
| throw new CoreException(415, "Rocord(s) at following line(s) not inserted:".json_encode($errors)); | ||
| } | ||
| } | ||
| else{ | ||
| throw new CoreException(404, "File not found"); | ||
| } | ||
|
|
||
| } else { | ||
| throw new CoreException(411, "Empty file"); | ||
| } | ||
| } else { | ||
| throw new CoreException(415, "Unsupported File Type"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Provide Contact Sample | ||
| * | ||
| * @url GET /import/users/sample | ||
| */ | ||
| public function sample_csv() | ||
|
Member
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. no need to repeat this sample
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. Removed |
||
| { | ||
| global $path_data; | ||
| $sample_contact = $path_data . DIRECTORY_SEPARATOR . 'users_sample.csv'; | ||
| if (file_exists($sample_contact)) { | ||
| return new SplFileInfo($sample_contact); | ||
| } else { | ||
| throw new CoreException(404, "File not found"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| protected static function rest_include() | ||
| { | ||
| return 'Api/User'; | ||
| } | ||
|
|
||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.