public function action_index() { define('JOB_STATUS_COLUMN', 'data.44'); if (!rand(0, 1000)) { Database_Mongo::collection('search')->remove(array('expires' => array('$lt' => time()))); } if ($_POST) { $data = array('request' => $_POST, 'expires' => time() + 3600); Database_Mongo::collection('search')->insert($data); $search_id = strval($data['_id']); Session::instance()->set('search-settings', $search_id); header('Content-type: application/json'); die(json_encode(array('success' => true, 'id' => $search_id))); } elseif (!$_GET && Session::instance()->get('search-settings')) { $params = Session::instance()->get('search-settings'); if (!is_array($params)) { $params = json_decode($params); } if ($params !== NULL) { $data = array('request' => $params, 'expires' => time() + 3600); Database_Mongo::collection('search')->insert($data); $search_id = strval($data['_id']); Session::instance()->set('search-settings', $search_id); } else { $search_id = Session::instance()->get('search-settings'); } if ($search_id) { $this->redirect('search?id=' . $search_id); } } elseif (isset($_GET['clear'])) { Session::instance()->delete('search-settings'); $_GET = array(); } elseif (isset($_GET['id'])) { $data = Database_Mongo::collection('search')->findOne(array('_id' => new MongoId($_GET['id']))); if ($data) { Session::instance()->set('search-settings', $_GET['id']); $_GET = $data['request']; } else { Session::instance()->delete('search-settings'); } } $actions = array('contain', 'does not contain', '=', '<', '<=', '>', '>=', '<>', 'empty', 'not empty'); $actions_mongo = array('=' => '$eq', '<' => '$lt', '<=' => '$lte', '>' => '$gt', '>=' => '$gte', '<>' => '$ne'); $columns = Columns::get_visible(); $reports = Columns::get_static(); $regs = DB::select('region_id')->from('user_regions')->where('user_id', '=', User::current('id'))->execute()->as_array('region_id', 'region_id'); $regions = DB::select('id', 'name')->from('regions'); if ($regs) { $regions->where('id', 'IN', $regs); } $regions = $regions->execute()->as_array('id', 'name'); $query = array(); $list_query = array(); if (Arr::get($_GET, 'submissions')) { $result = Database_Mongo::collection('submissions')->aggregate(array(array('$match' => array('active' => 1)), array('$group' => array('_id' => '$job_key')))); $result = array_column($result['result'], '_id'); $query['_id'] = array('$in' => $result); } if (Group::current('show_all_jobs')) { if (Arr::get($_GET, 'company')) { $companies = explode(',', $_GET['company']); if (count($companies) > 1) { $companies = array('$in' => array_map("intval", $companies)); } else { $companies = intval($_GET['company']); } if (Arr::get($_GET, 'ex')) { $list_query['$or'] = $query['$or'] = array(array('companies' => $companies), array('ex' => $companies)); } else { $list_query['companies'] = $query['companies'] = $companies; } } } else { if (Arr::get($_GET, 'status', -1) == 0) { $list_query['ex'] = $query['ex'] = intval(User::current('company_id')); } else { $list_query['companies'] = $query['companies'] = intval(User::current('company_id')); } } if (Group::current('allow_assign')) { foreach (Columns::$settings as $key => $value) { if (isset($_GET['settings'][$key])) { if ($_GET['settings'][$key]) { $query[$key] = array('$exists' => 1); } else { $query[$key] = array('$exists' => 0); } } } } if (Arr::get($_GET, 'ticket')) { $tickets = explode(',', $_GET['ticket']); $q = array(); foreach ($tickets as $ticket) { $ticket = preg_replace('/[^a-z0-9]/i', '', strval($ticket)); if (!$ticket) { continue; } if (preg_match('/^T1W[0-9]{12}$/', $ticket)) { $q[] = $ticket; } else { $q[] = new MongoRegex('/.*' . $ticket . '.*/i'); } } if (count($q) > 1) { $query['_id'] = array('$in' => $q); } elseif ($q) { $query['_id'] = $q[0]; } } if (Arr::get($_GET, 'start')) { $query['last_update']['$gt'] = Columns::parse($_GET['start'], 'date'); } if (Arr::get($_GET, 'end')) { $query['last_update']['$lt'] = Columns::parse($_GET['end'], 'date'); } if (Arr::get($_GET, 'submit-start')) { $query['last_submit']['$gt'] = Columns::parse($_GET['submit-start'], 'date'); } if (Arr::get($_GET, 'submit-end')) { $query['last_submit']['$lt'] = Columns::parse($_GET['submit-end'], 'date'); } $status = Arr::get($_GET, 'status', Group::current('allow_assign') ? -1 : Enums::STATUS_ALLOC); $_GET['status'] = intval($status); if ($status != -1 && (Group::current('show_all_jobs') || $status)) { $query['status'] = intval($status) ?: array('$exists' => 0); } if (!isset($_GET['region'])) { $_GET['region'] = User::current('default_region'); } if (Arr::get($_GET, 'region') && (!$regs || isset($regs[$_GET['region']]))) { $list_query['region'] = $query['region'] = strval($_GET['region']); } elseif ($regs) { $list_query['region'] = $query['region'] = array('$in' => array_values($regs)); } if (Arr::get($_GET, 'type')) { $query['assigned.' . $_GET['type']] = array('$exists' => 1); } foreach (Arr::get($_GET, 'columns', array()) as $id => $column) { if ($column) { $op = Arr::get($actions, Arr::path($_GET, 'actions.' . $id), 0); $value = Arr::path($_GET, 'values.' . $id, ''); $value = Columns::parse($value, Columns::get_type($column)); if (substr(Columns::get_type($column), 0, 4) == 'enum' && Enums::is_multi(substr(Columns::get_type($column), 5)) && $op == '=' && $value) { $op = 'contain'; } if (Columns::get_type($column) == 'date') { if ($op === 'contain') { $op = '='; } elseif ($op === 'does not contain') { $op = '<>'; } elseif ($op == '<=') { $value += 86399; } if ($op == '=' && date('His', $value) === '000000') { $query['data.' . $column]['$lte'] = $value + 86399; $op = '>='; } } if ($op === 'contain') { $op = '$eq'; $value = new MongoRegex('/.*' . preg_replace('/[^a-z0-9,.+:;!? -]/i', '', $value) . '.*/i'); } elseif ($op === 'does not contain') { $op = '$not'; $value = new MongoRegex('/.*' . preg_replace('/[^a-z0-9,.+:;!? -]/i', '', $value) . '.*/i'); } elseif ($op === 'empty') { $op = '$in'; $value = array(NULL, '', 0); } elseif ($op === 'not empty') { $op = '$nin'; $value = array(NULL, '', 0); } else { $op = Arr::get($actions_mongo, $op, '$eq'); } if ($op == '$eq' && !$value) { $value = NULL; } if (isset($query['data.' . $column]) && $op == '$eq') { if (isset($query['data.' . $column]['$in'])) { $query['data.' . $column]['$in'][] = $value; } elseif (isset($query['data.' . $column]['$eq'])) { $query['data.' . $column] = array('$in' => array($query['data.' . $column]['$eq'], $value)); } else { $query['data.' . $column]['$in'] = array($value); } } else { $query['data.' . $column][$op] = $value; } } } foreach ($query as $key => $ops) { if (substr($key, 0, 5) == 'data.' && count($ops) == 1 && key($ops) == '$eq') { $query[$key] = array_shift($ops); } } $jobs = Database_Mongo::collection('jobs'); $list_values = array(); foreach (Columns::get_search() as $key => $value) { if ($value == 2) { $list_values[$key] = $jobs->distinct('data.' . $key, $query ?: NULL) ?: array(); if (substr(Columns::get_type($key), 0, 4) == 'enum') { $list = array(); foreach ($list_values[$key] as $values) { if ($values) { $values = explode(', ', $values); foreach ($values as $value) { $list[$value] = 1; } } else { $list[''] = 1; } } $list_values[$key] = array_keys($list); } if ($list_values[$key]) { sort($list_values[$key]); } } } Pager::$count = $jobs->count($query); $show = array('region' => 1, 'status' => 1, 'created' => 1, 'last_update' => 1, 'last_submit' => 1, 'assigned' => 1, 'companies' => 1, 'ex' => 1); foreach (Columns::$settings as $key => $value) { $show[$key] = 1; } foreach (Columns::get_search() as $column => $type) { $show['data.' . $column] = 1; } $result = $jobs->find($query, $show); $sort = Arr::get($_GET, 'sort'); if (is_array($sort)) { $save = implode('|', $sort); DB::update('users')->set(array('default_sort' => $save))->where('id', '=', User::current('id'))->execute(); } else { $sort = explode('|', User::current('default_sort') ?: '-update'); $_GET['sort'] = $sort; } $sorting = array(); foreach ($sort as $order) { $dir = substr($order, 0, 1) == '-' ? -1 : 1; $order = substr($order, 1); switch ($order) { case 'id': $order = '_id'; break; case 'update': $order = 'last_update'; break; case 'submit': $order = 'last_submit'; break; case 'status': $order = 'status'; break; case 'data-8': $sorting['address'] = $dir; $order = false; break; default: if (substr($order, 0, 5) == 'data-') { $order = 'data.' . intval(substr($order, 5)); } else { $order = false; } } if ($order) { $sorting[$order] = $dir; } } $result->sort($sorting)->limit(Pager::limit())->skip(Pager::offset()); $tickets = array(); while ($row = $result->next()) { $tickets[] = $row; } $ids = array_column($tickets, '_id'); $types = DB::select('id', 'name')->from('job_types')->execute()->as_array('id', 'name'); $companies = DB::select('id', 'name')->from('companies')->execute()->as_array('id', 'name'); if ($ids) { $attachments = DB::select('job_id', DB::expr('COUNT(*) as cnt'))->from('attachments')->where('job_id', 'IN', $ids)->and_where('uploaded', '>', 0)->and_where('folder', '<>', 'Signatures')->group_by('job_id')->execute()->as_array('job_id', 'cnt'); } else { $attachments = array(); } if (Group::current('allow_assign')) { $result = Database_Mongo::collection('submissions')->aggregate(array(array('$match' => array('job_key' => array('$in' => $ids), 'active' => 1)), array('$group' => array('_id' => '$job_key', 'count' => array('$sum' => 1))))); $submissions = array(); foreach (Arr::get($result, 'result', array()) as $value) { $submissions[$value['_id']] = $value['count']; } } $result = Database_Mongo::collection('forms')->find(array(), array('_id' => 1, 'type' => 1, 'name' => 1)); $forms = array(); foreach ($result as $form) { $forms[$form['type']][strval($form['_id'])] = $form['name']; } $view = View::factory('Jobs/Search')->bind('reports', $reports)->bind('regions', $regions)->bind('columns', $columns)->bind('actions', $actions)->bind('tickets', $tickets)->bind('submissions', $submissions)->bind('attachments', $attachments)->bind('list_values', $list_values)->bind('types', $types)->bind('sort', $sort)->bind('companies', $companies)->bind('forms', $forms)->bind('query', $query); $this->response->body($view); }
public function action_index() { $ids = array_keys(Arr::get($_POST, 'job', array())); if (!$ids) { throw new HTTP_Exception_404('Not found'); } $regs = DB::select('region_id')->from('user_regions')->where('user_id', '=', User::current('id'))->execute()->as_array('region_id', 'region_id'); $query = array(); if ($regs) { $query['region'] = array('$in' => array_values($regs)); } if (!Group::current('show_all_jobs')) { $query['$or'] = array(array('ex' => intval(User::current('company_id'))), array('companies' => intval(User::current('company_id')))); } $query['_id'] = array('$in' => $ids); $jobs = Database_Mongo::collection('jobs')->find($query); $static = array_flip(explode(',', Group::current('columns'))); $header = array('Ticket ID'); $types = DB::select('id', 'name')->from('job_types')->execute()->as_array('id', 'name'); $companies = DB::select('id', 'name')->from('companies')->execute()->as_array('id', 'name'); if ($ids) { $attachments = DB::select('job_id', DB::expr('COUNT(*) as cnt'))->from('attachments')->where('job_id', 'IN', $ids)->and_where('uploaded', '>', 0)->group_by('job_id')->execute()->as_array('job_id', 'cnt'); } else { $attachments = array(); } if (Group::current('allow_assign')) { $result = Database_Mongo::collection('submissions')->aggregate(array(array('$match' => array('job_key' => array('$in' => $ids), 'active' => 1)), array('$group' => array('_id' => '$job_key', 'count' => array('$sum' => 1))))); $submissions = array(); foreach (Arr::get($result, 'result', array()) as $value) { $submissions[$value['_id']] = $value['count']; } } if (isset($static['last_update'])) { $header[] = 'Last update'; } if (isset($static['last_submit'])) { $header[] = 'Last submit'; } if (isset($static['status']) && Group::current('show_all_jobs')) { $header[] = 'Job status'; } if (isset($static['types'])) { $header[] = 'Assigned works'; } if (isset($static['companies'])) { $header[] = 'Assigned companies'; } if (isset($static['pending'])) { $header[] = 'Pending submissions'; } if (isset($static['attachments'])) { $header[] = 'Attachments'; } foreach (Columns::get_search() as $id => $type) { $header[] = Columns::get_name($id); } $list = array(); $request = array(); foreach ($jobs as $job) { if (!isset($job['lat'])) { $request[$job['_id']] = Arr::path($job, 'data.8'); } } if ($request) { $coords = MapQuest::locate($request); foreach ($coords as $key => $value) { Database_Mongo::collection('jobs')->update(array('_id' => $key), array('$set' => array('lng' => $value['lng'], 'lat' => $value['lat']))); } } else { $coords = array(); } foreach ($jobs as $ticket) { if (isset($coords[$ticket['_id']])) { $ticket['lng'] = $coords[$ticket['_id']]['lng']; $ticket['lat'] = $coords[$ticket['_id']]['lat']; } if (!isset($ticket['lat'])) { continue; } $row = array($ticket['_id']); if (isset($static['last_update'])) { $row[] = date('d-m-Y H:i', Arr::get($ticket, 'last_update', $ticket['created'])); } if (isset($static['last_submit'])) { $row[] = Arr::get($ticket, 'last_submit') ? date('d-m-Y H:i', $ticket['last_submit']) : ''; } if (isset($static['status']) && Group::current('show_all_jobs')) { $row[] = Arr::get(Enums::$statuses, Arr::get($ticket, 'status', 0), 'Unknown'); } if (isset($static['types'])) { if (Group::current('allow_assign')) { $row[] = implode(', ', array_intersect_key($types, Arr::get($ticket, 'assigned', array()))); } else { $row[] = implode(', ', array_intersect_key($types, array_filter(Arr::get($ticket, 'assigned', array()), function ($x) { return $x == User::current('company_id'); }))); } } if (isset($static['companies'])) { $row[] = implode(', ', array_intersect_key($companies, array_flip(Arr::get($ticket, 'assigned', array())))); } if (isset($static['pending'])) { $row[] = Arr::get($submissions, $ticket['_id']); } if (isset($static['attachments'])) { $row[] = Arr::get($attachments, $ticket['_id']); } foreach (Columns::get_search() as $id => $type) { $row[] = Arr::path($ticket, array('data', $id)) ? Columns::output($ticket['data'][$id], Columns::get_type($id), true) : ''; } $row = array_combine($header, $row); $row['lat'] = $ticket['lat']; $row['lng'] = $ticket['lng']; $list[] = $row; } if (!$list) { throw new HTTP_Exception_404('Not found'); } DB::delete('maps')->where('expire', '<', time())->execute(); $ids = DB::select('map_id')->distinct(true)->from('maps')->execute()->as_array('map_id', 'map_id'); do { $id = Text::random('alnum', 32); } while (isset($ids[$id])); $expire = time() + 60 * 60 * 24 * 7; $query = DB::insert('maps', array('map_id', 'job_key', 'lng', 'lat', 'info', 'expire')); foreach ($list as $values) { $lat = $values['lat']; $lng = $values['lng']; unset($values['lng']); unset($values['lat']); $query->values(array($id, $values['Ticket ID'], $lng, $lat, json_encode($values), $expire)); } $query->execute(); $this->redirect('/map.html#' . $id); }
<h4 class="modal-title">Ticket details</h4> </div> <div class="modal-body" id="table-row-details"> <table class="table small"> <tr class="text-center tr-header"> <th>Ticket ID</th> <?php if (isset($columns['last_update'])):?><th class="hidden-sm hidden-xs">Last update</th><?php endif;?> <?php if (isset($columns['last_submit'])):?><th class="hidden-sm hidden-xs"> Last submit</th><?php endif;?> <?php if (isset($columns['status']) && Group::current('show_all_jobs') && $_GET['status'] == -1):?><th class="hidden-sm hidden-xs">Job status</th><?php endif;?> <?php if (isset($columns['types'])):?><th class="hidden-xs">Assigned works</th><?php endif;?> <?php if (isset($columns['companies'])):?><th class="hidden-sm hidden-xs">Assigned companies</th><?php endif;?> <?php if (isset($columns['ex'])):?><th class="hidden-sm hidden-xs">Previously assigned companies</th><?php endif;?> <?php if (isset($columns['settings'])):?><th class="hidden-sm hidden-xs">Settings</th><?php endif;?> <?php if (isset($columns['pending'])):?><th class="hidden-sm hidden-xs">Pending submissions</th><?php endif;?> <?php if (isset($columns['attachments'])):?><th class="hidden-xs">Attachments</th><?php endif;?> <?php foreach (Columns::get_search() as $id => $type):?> <th><?=Columns::get_name($id)?></th> <?php endforeach;?> </tr> <tr class="text-center tr-body"></tr> </table> </div> <div class="modal-footer" id="tableRowButtons"> </div> </div> </div> </div> <div class="modal fade" id="batchEditModal" tabindex="-1" role="dialog"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-content">
public function action_index() { $action = $this->request->param('id'); $ids = array_keys(Arr::get($_POST, 'job', array())); if (!$ids) { throw new HTTP_Exception_404('Not found'); } $regs = DB::select('region_id')->from('user_regions')->where('user_id', '=', User::current('id'))->execute()->as_array('region_id', 'region_id'); $query = array(); if ($regs) { $query['region'] = array('$in' => array_values($regs)); } if (!Group::current('show_all_jobs')) { $query['$or'] = array(array('ex' => intval(User::current('company_id'))), array('companies' => intval(User::current('company_id')))); } $query['_id'] = array('$in' => $ids); $jobs = Database_Mongo::collection('jobs')->find($query); $static = array_flip(explode(',', Group::current('columns'))); $header = array('Ticket ID'); $types = DB::select('id', 'name')->from('job_types')->execute()->as_array('id', 'name'); $companies = DB::select('id', 'name')->from('companies')->execute()->as_array('id', 'name'); if ($action == 'excel') { $result = DB::select()->from('attachments')->where('job_id', 'IN', $ids)->and_where('uploaded', '>', 0)->and_where('folder', '<>', 'Signatures')->execute()->as_array(); $attachments = array(); $attachments_list = array(); foreach ($result as $row) { $attachments[$row['job_id']] = Arr::get($attachments, $row['job_id'], 0) + 1; if (preg_match('/^image\\/.*$/i', $row['mime'])) { $attachments_list[$row['job_id']][] = $row['id']; } } } elseif ($ids && $static['attachments']) { $attachments = DB::select('job_id', DB::expr('COUNT(*) as cnt'))->from('attachments')->where('job_id', 'IN', $ids)->and_where('uploaded', '>', 0)->and_where('folder', '<>', 'Signatures')->group_by('job_id')->execute()->as_array('job_id', 'cnt'); } else { $attachments = array(); } if (Group::current('allow_assign')) { $result = Database_Mongo::collection('submissions')->aggregate(array(array('$match' => array('job_key' => array('$in' => $ids), 'active' => 1)), array('$group' => array('_id' => '$job_key', 'count' => array('$sum' => 1))))); $submissions = array(); foreach (Arr::get($result, 'result', array()) as $value) { $submissions[$value['_id']] = $value['count']; } } if (isset($static['last_update'])) { $header[] = 'Last update'; } if (isset($static['last_submit'])) { $header[] = 'Last submit'; } if (isset($static['status']) && Group::current('show_all_jobs')) { $header[] = 'Job status'; } if (isset($static['types'])) { $header[] = 'Assigned works'; } if (isset($static['companies'])) { $header[] = 'Assigned companies'; } if (isset($static['ex'])) { $header[] = 'Previously assigned companies'; } if (isset($static['pending'])) { $header[] = 'Pending submissions'; } if (isset($static['attachments'])) { $header[] = 'Attachments'; } foreach (Columns::get_search() as $id => $type) { $header[] = Columns::get_name($id); } if ($action == 'excel') { $excel = new PHPExcel(); $sheet = $excel->getActiveSheet(); $sheet->setTitle('Search Results'); $sheet->fromArray($header, NULL, 'A1'); $i = 1; foreach (range('A', $sheet->getHighestDataColumn()) as $col) { $sheet->getColumnDimension($col)->setAutoSize(true); } $x = $attachments_list ? max(array_map('count', $attachments_list)) : 0; foreach (range(0, $x - 1) as $col) { $sheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex(count($header) + $col))->setWidth(14); } } else { $file = tmpfile(); fputcsv($file, $header); } foreach ($jobs as $ticket) { $row = array($ticket['_id']); if (isset($static['last_update'])) { $row[] = date('d-m-Y H:i', Arr::get($ticket, 'last_update', $ticket['created'])); } if (isset($static['last_submit'])) { $row[] = Arr::get($ticket, 'last_submit') ? date('d-m-Y H:i', $ticket['last_submit']) : ''; } if (isset($static['status']) && Group::current('show_all_jobs')) { $row[] = Arr::get(Enums::$statuses, Arr::get($ticket, 'status', 0), 'Unknown'); } if (isset($static['types'])) { if (Group::current('allow_assign')) { $row[] = implode(', ', array_intersect_key($types, Arr::get($ticket, 'assigned', array()))); } else { $row[] = implode(', ', array_intersect_key($types, array_filter(Arr::get($ticket, 'assigned', array()), function ($x) { return $x == User::current('company_id'); }))); } } if (isset($static['companies'])) { $row[] = implode(', ', array_intersect_key($companies, array_flip(Arr::get($ticket, 'assigned', array())))); } if (isset($static['ex'])) { $row[] = implode(', ', array_intersect_key($companies, array_flip(Arr::get($ticket, 'ex', array())))); } if (isset($static['pending'])) { $row[] = Arr::get($submissions, $ticket['_id']); } if (isset($static['attachments'])) { $row[] = Arr::get($attachments, $ticket['_id']); } foreach (Columns::get_search() as $id => $type) { $row[] = Arr::path($ticket, array('data', $id)) ? Columns::output($ticket['data'][$id], Columns::get_type($id), true) : ''; } if ($action == 'excel') { $i++; $sheet->fromArray($row, NULL, 'A' . $i); $x = count($row); if (isset($attachments_list[$ticket['_id']])) { $sheet->getRowDimension($i)->setRowHeight(80); } foreach (Arr::get($attachments_list, $ticket['_id'], array()) as $image) { if (!file_exists(DOCROOT . 'storage/' . $image . '.thumb')) { if (!file_exists(DOCROOT . 'storage/' . $image)) { continue; } $data = file_get_contents(DOCROOT . 'storage/' . $image); $er = error_reporting(0); $img = imagecreatefromstring($data); error_reporting($er); if (!$img) { continue; } $x = imagesx($img); $y = imagesy($img); $size = max($x, $y); $x = round($x / $size * 96); $y = round($y / $size * 96); $thumb = imagecreatetruecolor($x, $y); imagealphablending($thumb, false); imagesavealpha($thumb, true); imagecopyresampled($thumb, $img, 0, 0, 0, 0, $x, $y, imagesx($img), imagesy($img)); imagepng($thumb, DOCROOT . 'storage/' . $image . '.thumb', 9); } $data = file_get_contents(DOCROOT . 'storage/' . $image . '.thumb'); $img = imagecreatefromstring($data); $objDrawing = new PHPExcel_Worksheet_MemoryDrawing(); $objDrawing->setImageResource($img); $coord = PHPExcel_Cell::stringFromColumnIndex($x++); $objDrawing->setCoordinates($coord . $i); $objDrawing->setOffsetY(5); $objDrawing->setResizeProportional(true); $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG); $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT); $objDrawing->setWorksheet($sheet); //$sheet->getCell($coord . $i)->setHyperlink(new PHPExcel_Cell_Hyperlink(URL::site('download/attachment/' . $image, 'http'), 'Show Image')); } } else { fputcsv($file, $row); } } if ($action == 'excel') { $name = tempnam(sys_get_temp_dir(), 'excel'); header('Content-type: application/xlsx'); header('Content-disposition: filename="SearchResults.xlsx"'); $writer = new PHPExcel_Writer_Excel2007($excel); $writer->save($name); readfile($name); unlink($name); } else { fseek($file, 0); header('Content-type: text/csv'); header('Content-disposition: filename="SearchResults.csv"'); fpassthru($file); fclose($file); } die; }