[ Index ]

Source Code Reference for V1.00

title

Body

[close]

/modules/contacts/ -> csvexport.php (source)

   1  <?php /* $Id: csvexport.php 135 2008-04-04 13:49:13Z pedroix $ $URL: https://web2project.svn.sourceforge.net/svnroot/web2project/trunk/modules/contacts/csvexport.php $ */
   2  if (!defined('W2P_BASE_DIR')) {
   3      die('You should not access this file directly.');
   4  }
   5  
   6  // get GETPARAMETER for contact_id

   7  $contact_id = 1;
   8  
   9  $canRead = !getDenyRead('contacts');
  10  if (!$canRead) {
  11      $AppUI->redirect("m=public&a=access_denied");
  12  }
  13  if (1 == 1) {
  14      // Fields 1 - 5

  15      $text = sprintf("%s", "\"Title\",\"First Name\",\"Middle Name\",\"Last Name\",\"Suffix\",");
  16      // Fields 6 - 10

  17      $text .= sprintf("%s", "\"Company\",\"Department\",\"Job Title\",\"Business Street\",\"Business Street 2\",");
  18      // Fields 11 - 15

  19      $text .= sprintf("%s", "\"Business Street 3\",\"Business City\",\"Business State\",\"Business Postal Code\",\"Business Country\",");
  20      // Fields 16 - 20

  21      $text .= sprintf("%s", "\"Home Street\",\"Home Street 2\",\"Home Street 3\",\"Home City\",\"Home State\",");
  22      // Fields 21 - 25

  23      $text .= sprintf("%s", "\"Home Postal Code\",\"Home Country\",\"Other Street\",\"Other Street 2\",\"Other Street 3\",");
  24      // Fields 26 - 30

  25      $text .= sprintf("%s", "\"Other City\",\"Other State\",\"Other Postal Code\",\"Other Country\",\"Assistant's Phone\",");
  26      // Fields 31 - 35

  27      $text .= sprintf("%s", "\"Business Fax\",\"Business Phone\",\"Business Phone 2\",\"Callback\",\"Car Phone\",");
  28      // Fields 36 - 40

  29      $text .= sprintf("%s", "\"Company Main Phone\",\"Home Fax\",\"Home Phone\",\"Home Phone 2\",\"ISDN\",");
  30      // Fields 41 - 45

  31      $text .= sprintf("%s", "\"Mobile Phone\",\"Other Fax\",\"Other Phone\",\"Pager\",\"Primary Phone\",");
  32      // Fields 46 - 50

  33      $text .= sprintf("%s", "\"Radio Phone\",\"TTY/TDD Phone\",\"Telex\",\"Account\",\"Anniversary\",");
  34      // Fields 51 - 55

  35      $text .= sprintf("%s", "\"Assistant's Name\",\"Billing Information\",\"Birthday\",\"Categories\",\"Children\",");
  36      // Fields 56 - 60

  37      $text .= sprintf("%s", "\"Directory Server\",\"E-mail Address\",\"E-mail Type\",\"E-mail Display Name\",\"E-mail 2 Address\",");
  38      // Fields 61 - 65

  39      $text .= sprintf("%s", "\"E-mail 2 Type\",\"E-mail 2 Display Name\",\"E-mail 3 Address\",\"E-mail 3 Type\",\"E-mail 3 Display Name\",");
  40      // Fields 66 - 70

  41      $text .= sprintf("%s", "\"Gender\",\"Government ID Number\",\"Hobby\",\"Initials\",\"Internet Free Busy\",");
  42      // Fields 71 - 75

  43      $text .= sprintf("%s", "\"Keywords\",\"Language\",\"Location\",\"Manager's Name\",\"Mileage\",");
  44      // Fields 76 - 80

  45      $text .= sprintf("%s", "\"Notes\",\"Office Location\",\"Organizational ID Number\",\"PO Box\",\"Priority\",");
  46      // Fields 81 - 85

  47      $text .= sprintf("%s", "\"Private\",\"Profession\",\"Referred By\",\"Sensitivity\",\"Spouse\",");
  48      // Fields 86 - 90

  49      $text .= sprintf("%s", "\"User 1\",\"User 2\",\"User 3\",\"User 4\",\"Web Page\",");
  50      $q = new DBQuery;
  51      $q->addTable('custom_fields_struct', 'cfs');
  52      $q->addWhere('cfs.field_module = "contacts"');
  53      $q->addOrder('cfs.field_order');
  54      $custom_fields = $q->loadList();
  55      //                print_r($custom_fields);die;

  56      foreach ($custom_fields as $f) {
  57          $text .= sprintf("%s", "\"$f[field_description]\",");
  58      }
  59      $text .= sprintf("%s\r\n", "");
  60      $q->clear();
  61      require_once $AppUI->getModuleClass('companies');
  62      $company =& new CCompany;
  63      $allowedCompanies = $company->getAllowedSQL($AppUI->user_id);
  64      
  65      require_once $AppUI->getModuleClass('departments');
  66      $department =& new CDepartment;
  67      $allowedDepartments = $department->getAllowedSQL($AppUI->user_id);
  68      $q = new DBQuery;
  69      $q->addTable('contacts', 'con');
  70      $q->leftJoin('companies', 'co', 'co.company_id = con.contact_company');
  71      $q->leftJoin('departments', 'de', 'de.dept_id = con.contact_department');
  72      $q->addQuery('con.*');
  73      $q->addQuery('co.company_name');
  74      $q->addQuery('de.dept_name');
  75      $q->addWhere('
  76          (contact_private=0
  77              OR (contact_private=1 AND contact_owner=' . $AppUI->user_id . ')
  78              OR contact_owner IS NULL OR contact_owner = 0
  79          )');
  80      if (count($allowedCompanies)) {
  81          $comp_where = implode(' AND ', $allowedCompanies);
  82          $q->addWhere('( (' . $comp_where . ') OR contact_company = 0 )');
  83      }
  84      if (count($allowedDepartments)) {
  85          $dpt_where = implode(' AND ', $allowedDepartments);
  86          $q->addWhere('( (' . $dpt_where . ') OR contact_department = 0 )');
  87      }
  88      $q->addOrder('contact_first_name');
  89      $q->addOrder('contact_last_name');
  90      $contacts = $q->loadList();
  91      $q->clear();
  92      foreach ($contacts as $row) {
  93          // Fields 1- 10

  94          $text .= sprintf("\"\",\"%s\",\"\",\"%s\",\"\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",", $row['contact_first_name'], $row['contact_last_name'], $row['company_name'], $row['dept_name'], $row['contact_title'], $row['contact_address1'], $row['contact_address2']);
  95          // Fields 11- 20

  96          //$text .= sprintf("\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",");

  97          $text .= sprintf(",\"%s\",\"%s\",\"%s\",,,,,,,", $row['contact_city'], $row['contact_state'], $row['contact_zip']);
  98          // Fields 21- 30

  99          $text .= sprintf(",,,,,,,,,,");
 100          // Fields 31- 40

 101          settype($row['contact_phone'], 'string');
 102          $text .= sprintf(",\"%s\",,,,,,,,,", $row['contact_phone']);
 103          // Fields 41- 50

 104          settype($row['contact_mobile'], 'string');
 105          $text .= sprintf("\"%s\",,,,,,,,\"\",\"0/0/00\",", '' . $row['contact_mobile']);
 106          // Fields 51- 60

 107          if ($row['contact_type'] != '') {
 108              $categories = "web2Project; " . $row['contact_type'];
 109          } else {
 110              $categories = "web2Project;";
 111          }
 112          $text .= sprintf(",,\"%s\",\"%s\",,,\"%s\",\"%s\",\"%s\",,", $row['contact_birthday'], $categories, $row['contact_email'], "SMTP", $row['contact_first_name'] . " " . $row['contact_last_name']);
 113          // Fields 61- 70

 114          $text .= sprintf(",,,,,\"Unspecified\",,,,,");
 115          // Fields 71- 80

 116          $notes = str_replace("\"", "\"\"", $row['contact_notes']);
 117          $text .= sprintf("\"\",\"\",\"\",,,\"%s\",,,,\"Normal\",", $notes);
 118          // Fields 81- 90

 119          $text .= sprintf("\"False\",,,\"Normal\",,,,,,,");
 120          $q = new DBQuery;
 121          $q->addTable('custom_fields_struct', 'cfs');
 122          $q->addQuery('cfv.value_charvalue, cfl.list_value');
 123          $q->leftJoin('custom_fields_values', 'cfv', 'cfv.value_field_id = cfs.field_id');
 124          $q->leftJoin('custom_fields_lists', 'cfl', 'cfl.list_option_id = cfv.value_intvalue');
 125          $q->addWhere('cfs.field_module = \'contacts\'');
 126          $q->addWhere('cfv.value_object_id = \'' . $row['contact_id'] . '\'');
 127          $custom_fields = $q->loadList();
 128          $q->clear();
 129          foreach ($custom_fields as $f) {
 130              if ($f['value_intvalue']) {
 131                  $text .= sprintf("%s", "\"$f[list_value]\",");
 132              } else {
 133                  $text .= sprintf("%s", "\"" . str_replace("\r\n", " ", $f[value_charvalue]) . "\",");
 134              }
 135          }
 136          $text .= sprintf("%s\r\n", '');
 137      }
 138      //send http-output in csv format

 139  
 140      // BEGIN extra headers to resolve IE caching bug (JRP 9 Feb 2003)

 141      // [http://bugs.php.net/bug.php?id=16173]

 142      header('Pragma: ');
 143      header('Cache-Control: ');
 144      header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
 145      header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
 146      header('Cache-Control: no-store, no-cache, must-revalidate'); //HTTP/1.1

 147      header('Cache-Control: post-check=0, pre-check=0', false);
 148      // END extra headers to resolve IE caching bug

 149  
 150      header('MIME-Version: 1.0');
 151      header('Content-Type: text/x-csv');
 152      header('Content-Disposition: attachment; filename="'.$w2Pconfig['company_name'].'Contacts.csv"');
 153      print_r($text);
 154  } else {
 155      $AppUI->setMsg('contactIdError', UI_MSG_ERROR);
 156      $AppUI->redirect();
 157  }
 158  ?>


Generated: Wed Jan 7 03:00:01 2009 Cross-referenced by PHPXref 0.7