Exemplo n.º 1
0
	
	<div class="coInputMainBlock adminMainBlock">
	
	<table><tr><th></th><th><?php 
    echo $import_type == 'contact' ? lang('contact fields') : lang('company fields');
    ?>
</th><th><?php 
    echo lang('fields from file');
    ?>
</th></tr>
	
	<?php 
    if ($import_type == 'contact') {
        $contact_fields = Contacts::getContactFieldNames();
    } else {
        $contact_fields = Contacts::getCompanyFieldNames();
    }
    $custom_properties = CustomProperties::getAllCustomPropertiesByObjectType(Contacts::instance()->getObjectTypeId());
    +($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');
		$ids = array_var($_GET, 'ids');
		$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;
		}
                
		$checked_fields = ($type == 'contact') ? array_var($_POST, 'check_contact') : 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 = Contacts::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);
			$conditions = '';
			$ids_sql = ($ids)? " AND id IN (".$ids.") " : "";
			if (array_var($_SESSION, 'import_type', 'contact') == 'contact') {
                                $conditions .= ($conditions == "" ? "" : " AND ") . "`archived_by_id` = 0" . ($conditions ? " AND $conditions" : "");
                                $conditions .= $ids_sql;
				$contacts = Contacts::instance()->getAllowedContacts($conditions);
				foreach ($contacts as $contact) {
					fwrite($handle, $this->build_csv_from_contact($contact, $checked_fields) . "\n");
				}
			}else{
                                $conditions .= ($conditions == "" ? "" : " AND ") . "`archived_by_id` = 0" . ($conditions ? " AND $conditions" : "");
                                $conditions .=$ids_sql;
				$companies = Contacts::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;
		}
	}
 function export_to_csv_file()
 {
     $ids = array_var($_REQUEST, 'ids');
     $idsall = array_var($_REQUEST, 'allIds');
     $export_all = array_var($_REQUEST, 'export_all');
     $this->setTemplate('csv_export');
     $type = array_var($_REQUEST, '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;
     }
     $delimiter = array_var($_REQUEST, 'delimiter', ',');
     if ($delimiter == '') {
         $delimiter = ',';
     }
     $checked_fields = $type == 'contact' ? array_var($_REQUEST, 'check_contact') : array_var($_REQUEST, 'check_company');
     if (is_array($checked_fields) && ($ids || $idsall || $export_all)) {
         $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}]"] . $delimiter;
                 }
             }
             $titles = substr_utf($titles, 0, strlen_utf($titles) - 1) . "\n";
         } else {
             $field_names = Contacts::getCompanyFieldNames();
             foreach ($checked_fields as $k => $v) {
                 if (isset($field_names["company[{$k}]"]) && $v == 'checked') {
                     $titles .= $field_names["company[{$k}]"] . $delimiter;
                 }
             }
             $titles = substr_utf($titles, 0, strlen_utf($titles) - 1) . "\n";
         }
         // export the same type of contact objects that are enabled in the contacts tab.
         $extra_conditions = "";
         if (array_var($_SESSION, 'import_type', 'contact') == 'contact') {
             $extra_conditions = '  `is_company` = 0 ';
             if (!user_config_option("viewContactsChecked")) {
                 $extra_conditions .= ' AND  `user_type` != 0 ';
             }
             if (!user_config_option("viewUsersChecked")) {
                 $extra_conditions .= ' AND `user_type` < 1 ';
             }
         }
         // --
         $filename = rand() . '.tmp';
         $handle = fopen(ROOT . '/tmp/' . $filename, 'wb');
         fwrite($handle, $titles);
         $conditions = $extra_conditions;
         $ids_sql = "";
         if (!$export_all) {
             $ids_sql = $ids ? " AND id IN (" . $ids . ") " : "";
         }
         $members = active_context_members(false);
         $context_condition = $this->getActiveContextConditions();
         if (array_var($_SESSION, 'import_type', 'contact') == 'contact') {
             $conditions .= " AND `archived_by_id` = 0 ";
             $conditions .= $ids_sql;
             $conditions .= $context_condition;
             $contacts = Contacts::instance()->getAllowedContacts($conditions);
             foreach ($contacts as $contact) {
                 fwrite($handle, $this->build_csv_from_contact($contact, $checked_fields, $delimiter) . "\n");
             }
         } else {
             $conditions .= ($conditions == "" ? "" : " AND ") . "`archived_by_id` = 0" . ($conditions ? " AND {$conditions}" : "");
             $conditions .= $ids_sql;
             $conditions .= $context_condition;
             $companies = Contacts::getVisibleCompanies(logged_user(), $conditions);
             foreach ($companies as $company) {
                 fwrite($handle, $this->build_csv_from_company($company, $checked_fields, $delimiter) . "\n");
             }
         }
         fclose($handle);
         $_SESSION['contact_export_filename'] = $filename;
         flash_success($imp_type == 'contact' ? lang('success export contacts') : lang('success export companies'));
     }
 }