/** * The function _exportCSVOrderCustom is an alternative implementation of _exportCS * specific for orders. * * It ensures that products of an order are each on a separate line instead of all * products on one line. * */ function _exportCSVOrderCustom($params) { $config = & hikashop_config (); $export = hikashop_get ( 'helper.spreadsheet' ); switch ($params->formatExport) { case 'csv' : $format = $config->get ( 'export_format', 'csv' ); $separator = $config->get ( 'csv_separator', ';' ); $force_quote = $config->get ( 'csv_force_quote', 1 ); $export->init ( $format, 'hikashop_export', $separator, $force_quote ); break; case 'xls' : $format = 'xls'; $export->init ( $format, 'hikashop_export', ';', true ); break; } /* * **************************** * * Printing header line. First the common headers, then the product headers. * * **************************** */ if (! empty ( $params->action )) { $row = array (); foreach ( $params->action as $keyTable => $table ) { $this->organizeExportColumn ( $params->table, $keyTable, $params->elements, $params->action, $params->types ); } // first list all headers which not start with product. foreach ( $params->action as $keyTable => $table ) { if ($this->startsWithCheck ( $keyTable, "product" )) { continue; } foreach ( $table as $column ) { // error_log ( "column = $column ; keyTable = $keyTable " ); if ($keyTable == 'files' || $keyTable == 'images') $column = str_replace ( 'file', $keyTable, $column ); if ($this->startsWithCheck ( $keyTable, "product" )) { $length = strlen ( $column ); // delete the first 6 items, because that is item#_ where # is a number. $column = substr ( $column, 6, $length ); $printedproduct = true; } $row [] = $column; } } // secondly list all product header fields only once. $printedproduct = false; foreach ( $params->action as $keyTable => $table ) { if ($this->startsWithCheck ( $keyTable, "product" )) { if (! $printedproduct) { foreach ( $table as $column ) { // error_log ( "column = $column ; keyTable = $keyTable " ); if ($keyTable == 'files' || $keyTable == 'images') $column = str_replace ( 'file', $keyTable, $column ); if ($this->startsWithCheck ( $keyTable, "product" )) { $length = strlen ( $column ); // delete the first 6 items, because that is item#_ where # is a number. $column = substr ( $column, 6, $length ); $printedproduct = true; } $row [] = $column; } } } } $export->writeLine ( $row ); } JRequest::setVar ( 'from_task', 'exportCsv' ); /* * **************************** * * Printing order lines. * * **************************** */ if (! empty ( $params->action )) { // For all elements in the parameters, where the elements are orders. foreach ( $params->elements as $k1 => $element ) { // error_log ( "First building up the prefix (non order line) part of the message." ); $prefixRow = array (); foreach ( $params->action as $key => $table ) { if ($this->startsWithCheck ( $key, "product" )) { // error_log ( "Found product: $key Skipping it as we are looking for general line info." ); continue; } foreach ( $table as $column ) { $find = false; $square = ''; if (isset ( $element->$column ) && ($key === $k1 || $key === $params->table)) { $square .= $this->displayByType ( $params->types, $element, $column ); $find = true; } else { $r = array (); foreach ( $element as $k => $elem ) { if (! is_array ( $elem )) { continue; } foreach ( $elem as $data ) { if (! isset ( $data->$column )) { if (isset ( $data->exportData->value ) && $data->exportData->name == $column) { $r [] = $data->exportData->value; $find = true; } continue; } if ($k != $key) { continue; } $r [] = $this->displayByType ( $params->types, $data, $column ); $find = true; } } $square .= $this->separator ( $r, $params->table, $key ); } if (! $find) { $prefixRow [] = ''; } else { $prefixRow [] = $square; } } } // We have built up the prefix line now. Lets use that to make individual lines of the product. foreach ( $params->action as $key => $table ) { // If they key is a product, if (($this->startsWithCheck ( $key, "product" ))) { // error_log ( "$key is product, so writing line." ); $currentOrder = array (); $currentOrderCount = count ( $currentOrder ); // The foundAnyValues variable is to check if any value is set. If no values are set for a product // it is assumed that it is emtpy and no order line should be printed. $foundAnyValues = false; foreach ( $table as $column ) { $find = false; $square = ''; if (isset ( $element->$column ) && ($key === $k1 || $key === $params->table)) { $square .= $this->displayByType ( $params->types, $element, $column ); $find = true; } else { $r = array (); foreach ( $element as $k => $elem ) { if (! is_array ( $elem )) { continue; } foreach ( $elem as $data ) { if (! isset ( $data->$column )) { if (isset ( $data->exportData->value ) && $data->exportData->name == $column) { $r [] = $data->exportData->value; $find = true; } continue; } if ($k != $key) { continue; } $r [] = $this->displayByType ( $params->types, $data, $column ); $find = true; } } $square .= $this->separator ( $r, $params->table, $key ); } if (! $find) { $currentOrder [] = ''; } else { $currentOrder [] = $square; } $foundAnyValues = $foundAnyValues || $find; } $currentOrderCount = count ( $currentOrder ); if ($foundAnyValues) { $fullRow = array_merge ( $prefixRow, $currentOrder ); $export->writeLine ( $fullRow ); } } } } } /* * ************** * * Done with writing the lines, now writing the file. This is standard HikaShop code again. * * ************** */ if (empty ( $params->path )) { $export->send (); } else { $file = hikashop_get ( 'class.file' ); $name = ''; $path = explode ( DS, $params->path ); $name = $path [count ( $path ) - 1]; unset ( $path [count ( $path ) - 1] ); $params->path = implode ( DS, $path ); $uploadFolder = rtrim ( JPath::clean ( html_entity_decode ( $params->path ) ), DS . ' ' ) . DS; if (! preg_match ( '#^([A-Z]:)?/.*#', $uploadFolder )) { if (! $uploadFolder [0] == '/' || ! is_dir ( $uploadFolder )) { $uploadFolder = JPath::clean ( $file->getPath ( 'file' ) . trim ( $uploadFolder, DS . ' ' ) . DS ); } } if (strstr ( $name, '.' )) { JFile::write ( $uploadFolder . $name, $export->buffer ); } } } /** * Function to check a string starts with a prefix */ function startsWithCheck($fullword, $prefix) { $length = strlen ( $prefix ); return (substr ( $fullword, 0, $length ) === $prefix); }