-
Notifications
You must be signed in to change notification settings - Fork 16
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
Export Users CSV Api #9
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,128 @@ | ||
<?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 ExportCsvApi extends Api | ||
{ | ||
|
||
|
||
|
||
/** | ||
* Export Csv | ||
* | ||
* @url GET /export/users | ||
* | ||
*/ | ||
public function export_list($query = array()) | ||
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 code into UserApi.php and drop this file 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 |
||
{ | ||
|
||
$this->_authorize('user_list'); | ||
$oUser = User::search((array)$query); | ||
if ($oUser) { | ||
|
||
$file_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'users.csv'; | ||
|
||
$handle = fopen($file_path, 'w'); | ||
if (!$handle) { | ||
throw new CoreException(500, "Unable to open file"); | ||
} | ||
|
||
foreach($oUser as $aValue) { | ||
$contact_row = '"'.$aValue['user_id'].'","'.$aValue['first_name'].'","'.$aValue['last_name'].'","'.$aValue['phone'].'","'.$aValue['email'].'",'. | ||
'"'.$aValue['address'].'","'.$aValue['company'].'","'.$aValue['country_id'].'","'.$aValue['language_id'].'",'. | ||
'"'.$aValue['timezone_id'].'"'."\n"; | ||
fwrite($handle, $contact_row); | ||
} | ||
|
||
fclose($handle); | ||
|
||
return new SplFileInfo($file_path); | ||
} else { | ||
throw new CoreException(404, "User not found"); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Export User by User id | ||
* | ||
* @url GET /export/user/$user_id | ||
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. drop this function in favor of sample_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. Dropped |
||
* | ||
*/ | ||
public function export_csv($user_id) | ||
{ | ||
$this->_authorize('user_read'); | ||
if ($user_id == 'sample') { | ||
return $this->sample_csv(); | ||
} | ||
|
||
$oUser=array(); | ||
|
||
$oUser = new User($user_id); | ||
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 export single user record to csv, if needed it can be filtered via above created export_csv function, see the search filter. 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 |
||
|
||
|
||
if ($oUser) { | ||
|
||
$file_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'user'.$user_id.'.csv'; | ||
|
||
$handle = fopen($file_path, 'w'); | ||
if (!$handle) { | ||
throw new CoreException(500, "Unable to open file"); | ||
} | ||
|
||
$oUser=(array)$oUser; | ||
$contact_row = '"'.$oUser['user_id'].'","'.$oUser['first_name'].'","'.$oUser['last_name'].'","'.$oUser['phone'].'","'.$oUser['email'].'",'. | ||
'"'.$oUser['address'].'","'.$oUser['company'].'","'.$oUser['country_id'].'","'.$oUser['language_id'].'",'. | ||
'"'.$oUser['timezone_id'].'"'."\n"; | ||
fwrite($handle, $contact_row); | ||
|
||
fclose($handle); | ||
|
||
return new SplFileInfo($file_path); | ||
} else { | ||
throw new CoreException(404, "User not found"); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Provide User Sample | ||
* | ||
* @url GET /export/users/sample | ||
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.
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. URLs resolved |
||
*/ | ||
public function sample_csv() | ||
{ | ||
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'; | ||
} | ||
|
||
|
||
} |
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.
Follow project standard for url, note the ending s in users and /csv suffix for file format
@url GET /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.
URLs resolved