Example #1
0
</div>
</div>
	<div class="coInputMainBlock adminMainBlock">
	<?php 
if (!isset($result_msg)) {
    ?>
	<table style="width:350px;"><tr><th style="text-align:center;" colspan="2"><?php 
    echo lang('fields to export');
    ?>
</th></tr>
	
	<?php 
    if ($import_type == 'contact') {
        $contact_fields = Contacts::getContactFieldNames();
    } else {
        $contact_fields = Companies::getCompanyFieldNames();
    }
    $isAlt = false;
    $i = 0;
    $label_w = $label_h = $label_o = false;
    foreach ($contact_fields as $c_field => $c_label) {
        if (str_starts_with($c_field, 'contact[w') && !$label_w) {
            ?>
<tr><td colspan="3" style="text-align:center;"><b><?php 
            echo lang('work');
            ?>
</b></td></tr> <?php 
            $label_w = true;
        } else {
            if (str_starts_with($c_field, 'contact[h') && !$label_h) {
                ?>
 function export_to_csv_file()
 {
     $this->setTemplate('csv_export');
     $type = array_var($_GET, 'type', array_var($_SESSION, 'import_type', 'contact'));
     //type of import (contact - company)
     tpl_assign('import_type', $type);
     if (!isset($_SESSION['import_type']) || $type != $_SESSION['import_type'] && $type != '') {
         $_SESSION['import_type'] = $type;
     }
     if ($type == 'contact') {
         $checked_fields = array_var($_POST, 'check_contact');
     } else {
         $checked_fields = array_var($_POST, 'check_company');
     }
     if (is_array($checked_fields)) {
         $titles = '';
         $imp_type = array_var($_SESSION, 'import_type', 'contact');
         if ($imp_type == 'contact') {
             $field_names = Contacts::getContactFieldNames();
             foreach ($checked_fields as $k => $v) {
                 if (isset($field_names["contact[{$k}]"]) && $v == 'checked') {
                     $titles .= $field_names["contact[{$k}]"] . ',';
                 }
             }
             $titles = substr_utf($titles, 0, strlen_utf($titles) - 1) . "\n";
         } else {
             $field_names = Companies::getCompanyFieldNames();
             foreach ($checked_fields as $k => $v) {
                 if (isset($field_names["company[{$k}]"]) && $v == 'checked') {
                     $titles .= $field_names["company[{$k}]"] . ',';
                 }
             }
             $titles = substr_utf($titles, 0, strlen_utf($titles) - 1) . "\n";
         }
         $filename = rand() . '.tmp';
         $handle = fopen(ROOT . '/tmp/' . $filename, 'wb');
         fwrite($handle, $titles);
         $project = active_project();
         if ($project instanceof Project) {
             $pids = $project->getAllSubWorkspacesQuery(true);
         }
         $wsConditions = null;
         $tag_str = null;
         $tag = array_var($_GET, 'active_tag');
         if (array_var($_SESSION, 'import_type', 'contact') == 'contact') {
             if (isset($pids)) {
                 $wsConditions = Contacts::getWorkspaceString($pids);
             }
             if (isset($tag) && $tag && $tag != '') {
                 $tag_str = " EXISTS (SELECT * FROM `" . TABLE_PREFIX . "tags` `t` WHERE `tag` = " . DB::escape($tag) . " AND `co`.`id` = `t`.`rel_object_id` AND `t`.`rel_object_manager` = 'Contacts') ";
             }
             $conditions = $wsConditions ? $wsConditions . ($tag_str ? " AND {$tag_str}" : '') : $tag_str;
             $conditions .= ($conditions == "" ? "" : " AND ") . "`archived_by_id` = 0" . ($conditions ? " AND {$conditions}" : "");
             $contacts = Contacts::instance()->getAllowedContacts($conditions);
             foreach ($contacts as $contact) {
                 fwrite($handle, $this->build_csv_from_contact($contact, $checked_fields) . "\n");
             }
         } else {
             if (isset($pids)) {
                 $wsConditions = Companies::getWorkspaceString($pids);
             }
             if (isset($tag) && $tag && $tag != '') {
                 $tag_str = " EXISTS (SELECT * FROM `" . TABLE_PREFIX . "tags` `t` WHERE `tag` = " . DB::escape($tag) . " AND `" . TABLE_PREFIX . "companies`.`id` = `t`.`rel_object_id` AND `t`.`rel_object_manager` = 'Companies') ";
             }
             $conditions = $wsConditions ? $wsConditions . ($tag_str ? " AND {$tag_str}" : '') : $tag_str;
             $conditions .= ($conditions == "" ? "" : " AND ") . "`archived_by_id` = 0" . ($conditions ? " AND {$conditions}" : "");
             $companies = Companies::getVisibleCompanies(logged_user(), $conditions);
             foreach ($companies as $company) {
                 fwrite($handle, $this->build_csv_from_company($company, $checked_fields) . "\n");
             }
         }
         fclose($handle);
         $_SESSION['contact_export_filename'] = $filename;
         flash_success($imp_type == 'contact' ? lang('success export contacts') : lang('success export companies'));
     } else {
         unset($_SESSION['contact_export_filename']);
         return;
     }
 }