/**
  * Import products
  */
 public function actionImport()
 {
     $importer = new CsvImporter();
     $importer->deleteDownloadedImages = Yii::app()->request->getPost('remove_images');
     if (Yii::app()->request->isPostRequest && isset($_FILES['file'])) {
         $importer->file = $_FILES['file']['tmp_name'];
         if ($importer->validate() && !$importer->hasErrors()) {
             // Create db backup
             if (isset($_POST['create_dump']) && $_POST['create_dump']) {
                 Yii::import('application.components.SDatabaseDumper');
                 $dumper = new SDatabaseDumper();
                 $file = Yii::getPathOfAlias('webroot.protected.backups') . DIRECTORY_SEPARATOR . 'dump_' . date('Y-m-d_H_i_s') . '.sql';
                 if (is_writable(Yii::getPathOfAlias('webroot.protected.backups'))) {
                     if (function_exists('gzencode')) {
                         file_put_contents($file . '.gz', gzencode($dumper->getDump()));
                     } else {
                         file_put_contents($file, $dumper->getDump());
                     }
                 } else {
                     throw new CHttpException(503, Yii::t('CsvModule.admin', 'Ошибка. Директория для бэкапов недоступна для записи.'));
                 }
             }
             $importer->import();
         }
     }
     $this->render('import', array('importer' => $importer));
 }
 /**
  * Import default demo data
  */
 private function importCsvFiles()
 {
     $files = array('laptops', 'computer_sound', 'monitors', 'phones', 'tablets');
     foreach ($files as $file) {
         $importer = new CsvImporter();
         $importer->file = Yii::getPathOfAlias('application.modules.install.data') . DIRECTORY_SEPARATOR . $file . '.csv';
         if ($importer->validate() && !$importer->hasErrors()) {
             $importer->import();
         }
     }
     StoreProduct::model()->updateAll(array('is_active' => 1));
 }
 /**
  * Read in the csv file and save as one json file for each agency
  *
  * http://www.codeigniter.com/user_guide/libraries/file_uploading.html?highlight=upload#CI_Upload::initialize
  *
  * @param <array> $data
  */
 public function csv_to_array($data)
 {
     $this->load->helper('csv');
     $this->load->helper('api');
     ini_set("auto_detect_line_endings", true);
     $column_headers = $this->getColumnHeaders($data);
     // Provide mapping between csv headings and POD schema
     $json_schema = $this->recommendation->datajson_schema();
     $datajson_model = $this->recommendation->schema_to_model($json_schema->properties);
     $mapping = $this->recommendation->get_mapping($json_schema->properties);
     $importer = new CsvImporter($data['full_path'], $parse_header = true, $delimiter = ",");
     $csv = $importer->get();
     $recommendations = $this->csv_row_to_objects($mapping, $csv);
     return $recommendations;
 }
Exemple #4
0
    /**
     * Import users from CSV file. 
     * @author Josep Ferràndiz Farré (jferran6@xtec.cat)
     * **CSV File header** Allowed fields: uname,new_uname,email,password,forcechgpass,activated,firstname,lastname1,lastname2,birthdate,gender,code,in,out
     * Fields "in" and "out" contains group IDs where the user will be added or removed, separated by "|" char
     * Deletions will be made before insertions.
     * 
     */
    public function import(){
        $this->throwForbiddenUnless(SecurityUtil::checkPermission('IWusers::', '::', ACCESS_ADMIN));
        // Sincronize tables users & IWUsers 
        ModUtil::apiFunc($this->name, 'admin', 'sincronize');
        $view = Zikula_View::getInstance($this->name);
        
        if ($this->request->isPost()) {
            $this->checkCsrfToken();
            $importFile = $this->request->files->get('importFile', null);
            $delimiter  = $this->request->request->get('delimiter', ';');
            // 1. Validate file: header & structure
            if (is_null($importFile)) {
                LogUtil::registerError($this->__('File error. Probably exceeds max. file size.'));
            } else {
                // Header fields table equivalents
                $headerTrans =array('uname'       => 'uname', 
                                    'new_uname'   => 'new_uname', 
                                    'email'       => 'email',
                                    'code'        => 'code',
                                    'password'    => 'password',
                                    'forcechgpass'=> 'forcechgpass',
                                    'activated'   => 'activated',
                                    'firstname'   => 'nom',
                                    'lastname1'   => 'cognom1',
                                    'lastname2'   => 'cognom2',
                                    'description' => 'description',
                                    'birthdate'   => 'naixement',
                                    'gender'      => 'sex',
                                    'in'          => 'in',
                                    'out'         => 'out'
                    );

                $import = new CsvImporter($importFile['tmp_name'],true, $headerTrans, $delimiter);  
                $header  = $import->getHeader();
                $oHeader = $import->getOriHeader();
                
                $check  = ModUtil::apiFunc($this->name, 'admin', 'checkCsvHeader', $header);
                // Check CSV file header
                if (!$check['correcte']) {
                    // CSV header incorrect
                    LogUtil::registerError($check['msg']);
                } else {
                    // Get CSV file content
                    $data = $import->get();
                    // Initialize 
                    $update = array();
                    $insert = array();
                    $optimizeGroups = ((in_array('in', $header)) || (in_array('out', $header)));
                    $forcechgpass   = in_array('forcechgpass', $header);
                    $iErrors = array();
                    // Check file info
                    foreach ($data as &$line) {
                        $uid = UserUtil::getIdFromName($line['uname']);
                        if ($optimizeGroups) {
                            $res = ModUtil::apiFunc($this->name, 'admin', 'optimizeGroups', array('uid' => $uid, 'data'=> $line));
                            $line['in'] = $res['in'];
                            $line['out'] = $res['out'];                            
                        }                            
                        if ($uid){       
                            $line['uid'] = $uid;
                            // Update
                            if (isset($line['activated'])){
                                if ($line['activated']!=""){
                                    $line['activated'] = $line['activated'] == '0' ? 0 : 1;                                    
                                } else {
                                    // Mantain actual value
                                    $line['activated'] = DBUtil::selectField('users', 'activated', 'uid='.$uid);
                                }                                
                            }
                            $update[] = $line;                                
                        } else {
                            // Insert new user
                            if ($line['uname'] !=""){
                                if (!isset($line['activated']) || (isset($line['activated']) && $line['activated']!='0')) $line['activated'] = 1;
                                $insert[] = $line;
                            } else {
                                // Error. No username 
                                $line['error'] = $this->__('No username.');
                                $iErrors[] = $line;
                            }
                        }                            
                    }
                    // Process file information
                    $allChanges = ModUtil::apiFunc($this->name, 'admin', 'applyCsvValues', array('update' => $update, 'insert' =>$insert));               
                    unset($update); $update = array();
                    unset($insert); $insert = array();
                    foreach ($allChanges as $user){                       
                        switch ($user['action']){
                            case 'm':
                                $update[] = $user;
                                break;
                            default:
                                $insert[] = $user;
                        }
                        if (isset($user['error'])){ 
                            // There are errors
                            $iErrors[] = $user;
                        }
                    }
                    $view->assign('insert', $insert);
                    $view->assign('update', $update);
                    $view->assign('iErrors', $iErrors);
                    $view->assign('header', $oHeader);
                    $view->assign('hc', count($oHeader));
                    return $view->fetch('IWusers_admin_importResults.tpl');        

                } // File header is correct
            } // exist import file                
        } // Is POST
         
        // Get zikula groups name and gid
        $g = UserUtil::getGroups('','name');
        foreach ($g as $key => $value) {
            $groupInfo[] = array_slice($value,0,2);
        }
                
        $view->assign('post_max_size', ini_get('post_max_size'));
        $view->assign('groupInfo', $groupInfo);
        
        return $view->fetch('IWusers_admin_import.tpl');        
    }
Exemple #5
0
 * Time: 12:28
 */
require __DIR__ . '/vendor/autoload.php';
require 'CsvImporter.php';
require 'pjud_scrap.php';
use Zend\Dom\Query;
use Zend\Http\Client;
$archivo = $_GET['archivo'];
//Recibimos el archivo con el que realizaremos la busqueda.
$formato = $_GET['formato'];
$cliente = $_GET['cliente'];
$busqueda = $_GET['busqueda'];
/*
 * Convertimos el archivo en un arreglo.
 */
$importer = new CsvImporter($archivo, false);
$data = $importer->get();
if ($formato == "1") {
    //$arreglo = $importer->convertToArray($data,1);
    echo '</pre>Entre en uno</pre>';
    $arreglo = $importer->convertToArray($data, $formato);
}
if ($formato == "2") {
    echo '</pre>Entre en dos</pre>';
    $arreglo = $importer->convertToArray($data, $formato);
}
$busqueda = new Scrap();
$busqueda->insertaBusqueda($arreglo);
/*
 * Configuramos el cliente
 */
 public function csv_to_json($schema = null)
 {
     $schema = $this->input->post('schema', TRUE) ? $this->input->post('schema', TRUE) : $schema;
     $csv_id = $this->input->post('csv_id', TRUE) ? $this->input->post('csv_id', TRUE) : null;
     $prefix = 'fitara';
     if (substr($schema, 0, strlen($prefix)) == $prefix) {
         $prefix_model = substr($schema, strlen($prefix) + 1);
     }
     // Initial file upload
     if (!empty($_FILES)) {
         $this->load->library('upload');
         if ($this->do_upload('csv_upload')) {
             $data = $this->upload->data();
             ini_set("auto_detect_line_endings", true);
             $csv_handle = fopen($data['full_path'], 'r');
             $headings = fgetcsv($csv_handle);
             // Sanitize input
             $headings = $this->security->xss_clean($headings);
             // Provide mapping between csv headings and POD schema
             $this->load->model('campaign_model', 'campaign');
             $json_schema = $this->campaign->datajson_schema($schema);
             if ($schema) {
                 if (!empty($prefix_model)) {
                     $datajson_model = $this->campaign->schema_to_model($json_schema->properties->{$prefix_model}->items->properties);
                 } else {
                     $datajson_model = $this->campaign->schema_to_model($json_schema->properties->dataset->items->properties);
                 }
             } else {
                 $datajson_model = $this->campaign->schema_to_model($json_schema->items->properties);
             }
             $output = array();
             $output['headings'] = $headings;
             $output['datajson_model'] = $datajson_model;
             $output['csv_id'] = $data['file_name'];
             $output['select_mapping'] = $this->csv_field_mapper($headings, $datajson_model);
             $output['schema'] = $schema;
             $this->load->view('csv_mapping', $output);
         }
     } else {
         if (!empty($csv_id)) {
             $mapping = $this->input->post('mapping', TRUE) ? $this->input->post('mapping', TRUE) : null;
             $schema = $this->input->post('schema', TRUE) ? $this->input->post('schema', TRUE) : 'federal';
             $this->config->load('upload', TRUE);
             $upload_config = $this->config->item('upload');
             $full_path = $upload_config['upload_path'] . $csv_id;
             $this->load->helper('csv');
             ini_set("auto_detect_line_endings", true);
             $importer = new CsvImporter($full_path, $parse_header = true, $delimiter = ",");
             $csv = $importer->get();
             $json = array();
             if ($schema == 'federal-v1.1') {
                 // Provide mapping between csv headings and POD schema
                 $this->load->model('campaign_model', 'campaign');
                 $json_schema = $this->campaign->datajson_schema($schema);
                 $datajson_model = $this->campaign->schema_to_model($json_schema->properties);
                 $datajson_model->dataset = array();
                 $dataset_model = clone $this->campaign->schema_to_model($json_schema->properties->dataset->items->properties);
                 $datasets = array();
                 foreach ($csv as $row) {
                     $count = 0;
                     $json_row = clone $dataset_model;
                     $json_row->contactPoint = clone $dataset_model->contactPoint;
                     $json_row->publisher = clone $dataset_model->publisher;
                     $distribution_row = clone $dataset_model->distribution[0];
                     foreach ($row as $key => $value) {
                         if ($mapping[$count] !== 'null') {
                             $value = $this->schema_map_filter($mapping[$count], $value, $schema);
                             if (strpos($mapping[$count], '.') !== false) {
                                 $field_path = explode('.', $mapping[$count]);
                                 if (array_key_exists($field_path[0], $json_row) && array_key_exists($field_path[1], $json_row->{$field_path}[0])) {
                                     $json_row->{$field_path}[0]->{$field_path}[1] = $value;
                                 }
                                 if ($field_path[0] == 'distribution') {
                                     if (array_key_exists($field_path[1], $distribution_row)) {
                                         $distribution_row->{$field_path}[1] = $value;
                                     }
                                 }
                             }
                             if (array_key_exists($mapping[$count], $json_row)) {
                                 $json_row->{$mapping}[$count] = $value;
                             }
                         }
                         $count++;
                     }
                     $json_row->distribution = array($distribution_row);
                     $this->campaign->unset_nulls($json_row);
                     $datasets[] = $json_row;
                 }
                 $id_field = '@id';
                 $context_field = '@context';
                 unset($datajson_model->{$id_field});
                 $datajson_model->{$context_field} = 'https://project-open-data.cio.gov/v1.1/schema/catalog.jsonld';
                 $datajson_model->conformsTo = 'https://project-open-data.cio.gov/v1.1/schema';
                 $datajson_model->describedBy = 'https://project-open-data.cio.gov/v1.1/schema/catalog.json';
                 $datajson_model->dataset = $datasets;
                 $json = $datajson_model;
             } else {
                 foreach ($csv as $row) {
                     $count = 0;
                     $json_row = array();
                     foreach ($row as $key => $value) {
                         if ($mapping[$count] !== 'null') {
                             $value = $this->schema_map_filter($mapping[$count], $value, $schema);
                             // Convert ints to strings for FITARA
                             if (!empty($prefix_model)) {
                                 $value = is_int($value) ? (string) $value : $value;
                             }
                             $json_row[$mapping[$count]] = $value;
                         }
                         $count++;
                     }
                     $json[] = $json_row;
                 }
                 if (!empty($prefix_model)) {
                     $container = new stdClass();
                     $container->{$prefix_model} = $json;
                     $json = $container;
                 }
             }
             // delete temporary uploaded csv file
             unlink($full_path);
             // provide json for download
             header("Pragma: public");
             header("Expires: 0");
             header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
             header("Cache-Control: private", false);
             header('Content-type: application/json');
             header("Content-Disposition: attachment; filename=\"{$csv_id}.json\";");
             header("Content-Transfer-Encoding: binary");
             print json_encode($json);
             exit;
         } else {
             $this->load->view('csv_upload');
         }
     }
 }
    if ($formato === "3") {
        $importer = new CsvImporter($archivo, false);
        $data = $importer->get();
        $demandados = $importer->importRuts($data);
        $arr_demandados = $scrap->insertaBusquedaRut($demandados);
        $ruts = $scrap->getRuts();
        $scrap->setPostRut($ruts);
        $scrap->buscaCausas();
        $scrap->exportData();
    }
    //$ruts = $scrap->getRuts();
    //$scrap->getRols($ruts);
}
if ($busqueda === "rol") {
    echo $archivo;
    $importer_rols = new CsvImporter($archivo, false);
    $data = $importer_rols->get();
    $rols = $importer_rols->importRols($data);
    $scrap->insertaBusquedaRol($rols);
    $scrap->buscaCausas();
    $scrap->exportData();
}
/*if ($cliente === "gene") {
    echo '<pre>';
    echo "vamos a procesar la informacion de manera generica";
    echo '</pre>';
}
if ($cliente === "beco") {
    echo '<pre>';
    echo "vamos a procesar la informacion en formato beco";
    echo '</pre>';
 public function match_bureaus()
 {
     $this->load->helper('csv');
     $bureaus_url = 'http://project-open-data.cio.gov/data/omb_bureau_codes.csv';
     $importer = new CsvImporter($bureaus_url, $parse_header = true, $delimiter = ",");
     $csv = $importer->get();
     $parent_offices = array();
     foreach ($csv as $row) {
         if ($row["Bureau Code"] == "00") {
             // Search for org
             $this->db->select('id, name');
             $this->db->where('name', $row["Bureau Name"]);
             $office_query = $this->db->get('offices');
             if ($office_query->num_rows() > 0) {
                 $office_matches = $office_query->result();
                 foreach ($office_matches as $office_match) {
                     $parent_offices[$row["Agency Code"]] = $office_match->id;
                 }
             }
         }
     }
     reset($csv);
     $bureaus_mapped = array();
     foreach ($csv as $row) {
         $bureau_mapped = array('agency_name' => '', 'bureau_name' => '', 'agency_code' => '', 'bureau_code' => '', 'treasury_code' => '', 'cgac_code' => '', 'usagov_directory_id' => '', 'parent_match' => '');
         // Search for org
         $this->db->select('id, name');
         $this->db->where('name', $row["Bureau Name"]);
         if (!empty($parent_offices[$row["Agency Code"]])) {
             $where = "(parent_office_id='" . $parent_offices[$row["Agency Code"]] . "' OR no_parent = 'true')";
             $this->db->where($where);
             $bureau_mapped['parent_match'] = 'true';
         } else {
             $bureau_mapped['parent_match'] = 'false';
         }
         $office_query = $this->db->get('offices');
         if ($office_query->num_rows() > 0) {
             $office_matches = $office_query->result();
             foreach ($office_matches as $office_match) {
                 $bureau_mapped['usagov_directory_id'] = $office_match->id;
             }
         }
         $bureau_mapped['agency_name'] = $row["Agency Name"];
         $bureau_mapped['bureau_name'] = $row['Bureau Name'];
         $bureau_mapped['agency_code'] = $row['Agency Code'];
         $bureau_mapped['bureau_code'] = $row['Bureau Code'];
         $bureau_mapped['treasury_code'] = $row['Treasury Code'];
         $bureau_mapped['cgac_code'] = $row['CGAC Code'];
         $bureaus_mapped[] = $bureau_mapped;
     }
     $headings = array_keys($bureaus_mapped[0]);
     // Open the output stream
     if ($this->environment == 'terminal' or $this->environment == 'cron') {
         $filepath = realpath('./downloads/bureaus_mapped.csv');
         $fh = fopen($filepath, 'w');
         echo 'Attempting to save csv to ' . $filepath . PHP_EOL;
     } else {
         $fh = fopen('php://output', 'w');
     }
     // Start output buffering (to capture stream contents)
     ob_start();
     fputcsv($fh, $headings);
     // Loop over the * to export
     if (!empty($bureaus_mapped)) {
         foreach ($bureaus_mapped as $row) {
             fputcsv($fh, $row);
         }
     }
     if ($this->environment !== 'terminal') {
         // Get the contents of the output buffer
         $string = ob_get_clean();
         $filename = 'bureaus_mapped_' . date('Ymd') . '_' . date('His');
         // Output CSV-specific headers
         header("Pragma: public");
         header("Expires: 0");
         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
         header("Cache-Control: private", false);
         header('Content-type: text/csv');
         header("Content-Disposition: attachment; filename=\"{$filename}.csv\";");
         header("Content-Transfer-Encoding: binary");
         exit($string);
     } else {
         echo 'Done' . PHP_EOL;
         exit;
     }
 }
Exemple #9
0
<?php 
$importer = new CsvImporter("export_all_products-prod_server.csv", true);
$data = $importer->get();
print_r($data);
?>
 



<?php 
/*
$importer = new CsvImporter("large.txt",true); 
while($data = $importer->get(2000)) 
{ 
print_r($data); 
} 
*/
?>
 



<?php 
class CsvImporter
{
    private $fp;
    private $parse_header;
    private $header;
    private $delimiter;
    private $length;
                // $salida = $scrap->exportBEco($datos_post['solicitud']);
                //$scrap->generaExcelBeco($salida,$datos_post['solicitud']);
                break;
        }
        break;
    case 'nombre':
        switch ($datos_post['formato']) {
            case '1':
                //$busquedaNOmbre = new busquedaNombre();
                $scrap = new Scrap();
                $importer_nombres = new CsvImporter($datos_post['archivo'], false);
                $data = $importer_nombres->get();
                $nombres = $importer_nombres->convertToArraY($data, $datos_post['formato']);
                $scrap->insertaBusquedaNombre($nombres);
                $scrap->buquedaPorNombre($nombres);
                //$scrap->buscaCausas($log, $tabla);
                $scrap->exportData();
                break;
            case '2':
                $scrap = new Scrap();
                $importer_nombres = new CsvImporter($datos_post['archivo'], false);
                $data = $importer_nombres->get();
                $nombres = $importer_nombres->convertToArraY($data, $datos_post['formato']);
                $scrap->insertaBusquedaNombre($nombres);
                $scrap->buquedaPorNombre($nombres);
                //$scrap->buscaCausas($log, $tabla);
                $scrap->exportData();
                break;
        }
        break;
}
Exemple #11
0
<?php

require_once "class/CsvImporter.class.php";
$contract_importer = new CsvImporter("uploads/contracts.csv");
$data_contracts = $contract_importer->get();
$award_importer = new CsvImporter("uploads/awards.csv");
$data_awards = $award_importer->get();
// add latitude and longitude in array
$data_awarded = $award_importer->put_latlng($data_awards);
$combined_data = $award_importer->combine_array($data_contracts, $data_awarded);
// print_r($combined_data);
// count total amount of closed contract
$closed_contract_amount = $award_importer->sum_closed_amount($combined_data);
// generating final.csv file.
$award_importer->array_to_csv_download($combined_data);
?>

<!DOCTYPE html>
<html>
<head>
	<title>Contract Export</title>

	<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
	
	<link rel="stylesheet" type="text/css" href="assets/style/style.css">	

	<script type="text/javascript">
        /*function startDownload() {
            // window.location = "/download.php?data=<?php 
echo serialize($combined_data);
?>
Exemple #12
0
 $handle = new upload($_FILES['file']);
 if ($handle->uploaded) {
     $handle->file_new_name_body = $file_name;
     $handle->file_safe_name = true;
     $handle->file_overwrite = false;
     $handle->file_auto_rename = true;
     $handle->allowed = $LETHE_EXP_IMP_MIMES;
     //*
     $handle->file_max_size = $LETHE_MAX_IMPORT_FILE_SIZE;
     //** Processing
     $handle->process($dest);
     if ($handle->processed) {
         # Uploaded
         /* Parsing Start */
         $uploadedFile = $dest . $handle->file_dst_name;
         $importer = new CsvImporter($uploadedFile, false, $sepMod[$delimiter]);
         $data = $importer->get(1);
         if (!array_key_exists(0, $data)) {
             die(errMod(subscribers_invalid_csv_content . "!", 'danger'));
         }
         if (count($data[0]) < 2) {
             die(errMod(subscribers_incorrect_delimiter . "!", 'danger'));
         }
         $parsedList = '<ul id="sortable1" class="list-unstyled csvlist connectedSortable">';
         foreach ($data[0] as $k => $v) {
             $parsedList .= '<li><input type="hidden" class="csvkey" value="' . $k . '">' . ($v == '' ? '<span class="label label-danger">{' . subscribers_empty . '}</span>' : '<span class="label label-success">' . showIn($v, 'page') . '</span>') . '</li>';
         }
         $parsedList .= '</ul>';
         /* Parsing End */
         $handle->clean();
     } else {
 /**
  * Creates an importer specific to the output of Screaming Frog
  */
 function __construct()
 {
     parent::__construct(false, 1, 2, "utf-8", ",", null, 2);
 }
Exemple #14
0
    /**
     * Importa, a la taula seleccionada, les dades d'un csv
     * 
     * Els registres existents s'actualitzen i els nous s'inserten
     * 
     * @return void (carrega la plantilla per importar/exportar taules)
     */
    public function importaTaula() {

        // Security check 
        $this->checkCsrfToken();
        $this->throwForbiddenUnless(SecurityUtil::checkPermission('Llicencies::', '::', ACCESS_ADMIN));
        if ($this->request->isPost()) {
            $taula = $this->request->request->get('taula_imp', false);
            $importFile = $this->request->files->get('importFile', null);
        }

        if (is_null($importFile)) {
            LogUtil::registerError(__('No s\'ha pogut processar l\'arxiu. Probablement supera la mida màxima.'));
        } else {
            $import = new CsvImporter($importFile['tmp_name'], true, null,';');

            $header = $import->getHeader();

            $check = ModUtil::apiFunc($this->name, 'admin', 'checkCSV', array('dbTable' => $taula, 'csvHeader' => $header));

            // Comprovar capçaleres del csv
            if (!$check['correcte']) {
                // Errades a l'arxiu CSV
                LogUtil::registerError($check['msg']);
            } else {
                // Obtenció del contingut del fitxer csv
                $data = $import->get();
                // Obtenció de les dades de la taula
                $tContent = DBUtil::selectFieldArray($taula, $check['clau']);
                // echo '<pre> tContent: ';print_r($tContent); echo '</pre>';

                LogUtil::registerStatus($check['msg']);
                //LogUtil::registerStatus(print_r($data,true));
                $update = array();
                $insert = array();
                foreach ($data as $row => $record) {
                    if (in_array($record[$check['clau']], $tContent)) {
                        $update[] = $record;
                    } else {
                        $insert[] = $record;
                    }
                }

                $inserts = count($insert);
                $updates = count($update);
                $ins = true;
                $upd = true;
                if ($inserts) {
                    $ins = (DBUtil::insertObjectArray($insert, $taula) && ($inserts));
                    $mi = __('S\'han afegit ' . $inserts . ' registres.');
                }
                if ($updates) {
                    $upd = (DBUtil::updateObjectArray($update, $taula, $check['clau'])) && ($updates);
                    $mu = __('S\'han actualitzat ' . $updates . ' registres.');
                }
                if (($ins) && ($upd))
                    LogUtil::registerStatus(__('La importació de dades cap a la taula:' . $taula . ' s\'ha realitzat correctament.') . " " . $mi . " " . $mu);
                else
                    LogUtil::registerError(__('No s\'han pogut modificar totes les dades de la taula: ' . $taula));
            }
        }
        $this->redirect(ModUtil::url('llicencies', 'admin', 'ieTables'));
    }
/**
 * Updates database prices from the supplied file
 * @param $filename string fully qualified filepath and name
 * @param $options  array of options; see declaration for details
 */
function updatePrices($dbc, $filename, array $options = array())
{
    $result = array('updated' => array(), 'failed' => array(), 'not_found' => array(), 'warning' => array(), 'disabled' => array(), 'modified' => array());
    $updated = array();
    // store product_id => array of product codes matched for it
    $stmts = getPreparedStatements($dbc, $options);
    $labels = $options['header_labels'];
    try {
        $importer = new CsvImporter($filename, true, $labels, ",");
        $select_only = $options['dry_run'] || $options['disable_only'];
        while ($data = $importer->get(2000)) {
            foreach ($data as $entry) {
                $manufacturer = trim(empty($entry[$labels['manufacturer']['label']]) ? $options['manufacturer'] : $entry[$labels['manufacturer']['label']]);
                $product_code = trim($entry[$labels['product_code']['label']]);
                $upc = !$options['upc_update'] || empty($entry[$labels['upc']['label']]) ? null : $entry[$labels['upc']['label']];
                $list_price = round_up(getAmount($entry[$labels['list_price']['label']]), 2);
                $cost_price = isset($entry[$labels['cost_price']['label']]) ? round_up(getAmount($entry[$labels['cost_price']['label']]), 2) : null;
                $sale_price = round_up(getAmount($entry[$labels['sale_price']['label']]), 2);
                if ($sale_price >= $list_price) {
                    $list_price = $options['allow_upsell'] ? $sale_price : $list_price;
                    $sale_price = null;
                }
                $changed = false;
                // flag indicating product (or matrix) prices changed
                if (!$stmts['select_product']->bind_param('ss', $product_code, $manufacturer) || !$stmts['select_product']->execute()) {
                    throw new \RuntimeException("Query failed for manufacturer {$manufacturer} and product code {$product_code}: {$stmts['select_product']->errno} - {$stmts['select_product']->error}");
                }
                $main_product_id = fetch_assoc_stmt($stmts['select_product']);
                $product_id = false;
                if ($select_only) {
                    if (is_int($main_product_id)) {
                        $result['updated'][] = "Product prices updated; Manufacturer: {$manufacturer} | Product Code: {$product_code} | List Price: \$" . sprintf('%.2f', $list_price) . " | Cost Price: \$" . sprintf('%.2f', $cost_price) . " | Sale Price: \$" . sprintf('%.2f', $sale_price);
                        $changed = true;
                    } elseif (!$options['ignore_missing']) {
                        $result['not_found'][$product_code] = "Product was either not found or prices did not change; Manufacturer: {$manufacturer} | Product Code: {$product_code}";
                    }
                    if ($options['update_matrix']) {
                        if (!$stmts['select_matrix']->bind_param('ss', $product_code, $manufacturer) || !$stmts['select_matrix']->execute()) {
                            throw new \RuntimeException("Query failed for manufacturer {$manufacturer} and product code {$product_code}: {$stmts['select_matrix']->errno} - {$stmts['select_matrix']->error}");
                        } elseif (!empty($product_id = fetch_assoc_stmt($stmts['select_matrix']))) {
                            $result['updated'][] = "Matrix prices updated; Manufacturer: {$manufacturer} | Product Code: {$product_code} | List Price: \$" . sprintf('%.2f', $list_price) . " | Sale Price: \$" . sprintf('%.2f', $sale_price);
                            $changed = true;
                            $updated[$product_id][] = $product_code;
                            // wasn't found as a product, but found as a matrix entry
                            if (array_key_exists($product_code, $result['not_found'])) {
                                unset($result['not_found'][$product_code]);
                            }
                        } elseif (array_key_exists($product_code, $result['not_found'])) {
                            $result['not_found'][$product_code] = "Neither product nor matrix entry not found; Manufacturer: {$manufacturer} | Product Code: {$product_code}";
                        }
                    }
                } else {
                    if (!$stmts['update_product']->bind_param('dddsi', $list_price, $cost_price, $sale_price, $upc, $main_product_id) || !$stmts['update_product']->execute()) {
                        throw new \RuntimeException("Query failed for manufacturer {$manufacturer} and product code {$product_code}: {$stmts['update_product']->errno} - {$stmts['update_product']->error}");
                    } elseif ($stmts['update_product']->affected_rows > 0) {
                        $result['updated'][] = "Product prices updated; Manufacturer: {$manufacturer} | Product Code: {$product_code} | List Price: \$" . sprintf('%.2f', $list_price) . " | Cost Price: \$" . sprintf('%.2f', $cost_price) . " | Sale Price: \$" . sprintf('%.2f', $sale_price);
                        $changed = true;
                    } elseif (!$options['ignore_missing']) {
                        $result['not_found'][$product_code] = "Product was either not found or prices did not change; Manufacturer: {$manufacturer} | Product Code: {$product_code}";
                    }
                    if ($options['update_matrix']) {
                        if (!$stmts['update_matrix']->bind_param('ddsss', $list_price, $sale_price, $upc, $product_code, $manufacturer) || !$stmts['update_matrix']->execute()) {
                            throw new \RuntimeException("Query failed for manufacturer {$manufacturer} and product code {$product_code}: {$stmts['update_matrix']->errno} - {$stmts['update_matrix']->error}");
                        } elseif ($stmts['update_matrix']->affected_rows > 0) {
                            if (!$stmts['select_matrix']->bind_param('ss', $product_code, $manufacturer) || !$stmts['select_matrix']->execute()) {
                                throw new \RuntimeException("Query to select product id from matrix table failed for manufacturer {$manufacturer} and product code {$product_code}: {$stmts['select_matrix']->errno} - {$stmts['select_matrix']->error}");
                            } elseif (empty($product_id = fetch_assoc_stmt($stmts['select_matrix']))) {
                                $result['failed'][] = "Matrix entry not found after updating! Manufacturer: {$manufacturer} | Product Code: {$product_code}";
                            } else {
                                $result['updated'][] = "Matrix prices updated; Manufacturer: {$manufacturer} | Product Code: {$product_code} | List Price: \$" . sprintf('%.2f', $list_price) . " | Sale Price: \$" . sprintf('%.2f', $sale_price);
                                $changed = true;
                                $updated[$product_id][] = $product_code;
                                // wasn't found as a product, but found as a matrix entry
                                if (array_key_exists($product_code, $result['not_found'])) {
                                    unset($result['not_found'][$product_code]);
                                }
                            }
                        } elseif (array_key_exists($product_code, $result['not_found'])) {
                            $result['not_found'][$product_code] = "Neither product nor matrix entry was found or updated; Manufacturer: {$manufacturer} | Product Code: {$product_code}";
                        }
                    }
                }
                // Product was found and updated - update 'date updated' field
                $id = $main_product_id ? $main_product_id : $product_id;
                if ($id && empty($result['modified'][$id]) && ($options['update_date_all'] || $changed && $options['update_date'])) {
                    if ($select_only) {
                        $result['modified'][$id] = "Date modified updated for product id {$id}: triggered by {$manufacturer} product {$product_code}";
                    } elseif (!$stmts['update_date']->bind_param('i', $id) || !$stmts['update_date']->execute()) {
                        throw new \RuntimeException("Update date query failed for manufacturer {$manufacturer} and product code {$product_code}: {$stmts['update_date']->errno} - {$stmts['update_date']->error}");
                    } else {
                        $result['modified'][$id] = "Date modified updated for product id {$id}: triggered by {$manufacturer} product {$product_code}";
                    }
                }
            }
        }
        // TODO option to disable warnings (including display thereof)
        // Array only contains entries when updating matrix codes, i.e. option_matrix table has been modified accordingly
        foreach ($updated as $product_id => $product_codes) {
            // select all product / matrix codes from database for this product
            if (!$stmts['select_product_codes']->bind_param('ii', $product_id, $product_id) || !$stmts['select_product_codes']->execute()) {
                throw new \RuntimeException("Query to select product codes while checking for warnings failed for product id {$product_id}: {$stmts['select_product_codes']->errno} - {$stmts['select_product_codes']->error}");
            }
            // disable / warn for any that were not found on the price list
            $codes = fetch_assoc_stmt($stmts['select_product_codes']);
            $diff = array_diff(is_array($codes) ? $codes : array($codes), $product_codes);
            if ($options['disable_products']) {
                if ($options['dry_run']) {
                    $result['disabled'][$product_id] = $diff;
                } else {
                    // Disable matrix entries first
                    foreach ($diff as $product_code) {
                        if (!$stmts['disable_matrix']->bind_param('is', $product_id, $product_code) || !$stmts['disable_matrix']->execute()) {
                            throw new \RuntimeException("Failed to disable matrix entry for product {$product_id} - {$product_code}: {$stmts['disable_matrix']->errno} - {$stmts['disable_matrix']->error}");
                        } elseif ($stmts['disable_matrix']->affected_rows > 0) {
                            $result['disabled'][$product_id][] = $product_code;
                        } else {
                            $result['warning'][$product_id][] = "Matrix entry for product {$product_id} - {$product_code} could not be disabled: it may already be disabled, but you should double-check";
                        }
                    }
                    // Then disable products that no longer have any enabled matrix options
                    if (!$stmts['disable_product']->bind_param('iii', $product_id, $product_id, $product_id) || !$stmts['disable_product']->execute()) {
                        throw new \RuntimeException("Failed to disable product id {$product_id}: {$stmts['disable_product']->errno} - {$stmts['disable_product']->error}");
                    } elseif ($stmts['disable_product']->affected_rows > 0) {
                        $result['disabled'][$product_id][] = "Product {$product_id} disabled";
                    } else {
                        $result['warning'][$product_id][] = "Product {$product_id} was not be disabled: it may either not need to be or already is disabled; you should double-check";
                    }
                }
            } elseif (!empty($diff)) {
                $result['warning'][$product_id] = $diff;
            }
            // Update main product price with the lowest (non-zero) of its enabled matrix options
            if ($options['update_main_price']) {
                if (!$stmts['lowest_price']->bind_param('i', $product_id) || !$stmts['lowest_price']->execute()) {
                    throw new \RuntimeException("Failed to fetch lowest matrix price for product {$product_id}: {$stmts['lowest_price']->errno} - {$stmts['lowest_price']->error}");
                }
                $prices = fetch_assoc_stmt($stmts['lowest_price']);
                if (!empty($prices)) {
                    extract($prices);
                    if (!$stmts['update_main_price']->bind_param('ddi', $price, $sale_price, $product_id) || !$stmts['update_main_price']->execute()) {
                        throw new \RuntimeException("Failed to update main prices for product {$product_id}: {$stmts['update_main_price']->errno} - {$stmts['update_main_price']->error}");
                    } elseif ($stmts['update_main_price']->affected_rows > 0) {
                        $result['updated'][] = "Main prices for product id {$product_id} set to lowest found in matrix: List Price=\${$price}, Sale Price=\$" . ($sale_price ? $sale_price : '0.00');
                    } else {
                        $result['warning'][$product_id][] = "Failed to update prices to \${$price} (sale: \$" . ($sale_price ? $sale_price : '0.00') . ") - prices may already be up-to-date";
                    }
                }
            }
        }
    } catch (\Exception $e) {
        $result['error'] = $e->getMessage();
    } finally {
        foreach ($stmts as $stmt) {
            $stmt->close();
        }
    }
    // Sort results by key
    foreach ($result as &$array) {
        ksort($array);
    }
    unset($array);
    // save puppies
    return $result;
}
 function tracker()
 {
     $this->load->helper('csv');
     $this->load->helper('api');
     $this->load->model('campaign_model', 'campaign');
     ini_set("auto_detect_line_endings", true);
     $full_path = $this->config->item('tmp_csv_import');
     $importer = new CsvImporter($full_path, $parse_header = true, $delimiter = ",");
     $csv = $importer->get();
     $model = (array) $this->campaign->datagov_model();
     $note_count = 0;
     $status_count = 0;
     $column_headers = array();
     foreach ($csv as $row) {
         $notes = array();
         foreach ($row as $key => $value) {
             if (substr($key, 0, 5) == 'note_') {
                 $key = substr($key, 5);
                 $notes[$key] = $value;
             }
         }
         reset($row);
         $filtered = array_mash($model, $row);
         $processed = array();
         foreach ($filtered as $key => $value) {
             if (strtolower($value) == 'yes' or strtolower($value) == 'no') {
                 $value = strtolower($value);
             }
             if (!empty($value)) {
                 $processed[$key] = $value;
             }
         }
         $update = (object) $processed;
         $this->campaign->update_status($update);
         $status_count++;
         foreach ($notes as $field_name => $note_data) {
             $note_data = array("note" => $note_data, "date" => null, "author" => null);
             $note_data = array("current" => $note_data, "previous" => null);
             $note_data = json_encode($note_data);
             $note = array('note' => $note_data, 'field_name' => $field_name, 'office_id' => $update->office_id);
             $note = (object) $note;
             $this->campaign->update_note($note);
             $note_count++;
         }
     }
     echo "Status count: {$status_count} / Note count: {$note_count}";
 }
Exemple #17
0
 /**
  * Read weights MxP from csv file. Only for Teranga, only for classic method
  */
 function parse_csv_weights($filename)
 {
     $importer = new CsvImporter($filename, true, ",");
     $readed = $importer->get();
     //..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-
     if (elgg_get_plugin_setting('exportTex', 'hflts') == 1) {
         weight2latex($readed, $this->label, $this->M);
     }
     //export to .tex just in case needed
     //..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-..-
     $mxp = count($readed);
     //numero de valoraciones es N*P
     if ($mxp != $this->P) {
         echo $mxp . "... esto pinta mal... actualizar P<br>";
         $this->P = $mxp;
     }
     //no valido el valor del campo expert, que debe estar en el mismo orden que los datos
     //otra opcion es que el índice sea $readed[$i]['expert'] en ambos casos
     for ($i = 0; $i < $mxp; $i++) {
         $this->E[$i] = $readed[$i]['we'];
         for ($j = 0; $j < $this->M; $j++) {
             $l = 'C' . ($j + 1);
             $this->superE[$i][$j] = $readed[$i][$l];
             if ($i == 0) {
                 $this->W[$j] = $readed[$i][$l];
             }
         }
     }
     if ($this->debug) {
         echo 'expert weights (E): <pre>';
         print_r($this->E);
         echo '</pre><br>';
         echo 'individual weights (superE): <pre>';
         print_r($this->superE);
         echo '</pre><br>';
         echo 'simple weights (W): <pre>';
         print_r($this->W);
         echo '</pre><br>';
     }
 }