function csv_export($option) { global $mtconf; $database =& JFactory::getDBO(); $fields = JRequest::getVar('fields', '', 'post'); $publishing = JRequest::getVar('publishing', '', 'post'); $nullDate = $database->getNullDate(); $jdate = JFactory::getDate(); $now = $jdate->toMySQL(); $custom_fields = array(); $core_fields = array(); foreach ($fields as $field) { if (substr($field, 0, 2) == 'cf') { $custom_fields[] = substr($field, 2); } elseif ($field == 'cat_id') { $core_fields[] = 'GROUP_CONCAT(DISTINCT cat_id ORDER BY cl.main DESC SEPARATOR \',\') AS cat_id'; } else { $core_fields[] = $field; } } $where = array(); switch ($publishing) { case 2: // Published $where[] = "( (publish_up = " . $database->Quote($nullDate) . " OR publish_up <= '{$now}') AND " . "(publish_down = " . $database->Quote($nullDate) . " OR publish_down >= '{$now}') AND " . "link_published = '1' )"; break; case 3: // Unpublished $where[] = "link_published = '0'"; break; case 4: // Pending $where[] = "( (publish_up => '{$now}' OR publish_up = " . $database->Quote($nullDate) . ") AND link_published = '1' )"; break; case 5: // Expired $where[] = "( publish_down < '{$now}' AND link_published = '1' )"; break; case 6: // Pending Listing, waiting for approval $where[] = "link_approved <= 0"; break; } # Get link_id(s) first if (count($where) > 0) { $database->setQuery('SELECT link_id FROM #__mt_links WHERE ' . implode(" AND ", $where)); } else { $database->setQuery('SELECT link_id FROM #__mt_links'); } $link_ids = $database->loadResultArray(); $header = ''; $data = ''; if (count($link_ids) > 0) { # Get the core fields value unset($where); $where = array(); $where[] = "l.link_id = cl.link_id"; // $where[] = "cl.main = '1'"; $where[] = "l.link_id IN (" . implode(',', $link_ids) . ")"; if (in_array('l.link_id', $core_fields)) { $sql = "SELECT " . implode(", ", $core_fields) . " FROM (#__mt_links AS l, #__mt_cl AS cl)"; } else { $sql = "SELECT " . implode(", ", array_merge(array('l.link_id'), $core_fields)) . " FROM (#__mt_links AS l, #__mt_cl AS cl)"; } if (count($where)) { $sql .= "\n WHERE " . implode(" AND ", $where); } if (in_array('cat_id', $fields)) { $sql .= "\n GROUP BY cl.link_id"; } $database->setQuery($sql); $rows = $database->loadObjectList('link_id'); # Get the custom fields' value if (count($custom_fields) > 0) { $database->setQuery('SELECT cf_id, link_id, value FROM #__mt_cfvalues WHERE cf_id IN (' . implode(',', $custom_fields) . ') AND link_id IN (' . implode(',', $link_ids) . ')'); $cfvalues = $database->loadObjectList(); foreach ($cfvalues as $cfvalue) { $rows[$cfvalue->link_id]->{'cf' . $cfvalue->cf_id} = $cfvalue->value; } } $seperator = ','; # Create the CSV data $header = ''; $data = ''; $i = 0; foreach ($fields as $field) { $i++; if ($field == 'l.link_id') { $header .= 'link_id'; } elseif (substr($field, 0, 2) == 'cf') { $header .= substr($field, 2); } else { $header .= $field; } if ($i < count($fields)) { $header .= $seperator; } } $header .= "\n"; foreach ($rows as $row) { $line = ''; $j = 0; foreach ($fields as $field) { if ($field == 'l.link_id') { if (!in_array('l.link_id', $core_fields)) { continue; } else { $field = 'link_id'; } } if (isset($row->{$field})) { $value = $row->{$field}; } else { $value = ''; } if ($j >= 0) { if (!empty($value)) { $line .= '"' . str_replace('"', '""', $value) . '"'; } if ($j + 1 < count($fields)) { $line .= $seperator; } } $j++; } if (!empty($line)) { $data .= trim($line) . "\n"; } } } # this line is needed because returns embedded in the data have "\r" # and this looks like a "box character" in Excel $data = str_replace("\r", "", $data); HTML_mtree::csv_export($header, $data, $option); }