Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions core/Api/exportCsvApi.php
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
Copy link
Member

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URLs resolved

*
*/
public function export_list($query = array())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this code into UserApi.php and drop this file
also rename it to export_csv, as we are going to delete the following function with the same name

Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drop this function in favor of sample_csv

Copy link
Author

Choose a reason for hiding this comment

The 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);
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. use project standard format for url
    @url GET users/sample/csv

Copy link
Author

Choose a reason for hiding this comment

The 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';
}


}