public static function appendLines(myCsvWrapper $csv, $table_data) { foreach ($table_data as $row) { $csv->addNewLine($row); } return $csv; }
/** * will store the content of the report on disk and return the Url for the file * * @param string $partner_id * @param string $report_title * @param string $report_text * @param string $headers * @param int $report_type * @param reportsInputFilter $input_filter * @param string $dimension * @param string $object_ids * @param int $page_size * @param int $page_index * @param string $order_by */ public static function getUrlForReportAsCsv($partner_id, $report_title, $report_text, $headers, $report_type, reportsInputFilter $input_filter, $dimension = null, $object_ids = null, $page_size = 10, $page_index = 0, $order_by) { // create file_name // TODO - check if file already exists - if so - serve it if not expired list($file_path, $file_name) = self::createFileName($partner_id, $report_type, $input_filter, $dimension, $object_ids, $page_size, $page_index, $order_by); // TODO - remove comment and read from disk /* if ( file_exists ( $file_path ) ) { return $url; } */ $arr = self::getGraph($partner_id, $report_type, $input_filter, $dimension, $object_ids); list($total_header, $total_data) = self::getTotal($partner_id, $report_type, $input_filter, $object_ids); $csv = new myCsvWrapper(); if ($page_size < self::REPORTS_TABLE_RESULTS_SINGLE_ITERATION_SIZE) { list($table_header, $table_data, $table_total_count) = self::getTable($partner_id, $report_type, $input_filter, $page_size, $page_index, $order_by, $object_ids); if ($input_filter instanceof endUserReportsInputFilter) { $table_total_count = self::getTotalTableCount($partner_id, $report_type, $input_filter, $page_size, $page_index, $order_by, $object_ids); } $csv = myCsvReport::createReport($report_title, $report_text, $headers, $report_type, $input_filter, $dimension, $arr, $total_header, $total_data, $table_header, $table_data, $table_total_count, $csv); $data = $csv->getData(); // return URLwq if (!file_exists(dirname($file_path))) { kFile::fullMkfileDir(dirname($file_path), 0777); } //adding BOM for fixing problem in open .csv file with special chars using excel. $BOM = ""; file_put_contents($file_path, $BOM . $data); } else { $tempCsv = new myCsvWrapper(); if (!$page_size || $page_size < 0) { $page_size = 10; } if (!$page_index || $page_index < 1) { $page_index = 1; } //checking if query is too big $table_amount = self::getTotalTableCount($partner_id, $report_type, $input_filter, $page_size, $page_index, $order_by, $object_ids); if ($table_amount > self::REPORTS_CSV_MAX_QUERY_SIZE && $page_size > self::REPORTS_CSV_MAX_QUERY_SIZE) { throw new kCoreException("Exceeded max query size: " . self::REPORTS_CSV_MAX_QUERY_SIZE, kCoreException::SEARCH_TOO_GENERAL); } $start_offest = ($page_index - 1) * $page_size; $end_offset = $start_offest + $page_size; $iteration_page_size = self::REPORTS_TABLE_RESULTS_SINGLE_ITERATION_SIZE; for ($current_offset = $start_offest; $current_offset < $end_offset; $current_offset = $current_offset + $iteration_page_size) { $offset_difference = $end_offset - $current_offset; if ($offset_difference < self::REPORTS_TABLE_RESULTS_SINGLE_ITERATION_SIZE) { $iteration_page_size = $offset_difference; } //here page_index is redundant list($table_header, $table_data, $table_total_count) = self::getTable($partner_id, $report_type, $input_filter, $iteration_page_size, $page_index, $order_by, $object_ids, $current_offset); if (!$table_data) { break; } //first iteration - create the beginning of the report if ($current_offset == $start_offest) { $csv = myCsvReport::createReport($report_title, $report_text, $headers, $report_type, $input_filter, $dimension, $arr, $total_header, $total_data, $table_header, $table_data, $table_amount, $csv); $data = $csv->getData(); // return URL if (!file_exists(dirname($file_path))) { kFile::fullMkfileDir(dirname($file_path), 0777); } //adding BOM for fixing problem in open .csv file with special chars using excel. $BOM = ""; file_put_contents($file_path, $BOM . $data); } else { //append data from query to file $tempCsv->clearData(); $tempCsv = myCsvReport::appendLines($tempCsv, $table_data); $data = $tempCsv->getData(); file_put_contents($file_path, $data, FILE_APPEND); } } } $url = self::createUrl($partner_id, $file_name); return $url; }
public static function createReport($report_title, $report_text, $headers, $report_type, reportsInputFilter $input_filter, $dimension, $graphs, $total_header, $total_data, $table_header, $table_data, $table_total_count) { list($total_dictionary, $table_dictionary) = self::buildDictionaries($headers); // store on disk $csv = new myCsvWrapper(); $csv->addNewLine($report_title); $csv->addNewLine($csv->formatDate($input_filter->from_date), $csv->formatDate($input_filter->to_date)); $csv->addNewLine($report_text); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine(""); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine("# Graph"); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine(""); if ($dimension) { $graph = @$graphs[$dimension]; } else { $graph = $graphs; } foreach ($graph as $data => $value) { if ($report_type == myReportsMgr::REPORT_TYPE_CONTENT_DROPOFF) { $csv->addNewLine($data, $value); } else { $csv->addNewLine($csv->formatDate(myReportsMgr::formatDateFromDateId($data)), $value); } } $csv->addNewLine(""); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine("# Total"); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine($total_dictionary); $csv->addNewLine($total_data); $csv->addNewLine(""); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine("# Table", "", "Total Count", $table_total_count); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine($table_dictionary); foreach ($table_data as $row) { $csv->addNewLine($row); } $data = $csv->getData(); return $data; }
public static function createReport($report_title, $report_text, $headers, $report_type, reportsInputFilter $input_filter, $dimension, $graphs, $total_header, $total_data, $table_header, $table_data, $table_total_count) { list($total_dictionary, $table_dictionary) = self::buildDictionaries($headers); // store on disk $csv = new myCsvWrapper(); $csv->addNewLine($report_title); $origTimeZone = date_default_timezone_get(); date_default_timezone_set('UTC'); $csv->addNewLine($csv->formatDate($input_filter->from_date), $csv->formatDate($input_filter->to_date)); date_default_timezone_set($origTimeZone); $csv->addNewLine($report_text); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine(""); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine("# Graph"); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine(""); if ($dimension) { $graph = @$graphs[$dimension]; } else { $graph = $graphs; } foreach ($graph as $data => $value) { if ($report_type == myReportsMgr::REPORT_TYPE_CONTENT_DROPOFF || $report_type == myReportsMgr::REPORT_TYPE_USER_CONTENT_DROPOFF) { $csv->addNewLine($data, $value); } else { if ($dimension) { if (strlen($data) == 6) { //foramt is yyyymm $date = DateTime::createFromFormat("Ym", $data); $csv->addNewLine($date->format("M Y"), $value); } else { $csv->addNewLine($csv->formatDate(myReportsMgr::formatDateFromDateId($data)), $value); } } } } $csv->addNewLine(""); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine("# Total"); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine($total_dictionary); $csv->addNewLine($total_data); $csv->addNewLine(""); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine("# Table", "", "Total Count", $table_total_count); $csv->addNewLine("# ------------------------------------"); $csv->addNewLine($table_dictionary); foreach ($table_data as $row) { $csv->addNewLine($row); } $data = $csv->getData(); return $data; }