<?/*
Copyright (C) 2003 Media Net Link, Inc. Danville, CA 94526 USA

This file is part of AthenaRMS.

AthenaRMS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

AthenaRMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/?>
<?php
  header ('Content-Type: text/csv');

  $DELIM = ',';

  require_once '../../../lib/db.php';
  require_once '../../../lib/comment.php';

  $comm = new Comment ();

  foreach (array('ticket_id','open_date','close_date','user_last_name','user_first_name','company','email','work_phone','problem_type','source','account_id','user_person_id','message') as $v) {
    print '"'.$v.'"'.$DELIM;
  }
  print "\n";

  $h = $dbi->prepare ("
    select sr.ticket_id,
           to_char(t.created_date, 'YYYY/MM/DD HH24:MI:SS') as open_date,
           to_char(tm.close_date, 'YYYY/MM/DD HH24:MI:SS') as close_date,
           p.last_name as user_last_name,
           p.first_name as user_first_name,
           p.company,
           p.email,
           p.work_phone,
           pt.problem_type,
           s.source,
           t.account_id,
           t.user_person_id
      from search_results sr
             inner join ticket_crm_map tm on (sr.ticket_id = tm.ticket_id)
               inner join ticket t on (tm.ticket_id = t.ticket_id)
                 inner join person p on (t.user_person_id = p.person_id)
               inner join problem_type pt on (tm.problem_id = pt.problem_id)
               inner join source s on (tm.source_id = s.source_id)
     where sr.sr_query_id = ?
  ");
  $res = $dbi->execute ($h, array($query_id));

  $first = 1;
  
  while ($r = $res->fetchRow()) {
    foreach ($r as $v) {
      print '"'.strip_csv($v).'"'.$DELIM;
    }
    print '"'.strip_csv(substr($comm->First_User_Comments($r[0],$r[11]),0.250)).'"';
    print "\n";
  }

  function strip_csv ($field) {
    foreach (array("\n","\r",',','"') as $sub) {
      $field = str_replace($sub,'',$field);
    }

    return $field;
  }
?>
